Skip to content

Commit

Permalink
Fix app crashing when trying to replacing the last screen during navi…
Browse files Browse the repository at this point in the history
  • Loading branch information
msasikanth authored Nov 22, 2023
1 parent 47f8990 commit 78e8538
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
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

0 comments on commit 78e8538

Please sign in to comment.