-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Saved instance state as backup to handle stack routing
- Loading branch information
1 parent
e25ae82
commit fc6dfe1
Showing
16 changed files
with
623 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
@file:Suppress("UNUSED_VARIABLE") | ||
|
||
import com.android.build.gradle.BaseExtension | ||
import org.gradle.api.JavaVersion | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.* | ||
|
||
fun Project.configureAndroid() { | ||
kotlin { | ||
android() | ||
|
||
sourceSets.apply { | ||
val androidMain by getting { | ||
findByName("commonMain")?.let { dependsOn(it) } | ||
findByName("jvmAndNixMain")?.let { dependsOn(it) } | ||
findByName("jvmMain")?.let { dependsOn(it) } | ||
} | ||
|
||
val androidTest by creating { | ||
findByName("commonTest")?.let { dependsOn(it) } | ||
findByName("jvmAndNixTest")?.let { dependsOn(it) } | ||
findByName("jvmTest")?.let { dependsOn(it) } | ||
} | ||
} | ||
} | ||
|
||
extensions.findByType<BaseExtension>()?.apply { | ||
compileSdkVersion(34) | ||
sourceSets["main"].java.srcDirs("android/src/kotlin") | ||
sourceSets["main"].manifest.srcFile("android/src/AndroidManifest.xml") | ||
sourceSets["main"].res.srcDirs("android/src/res") | ||
|
||
defaultConfig { | ||
minSdk = 23 | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<application> | ||
<provider | ||
android:name="androidx.startup.InitializationProvider" | ||
android:authorities="${applicationId}.androidx-startup" | ||
android:exported="false" | ||
tools:node="merge"> | ||
<meta-data | ||
android:name="dev.programadorthi.routing.core.StackInitializer" | ||
android:value="androidx.startup" /> | ||
</provider> | ||
|
||
</application> | ||
|
||
</manifest> |
12 changes: 12 additions & 0 deletions
12
...tack/android/src/kotlin/dev/programadorthi/routing/core/StackApplicationCallParcelable.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,12 @@ | ||
package dev.programadorthi.routing.core | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
internal data class StackApplicationCallParcelable( | ||
val name: String, | ||
val routeMethod: String, | ||
val uri: String, | ||
val parameters: Map<String, List<String>>, | ||
) : Parcelable |
110 changes: 110 additions & 0 deletions
110
core-stack/android/src/kotlin/dev/programadorthi/routing/core/StackInitializer.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,110 @@ | ||
package dev.programadorthi.routing.core | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import android.content.Context | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.activity.ComponentActivity | ||
import androidx.savedstate.SavedStateRegistry | ||
import androidx.startup.Initializer | ||
import java.lang.ref.WeakReference | ||
|
||
internal class StackInitializer : | ||
Initializer<Unit>, | ||
Application.ActivityLifecycleCallbacks, | ||
StackManagerNotifier { | ||
|
||
private val lock = Any() | ||
private var currentActivity = WeakReference<ComponentActivity?>(null) | ||
|
||
init { | ||
StackManager.stackManagerNotifier = this | ||
} | ||
|
||
//region Initializer | ||
override fun create(context: Context) { | ||
val application = context.applicationContext as Application | ||
application.registerActivityLifecycleCallbacks(this) | ||
} | ||
|
||
override fun dependencies(): List<Class<out Initializer<*>>> = emptyList() | ||
//endregion | ||
|
||
//region Application.ActivityLifecycleCallbacks | ||
override fun onActivityCreated(activity: Activity, bundle: Bundle?) { | ||
updateCurrentActivity(activity = activity) | ||
} | ||
|
||
override fun onActivityStarted(activity: Activity) { | ||
updateCurrentActivity(activity = activity) | ||
} | ||
|
||
override fun onActivityResumed(p0: Activity) {} | ||
override fun onActivityPaused(p0: Activity) {} | ||
override fun onActivityStopped(p0: Activity) {} | ||
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {} | ||
override fun onActivityDestroyed(p0: Activity) {} | ||
//endregion | ||
|
||
//region StackManagerNotifier | ||
override fun onRegistered(providerId: String, stackManager: StackManager) = synchronized(lock) { | ||
val registry = currentActivity.get()?.savedStateRegistry ?: return | ||
processRegistration( | ||
registry = registry, | ||
providerId = providerId, | ||
stackManager = stackManager, | ||
) | ||
} | ||
|
||
override fun onUnRegistered(providerId: String) = synchronized(lock) { | ||
val registry = currentActivity.get()?.savedStateRegistry ?: return | ||
registry.unregisterSavedStateProvider(providerId) | ||
} | ||
//endregion | ||
|
||
private fun updateCurrentActivity(activity: Activity) = synchronized(lock) { | ||
if (activity !is ComponentActivity) { | ||
Log.w( | ||
"kotlin-routing", | ||
"Your activity must be an androidx.activity.ComponentActivity. Current is: $activity" | ||
) | ||
return | ||
} | ||
currentActivity = WeakReference(activity) | ||
val registry = activity.savedStateRegistry | ||
if (registry.isRestored) { | ||
processRestoration(registry) | ||
} else { | ||
StackManager.subscriptions().forEach { (providerId, stackManager) -> | ||
processRegistration( | ||
registry = registry, | ||
providerId = providerId, | ||
stackManager = stackManager, | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun processRegistration( | ||
registry: SavedStateRegistry, | ||
providerId: String, | ||
stackManager: StackManager | ||
) { | ||
registry.unregisterSavedStateProvider(providerId) | ||
registry.registerSavedStateProvider( | ||
key = providerId, | ||
provider = StackSavedStateProvider(providerId, stackManager), | ||
) | ||
} | ||
|
||
private fun processRestoration(registry: SavedStateRegistry) { | ||
StackManager.subscriptions().forEach { (providerId, stackManager) -> | ||
val previousState = registry.consumeRestoredStateForKey(providerId) | ||
if (previousState?.isEmpty == false) { | ||
val saver = StackSavedStateProvider(providerId, stackManager) | ||
saver.restoreState(previousState) | ||
} | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
core-stack/android/src/kotlin/dev/programadorthi/routing/core/StackSavedStateProvider.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,92 @@ | ||
package dev.programadorthi.routing.core | ||
|
||
import android.os.Build | ||
import android.os.Bundle | ||
import androidx.core.os.bundleOf | ||
import androidx.savedstate.SavedStateRegistry | ||
import io.ktor.http.parametersOf | ||
import io.ktor.util.toMap | ||
|
||
internal class StackSavedStateProvider( | ||
private val providerId: String, | ||
private val stackManager: StackManager | ||
) : SavedStateRegistry.SavedStateProvider { | ||
|
||
override fun saveState(): Bundle { | ||
val calls = stackManager.toSave() | ||
val parcelables = calls.map { call -> | ||
StackApplicationCallParcelable( | ||
name = call.name, | ||
routeMethod = call.routeMethod.value, | ||
uri = call.uri, | ||
parameters = call.parameters.toMap(), | ||
) | ||
}.toTypedArray() | ||
return bundleOf(providerId to parcelables) | ||
} | ||
|
||
fun restoreState(previousState: Bundle) { | ||
val parcelables = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
previousState | ||
.getParcelableArray(providerId, StackApplicationCallParcelable::class.java) | ||
?.toList() ?: emptyList() | ||
} else { | ||
buildList<StackApplicationCallParcelable> { | ||
previousState.getParcelableArray(providerId)?.forEach { parcelable -> | ||
if (parcelable is StackApplicationCallParcelable) { | ||
add(parcelable) | ||
} | ||
} | ||
} | ||
} | ||
val calls = parcelables.mapNotNull(::mapToCall) | ||
stackManager.toRestore(calls) | ||
} | ||
|
||
private fun mapToCall(parcelable: StackApplicationCallParcelable): StackApplicationCall? { | ||
return when (val routhMethod = StackRouteMethod.parse(parcelable.routeMethod)) { | ||
StackRouteMethod.Push -> mapToPush(parcelable) | ||
StackRouteMethod.Replace, | ||
StackRouteMethod.ReplaceAll -> mapToReplace(parcelable, routhMethod) | ||
|
||
else -> null | ||
} | ||
} | ||
|
||
private fun mapToPush(parcelable: StackApplicationCallParcelable): StackApplicationCall { | ||
return when { | ||
parcelable.name.isBlank() -> StackApplicationCall.Push( | ||
application = stackManager.application, | ||
uri = parcelable.uri, | ||
parameters = parametersOf(parcelable.parameters), | ||
) | ||
|
||
else -> StackApplicationCall.PushNamed( | ||
application = stackManager.application, | ||
name = parcelable.name, | ||
parameters = parametersOf(parcelable.parameters), | ||
) | ||
} | ||
} | ||
|
||
private fun mapToReplace( | ||
parcelable: StackApplicationCallParcelable, | ||
routeMethod: StackRouteMethod, | ||
): StackApplicationCall { | ||
return when { | ||
parcelable.name.isBlank() -> StackApplicationCall.Replace( | ||
all = routeMethod == StackRouteMethod.ReplaceAll, | ||
application = stackManager.application, | ||
uri = parcelable.uri, | ||
parameters = parametersOf(parcelable.parameters), | ||
) | ||
|
||
else -> StackApplicationCall.ReplaceNamed( | ||
all = routeMethod == StackRouteMethod.ReplaceAll, | ||
application = stackManager.application, | ||
name = parcelable.name, | ||
parameters = parametersOf(parcelable.parameters), | ||
) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.