如何将数据存储 api 与 Jetpack Compose 一起使用?
How to use datastore api with jetpack compose?
我在 viewModel 中保留时间状态,需要在首选项中存储当前状态,并在用户关闭并再次打开应用程序时再次加载时间状态。这是我当前的代码。
视图模型
class TimeViewModel(): ViewModel(){
private val _time = MutableLiveData<Long>()
val time: LiveData<Long> = _time
fun onTimeChange(newTime: Long) {
_time.value = newTime
}
}
可组合函数
@Composable
fun Timer(timeViewModel:TimeViewModel = viewModel()){
LaunchedEffect(key1 = time ){
delay(1000L)
timeViewModel.onTimeChange(time + 1)
}
val time: Long by timeViewModel.time.observeAsState(0L)
val dec = DecimalFormat("00")
val min = time / 60
val sec = time % 60
Text(
text = dec.format(min) + ":" + dec.format(sec),
style = MaterialTheme.typography.body1
)
}
尝试使用 dagger 进行依赖注入,您可以通过这种方式为您的商店创建单例:
@Module
@InstallIn(SingletonComponent::class)
object MyModule {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "preferences")
@Singleton
@Provides
fun provideDataStore(@ApplicationContext app: Context ) : DataStore<Preferences> = app.dataStore
}
然后只需注入您的 viewModels 并使用它!
@HiltViewModel
class HomeViewModel @Inject constructor(
private val dataStore: DataStore<Preferences>
) : ViewModel() {
private val myKey = stringPreferencesKey("USER_KEY")// init your key identifier here
fun mySuperCoolWrite(){
viewModelScope.launch {
dataStore.edit {
it[myKey] = body.auth
}
}
}
fun mySuperCoolRead(){
viewModelScope.launch {
val preferences = dataStore.data.first()
preferences[myKey]?.let {
// here access your stored value with "it"
}
}
}
}
或者只是在你的控制器构造函数中注入
@ApplicationContext app: Context
here您可以找到更多信息
我在 viewModel 中保留时间状态,需要在首选项中存储当前状态,并在用户关闭并再次打开应用程序时再次加载时间状态。这是我当前的代码。
视图模型
class TimeViewModel(): ViewModel(){
private val _time = MutableLiveData<Long>()
val time: LiveData<Long> = _time
fun onTimeChange(newTime: Long) {
_time.value = newTime
}
}
可组合函数
@Composable
fun Timer(timeViewModel:TimeViewModel = viewModel()){
LaunchedEffect(key1 = time ){
delay(1000L)
timeViewModel.onTimeChange(time + 1)
}
val time: Long by timeViewModel.time.observeAsState(0L)
val dec = DecimalFormat("00")
val min = time / 60
val sec = time % 60
Text(
text = dec.format(min) + ":" + dec.format(sec),
style = MaterialTheme.typography.body1
)
}
尝试使用 dagger 进行依赖注入,您可以通过这种方式为您的商店创建单例:
@Module
@InstallIn(SingletonComponent::class)
object MyModule {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "preferences")
@Singleton
@Provides
fun provideDataStore(@ApplicationContext app: Context ) : DataStore<Preferences> = app.dataStore
}
然后只需注入您的 viewModels 并使用它!
@HiltViewModel
class HomeViewModel @Inject constructor(
private val dataStore: DataStore<Preferences>
) : ViewModel() {
private val myKey = stringPreferencesKey("USER_KEY")// init your key identifier here
fun mySuperCoolWrite(){
viewModelScope.launch {
dataStore.edit {
it[myKey] = body.auth
}
}
}
fun mySuperCoolRead(){
viewModelScope.launch {
val preferences = dataStore.data.first()
preferences[myKey]?.let {
// here access your stored value with "it"
}
}
}
}
或者只是在你的控制器构造函数中注入
@ApplicationContext app: Context
here您可以找到更多信息