|
|
@@ -1,5 +1,6 @@
|
|
|
package org.sirekanyan.warmongr
|
|
|
|
|
|
+import android.os.Parcelable
|
|
|
import androidx.compose.foundation.lazy.LazyListState
|
|
|
import androidx.compose.material.ExperimentalMaterialApi
|
|
|
import androidx.compose.material.ModalBottomSheetState
|
|
|
@@ -10,26 +11,35 @@ import androidx.compose.runtime.getValue
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
import androidx.compose.runtime.remember
|
|
|
import androidx.compose.runtime.rememberCoroutineScope
|
|
|
+import androidx.compose.runtime.saveable.Saver
|
|
|
+import androidx.compose.runtime.saveable.rememberSaveable
|
|
|
import androidx.compose.runtime.setValue
|
|
|
import androidx.compose.ui.platform.LocalDensity
|
|
|
+import androidx.compose.ui.text.TextRange
|
|
|
import androidx.compose.ui.text.input.TextFieldValue
|
|
|
import androidx.compose.ui.unit.Density
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
-import org.sirekanyan.warmongr.ext.isCyrillicResources
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
import kotlinx.coroutines.launch
|
|
|
+import kotlinx.parcelize.Parcelize
|
|
|
+import org.sirekanyan.warmongr.ext.isCyrillicResources
|
|
|
|
|
|
@Composable
|
|
|
fun rememberMainState(): MainState {
|
|
|
val coroutineScope = rememberCoroutineScope()
|
|
|
val density = LocalDensity.current
|
|
|
val isCyrillic = isCyrillicResources()
|
|
|
- return remember { MainState(coroutineScope, density, isCyrillic) }
|
|
|
+ val searchState = rememberSaveable(saver = SearchState.Saver) { SearchState() }
|
|
|
+ return remember { MainState(coroutineScope, density, isCyrillic, searchState) }
|
|
|
}
|
|
|
|
|
|
-class MainState(coroutineScope: CoroutineScope, density: Density, val isCyrillic: Boolean) {
|
|
|
+class MainState(
|
|
|
+ coroutineScope: CoroutineScope,
|
|
|
+ density: Density,
|
|
|
+ val isCyrillic: Boolean,
|
|
|
+ val search: SearchState,
|
|
|
+) {
|
|
|
val list = ListState()
|
|
|
- val search = SearchState()
|
|
|
val progress = ProgressState()
|
|
|
val toolbarElevation by derivedStateOf {
|
|
|
if (search.isOpened || !list.isTopVisible) {
|
|
|
@@ -50,6 +60,7 @@ class ListState {
|
|
|
}
|
|
|
|
|
|
class SearchState {
|
|
|
+
|
|
|
var tag by mutableStateOf<Int?>(null)
|
|
|
var query by mutableStateOf(TextFieldValue())
|
|
|
var isOpened by mutableStateOf(false)
|
|
|
@@ -62,6 +73,39 @@ class SearchState {
|
|
|
tag?.let { "tags:$it" }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ companion object {
|
|
|
+
|
|
|
+ @Parcelize
|
|
|
+ class Saveable(
|
|
|
+ val isOpened: Boolean,
|
|
|
+ val tag: Int?,
|
|
|
+ val query: String,
|
|
|
+ val start: Int,
|
|
|
+ val end: Int,
|
|
|
+ ) : Parcelable
|
|
|
+
|
|
|
+ val Saver = Saver<SearchState, Saveable>(
|
|
|
+ save = {
|
|
|
+ Saveable(
|
|
|
+ it.isOpened,
|
|
|
+ it.tag,
|
|
|
+ it.query.text,
|
|
|
+ it.query.selection.start,
|
|
|
+ it.query.selection.end,
|
|
|
+ )
|
|
|
+ },
|
|
|
+ restore = {
|
|
|
+ SearchState().apply {
|
|
|
+ isOpened = it.isOpened
|
|
|
+ tag = it.tag
|
|
|
+ query = TextFieldValue(it.query, TextRange(it.start, it.end))
|
|
|
+ }
|
|
|
+ },
|
|
|
+ )
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
class ProgressState {
|