-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from copper-leaf/dev
Dev
- Loading branch information
Showing
17 changed files
with
492 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...Main/kotlin/com/copperleaf/ballast/navigation/browser/BaseBrowserNavigationInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.copperleaf.ballast.navigation.browser | ||
|
||
import com.copperleaf.ballast.Queued | ||
import com.copperleaf.ballast.awaitViewModelStart | ||
import com.copperleaf.ballast.events | ||
import com.copperleaf.ballast.navigation.internal.Uri | ||
import com.copperleaf.ballast.navigation.internal.UriBuilder | ||
import com.copperleaf.ballast.navigation.routing.Route | ||
import com.copperleaf.ballast.navigation.routing.RouterContract | ||
import com.copperleaf.ballast.navigation.routing.build | ||
import com.copperleaf.ballast.navigation.routing.directions | ||
import com.copperleaf.ballast.navigation.routing.mapCurrentDestination | ||
import com.copperleaf.ballast.navigation.vm.RouterInterceptor | ||
import com.copperleaf.ballast.navigation.vm.RouterInterceptorScope | ||
import com.copperleaf.ballast.navigation.vm.RouterNotification | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.CoroutineStart | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.filterIsInstance | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.mapNotNull | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.joinAll | ||
import kotlinx.coroutines.launch | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
public abstract class BaseBrowserNavigationInterceptor<T : Route> internal constructor( | ||
private val initialRoute: T | ||
) : RouterInterceptor<T> { | ||
|
||
internal abstract fun getInitialUrl(): Uri? | ||
internal abstract fun watchForUrlChanges(): Flow<Uri> | ||
internal abstract fun setDestinationUrl(url: Uri) | ||
|
||
final override fun RouterInterceptorScope<T>.start( | ||
notifications: Flow<RouterNotification<T>> | ||
) { | ||
launch(start = CoroutineStart.UNDISPATCHED) { | ||
// start by setting the initial route from the browser hash, if provided when the webpage first loads | ||
onViewModelInitSetStateFromBrowser(notifications) | ||
|
||
// then sync the hash state to the router state (in both directions) | ||
joinAll( | ||
updateBrowserOnStateChange(notifications), | ||
updateStateOnBrowserChange(notifications) | ||
) | ||
} | ||
} | ||
|
||
private suspend fun RouterInterceptorScope<T>.onViewModelInitSetStateFromBrowser( | ||
notifications: Flow<RouterNotification<T>> | ||
) { | ||
// wait for the BallastNotification.ViewModelStarted to be sent | ||
notifications.awaitViewModelStart() | ||
|
||
val initialDestinationUrl = getInitialUrl()?.encodedPathAndQuery | ||
?: initialRoute.directions().build() | ||
|
||
val deferred = CompletableDeferred<Unit>() | ||
|
||
sendToQueue( | ||
Queued.HandleInput( | ||
deferred, | ||
RouterContract.Inputs.GoToDestination( | ||
destination = initialDestinationUrl | ||
) | ||
) | ||
) | ||
|
||
// wait for the initial URL to be set in the state, before allowing the rest of the address bar syncing to begin | ||
deferred.await() | ||
} | ||
|
||
private fun RouterInterceptorScope<T>.updateBrowserOnStateChange( | ||
notifications: Flow<RouterNotification<T>> | ||
): Job = launch(start = CoroutineStart.UNDISPATCHED) { | ||
notifications | ||
.events { it } | ||
.filterIsInstance<RouterContract.Events.BackstackChanged<T>>() | ||
.mapNotNull { ev -> | ||
ev.backstack.mapCurrentDestination( | ||
route = { | ||
if (annotations.any { it is WebEventRouteAnnotation }) { | ||
// ignore this request | ||
null | ||
} else { | ||
UriBuilder.parse(originalDestinationUrl) | ||
} | ||
}, | ||
notFound = { UriBuilder.parse(it) }, | ||
) | ||
} | ||
.distinctUntilChanged() | ||
.onEach { destination -> setDestinationUrl(destination) } | ||
.launchIn(this) | ||
} | ||
|
||
private fun RouterInterceptorScope<T>.updateStateOnBrowserChange( | ||
notifications: Flow<RouterNotification<T>> | ||
): Job = launch(start = CoroutineStart.UNDISPATCHED) { | ||
watchForUrlChanges() | ||
.onEach { destination -> | ||
sendToQueue( | ||
Queued.HandleInput( | ||
null, | ||
RouterContract.Inputs.GoToDestination( | ||
destination = destination.encodedPathAndQuery, | ||
extraAnnotations = setOf(WebEventRouteAnnotation), | ||
) | ||
) | ||
) | ||
} | ||
.launchIn(this) | ||
} | ||
} |
Oops, something went wrong.