Replace DataStore delegate magic with direct usage
This commit is contained in:
parent
5621e3b4ac
commit
80eb10f018
2 changed files with 7 additions and 14 deletions
|
@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.*
|
import androidx.compose.ui.*
|
||||||
import androidx.compose.ui.unit.*
|
import androidx.compose.ui.unit.*
|
||||||
|
import androidx.datastore.preferences.*
|
||||||
import androidx.datastore.preferences.core.*
|
import androidx.datastore.preferences.core.*
|
||||||
import androidx.lifecycle.*
|
import androidx.lifecycle.*
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
@ -15,13 +16,15 @@ import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : EdgeToEdgeActivity () {
|
class MainActivity : EdgeToEdgeActivity () {
|
||||||
|
val dataStore = PreferenceDataStoreFactory . create { preferencesDataStoreFile ("state") }
|
||||||
|
|
||||||
override fun onCreate ( savedInstanceState : Bundle ? ) {
|
override fun onCreate ( savedInstanceState : Bundle ? ) {
|
||||||
super . onCreate (savedInstanceState)
|
super . onCreate (savedInstanceState)
|
||||||
|
|
||||||
Config . groups . forEach { AppState . addGroup (it) }
|
Config . groups . forEach { AppState . addGroup (it) }
|
||||||
|
|
||||||
var groups by mutableStateOf ( AppState . groups )
|
val groups = mutableStateOf ( AppState . groups )
|
||||||
AppState . subscribe { groups = it }
|
AppState . subscribe { groups . value = it }
|
||||||
|
|
||||||
lifecycleScope . launch (IO) {
|
lifecycleScope . launch (IO) {
|
||||||
val prefs = dataStore . data . firstOrNull ()
|
val prefs = dataStore . data . firstOrNull ()
|
||||||
|
@ -35,7 +38,7 @@ class MainActivity : EdgeToEdgeActivity () {
|
||||||
setContent {
|
setContent {
|
||||||
AppFrame {
|
AppFrame {
|
||||||
Column ( Modifier . width ( 300 . dp ) ) {
|
Column ( Modifier . width ( 300 . dp ) ) {
|
||||||
groups . forEach { group ->
|
groups . value . forEach { group ->
|
||||||
SceneSwitcher (group) { newScene ->
|
SceneSwitcher (group) { newScene ->
|
||||||
AppState . setCurrentScene ( group . id , newScene )
|
AppState . setCurrentScene ( group . id , newScene )
|
||||||
publishSceneChange ( group , newScene )
|
publishSceneChange ( group , newScene )
|
||||||
|
@ -53,7 +56,7 @@ class MainActivity : EdgeToEdgeActivity () {
|
||||||
|
|
||||||
val currentScene = AppState . groups [0] . currentScene
|
val currentScene = AppState . groups [0] . currentScene
|
||||||
if ( currentScene != null ) lifecycleScope . launch (IO) {
|
if ( currentScene != null ) lifecycleScope . launch (IO) {
|
||||||
applicationContext . dataStore . edit {
|
dataStore . edit {
|
||||||
it [ stringPreferencesKey ("scene") ] = currentScene . name
|
it [ stringPreferencesKey ("scene") ] = currentScene . name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
package com.kernelmaft.zanbur
|
|
||||||
|
|
||||||
import android.content.*
|
|
||||||
import androidx.datastore.core.*
|
|
||||||
import androidx.datastore.preferences.*
|
|
||||||
import androidx.datastore.preferences.core.*
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
val Context . dataStore : DataStore <Preferences> by preferencesDataStore ("state")
|
|
Loading…
Add table
Add a link
Reference in a new issue