Skip to content

Commit

Permalink
Annotate StackNavigator#push as delicate API
Browse files Browse the repository at this point in the history
  • Loading branch information
arkivanov committed Jul 29, 2024
1 parent 4cb68bc commit 82f85be
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ annotation class ExperimentalDecomposeApi
@RequiresOptIn(level = RequiresOptIn.Level.WARNING)
@Retention(AnnotationRetention.BINARY)
annotation class FaultyDecomposeApi

/**
* Marks delicate Decompose API that requires special attention when used.
* See the docs of the annotated API for more information.
*/
@RequiresOptIn(level = RequiresOptIn.Level.WARNING)
@Retention(AnnotationRetention.BINARY)
annotation class DelicateDecomposeApi
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.arkivanov.decompose.router.stack

import com.arkivanov.decompose.DelicateDecomposeApi

/**
* A convenience method for [StackNavigator.navigate].
*/
Expand All @@ -11,12 +13,15 @@ fun <C : Any> StackNavigator<C>.navigate(transformer: (stack: List<C>) -> List<C
* Pushes the provided [configuration] at the top of the stack.
*
* Decompose will throw an exception if the provided [configuration] is already present in the stack.
* This usually happens when a component is pushed on user interaction (e.g. a button click).
* Consider using [pushNew] instead.
* You can also try enabling the experimental
* [Duplicate Configurations][com.arkivanov.decompose.DecomposeExperimentFlags.duplicateConfigurationsEnabled] feature
* to avoid the error.
* to avoid the error. But still, pushing duplicated components on top of the stack might be wrong.
*
* @param onComplete called when the navigation is finished (either synchronously or asynchronously).
*/
@DelicateDecomposeApi
inline fun <C : Any> StackNavigator<C>.push(configuration: C, crossinline onComplete: () -> Unit = {}) {
navigate(transformer = { it + configuration }, onComplete = { _, _ -> onComplete() })
}
Expand Down
2 changes: 1 addition & 1 deletion docs/navigation/stack/navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ There are `StackNavigator` [extension functions](https://github.com/arkivanov/De

### push(configuration)

Pushes the provided `Configuration` at the top of the stack. Decompose will throw an exception if the provided `Configuration` is already present in the stack.
Pushes the provided `Configuration` at the top of the stack. Decompose will throw an exception if the provided `Configuration` is already present in the stack. This usually happens when a component is pushed on user interaction (e.g. a button click). Consider using [pushNew](#pushnewconfiguration) instead.

!!! note "Illustration"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.arkivanov.decompose.router.stack.childStack
import com.arkivanov.decompose.router.stack.items
import com.arkivanov.decompose.router.stack.navigate
import com.arkivanov.decompose.router.stack.pop
import com.arkivanov.decompose.router.stack.push
import com.arkivanov.decompose.router.stack.pushNew
import com.arkivanov.decompose.value.Value
import com.arkivanov.sample.shared.cards.card.CardComponent
import com.arkivanov.sample.shared.cards.card.DefaultCardComponent
Expand Down Expand Up @@ -54,7 +54,7 @@ class DefaultCardsComponent(

val maxNumber = _stack.items.maxOf { it.configuration.number }

navigation.push(
navigation.pushNew(
Config(
color = COLORS[maxNumber % COLORS.size],
number = maxNumber + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.arkivanov.decompose.router.stack.StackNavigation
import com.arkivanov.decompose.router.stack.childStack
import com.arkivanov.decompose.router.stack.pop
import com.arkivanov.decompose.router.stack.popTo
import com.arkivanov.decompose.router.stack.push
import com.arkivanov.decompose.router.stack.pushNew
import com.arkivanov.decompose.value.Value
import com.arkivanov.sample.shared.counters.counter.CounterComponent
import com.arkivanov.sample.shared.counters.counter.DefaultCounterComponent
Expand Down Expand Up @@ -36,7 +36,7 @@ internal class DefaultCountersComponent(
componentContext = componentContext,
title = "Counter ${config.index}",
isBackEnabled = config.isBackEnabled,
onNext = { navigation.push(Config(index = config.index + 1, isBackEnabled = true)) },
onNext = { navigation.pushNew(Config(index = config.index + 1, isBackEnabled = true)) },
onPrev = navigation::pop,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.arkivanov.decompose.router.stack.ChildStack
import com.arkivanov.decompose.router.stack.StackNavigation
import com.arkivanov.decompose.router.stack.childStack
import com.arkivanov.decompose.router.stack.pop
import com.arkivanov.decompose.router.stack.push
import com.arkivanov.decompose.router.stack.pushNew
import com.arkivanov.decompose.value.Value
import com.arkivanov.sample.shared.dynamicfeatures.DynamicFeaturesComponent.Child
import com.arkivanov.sample.shared.dynamicfeatures.DynamicFeaturesComponent.Child.Feature1Child
Expand Down Expand Up @@ -51,7 +51,7 @@ internal class DefaultDynamicFeaturesComponent(
factory = { featureComponentContext ->
Feature1(
componentContext = featureComponentContext,
onFeature2 = { navigation.push(Config.Feature2(magicNumber = Random.nextInt())) },
onFeature2 = { navigation.pushNew(Config.Feature2(magicNumber = Random.nextInt())) },
)
}
)
Expand Down

0 comments on commit 82f85be

Please sign in to comment.