Skip to content

Commit

Permalink
Add default router context for ios targets
Browse files Browse the repository at this point in the history
  • Loading branch information
xxfast committed Sep 27, 2023
1 parent 9e493ac commit 20c94f0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
17 changes: 16 additions & 1 deletion app/ios/ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@ import app
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

var rootRouterContext = RouterContextKt.defaultRouterContext()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainViewController = ApplicationKt.Main()
let mainViewController = ApplicationKt.MainUIController(routerContext: rootRouterContext)
window?.rootViewController = mainViewController
window?.makeKeyAndVisible()
return true
}


func applicationDidBecomeActive(_ application: UIApplication) {
RouterContextKt.resume(rootRouterContext.lifecycle)
}

func applicationWillResignActive(_ application: UIApplication) {
RouterContextKt.stop(rootRouterContext.lifecycle)
}

func applicationWillTerminate(_ application: UIApplication) {
RouterContextKt.destroy(rootRouterContext.lifecycle)
}
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.xxfast.decompose.router.app

import com.arkivanov.essenty.backhandler.BackDispatcher
import com.arkivanov.essenty.lifecycle.LifecycleRegistry
import com.arkivanov.essenty.lifecycle.destroy
import com.arkivanov.essenty.lifecycle.resume
import com.arkivanov.essenty.lifecycle.stop
import io.github.xxfast.decompose.router.RouterContext
import io.github.xxfast.decompose.router.app.utils.registry
import io.github.xxfast.decompose.router.defaultRouterContext
import kotlinx.cinterop.BetaInteropApi
import kotlinx.cinterop.ExperimentalForeignApi
import platform.UIKit.UIApplication
Expand All @@ -20,13 +20,7 @@ import platform.UIKit.UIWindow
class AppDelegate @OverrideInit constructor() : UIResponder(), UIApplicationDelegateProtocol {
companion object : UIResponderMeta(), UIApplicationDelegateProtocolMeta

private val backDispatcher = BackDispatcher()
private val lifecycle = LifecycleRegistry()

private val routerContext = RouterContext(
lifecycle = lifecycle,
backHandler = backDispatcher
)
private val routerContext: RouterContext = defaultRouterContext()

private var _window: UIWindow? = null
override fun window() = _window
Expand All @@ -46,15 +40,14 @@ class AppDelegate @OverrideInit constructor() : UIResponder(), UIApplicationDele
}

override fun applicationDidBecomeActive(application: UIApplication) {
lifecycle.resume()
routerContext.lifecycle.registry.resume()
}

override fun applicationWillResignActive(application: UIApplication) {
lifecycle.stop()

routerContext.lifecycle.registry.stop()
}

override fun applicationWillTerminate(application: UIApplication) {
lifecycle.destroy()
routerContext.lifecycle.registry.destroy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,22 @@ fun main() {
}

@OptIn(ExperimentalDecomposeApi::class)
fun MainUIController(routerContext: RouterContext): UIViewController {
return ComposeUIViewController {
CompositionLocalProvider(
LocalRouterContext provides routerContext,
) {
MaterialTheme {
PredictiveBackGestureOverlay(
backDispatcher = routerContext.backHandler as BackDispatcher, // Use the same BackDispatcher as above
backIcon = { progress, _ ->
PredictiveBackGestureIcon(
imageVector = Icons.Default.ArrowBack,
progress = progress,
)
},
modifier = Modifier.fillMaxSize(),
) {
HomeScreen()
}
fun MainUIController(routerContext: RouterContext): UIViewController = ComposeUIViewController {
CompositionLocalProvider(
LocalRouterContext provides routerContext,
) {
MaterialTheme {
PredictiveBackGestureOverlay(
backDispatcher = routerContext.backHandler as BackDispatcher, // Use the same BackDispatcher as above
backIcon = { progress, _ ->
PredictiveBackGestureIcon(
imageVector = Icons.Default.ArrowBack,
progress = progress,
)
},
modifier = Modifier.fillMaxSize(),
) {
HomeScreen()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.xxfast.decompose.router.app.utils

import com.arkivanov.essenty.lifecycle.Lifecycle
import com.arkivanov.essenty.lifecycle.LifecycleRegistry
import com.arkivanov.essenty.lifecycle.destroy
import com.arkivanov.essenty.lifecycle.resume
import com.arkivanov.essenty.lifecycle.stop
import io.github.xxfast.decompose.router.RouterContext
import io.github.xxfast.decompose.router.defaultRouterContext

fun defaultRouterContext(): RouterContext = defaultRouterContext()
val Lifecycle.registry get() = this as LifecycleRegistry
fun Lifecycle.destroy() = registry.destroy()
fun Lifecycle.resume() = registry.resume()
fun Lifecycle.stop() = registry.stop()
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.xxfast.decompose.router

import com.arkivanov.essenty.backhandler.BackDispatcher
import com.arkivanov.essenty.lifecycle.LifecycleRegistry

fun defaultRouterContext(): RouterContext {
val backDispatcher = BackDispatcher()
val lifecycle = LifecycleRegistry()
return RouterContext(lifecycle = lifecycle, backHandler = backDispatcher)
}

0 comments on commit 20c94f0

Please sign in to comment.