Skip to content

Commit

Permalink
Impl. datastore to save username locally
Browse files Browse the repository at this point in the history
  • Loading branch information
aritra-tech committed Nov 8, 2024
1 parent 7824d72 commit 17fd42b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/com/aritradas/uncrack/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package com.aritradas.uncrack.di

import android.app.Application
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStoreFile
import androidx.room.Room
import com.aritradas.uncrack.data.datastore.DataStoreUtil
import com.aritradas.uncrack.data.db.AccountDatabase
Expand Down Expand Up @@ -76,4 +80,12 @@ object AppModule {
@Provides
@Singleton
fun provideFirebaseFirestore(): FirebaseFirestore = FirebaseFirestore.getInstance()

@Provides
@Singleton
fun provideDataStore(@ApplicationContext appContext: Context): DataStore<Preferences> {
return PreferenceDataStoreFactory.create(
produceFile = { appContext.preferencesDataStoreFile("user_preferences") }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fun VaultScreen(

LaunchedEffect(Unit) {
vaultViewModel.getAccounts()
userViewModel.getCurrentUser()
}

Scaffold(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.aritradas.uncrack.sharedViewModel

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.aritradas.uncrack.domain.model.User
Expand All @@ -10,6 +14,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import timber.log.Timber
Expand All @@ -18,22 +23,37 @@ import javax.inject.Inject
@HiltViewModel
class UserViewModel @Inject constructor(
private val auth: FirebaseAuth,
private val firestore: FirebaseFirestore
private val firestore: FirebaseFirestore,
private val dataStore: DataStore<Preferences>
) : ViewModel() {

private val _state = MutableStateFlow(User())
val state: StateFlow<User> = _state

init {
loadUserFromDataStore()
getCurrentUser()
}

private fun getCurrentUser() = viewModelScope.launch(Dispatchers.IO) {
private fun loadUserFromDataStore() {
viewModelScope.launch(Dispatchers.IO) {
try {
val userNamePreference = dataStore.data.first()
val userName = userNamePreference[USER_NAME_KEY] ?: ""
_state.value = _state.value.copy(name = userName)
} catch (e: Exception) {
Timber.e("Error loading user name from DataStore: $e")
}
}
}

fun getCurrentUser() = viewModelScope.launch(Dispatchers.IO) {
try {
val currentUser = auth.currentUser
if (currentUser != null) {
val user = fetchUserFromFirestore(currentUser.uid)
_state.value = user
saveUserNameToDataStore(user.name)
} else {
Timber.e("No user is currently logged in")
}
Expand All @@ -57,4 +77,18 @@ class UserViewModel @Inject constructor(
User()
}
}

private suspend fun saveUserNameToDataStore(userName: String) {
try {
dataStore.edit { preferences ->
preferences[USER_NAME_KEY] = userName
}
} catch (e: Exception) {
Timber.e("Error saving user name to DataStore: $e")
}
}

companion object {
private val USER_NAME_KEY = stringPreferencesKey("user_name")
}
}

0 comments on commit 17fd42b

Please sign in to comment.