Francis Mariano
08/16/2023, 3:34 PMinit {
lifecycle.doOnStart { // make some action }
}
But the action is not called in iOS. Are there some reason???Jan
08/16/2023, 3:36 PMFrancis Mariano
08/16/2023, 3:38 PMJan
08/16/2023, 3:38 PMFrancis Mariano
08/16/2023, 3:39 PMJan
08/16/2023, 3:43 PM// iOSApp.swift
@main
struct iOSApp: App {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
@Environment(\.scenePhase) private var scenePhase
var lifecycleHolder: LifecycleHolder { appDelegate.lifecycleHolder }
var body: some Scene {
WindowGroup {
ComposeViewControllerToSwiftUI(lifecycleHolder.lifecycle)
//.ignoresSafeArea()
//.onTapGesture {
// Hide keyboard on tap outside of TextField
// UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
//}
}.onChange(of: scenePhase) { newPhase in
switch newPhase {
case .background: LifecycleRegistryExtKt.stop(lifecycleHolder.lifecycle)
case .inactive: LifecycleRegistryExtKt.pause(lifecycleHolder.lifecycle)
case .active: LifecycleRegistryExtKt.resume(lifecycleHolder.lifecycle)
@unknown default: break
}
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
let lifecycleHolder: LifecycleHolder = LifecycleHolder()
}
struct ComposeViewControllerToSwiftUI: UIViewControllerRepresentable {
private let lifecycle: LifecycleRegistry
init(_ lifecycle: LifecycleRegistry) {
self.lifecycle = lifecycle
}
func makeUIViewController(context: Context) -> UIViewController {
Main_iosKt.MainViewController(lifecycle: lifecycle)
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}
class LifecycleHolder : ObservableObject {
let lifecycle: LifecycleRegistry
init() {
lifecycle = LifecycleRegistryKt.LifecycleRegistry()
LifecycleRegistryExtKt.create(lifecycle)
}
deinit {
// Destroy the root component before it is deallocated
LifecycleRegistryExtKt.destroy(lifecycle)
}
}
and for compose-multiplatform I have something like this in src-set iOSMain defined:
fun MainViewController(lifecycle: Lifecycle): UIViewController {
val root = RootComponent(
componentContext = DefaultComponentContext(
lifecycle = lifecycle,
stateKeeper = StateKeeperDispatcher(),
instanceKeeper = InstanceKeeperDispatcher()
)
)
return ComposeUIViewController {
AppContent(root)
}
}
Francis Mariano
08/16/2023, 3:44 PMFrancis Mariano
08/16/2023, 3:45 PMJan
08/16/2023, 3:45 PMFrancis Mariano
08/17/2023, 1:14 PMerror: cannot find type 'LifecycleRegistry' in scope
let lifecycle: LifecycleRegistry
Francis Mariano
08/17/2023, 1:15 PMimport shared
is okJan
08/17/2023, 1:18 PMJan
08/17/2023, 1:20 PMFrancis Mariano
08/17/2023, 1:22 PMmaybe you added the wrong decompose deps?No, the dependencies is right, but I did not export the deps
Francis Mariano
08/17/2023, 1:23 PMJan
08/17/2023, 1:25 PMFrancis Mariano
08/17/2023, 1:31 PMFrancis Mariano
08/17/2023, 2:32 PM