Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix app crashing when trying to replacing the last screen during navigation #4823

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## Next Release

### Fixes

- Fix app crashing when trying to replacing the last screen during navigation

## 2023-11-20-8926

### Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ import org.simple.clinic.R
import org.simple.clinic.databinding.SheetAlertFacilityChangeBinding
import org.simple.clinic.di.injector
import org.simple.clinic.facility.alertchange.Continuation.ContinueToActivity
import org.simple.clinic.facility.alertchange.Continuation.ContinueToScreen_Old
import org.simple.clinic.facility.change.FacilityChangeScreen
import org.simple.clinic.feature.Features
import org.simple.clinic.mobius.ViewRenderer
import org.simple.clinic.navigation.v2.Router
import org.simple.clinic.navigation.v2.ScreenKey
import org.simple.clinic.navigation.v2.ScreenResultBus
import org.simple.clinic.navigation.v2.Succeeded
import org.simple.clinic.navigation.v2.compat.FullScreenKey
import org.simple.clinic.navigation.v2.compat.wrap
import org.simple.clinic.navigation.v2.fragments.BaseBottomSheet
import org.simple.clinic.util.resolveFloat
import org.simple.clinic.util.setFragmentResultListener
Expand Down Expand Up @@ -142,10 +139,6 @@ class AlertFacilityChangeSheet :

private fun closeSheetWithContinuation() {
when (continuation) {
is ContinueToScreen_Old -> {
val screenKey = (continuation as ContinueToScreen_Old).screenKey
router.replaceTop(screenKey.wrap())
}
is ContinueToActivity -> {
val (intent, requestCode) = (continuation as ContinueToActivity).run {
intent to requestCode
Expand Down Expand Up @@ -191,9 +184,6 @@ class AlertFacilityChangeSheet :

sealed class Continuation : Parcelable {

@Parcelize
data class ContinueToScreen_Old(val screenKey: FullScreenKey) : Continuation()

@Parcelize
data class ContinueToScreen(val screenKey: ScreenKey) : Continuation()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data class History(val requests: List<NavRequest>) : Parcelable {
fun removeLast(): History {
require(requests.size > 1) {
val lastKey = requests.lastOrNull()?.key
"Cannot remove last key ($lastKey) when there is only one key left"
"Cannot remove last key (${lastKey?.analyticsName}) when there is only one key left"
}

return copy(requests = requests.dropLast(1))
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/org/simple/clinic/navigation/v2/Router.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ class Router(
}

fun replaceTop(screenKey: ScreenKey, transactionOptions: TransactionOptions? = null) {
val navRequest = Normal(screenKey)
if (history.matchesTop(navRequest)) {
return
}

val newHistory = history
.removeLast()
.add(Normal(screenKey))
.add(navRequest)

executeStateChange(newHistory, Direction.Replace, null, transactionOptions)
}
Expand All @@ -102,9 +107,14 @@ class Router(
screenKey: ScreenKey,
transactionOptions: TransactionOptions? = null
) {
val navRequest = ExpectingResult(requestType, screenKey)
if (history.matchesTop(navRequest)) {
return
}

val newHistory = history
.removeLast()
.add(ExpectingResult(requestType, screenKey))
.add(navRequest)

executeStateChange(newHistory, Direction.Forward, null, transactionOptions)
}
Expand Down