Skip to content

Commit

Permalink
safely remove navigables
Browse files Browse the repository at this point in the history
  • Loading branch information
gerin98 committed Oct 24, 2024
1 parent 8ad37ae commit c9fa578
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.wealthfront.magellan.navigation

import android.content.Context
import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.annotation.VisibleForTesting
import com.wealthfront.magellan.Direction
import com.wealthfront.magellan.ScreenContainer
Expand Down Expand Up @@ -43,15 +45,19 @@ public open class LazySetNavigator(
lifecycleRegistry.attachToLifecycleWithMaxState(navigable, LifecycleLimit.CREATED)
}

@RequiresApi(Build.VERSION_CODES.N)
public fun removeNavigables(navigables: Set<NavigableCompat>) {
for (navigable in navigables) {
removeNavigable(navigable)
}
}

@RequiresApi(Build.VERSION_CODES.N)
public fun removeNavigable(navigable: NavigableCompat) {
existingNavigables.remove(navigable)
lifecycleRegistry.removeFromLifecycle(navigable)
existingNavigables.removeIf { it == navigable }
if (lifecycleRegistry.children.contains(navigable)) {
lifecycleRegistry.removeFromLifecycle(navigable)
}
}

public fun safeAddNavigable(navigable: NavigableCompat) {
Expand Down Expand Up @@ -87,6 +93,7 @@ public open class LazySetNavigator(

override fun onDestroy(context: Context) {
lifecycleRegistry.children.forEach { lifecycleRegistry.removeFromLifecycle(it) }
existingNavigables.clear()
}

public fun replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,36 @@ class LazySetNavigatorTest {
verify(exactly = 0) { navigableListener.onNavigatedTo(any()) }
}

@Test
fun attemptRemoveNavigables_afterOnDestroy() {
navigator.addNavigables(setOf(step1, step2, step3, step4))
navigator.transitionToState(LifecycleState.Resumed(activityController.get()))

navigator.replace(step1, CrossfadeTransition())
step1.view!!.viewTreeObserver.dispatchOnPreDraw()
shadowOf(Looper.getMainLooper()).idle()
assertThat(navigator.containerView!!.childCount).isEqualTo(1)
assertThat(navigator.containerView!!.getChildAt(0)).isEqualTo(step1.view)
assertThat(step1.currentState).isInstanceOf(LifecycleState.Resumed::class.java)
assertThat(step2.currentState).isInstanceOf(LifecycleState.Created::class.java)
assertThat(step3.currentState).isInstanceOf(LifecycleState.Created::class.java)
assertThat(step4.currentState).isInstanceOf(LifecycleState.Created::class.java)
assertThat(navigator.existingNavigables).containsExactly(step1, step2, step3, step4)

verify { navigableListener.beforeNavigation() }
verify(exactly = 0) { navigableListener.onNavigatedFrom(any()) }
verify { navigableListener.onNavigatedTo(step1) }
verify { navigableListener.afterNavigation() }
clearMocks(navigableListener)
initMocks()

navigator.transitionToState(LifecycleState.Destroyed)
navigator.removeNavigables(setOf(step2, step4))

assertThat(navigator.existingNavigables).isEmpty()
verify(exactly = 0) { navigableListener.onNavigatedTo(any()) }
}

@Test
fun updateMultipleNavigables() {
navigator.updateNavigables(setOf(step1, step2)) {}
Expand Down

0 comments on commit c9fa578

Please sign in to comment.