Skip to content

Instantly share code, notes, and snippets.

@meet30997
Created September 5, 2022 06:29
Show Gist options
  • Save meet30997/638d59ad928b0e14493422b362449c63 to your computer and use it in GitHub Desktop.
Save meet30997/638d59ad928b0e14493422b362449c63 to your computer and use it in GitHub Desktop.
DataStore Viewmodel
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class MainViewModel @Inject constructor(private val dataStoreManager: DataStoreManager) :
ViewModel() {
val getUsername: LiveData<String?> = dataStoreManager.getUserNameWithTryCatch().asLiveData()
fun setUserName(userName: String) {
viewModelScope.launch {
dataStoreManager.storeUserName(userName)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment