Created
September 5, 2022 06:29
-
-
Save meet30997/638d59ad928b0e14493422b362449c63 to your computer and use it in GitHub Desktop.
DataStore Viewmodel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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