added async settings access and edit
This commit is contained in:
parent
2cb498bb37
commit
3e2c8cb301
9 changed files with 160 additions and 4 deletions
57
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
57
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
|
@ -0,0 +1,57 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="ComposePreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="ComposePreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="ComposePreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="ComposePreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="GlancePreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="GlancePreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="GlancePreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="GlancePreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewAnnotationInFunctionWithParameters" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewApiLevelMustBeValid" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewDeviceShouldUseNewSpec" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewFontScaleMustBeGreaterThanZero" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewMultipleParameterProviders" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewPickerAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
|
@ -2,6 +2,8 @@ plugins {
|
|||
alias(libs.plugins.android.application)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
alias(libs.plugins.kotlin.compose)
|
||||
id("kotlin-kapt")
|
||||
id("com.google.dagger.hilt.android")
|
||||
}
|
||||
|
||||
android {
|
||||
|
@ -40,6 +42,10 @@ android {
|
|||
}
|
||||
}
|
||||
|
||||
kapt {
|
||||
correctErrorTypes = true
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.androidx.core.ktx)
|
||||
implementation(libs.androidx.lifecycle.runtime.ktx)
|
||||
|
@ -51,7 +57,13 @@ dependencies {
|
|||
implementation(libs.androidx.ui.tooling.preview)
|
||||
implementation(libs.androidx.material3)
|
||||
implementation(libs.androidx.material.icons.extended)
|
||||
testImplementation(libs.junit)
|
||||
implementation(libs.androidx.datastore)
|
||||
implementation(libs.androidx.datastore.preferences)
|
||||
|
||||
implementation(libs.dagger.hilt.android)
|
||||
kapt(libs.hilt.compiler)
|
||||
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.androidx.junit)
|
||||
androidTestImplementation(libs.androidx.espresso.core)
|
||||
androidTestImplementation(platform(libs.androidx.compose.bom))
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<application
|
||||
android:name=".CatCommApplication"
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
|
6
app/src/main/java/pet/catcomm/CatCommApplication.kt
Normal file
6
app/src/main/java/pet/catcomm/CatCommApplication.kt
Normal file
|
@ -0,0 +1,6 @@
|
|||
package pet.catcomm
|
||||
|
||||
import android.app.Application
|
||||
|
||||
class CatCommApplication : Application() {
|
||||
}
|
|
@ -13,6 +13,7 @@ import androidx.navigation.compose.NavHost
|
|||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import pet.catcomm.ui.compose.BottomBar
|
||||
import pet.catcomm.ui.compose.Peers
|
||||
import pet.catcomm.ui.compose.TopBar
|
||||
|
|
|
@ -10,10 +10,17 @@ import androidx.compose.material3.CardDefaults
|
|||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.map
|
||||
import pet.catcomm.ui.theme.CatCommTheme
|
||||
import pet.catcomm.ui.viewmodel.DataStoreViewModel
|
||||
|
||||
@Composable
|
||||
fun Peer(name: String, modifier: Modifier = Modifier) {
|
||||
|
@ -38,9 +45,20 @@ fun PeerPreview() {
|
|||
}
|
||||
|
||||
@Composable
|
||||
fun Peers(modifier: Modifier = Modifier) {
|
||||
fun Peers(
|
||||
modifier: Modifier = Modifier,
|
||||
dataStoreViewModel: DataStoreViewModel = viewModel(factory = DataStoreViewModel.Factory),
|
||||
) {
|
||||
val username = dataStoreViewModel.usernameSetting.collectAsStateWithLifecycle("")
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
// Test: it'll change the username setting after 2s and this should refresh the UI as expected
|
||||
delay(2000)
|
||||
dataStoreViewModel.changeUsername("woof")
|
||||
}
|
||||
|
||||
LazyColumn(modifier = modifier.padding(vertical = 4.dp)) {
|
||||
items(items = listOf("nyx", "1686a", "8098")) {
|
||||
items(items = listOf("nyx", "1686a", "8098", username.value)) {
|
||||
name -> Peer(name = name)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package pet.catcomm.ui.viewmodel
|
||||
|
||||
import android.content.Context
|
||||
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.datastore.preferences.preferencesDataStore
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import kotlinx.coroutines.flow.map
|
||||
import pet.catcomm.CatCommApplication
|
||||
|
||||
private const val PREFERENCES = "settings"
|
||||
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = PREFERENCES)
|
||||
|
||||
const val SETTINGS_USERNAME_KEY = "cat_comm_username"
|
||||
val SETTINGS_USERNAME = stringPreferencesKey(SETTINGS_USERNAME_KEY)
|
||||
|
||||
class DataStoreViewModel(
|
||||
private val settingsDataStore: DataStore<Preferences>,
|
||||
) : ViewModel() {
|
||||
val usernameSetting = settingsDataStore.data.map { preferences ->
|
||||
preferences[SETTINGS_USERNAME] ?: ""
|
||||
}
|
||||
|
||||
suspend fun changeUsername(new: String) {
|
||||
settingsDataStore.edit { settings ->
|
||||
settings[SETTINGS_USERNAME] = new
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val Factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel> create(
|
||||
modelClass: Class<T>,
|
||||
extras: CreationExtras,
|
||||
): T {
|
||||
val app =
|
||||
checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as CatCommApplication
|
||||
|
||||
return DataStoreViewModel(
|
||||
settingsDataStore = app.baseContext.dataStore,
|
||||
) as T
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,4 +3,5 @@ plugins {
|
|||
alias(libs.plugins.android.application) apply false
|
||||
alias(libs.plugins.kotlin.android) apply false
|
||||
alias(libs.plugins.kotlin.compose) apply false
|
||||
alias(libs.plugins.kotlin.hilt) apply false
|
||||
}
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
[versions]
|
||||
agp = "8.8.2"
|
||||
datastore = "1.1.3"
|
||||
datastorePreferences = "1.1.3"
|
||||
hiltPlugin = "2.51.1"
|
||||
hiltAndroidVersion = "2.55"
|
||||
hiltCompiler = "2.55"
|
||||
kotlin = "2.0.0"
|
||||
coreKtx = "1.15.0"
|
||||
junit = "4.13.2"
|
||||
|
@ -12,8 +17,13 @@ navigationCompose = "2.8.8"
|
|||
|
||||
[libraries]
|
||||
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
||||
androidx-datastore = { module = "androidx.datastore:datastore", version.ref = "datastore" }
|
||||
androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferences" }
|
||||
androidx-material-icons-extended = { module = "androidx.compose.material:material-icons-extended" }
|
||||
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigationCompose" }
|
||||
dagger-hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroidVersion" }
|
||||
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler" }
|
||||
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltCompiler" }
|
||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
||||
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
|
||||
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
|
||||
|
@ -32,4 +42,4 @@ androidx-material3 = { group = "androidx.compose.material3", name = "material3"
|
|||
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
|
||||
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
|
||||
|
||||
kotlin-hilt = { id = "com.google.dagger.hilt.android", version.ref = "hiltPlugin" }
|
||||
|
|
Loading…
Add table
Reference in a new issue