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
|
package com.kernelmaft.zanbur
|
||||||
|
|
||||||
|
import androidx.compose.runtime.*
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
object AppState {
|
object AppState {
|
||||||
val groups : List <Group> get () = groupsAsMutable
|
// The index of a group in this list is always equal to its ID
|
||||||
private var groupsAsMutable : List <Group> = emptyList ()
|
val groups : State < List <Group> > get () = groupsAsMutable
|
||||||
|
private val groupsAsMutable : MutableState < List <Group> > = mutableStateOf ( 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) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setCurrentScene ( groupId : Int , scene : Scene ) {
|
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 )
|
if ( index == groupId ) group . copy ( currentScene = scene )
|
||||||
else group
|
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 android.os.*
|
||||||
import androidx.activity.compose.*
|
import androidx.activity.compose.*
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
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.*
|
||||||
|
@ -23,14 +22,12 @@ class MainActivity : EdgeToEdgeActivity () {
|
||||||
|
|
||||||
Config . groups . forEach { AppState . addGroup (it) }
|
Config . groups . forEach { AppState . addGroup (it) }
|
||||||
|
|
||||||
val groups = mutableStateOf ( AppState . groups )
|
|
||||||
AppState . subscribe { groups . value = it }
|
|
||||||
|
|
||||||
lifecycleScope . launch (IO) {
|
lifecycleScope . launch (IO) {
|
||||||
val prefs = dataStore . data . firstOrNull ()
|
val prefs = dataStore . data . firstOrNull ()
|
||||||
val savedSceneName = prefs ?. get ( stringPreferencesKey ("scene") )
|
val savedSceneName = prefs ?. get ( stringPreferencesKey ("scene") )
|
||||||
if ( savedSceneName != null ) {
|
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 ) }
|
savedScene ?. let { AppState . setCurrentScene ( 0 , it ) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +35,7 @@ class MainActivity : EdgeToEdgeActivity () {
|
||||||
setContent {
|
setContent {
|
||||||
AppFrame {
|
AppFrame {
|
||||||
Column ( Modifier . width ( 300 . dp ) ) {
|
Column ( Modifier . width ( 300 . dp ) ) {
|
||||||
groups . value . forEach { group ->
|
AppState . 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 )
|
||||||
|
@ -54,7 +51,7 @@ class MainActivity : EdgeToEdgeActivity () {
|
||||||
override fun onStop () {
|
override fun onStop () {
|
||||||
super . onStop ()
|
super . onStop ()
|
||||||
|
|
||||||
val currentScene = AppState . groups [0] . currentScene
|
val currentScene = AppState . groups . value [0] . currentScene
|
||||||
if ( currentScene != null ) lifecycleScope . launch (IO) {
|
if ( currentScene != null ) lifecycleScope . launch (IO) {
|
||||||
dataStore . edit {
|
dataStore . edit {
|
||||||
it [ stringPreferencesKey ("scene") ] = currentScene . name
|
it [ stringPreferencesKey ("scene") ] = currentScene . name
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue