Skip to content

Commit

Permalink
dispose steps after transition
Browse files Browse the repository at this point in the history
  • Loading branch information
shpasha committed May 21, 2024
1 parent 6f8fdf0 commit a3fe7a7
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 17 deletions.
3 changes: 2 additions & 1 deletion samples/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
</activity>
<activity android:name=".stateStack.StateStackActivity"/>
<activity android:name=".basicNavigation.BasicNavigationActivity"/>
<activity android:name=".tabNavigation.TabNavigationActivity"/>
<activity android:name=".disposeSample.DisposeWhenTransitionFinishedSampleActivity"/>
<activity android:name=".disposeSample.DisposeWhenStackChangedSampleActivity"/>
<activity android:name=".nestedNavigation.NestedNavigationActivity"/>
<activity android:name=".parcelableScreen.ParcelableActivity"/>
<activity android:name=".screenModel.ScreenModelActivity"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import cafe.adriel.voyager.sample.androidLegacy.LegacyActivity
import cafe.adriel.voyager.sample.androidViewModel.AndroidViewModelActivity
import cafe.adriel.voyager.sample.basicNavigation.BasicNavigationActivity
import cafe.adriel.voyager.sample.bottomSheetNavigation.BottomSheetNavigationActivity
import cafe.adriel.voyager.sample.disposeSample.DisposeWhenStackChangedSampleActivity
import cafe.adriel.voyager.sample.disposeSample.DisposeWhenTransitionFinishedSampleActivity
import cafe.adriel.voyager.sample.hiltIntegration.HiltMainActivity
import cafe.adriel.voyager.sample.kodeinIntegration.KodeinIntegrationActivity
import cafe.adriel.voyager.sample.koinIntegration.KoinIntegrationActivity
Expand Down Expand Up @@ -52,6 +54,8 @@ class SampleActivity : ComponentActivity() {
contentPadding = PaddingValues(24.dp)
) {
item {
StartSampleButton<DisposeWhenTransitionFinishedSampleActivity>("DisposeWhenTransitionFinished")
StartSampleButton<DisposeWhenStackChangedSampleActivity>("DisposeWhenStackChanged")
StartSampleButton<StateStackActivity>("SnapshotStateStack")
StartSampleButton<BasicNavigationActivity>("Basic Navigation")
StartSampleButton<ParcelableActivity>("Basic Navigation with Parcelable")
Expand All @@ -76,7 +80,9 @@ class SampleActivity : ComponentActivity() {

Button(
onClick = { context.startActivity(Intent(this, T::class.java)) },
modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp)
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 8.dp)
) {
Text(text = text)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cafe.adriel.voyager.sample.disposeSample

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.tween
import androidx.compose.runtime.Composable
import cafe.adriel.voyager.navigator.DisposeStepsBehavior
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.NavigatorDisposeBehavior
import cafe.adriel.voyager.transitions.SlideTransition

open class DisposeSampleActivity(
private val disposeStepsBehavior: DisposeStepsBehavior
) : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
Content()
}
}

