Skip to content

Commit

Permalink
Updated ComposeNavigator.currentKey to be non-null
Browse files Browse the repository at this point in the history
  • Loading branch information
chRyNaN committed Dec 30, 2021
1 parent 1bc7c78 commit eba2654
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class ComposeNavigationIntentNavigatorByKeyViewModel<I : NavigationIntent> inter
override val keyChanges: Flow<I>
get() = mutableKeyFlow.filterNotNull()

override val currentKey: I?
override val currentKey: I
get() = mutableKeyFlow.value

override val isInitialized: Boolean = true

private val mutableKeyFlow = MutableStateFlow<I?>(value = initialKey)
private val mutableKeyFlow = MutableStateFlow(value = initialKey)

private val keyStack = mutableListOf<I>()

Expand Down Expand Up @@ -78,7 +78,7 @@ class ComposeNavigationIntentNavigatorByKeyViewModel<I : NavigationIntent> inter
return wentBack
}

override fun canGoBack(): Boolean = keyStack.isNotEmpty()
override fun canGoBack(): Boolean = keyStack.size > 1

private fun addToStack(key: I) {
keyStack.add(key)
Expand All @@ -87,6 +87,6 @@ class ComposeNavigationIntentNavigatorByKeyViewModel<I : NavigationIntent> inter

private fun removeLastFromStack() {
keyStack.removeLast()
mutableKeyFlow.value = keyStack.lastOrNull()
mutableKeyFlow.value = keyStack.last()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ComposeNavigator<T> : Navigator {

val initialKey: T

val currentKey: T?
val currentKey: T

val keyChanges: Flow<T>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ class ComposeNavigatorByContentViewModel<T> internal constructor(
override val keyChanges: Flow<T>
get() = mutableKeyFlow.filterNotNull()

override val currentKey: T?
override val currentKey: T
get() = mutableKeyFlow.value

override var isInitialized: Boolean = false
private set

private val mutableKeyFlow = MutableStateFlow<T?>(value = null)
private val mutableKeyFlow = MutableStateFlow(value = initialKey)

private val contents = mutableMapOf<T, (@Composable ComposeNavigationContentScope<T>.() -> Unit)>()
private val contents = mutableMapOf<T, (@Composable ComposeNavigationContentScope<T>.() -> Unit)>(
initialKey to initialContent
)
private val keyStack = mutableListOf<T>()

@Composable
Expand Down Expand Up @@ -77,7 +79,7 @@ class ComposeNavigatorByContentViewModel<T> internal constructor(
return wentBack
}

override fun canGoBack(): Boolean = contents.isNotEmpty() && keyStack.isNotEmpty()
override fun canGoBack(): Boolean = contents.size > 1 && keyStack.size > 1

@Composable
override fun ComposeNavigationContentScope<T>.content(key: T) {
Expand Down Expand Up @@ -106,6 +108,6 @@ class ComposeNavigatorByContentViewModel<T> internal constructor(
contents.remove(removedKey)
}

mutableKeyFlow.value = keyStack.lastOrNull()
mutableKeyFlow.value = keyStack.last()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class ComposeNavigatorByKeyViewModel<T> internal constructor(
override val keyChanges: Flow<T>
get() = mutableKeyFlow.filterNotNull()

override val currentKey: T?
override val currentKey: T
get() = mutableKeyFlow.value

override val isInitialized: Boolean = true

private val mutableKeyFlow = MutableStateFlow<T?>(value = initialKey)
private val mutableKeyFlow = MutableStateFlow(value = initialKey)

private val keyStack = mutableListOf<T>()

Expand Down Expand Up @@ -62,7 +62,7 @@ class ComposeNavigatorByKeyViewModel<T> internal constructor(
return wentBack
}

override fun canGoBack(): Boolean = keyStack.isNotEmpty()
override fun canGoBack(): Boolean = keyStack.size > 1

private fun addToStack(key: T) {
keyStack.add(key)
Expand All @@ -71,6 +71,6 @@ class ComposeNavigatorByKeyViewModel<T> internal constructor(

private fun removeLastFromStack() {
keyStack.removeLast()
mutableKeyFlow.value = keyStack.lastOrNull()
mutableKeyFlow.value = keyStack.last()
}
}

0 comments on commit eba2654

Please sign in to comment.