Reintroduce Compose State to AppState
This commit is contained in:
parent
5bece30b4b
commit
8413cf7ae7
2 changed files with 18 additions and 20 deletions
|
@ -1,25 +1,26 @@
|
|||
package com.kernelmaft.zanbur
|
||||
|
||||
import androidx.compose.runtime.*
|
||||
|
||||
|
||||
|
||||
object AppState {
|
||||
val groups : List <Group> get () = groupsAsMutable
|
||||
private var groupsAsMutable : List <Group> = emptyList ()
|
||||
|
||||
private val subscribers : MutableList < ( List <Group> ) -> Unit > = mutableListOf ()
|
||||
|
||||
fun subscribe ( subscriber : ( List <Group> ) -> Unit ) = subscribers . add (subscriber)
|
||||
|
||||
fun addGroup ( group : Group ) {
|
||||
groupsAsMutable += group
|
||||
subscribers . forEach { it (groups) }
|
||||
}
|
||||
// The index of a group in this list is always equal to its ID
|
||||
val groups : State < List <Group> > get () = groupsAsMutable
|
||||
private val groupsAsMutable : MutableState < List <Group> > = mutableStateOf ( emptyList () )
|
||||
|
||||
fun setCurrentScene ( groupId : Int , scene : Scene ) {
|
||||
groupsAsMutable = groupsAsMutable . mapIndexed { index , group ->
|
||||
groupsAsMutable . value = groupsAsMutable . value . mapIndexed { index , group ->
|
||||
if ( index == groupId ) group . copy ( currentScene = scene )
|
||||
else group
|
||||
}
|
||||
subscribers . forEach { it (groups) }
|
||||
}
|
||||
|
||||
fun addGroup ( group : Group ) {
|
||||
val newGroups = groupsAsMutable . value . toMutableList ()
|
||||
// Wow this is sooo much better than Java
|
||||
if ( newGroups . getOrNull ( group . id ) != null ) newGroups . removeAt ( group . id )
|
||||
newGroups . add ( group . id , group )
|
||||
groupsAsMutable . value = newGroups
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package com.kernelmaft.zanbur
|
|||
import android.os.*
|
||||
import androidx.activity.compose.*
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.*
|
||||
import androidx.compose.ui.unit.*
|
||||
import androidx.datastore.preferences.*
|
||||
|
@ -23,14 +22,12 @@ class MainActivity : EdgeToEdgeActivity () {
|
|||
|
||||
Config . groups . forEach { AppState . addGroup (it) }
|
||||
|
||||
val groups = mutableStateOf ( AppState . groups )
|
||||
AppState . subscribe { groups . value = it }
|
||||
|
||||
lifecycleScope . launch (IO) {
|
||||
val prefs = dataStore . data . firstOrNull ()
|
||||
val savedSceneName = prefs ?. get ( stringPreferencesKey ("scene") )
|
||||
if ( savedSceneName != null ) {
|
||||
val savedScene = AppState . groups [0] . scenes . find { it . name == savedSceneName }
|
||||
val savedScene = AppState . groups . value [0] . scenes
|
||||
. find { it . name == savedSceneName }
|
||||
savedScene ?. let { AppState . setCurrentScene ( 0 , it ) }
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +35,7 @@ class MainActivity : EdgeToEdgeActivity () {
|
|||
setContent {
|
||||
AppFrame {
|
||||
Column ( Modifier . width ( 300 . dp ) ) {
|
||||
groups . value . forEach { group ->
|
||||
AppState . groups . value . forEach { group ->
|
||||
SceneSwitcher (group) { newScene ->
|
||||
AppState . setCurrentScene ( group . id , newScene )
|
||||
publishSceneChange ( group , newScene )
|
||||
|
@ -54,7 +51,7 @@ class MainActivity : EdgeToEdgeActivity () {
|
|||
override fun onStop () {
|
||||
super . onStop ()
|
||||
|
||||
val currentScene = AppState . groups [0] . currentScene
|
||||
val currentScene = AppState . groups . value [0] . currentScene
|
||||
if ( currentScene != null ) lifecycleScope . launch (IO) {
|
||||
dataStore . edit {
|
||||
it [ stringPreferencesKey ("scene") ] = currentScene . name
|
||||
|
|
Loading…
Add table
Reference in a new issue