-
-
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.
- Loading branch information
Peter Bryant
committed
Oct 5, 2021
1 parent
0683340
commit e1b140d
Showing
5 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
compose/src/main/java/com/ptrbrynt/kotlin_bloc/compose/BlocSaver.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,17 @@ | ||
package com.ptrbrynt.kotlin_bloc.compose | ||
|
||
import androidx.compose.runtime.saveable.Saver | ||
import androidx.compose.runtime.saveable.SaverScope | ||
import com.ptrbrynt.kotlin_bloc.core.BlocBase | ||
|
||
/** | ||
* A [Saver] which enables the state of a Bloc or Cubit to be saved | ||
* and restored. | ||
*/ | ||
internal fun <State : Any, B : BlocBase<State>> blocSaver( | ||
save: SaverScope.(B) -> State = { it.state }, | ||
restore: (State) -> B, | ||
) = Saver( | ||
save = save, | ||
restore = { restore(it) }, | ||
) |
55 changes: 55 additions & 0 deletions
55
compose/src/main/java/com/ptrbrynt/kotlin_bloc/compose/RememberSaveableBloc.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,55 @@ | ||
package com.ptrbrynt.kotlin_bloc.compose | ||
|
||
import android.os.Bundle | ||
import android.os.Parcelable | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.saveable.SaverScope | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import com.ptrbrynt.kotlin_bloc.core.BlocBase | ||
|
||
/** | ||
* Remembers the [State] of a Bloc or Cubit. | ||
* | ||
* The [State] will survive activity or process recreation (e.g. when the screen is rotated). | ||
* | ||
* You must provide a [create] function, which should return an instance of the Bloc or Cubit | ||
* with the given state as its initial state. | ||
* | ||
* If the [State] **cannot** be saved in a [Bundle] (i.e. it's not a primitive or [Parcelable]) | ||
* then you should provide a custom [save] function which converts your [State] into something which | ||
* can be saved in a [Bundle]. | ||
* | ||
* If you omit the [save] parameter, [rememberSaveableBloc] assumes that the [State] type can be | ||
* saved in a [Bundle]. | ||
* | ||
* ```kotlin | ||
* enum class CounterEvent { Incremented, Decremented } | ||
* | ||
* class CounterBloc(initial: Int): Bloc<CounterEvent, Int>(initial) { | ||
* | ||
* init { | ||
* on<CounterEvent> { | ||
* // ... | ||
* } | ||
* } | ||
* } | ||
* | ||
* @Composable | ||
* fun Counter() { | ||
* val bloc = rememberSaveableBloc(initialState = 0) { CounterBloc(it) } | ||
* | ||
* // ... | ||
* } | ||
* ``` | ||
* | ||
* @throws AssertionError if no [save] parameter is provided and the [State] type cannot be saved. | ||
*/ | ||
@Composable | ||
fun <State : Any, B : BlocBase<State>> rememberSaveableBloc( | ||
save: SaverScope.(B) -> State = { | ||
assert(canBeSaved(it)) | ||
it.state | ||
}, | ||
initialState: State, | ||
create: (State) -> B, | ||
) = rememberSaveable(saver = blocSaver(save, create), init = { create(initialState) }) |
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
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