-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented being robust to configuration change;
- Loading branch information
1 parent
6573ca6
commit f0c4894
Showing
3 changed files
with
37 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...lock_compose/src/main/java/xyz/teamgravity/pin_lock_compose/rememberMutableStateListOf.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |