Not 100% directly iOS but seemed as good a place a...
# compose-ios
s
Not 100% directly iOS but seemed as good a place as any to ask - might be a stupid question but is there a way to hook into the compose app prior to the initial view composition to do some app initialization much like AppDelegate or Application implementation on Android? I hadn't stumbled across anything in reference projects etc, basically I have a proprietary lib I have exposed common functionality via protocol and configured target specific, I am looking for a way to kick off the libs functionality at a common point without having to do so per target.
p
If you don't want to write independent platform code. First you will need to commonize what you call initialization work. Second, is a matter of App architecture, I don't think there is a standard way right now. But the simplest way is to do the initialization in the root Composable State/ViewModel. If you use state management libraries like decompose/Appyx/circuit, you can do the work in the root component. I tend to have a component I call the StartUp component, just for that.
But yeah your question is more of: What would be the architecture of a pure compose App. Accounting for App startup, lifecycle, background jobs etc.
👍 1
s
Yeah not well worded - thanks for the insights
👍 1
i
JFYI - we're working on commonizing
androidx.lifecycle.*
to use it as lifecycle state/events abstraction from common code, it will be available in next versions (not sure in which exactly at the moment)
🎉 6
thank you color 1
kodee happy 1
p
This is great news 😀👏
m
Such a commonization is certainly the way to go but how do you solve this problem right now within the given structure? I need a place for an early initialization of my model and a way to hook into
applicationDidBecomeActive
and
applicationDidEnterBackground
.
i
For Compose view - there are a way to subscribe to it: https://github.com/JetBrains/compose-multiplatform-core/blob/release/1.5.11/compos[…]droidx/compose/ui/uikit/ComposeUIViewControllerConfiguration.kt For application - it's done via regular iOS API since Compose doesn't replace entry point of your application and you can implement
UIApplicationDelegate
with any required logic
m
I guess this is the way this API is supposed to be used, right?
Copy code
fun MainViewController() = ComposeUIViewController(
    configure = {
        delegate = object: ComposeUIViewControllerDelegate {
            override fun viewDidLoad() {
                println("### viewDidLoad ###")
            }
            override fun viewDidAppear(animated: kotlin.Boolean) {
                println("### viewDidAppear ###")
            }
            override fun viewDidDisappear(animated: kotlin.Boolean) {
                println("### viewDidDisappear ###")
            }
        }
    },
    content = {
        MaterialTheme {
            App()
        }
    }
)
This took me a while to figure out because AndroidStudio reacts to it with an Internal Error. But when I finally decided, as a last action, to just run it the compiler was actually happy and the app launched in the simulator printing the lines
Copy code
### viewDidLoad ###
### viewDidAppear ###
I am wondering though why I never see a
Copy code
### viewDidDisappear ###
even if I press the home button. Did I misunderstand the meaning of view did disappear?
p
Are using Swift UI or UiViewController for the integration?
s
Compose
Which is essentially swift UI iOS side I guess
p
I mean in the integration side? You might be hitting this wall: https://github.com/JetBrains/compose-multiplatform/issues/3462#issuecomment-1901579032
👍 1
m
I am using whatever the Jetbrains Multiplatform Wizard has created for me and that’s Compose.
p
Yeah the default is swift UI so you are probably hitting above case. By default swift UI doesn't propagate App lifecycle to the UiViewControlleraRepresentable. You have to do it manually.
Apple does bad APIs too
🙏 1
m
But this doesn’t explain the AS crash, right? Today is one of those days when it would have been better to stay in bed and not touch any technical devices 😢.
😁 3
😂 1
p
Yes you are right, the crash is a different thing. It explains not seeing the ViewDidDissappear event. The crash might be related tho 😀