Skip to content

Commit

Permalink
Implemented being robust to configuration change;
Browse files Browse the repository at this point in the history
  • Loading branch information
raheemadamboev committed Feb 26, 2023
1 parent 6573ca6 commit f0c4894
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -37,7 +38,7 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
var navigation by remember { mutableStateOf(Screen.Authenticate) }
var navigation by rememberSaveable { mutableStateOf(Screen.Authenticate) }

when (navigation) {
Screen.Authenticate -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand Down Expand Up @@ -46,7 +47,7 @@ fun PinLock(
) {
var animate by remember { mutableStateOf(false) }

val numbers = remember { mutableStateListOf<Int>() }
val numbers = rememberMutableStateListOf<Int>()
val pinExists = remember { PinManager.pinExists() }

BasePinLock(
Expand Down Expand Up @@ -106,10 +107,10 @@ fun ChangePinLock(
onPinIncorrect: () -> Unit,
onPinChanged: () -> Unit,
) {
var authenticated by remember { mutableStateOf(false) }
var authenticated by rememberSaveable { mutableStateOf(false) }
var animate by remember { mutableStateOf(false) }

val numbers = remember { mutableStateListOf<Int>() }
val numbers = rememberMutableStateListOf<Int>()

BasePinLock(
title = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package xyz.teamgravity.pin_lock_compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.toMutableStateList

/**
* It behaves similarly to remember, but the stored value will survive the activity or process recreation using the saved instance
* state mechanism (for example it happens when the screen is rotated in the Android application).
*/
@Composable
internal fun <T : Any> rememberMutableStateListOf(vararg elements: T): SnapshotStateList<T> {
return rememberSaveable(
saver = listSaver(
save = { stateList ->
if (stateList.isNotEmpty()) {
val first = stateList.first()
if (!canBeSaved(first)) {
throw IllegalStateException("${first::class} cannot be saved. By default only types which can be stored in the Bundle class can be saved.")
}
}
stateList.toList()
},
restore = { it.toMutableStateList() }
)
) {
elements.toList().toMutableStateList()
}
}

0 comments on commit f0c4894

Please sign in to comment.