Skip to content

Commit

Permalink
Create and pass LifecycleHelper from iOS apps to main.ios.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
mapm14 committed Jan 21, 2024
1 parent 4b3c47a commit 7df9251
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 12 deletions.
2 changes: 2 additions & 0 deletions demos/appyx-navigation/ios/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kotlin {
framework {
baseName = "ios"
isStatic = true
export(project(":appyx-navigation:appyx-navigation"))
}
license = "Apache License, Version 2.0"
authors = "https://github.com/bumble-tech/"
Expand All @@ -36,6 +37,7 @@ kotlin {
iosSimulatorArm64Main.dependsOn(this)
dependencies {
implementation(project(":demos:appyx-navigation:common"))
api(project(":appyx-navigation:appyx-navigation"))
api(compose.runtime)
api(compose.foundation)
api(compose.material)
Expand Down
6 changes: 4 additions & 2 deletions demos/appyx-navigation/ios/src/iosMain/kotlin/main.ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import com.bumble.appyx.demos.navigation.node.root.RootNode
import com.bumble.appyx.demos.navigation.ui.AppyxSampleAppTheme
import com.bumble.appyx.navigation.integration.IosNodeHost
import com.bumble.appyx.navigation.integration.MainIntegrationPoint
import com.bumble.appyx.navigation.platform.LifecycleHelper
import kotlinx.coroutines.flow.flowOf
import platform.Foundation.NSURL

private val integrationPoint = MainIntegrationPoint()
private val navigator = Navigator()

@Suppress("FunctionNaming")
fun MainViewController() = ComposeUIViewController {
fun MainViewController(lifecycleHelper: LifecycleHelper) = ComposeUIViewController {
AppyxSampleAppTheme {
Scaffold(
modifier = Modifier
Expand All @@ -33,7 +34,8 @@ fun MainViewController() = ComposeUIViewController {
IosNodeHost(
modifier = Modifier,
onBackPressedEvents = flowOf(),
integrationPoint = integrationPoint
integrationPoint = integrationPoint,
lifecycle = lifecycleHelper.lifecycle,
) { nodeContext ->
RootNode(
nodeContext = nodeContext,
Expand Down
9 changes: 7 additions & 2 deletions demos/appyx-navigation/iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import SwiftUI
import ios

struct ComposeView: UIViewControllerRepresentable {

var lifecycleHelper: LifecycleHelper

func makeUIViewController(context: Context) -> UIViewController {
Main_iosKt.MainViewController()
Main_iosKt.MainViewController(lifecycleHelper: lifecycleHelper)
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

struct ContentView: View {
var lifecycleHelper: LifecycleHelper

var body: some View {
ComposeView()
ComposeView(lifecycleHelper: lifecycleHelper)
.ignoresSafeArea(.all)
}
}
37 changes: 36 additions & 1 deletion demos/appyx-navigation/iosApp/iosApp/iOSApp.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
import SwiftUI
import ios
import Foundation
import UIKit

@main
struct iOSApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self)
var appDelegate: AppDelegate

@Environment(\.scenePhase) var scenePhase

var body: some Scene {
WindowGroup {
ZStack {
Color.white.ignoresSafeArea(.all) // status bar color
ContentView()
ContentView(lifecycleHelper: appDelegate.lifecycleHolder.lifecycleHelper)
}
.onOpenURL { incomingURL in
Main_iosKt.handleDeepLinks(url: incomingURL)
}
.onChange(of: scenePhase) { newPhase in
switch newPhase {
case .background: appDelegate.lifecycleHolder.lifecycleHelper.created()
case .inactive: appDelegate.lifecycleHolder.lifecycleHelper.created()
case .active: appDelegate.lifecycleHolder.lifecycleHelper.resumed()
@unknown default: break
}
}
}
}
}

class AppDelegate: NSObject, UIApplicationDelegate {
let lifecycleHolder: LifecycleHolder = LifecycleHolder()
}

class LifecycleHolder {

let lifecycleHelper: LifecycleHelper

init() {
lifecycleHelper = LifecycleHelper()
lifecycleHelper.created()
}

deinit {
// Destroy the root component before it is deallocated
lifecycleHelper.destroyed()
}
}
2 changes: 2 additions & 0 deletions demos/sandbox-appyx-navigation/ios/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kotlin {
framework {
baseName = "ios"
isStatic = true
export(project(":appyx-navigation:appyx-navigation"))
}
}

Expand All @@ -34,6 +35,7 @@ kotlin {
iosSimulatorArm64Main.dependsOn(this)
dependencies {
implementation(project(":demos:sandbox-appyx-navigation:common"))
api(project(":appyx-navigation:appyx-navigation"))
api(compose.runtime)
api(compose.foundation)
api(compose.material)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.bumble.appyx.demos.sandbox.navigation.node.container.MainNavNode
import com.bumble.appyx.demos.sandbox.navigation.ui.AppyxSampleAppTheme
import com.bumble.appyx.navigation.integration.IosNodeHost
import com.bumble.appyx.navigation.integration.MainIntegrationPoint
import com.bumble.appyx.navigation.platform.LifecycleHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.receiveAsFlow
Expand All @@ -29,7 +30,7 @@ val backEvents: Channel<Unit> = Channel()
private val integrationPoint = MainIntegrationPoint()

@Suppress("FunctionNaming")
fun MainViewController() = ComposeUIViewController {
fun MainViewController(lifecycleHelper: LifecycleHelper) = ComposeUIViewController {

AppyxSampleAppTheme {
val coroutineScope = rememberCoroutineScope()
Expand All @@ -45,7 +46,8 @@ fun MainViewController() = ComposeUIViewController {
IosNodeHost(
modifier = Modifier,
onBackPressedEvents = backEvents.receiveAsFlow(),
integrationPoint = remember { integrationPoint }
lifecycle = lifecycleHelper.lifecycle,
integrationPoint = remember { integrationPoint },
) { nodeContext ->
MainNavNode(
nodeContext = nodeContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import SwiftUI
import ios

struct ComposeView: UIViewControllerRepresentable {

var lifecycleHelper: LifecycleHelper

func makeUIViewController(context: Context) -> UIViewController {
Main_iosKt.MainViewController()
Main_iosKt.MainViewController(lifecycleHelper: lifecycleHelper)
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

struct ContentView: View {
var lifecycleHelper: LifecycleHelper

var body: some View {
ComposeView()
ComposeView(lifecycleHelper: lifecycleHelper)
.ignoresSafeArea(.all)
}
}
43 changes: 40 additions & 3 deletions demos/sandbox-appyx-navigation/iosApp/iosApp/iOSApp.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
import SwiftUI
import ios
import Foundation
import UIKit

@main
struct iOSApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self)
var appDelegate: AppDelegate

@Environment(\.scenePhase) var scenePhase

var body: some Scene {
WindowGroup {
ZStack {
Color.white.ignoresSafeArea(.all) // status bar color
ContentView()
}.preferredColorScheme(.light)
ContentView(lifecycleHelper: appDelegate.lifecycleHolder.lifecycleHelper)
}
.preferredColorScheme(.light)
.onChange(of: scenePhase) { newPhase in
switch newPhase {
case .background: appDelegate.lifecycleHolder.lifecycleHelper.created()
case .inactive: appDelegate.lifecycleHolder.lifecycleHelper.created()
case .active: appDelegate.lifecycleHolder.lifecycleHelper.resumed()
@unknown default: break
}
}
}
}
}
}

class AppDelegate: NSObject, UIApplicationDelegate {
let lifecycleHolder: LifecycleHolder = LifecycleHolder()
}

class LifecycleHolder {

let lifecycleHelper: LifecycleHelper

init() {
lifecycleHelper = LifecycleHelper()
lifecycleHelper.created()
}

deinit {
// Destroy the root component before it is deallocated
lifecycleHelper.destroyed()
}
}

0 comments on commit 7df9251

Please sign in to comment.