decompose-router 0.5.1 Help

Platform configurations

Android / WearOS

You will need to provide your default RouterContext from an Activity/Fragment

class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val rootRouterContext: RouterContext = defaultRouterContext() setContent { CompositionLocalProvider(LocalRouterContext provides rootRouterContext) { // Your app goes here } } } }

Desktop

You will need to provide your default RouterContext at the Application level

fun main() { application { val windowState: WindowState = rememberWindowState() val rootRouterContext: RouterContext = defaultRouterContext(windowState = windowState) Window(..) { CompositionLocalProvider(LocalRouterContext provides rootRouterContext) { // Your app goes here } } } }

iOS

Create your default RouterContext outside ComposeUIViewController's composable lambda and pass it in to LocalRouterContext

fun HomeUIViewController(routerContext: RouterContext): UIViewController = ComposeUIViewController { CompositionLocalProvider( LocalRouterContext provides routerContext, ) { // Your app goes here } }

You will need to tie root RouterContext's lifecycle to an AppDelegate.

If you are using SwiftUI

@main struct SwiftUIApp: App { @UIApplicationDelegateAdaptor var delegate: AppDelegate @Environment(\.scenePhase) var scenePhase: ScenePhase var defaultRouterContext: RouterContext { delegate.holder.defaultRouterContext } var body: some Scene { WindowGroup { HomeView(routerContext: defaultRouterContext) } .onChange(of: scenePhase) { newPhase in switch newPhase { case .background: defaultRouterContext.stop() case .inactive: defaultRouterContext.pause() case .active: defaultRouterContext.resume() @unknown default: break } } } } class DefaultRouterHolder : ObservableObject { let defaultRouterContext: RouterContext = DefaultRouterContextKt.defaultRouterContext() deinit { // Destroy the root component before it is deallocated defaultRouterContext.destroy() } } class AppDelegate: NSObject, UIApplicationDelegate { let holder: DefaultRouterHolder = DefaultRouterHolder() } struct HomeView: UIViewControllerRepresentable { let routerContext: RouterContext func makeUIViewController(context: Context) -> UIViewController { return ApplicationKt.HomeUIViewController(routerContext: routerContext) } func updateUIViewController(_ uiViewController: UIViewController, context: Context) {} }

If you are using UIKit

@UIApplicationMain class UIKitAppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var rootRouterContext = DefaultRouterContextKt.defaultRouterContext() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) let mainViewController = ApplicationKt.HomeUIViewController(routerContext: rootRouterContext) window?.rootViewController = mainViewController window?.makeKeyAndVisible() return true } func applicationDidBecomeActive(_ application: UIApplication) { rootRouterContext.resume() } func applicationWillResignActive(_ application: UIApplication) { rootRouterContext.stop() } func applicationWillTerminate(_ application: UIApplication) { rootRouterContext.destroy() } }

Web

Create your default RouterContext once and supply with CompositionLocalProvider

JsBrowser

fun main() { val rootRouterContext: RouterContext = defaultRouterContext() onWasmReady { BrowserViewportWindow("App") { CompositionLocalProvider(LocalRouterContext provides rootRouterContext) { MaterialTheme { // Your app goes here } } } } }

WasmJsBrowser

fun main() { val rootRouterContext: RouterContext = defaultRouterContext() BrowserViewportWindow("App") { CompositionLocalProvider(LocalRouterContext provides rootRouterContext) { MaterialTheme { // Your app goes here } } } }
Last modified: 19 June 2024