[add] function implementation

This commit is contained in:
acite
2025-08-24 20:07:38 +08:00
parent 996c1ff5cf
commit d0a6497dd6
64 changed files with 2924 additions and 1 deletions

View File

@@ -0,0 +1,52 @@
package com.acitelight.aether.viewModel
import android.app.Application
import android.widget.Toast
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.LocalContext
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.acitelight.aether.dataStore
import com.acitelight.aether.model.Video
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
class MeScreenViewModel(application: Application) : AndroidViewModel(application)
{
private val dataStore = application.dataStore
private val USER_NAME_KEY = stringPreferencesKey("user_name")
private val PRIVATE_KEY = stringPreferencesKey("private_key")
val userNameFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[USER_NAME_KEY] ?: ""
}
val privateKeyFlow: Flow<String> = dataStore.data.map { preferences ->
preferences[PRIVATE_KEY] ?: ""
}
val username = mutableStateOf("");
val privateKey = mutableStateOf("")
init {
viewModelScope.launch {
username.value = userNameFlow.first()
privateKey.value = privateKeyFlow.first()
}
}
fun updateAccount(u: String, p: String)
{
viewModelScope.launch {
dataStore.edit { preferences ->
preferences[USER_NAME_KEY] = u
preferences[PRIVATE_KEY] = p
}
}
}
}