Skip to content

Commit

Permalink
Add AppDelegate with RootHolder for ios app lifecycle implementation …
Browse files Browse the repository at this point in the history
…with decompose
  • Loading branch information
sunildhiman90 committed Jan 5, 2024
1 parent 0aaa5fa commit 35b31d0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 18 deletions.
7 changes: 7 additions & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ kotlin {
iosTarget.binaries.framework {
baseName = "ComposeApp"
isStatic = true

export("com.arkivanov.decompose:decompose:2.2.2-compose-experimental")
export("com.arkivanov.essenty:lifecycle:1.3.0")
}
}

Expand Down Expand Up @@ -93,6 +96,10 @@ kotlin {

iosMain.dependencies {
implementation(libs.ktor.client.darwin)

api("com.arkivanov.decompose:decompose:2.2.2-compose-experimental")
api("com.arkivanov.essenty:lifecycle:1.3.0")

}
}
}
Expand Down
34 changes: 20 additions & 14 deletions composeApp/src/iosMain/kotlin/MainViewController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,44 @@ import platform.Foundation.NSCachesDirectory
import platform.Foundation.NSSearchPathForDirectoriesInDomains
import platform.Foundation.NSUserDomainMask
import root.DefaultRootComponent
import root.RootComponent
import root.RootContent

fun MainViewController() = ComposeUIViewController {
fun MainViewController(rootComponent: RootComponent) = ComposeUIViewController {
CompositionLocalProvider(
LocalImageLoader provides remember { generateImageLoader() },
) {
val lifecycle = LifecycleRegistry()
lifecycle.subscribe(LifecycleCallbacksImpl())
val homeViewModel = HomeViewModel()
val root =
DefaultRootComponent(
componentContext = DefaultComponentContext(LifecycleRegistry()),
homeViewModel
)
RootContent(root, modifier = Modifier)
// val lifecycle = LifecycleRegistry()
// lifecycle.subscribe(LifecycleCallbacksImpl())
// val homeViewModel = HomeViewModel()
// val root =
// DefaultRootComponent(
// componentContext = DefaultComponentContext(LifecycleRegistry()),
// homeViewModel
// )
RootContent(rootComponent, modifier = Modifier)
}
}

class LifecycleCallbacksImpl: Lifecycle.Callbacks {
override fun onCreate() {
override fun onResume() {
super.onCreate()
println("onCreate")
println("Compose onResume")
}

override fun onDestroy() {
super.onDestroy()
println("onDestroy")
println("Compose onDestroy")
}

override fun onStop() {
super.onStop()
println("Compose onStop")
}

override fun onPause() {
super.onPause()
println("onPause")
println("Compose onPause")
}

}
Expand Down
9 changes: 7 additions & 2 deletions iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import SwiftUI
import ComposeApp

struct ComposeView: UIViewControllerRepresentable {

let rootComponet: RootComponent

func makeUIViewController(context: Context) -> UIViewController {
MainViewControllerKt.MainViewController()
MainViewControllerKt.MainViewController(rootComponent: rootComponet)
}

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

struct ContentView: View {
let rootComponet: RootComponent

var body: some View {
ComposeView()
ComposeView(rootComponet: rootComponet)
.ignoresSafeArea(.keyboard) // Compose has own keyboard handler
}
}
Expand Down
57 changes: 55 additions & 2 deletions iosApp/iosApp/iOSApp.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
import SwiftUI
import ComposeApp

@main
struct iOSApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self)
var appDelegate: AppDelegate

var rootHolder: RootHolder {
appDelegate.rootHolder
}

var body: some Scene {
WindowGroup {
ContentView()
ContentView(rootComponet: rootHolder.root)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
debugPrint("Swift onResume")
LifecycleRegistryExtKt.resume(rootHolder.lifecycle)
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
debugPrint("Swift onPause")
LifecycleRegistryExtKt.pause(rootHolder.lifecycle)
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)) { _ in
debugPrint("Swift onStop")
LifecycleRegistryExtKt.stop(rootHolder.lifecycle)
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willTerminateNotification)) { _ in
debugPrint("Swift onDestroy")
LifecycleRegistryExtKt.destroy(rootHolder.lifecycle)
}
}
}
}
}

class RootHolder: ObservableObject {

let lifecycle: LifecycleRegistry
let root: RootComponent

init() {
lifecycle = LifecycleRegistryKt.LifecycleRegistry()
lifecycle.subscribe(callbacks: LifecycleCallbacksImpl())
let homeViewModel = HomeViewModel()

root = DefaultRootComponent(
componentContext: DefaultComponentContext(lifecycle: lifecycle),
homeViewModel: homeViewModel
)

LifecycleRegistryExtKt.create(lifecycle)
}

deinit {
LifecycleRegistryExtKt.destroy(lifecycle)
}

}

class AppDelegate: NSObject, UIApplicationDelegate {
let rootHolder: RootHolder = RootHolder()
}

0 comments on commit 35b31d0

Please sign in to comment.