package com.parkplants.data.repository import android.content.Context import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.map import javax.inject.Inject import javax.inject.Singleton private val Context.dataStore by preferencesDataStore(name = "auth") @Singleton class TokenRepository @Inject constructor( @ApplicationContext private val context: Context, ) { private val TOKEN_KEY = stringPreferencesKey("auth_token") suspend fun saveToken(token: String) { context.dataStore.edit { it[TOKEN_KEY] = token } } suspend fun getToken(): String? = context.dataStore.data.map { it[TOKEN_KEY] }.firstOrNull() suspend fun clearToken() { context.dataStore.edit { it.remove(TOKEN_KEY) } } }