-
Notifications
You must be signed in to change notification settings - Fork 72
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
Support removing navigables from LazySetNavigator #304
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,9 @@ public open class LazySetNavigator( | |
private val navigationPropagator: NavigationPropagator = NavigationPropagator | ||
private var ongoingTransition: MagellanTransition? = null | ||
|
||
@VisibleForTesting | ||
internal var existingNavigables: MutableSet<NavigableCompat> = mutableSetOf() | ||
|
||
@VisibleForTesting | ||
internal var containerView: ScreenContainer? = null | ||
private var currentNavigable: NavigableCompat? = null | ||
|
@@ -36,9 +39,39 @@ public open class LazySetNavigator( | |
} | ||
|
||
public fun addNavigable(navigable: NavigableCompat) { | ||
existingNavigables.add(navigable) | ||
lifecycleRegistry.attachToLifecycleWithMaxState(navigable, LifecycleLimit.CREATED) | ||
} | ||
|
||
public fun removeNavigables(navigables: Set<NavigableCompat>) { | ||
for (navigable in navigables) { | ||
removeNavigable(navigable) | ||
} | ||
} | ||
|
||
public fun removeNavigable(navigable: NavigableCompat) { | ||
existingNavigables.remove(navigable) | ||
lifecycleRegistry.removeFromLifecycle(navigable) | ||
} | ||
|
||
public fun safeAddNavigable(navigable: NavigableCompat) { | ||
if (!existingNavigables.contains(navigable)) { | ||
addNavigable(navigable) | ||
} | ||
} | ||
|
||
public fun updateNavigables(navigables: Set<NavigableCompat>, handleCurrentTabRemoval: () -> Unit) { | ||
val navigablesToRemove = existingNavigables subtract navigables | ||
val navigablesToAdd = navigables subtract existingNavigables | ||
|
||
if (navigablesToRemove.contains(currentNavigable)) { | ||
handleCurrentTabRemoval() | ||
} | ||
Comment on lines
+74
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an edge case to be aware of when removing a Navigable that is currently visible. We need to switch to another navigable before we can remove it because once you call |
||
|
||
removeNavigables(navigablesToRemove) | ||
addNavigables(navigablesToAdd) | ||
} | ||
|
||
override fun onShow(context: Context) { | ||
containerView = container() | ||
currentNavigable?.let { currentNavigable -> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a risk of
ConcurrentModificationException
if one of the navigables tries to add / remove a navigable in itsonDestroy
? I don't know enough aboutmutableSetOf
to know if it allows concurrent modification