@Composable
fun Content() {
Navigator(
screen = SampleScreen(0),
disposeBehavior = NavigatorDisposeBehavior(
disposeStepsBehavior = disposeStepsBehavior
)
) {
SlideTransition(
navigator = it,
animationSpec = tween(1000)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cafe.adriel.voyager.sample.disposeSample

import cafe.adriel.voyager.navigator.DisposeStepsBehavior

class DisposeWhenStackChangedSampleActivity : DisposeSampleActivity(DisposeStepsBehavior.DisposeWhenStackChanged)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cafe.adriel.voyager.sample.disposeSample

import cafe.adriel.voyager.navigator.DisposeStepsBehavior

class DisposeWhenTransitionFinishedSampleActivity : DisposeSampleActivity(
DisposeStepsBehavior.DisposeWhenTransitionFinished
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cafe.adriel.voyager.sample.disposeSample

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.screen.ScreenKey
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SampleScreenModel : ViewModel() {

init {
Log.d("SampleScreenModel", "start doing job")
viewModelScope.launch {
while (true) {
Log.d("SampleScreenModel", "Doing job")
delay(1000)
}
}
}

override fun onCleared() {
Log.d("SampleScreenModel", "Disposed")
}
}

data class SampleScreen(
private val index: Int
) : Screen {

override val key: ScreenKey = "SampleScreen$index"

@Composable
override fun Content() {
if (index == 1) {
viewModel(key = key) { SampleScreenModel() }
}

val navigator = LocalNavigator.currentOrThrow

Column(
modifier = Modifier
.fillMaxSize()
.padding(40.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(text = "Screen $index")

Spacer(modifier = Modifier.height(20.dp))

Button(
modifier = Modifier.fillMaxWidth(),
onClick = { navigator.push(SampleScreen(index = index + 1)) }
) {
Text(text = "Next")
}

Button(
modifier = Modifier.fillMaxWidth(),
onClick = { navigator.pop() }
) {
Text(text = "Back")
}
}
}
}
20 changes: 15 additions & 5 deletions voyager-navigator/api/android/voyager-navigator.api
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ public final class cafe/adriel/voyager/navigator/ComposableSingletons$NavigatorK
public final fun getLambda-2$voyager_navigator_release ()Lkotlin/jvm/functions/Function3;
}

public final class cafe/adriel/voyager/navigator/DisposeStepsBehavior : java/lang/Enum {
public static final field DisposeWhenStackChanged Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public static final field DisposeWhenTransitionFinished Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public static final field None Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public static fun values ()[Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
}

public final class cafe/adriel/voyager/navigator/Navigator : cafe/adriel/voyager/core/stack/Stack {
public static final field $stable I
public fun clearEvent ()V
Expand Down Expand Up @@ -41,16 +50,17 @@ public final class cafe/adriel/voyager/navigator/Navigator : cafe/adriel/voyager

public final class cafe/adriel/voyager/navigator/NavigatorDisposeBehavior {
public static final field $stable I
public fun <init> ()V
public fun <init> (ZLcafe/adriel/voyager/navigator/DisposeStepsBehavior;)V
public synthetic fun <init> (ZLcafe/adriel/voyager/navigator/DisposeStepsBehavior;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (ZZ)V
public synthetic fun <init> (ZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Z
public final fun component2 ()Z
public final fun copy (ZZ)Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;
public static synthetic fun copy$default (Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;ZZILjava/lang/Object;)Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;
public final fun component2 ()Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public final fun copy (ZLcafe/adriel/voyager/navigator/DisposeStepsBehavior;)Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;
public static synthetic fun copy$default (Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;ZLcafe/adriel/voyager/navigator/DisposeStepsBehavior;ILjava/lang/Object;)Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;
public fun equals (Ljava/lang/Object;)Z
public final fun getDisposeNestedNavigators ()Z
public final fun getDisposeSteps ()Z
public final fun getDisposeStepsBehavior ()Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
Expand Down
20 changes: 15 additions & 5 deletions voyager-navigator/api/desktop/voyager-navigator.api
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ public final class cafe/adriel/voyager/navigator/ComposableSingletons$NavigatorK
public final fun getLambda-2$voyager_navigator ()Lkotlin/jvm/functions/Function3;
}

public final class cafe/adriel/voyager/navigator/DisposeStepsBehavior : java/lang/Enum {
public static final field DisposeWhenStackChanged Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public static final field DisposeWhenTransitionFinished Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public static final field None Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public static fun values ()[Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
}

public final class cafe/adriel/voyager/navigator/Navigator : cafe/adriel/voyager/core/stack/Stack {
public static final field $stable I
public fun clearEvent ()V
Expand Down Expand Up @@ -41,16 +50,17 @@ public final class cafe/adriel/voyager/navigator/Navigator : cafe/adriel/voyager

public final class cafe/adriel/voyager/navigator/NavigatorDisposeBehavior {
public static final field $stable I
public fun <init> ()V
public fun <init> (ZLcafe/adriel/voyager/navigator/DisposeStepsBehavior;)V
public synthetic fun <init> (ZLcafe/adriel/voyager/navigator/DisposeStepsBehavior;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (ZZ)V
public synthetic fun <init> (ZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Z
public final fun component2 ()Z
public final fun copy (ZZ)Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;
public static synthetic fun copy$default (Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;ZZILjava/lang/Object;)Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;
public final fun component2 ()Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public final fun copy (ZLcafe/adriel/voyager/navigator/DisposeStepsBehavior;)Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;
public static synthetic fun copy$default (Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;ZLcafe/adriel/voyager/navigator/DisposeStepsBehavior;ILjava/lang/Object;)Lcafe/adriel/voyager/navigator/NavigatorDisposeBehavior;
public fun equals (Ljava/lang/Object;)Z
public final fun getDisposeNestedNavigators ()Z
public final fun getDisposeSteps ()Z
public final fun getDisposeStepsBehavior ()Lcafe/adriel/voyager/navigator/DisposeStepsBehavior;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
Expand Down
1 change: 1 addition & 0 deletions voyager-navigator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ kotlin {
sourceSets {
commonMain.dependencies {
api(projects.voyagerCore)
implementation(libs.coroutines.core)
compileOnly(compose.runtime)
compileOnly(compose.runtimeSaveable)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ import cafe.adriel.voyager.navigator.internal.ChildrenNavigationDisposableEffect
import cafe.adriel.voyager.navigator.internal.LocalNavigatorStateHolder
import cafe.adriel.voyager.navigator.internal.NavigatorBackHandler
import cafe.adriel.voyager.navigator.internal.NavigatorDisposableEffect
import cafe.adriel.voyager.navigator.internal.StepDisposableAfterTransitionEffect
import cafe.adriel.voyager.navigator.internal.StepDisposableEffect
import cafe.adriel.voyager.navigator.internal.getNavigatorScreenLifecycleProvider
import cafe.adriel.voyager.navigator.internal.rememberNavigator
import cafe.adriel.voyager.navigator.lifecycle.NavigatorKey
import kotlinx.coroutines.channels.Channel

public typealias NavigatorContent = @Composable (navigator: Navigator) -> Unit

public typealias OnBackPressed = ((currentScreen: Screen) -> Boolean)?

@InternalVoyagerApi
public val LocalTransitionFinishedEvents: ProvidableCompositionLocal<Channel<Unit>> =
staticCompositionLocalOf { error("No LocalTransitionFinishedEvents provided") }

public val LocalNavigator: ProvidableCompositionLocal<Navigator?> =
staticCompositionLocalOf { null }

Expand Down Expand Up @@ -86,11 +92,20 @@ public fun Navigator(
NavigatorDisposableEffect(navigator)
}

val transitionFinishedEvents = remember { Channel<Unit>() }

CompositionLocalProvider(
LocalNavigator provides navigator
LocalNavigator provides navigator,
LocalTransitionFinishedEvents provides transitionFinishedEvents
) {
if (disposeBehavior.disposeSteps) {
StepDisposableEffect(navigator)
when (disposeBehavior.disposeStepsBehavior) {
DisposeStepsBehavior.DisposeWhenTransitionFinished -> StepDisposableAfterTransitionEffect(
transitionFinishedEvents = transitionFinishedEvents,
navigator = navigator
)

DisposeStepsBehavior.DisposeWhenStackChanged -> StepDisposableEffect(navigator)
DisposeStepsBehavior.None -> Unit
}

NavigatorBackHandler(navigator, onBackPressed)
Expand Down Expand Up @@ -180,10 +195,27 @@ public class Navigator @InternalVoyagerApi constructor(
}
}

public enum class DisposeStepsBehavior {
DisposeWhenTransitionFinished,
DisposeWhenStackChanged,
None
}

public data class NavigatorDisposeBehavior(
val disposeNestedNavigators: Boolean = true,
val disposeSteps: Boolean = true
)
val disposeStepsBehavior: DisposeStepsBehavior
) {
public constructor(
disposeNestedNavigators: Boolean = true,
disposeSteps: Boolean = true
) : this(
disposeNestedNavigators = disposeNestedNavigators,
disposeStepsBehavior = when {
disposeSteps -> DisposeStepsBehavior.DisposeWhenStackChanged
else -> DisposeStepsBehavior.None
}
)
}

@InternalVoyagerApi
@Composable
Expand Down
Loading

0 comments on commit a3fe7a7

Please sign in to comment.