Skip to content

Commit c5e790c

Browse files
committed
Add new inline remember variants
1 parent 5c3d1ba commit c5e790c

File tree

7 files changed

+63
-24
lines changed

7 files changed

+63
-24
lines changed

app/src/commonMain/kotlin/io/github/xxfast/decompose/router/screens/HomeScreen.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ import io.github.xxfast.decompose.router.screens.pages.PagesScreen
2929
import io.github.xxfast.decompose.router.screens.slot.SlotScreen
3030
import io.github.xxfast.decompose.router.screens.stack.StackScreen
3131

32-
@OptIn(ExperimentalDecomposeApi::class, ExperimentalFoundationApi::class)
3332
@Composable
3433
fun HomeScreen() {
35-
val pager: Router<HomeScreens> = rememberRouter(HomeScreens::class) { pagesOf(Stack, Pages, Slot) }
34+
val pager: Router<HomeScreens> = rememberRouter { pagesOf(Stack, Pages, Slot) }
3635

3736
Scaffold(
3837
bottomBar = {

app/src/commonMain/kotlin/io/github/xxfast/decompose/router/screens/pages/PagesScreen.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.xxfast.decompose.router.screens.pages
22

3-
import androidx.compose.foundation.ExperimentalFoundationApi
43
import androidx.compose.foundation.layout.Box
54
import androidx.compose.foundation.layout.WindowInsets
65
import androidx.compose.foundation.layout.fillMaxSize
@@ -20,21 +19,17 @@ import androidx.compose.ui.Alignment
2019
import androidx.compose.ui.Modifier
2120
import androidx.compose.ui.platform.testTag
2221
import androidx.compose.ui.unit.dp
23-
import com.arkivanov.decompose.ExperimentalDecomposeApi
2422
import com.arkivanov.decompose.router.pages.Pages
2523
import com.arkivanov.decompose.router.pages.selectFirst
2624
import io.github.xxfast.decompose.router.pages.RoutedContent
25+
import io.github.xxfast.decompose.router.pages.Router
2726
import io.github.xxfast.decompose.router.pages.rememberRouter
2827
import io.github.xxfast.decompose.router.screens.PAGER
2928

30-
@OptIn(
31-
ExperimentalDecomposeApi::class,
32-
ExperimentalFoundationApi::class,
33-
ExperimentalMaterial3Api::class
34-
)
29+
@OptIn(ExperimentalMaterial3Api::class)
3530
@Composable
3631
fun PagesScreen() {
37-
val router = rememberRouter(PagesScreens::class) {
32+
val router: Router<PagesScreens> = rememberRouter {
3833
Pages(
3934
items = List(10) { PagesScreens(it) },
4035
selectedIndex = 0,

app/src/commonMain/kotlin/io/github/xxfast/decompose/router/screens/slot/SlotScreen.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ import io.github.xxfast.decompose.router.slot.rememberRouter
3737
@OptIn(ExperimentalMaterial3Api::class)
3838
@Composable
3939
fun SlotScreen() {
40-
val dialogRouter: Router<DialogScreens> = rememberRouter(DialogScreens::class) { null }
41-
val bottomSheetRouter: Router<BottomSheetScreens> =
42-
rememberRouter(BottomSheetScreens::class) { null }
40+
val dialogRouter: Router<DialogScreens> = rememberRouter { null }
41+
val bottomSheetRouter: Router<BottomSheetScreens> = rememberRouter { null }
4342

4443
Scaffold(
4544
topBar = {

app/src/commonMain/kotlin/io/github/xxfast/decompose/router/screens/stack/StackScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import io.github.xxfast.decompose.router.stack.rememberRouter
2222
@OptIn(ExperimentalDecomposeApi::class)
2323
@Composable
2424
fun StackScreen() {
25-
val router: Router<StackScreens> = rememberRouter(StackScreens::class) { listOf(List) }
25+
val router: Router<StackScreens> = rememberRouter { listOf(List) }
2626

2727
RoutedContent(
2828
router = router,

decompose-router/src/commonMain/kotlin/io/github/xxfast/decompose/router/pages/Router.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,32 @@ import kotlinx.serialization.Serializable
1919
import kotlinx.serialization.serializerOrNull
2020
import kotlin.reflect.KClass
2121

22-
@OptIn(ExperimentalDecomposeApi::class)
2322
class Router<C: Any> internal constructor(
2423
private val navigation: PagesNavigation<C>,
2524
val pages: State<ChildPages<C, RouterContext>>,
2625
) : PagesNavigation<C> by navigation
2726

28-
@OptIn(ExperimentalDecomposeApi::class, InternalSerializationApi::class)
27+
/***
28+
* Creates a router that retains pages of [C] configuration
29+
* @param key
30+
* @param initialPages initial list of pages
31+
* @param handleBackButton should the router handle back button
32+
*/
33+
@Suppress("DEPRECATION") // For migration purposes
34+
@Composable
35+
inline fun <reified C: @Serializable Any> rememberRouter(
36+
key: Any = C::class,
37+
handleBackButton: Boolean = true,
38+
noinline initialPages: () -> Pages<C>,
39+
): Router<C> = rememberRouter(
40+
type = C::class,
41+
key = key,
42+
handleBackButton = handleBackButton,
43+
initialPages = initialPages
44+
)
45+
46+
@Deprecated(message = "Use rememberRouter with reified type parameter")
47+
@OptIn(InternalSerializationApi::class)
2948
@Composable
3049
fun <C: @Serializable Any> rememberRouter(
3150
type: KClass<C>,

decompose-router/src/commonMain/kotlin/io/github/xxfast/decompose/router/slot/Router.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ class Router<C : Any> internal constructor(
2222
val slot: State<ChildSlot<C, RouterContext>>,
2323
): SlotNavigation<C> by navigation
2424

25+
26+
/***
27+
* Creates a router that retains a slot of [C] configuration
28+
* @param key
29+
* @param initialConfiguration initial configuration
30+
* @param handleBackButton should the router handle back button
31+
*/
32+
@Suppress("DEPRECATION") // For migration purposes
33+
@Composable
34+
inline fun <reified C: @Serializable Any> rememberRouter(
35+
key: Any = C::class,
36+
handleBackButton: Boolean = true,
37+
noinline initialConfiguration: () -> C?,
38+
): Router<C> = rememberRouter(
39+
type = C::class,
40+
key = key,
41+
handleBackButton = handleBackButton,
42+
initialConfiguration = initialConfiguration
43+
)
44+
45+
@Deprecated(message = "Use rememberRouter with reified type parameter")
2546
@OptIn(InternalSerializationApi::class)
2647
@Composable
2748
fun <C : @Serializable Any> rememberRouter(

decompose-router/src/commonMain/kotlin/io/github/xxfast/decompose/router/stack/Router.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,28 @@ class Router<C: Any> internal constructor(
2626
val stack: State<ChildStack<C, RouterContext>>,
2727
) : StackNavigation<C> by navigation
2828

29-
// TODO: Add this back to API once this [issue](https://github.com/JetBrains/compose-multiplatform/issues/2900) is fixed
30-
//@Composable
31-
//inline fun <reified C: @Serializable Any> rememberRouter(
32-
// key: Any = C::class,
33-
// handleBackButton: Boolean = true,
34-
// noinline initialStack: () -> List<C>,
35-
//): Router<C> = rememberRouter(C::class, key, handleBackButton, initialStack)
29+
/***
30+
* Creates a router that retains a stack of [C] configuration
31+
* @param key
32+
* @param initialStack initial stack of configurations
33+
* @param handleBackButton should the router handle back button
34+
*/
35+
@Suppress("DEPRECATION") // For migration purposes
36+
@Composable
37+
inline fun <reified C: @Serializable Any> rememberRouter(
38+
key: Any = C::class,
39+
handleBackButton: Boolean = true,
40+
noinline initialStack: () -> List<C>,
41+
): Router<C> = rememberRouter(C::class, key, handleBackButton, initialStack = initialStack)
3642

3743
/***
3844
* Creates a router that retains a stack of [C] configuration
39-
*
4045
* @param type configuration class type
4146
* @param key
4247
* @param initialStack initial stack of configurations
4348
* @param handleBackButton should the router handle back button
4449
*/
50+
@Deprecated(message = "Use rememberRouter with reified type parameter")
4551
@OptIn(InternalSerializationApi::class)
4652
@Composable
4753
fun <C: @Serializable Any> rememberRouter(

0 commit comments

Comments
 (0)