https://kotlinlang.org logo
#mvikotlin
Title
# mvikotlin
r

Robert Munro

03/29/2024, 2:13 PM
hiya, I have some controller and stores that i want to reuse for a desktop app. and i have made a single
UiComponent
interface with
create()
and
destory()
methods. So now i want to create an essenty lifecycle object that i can pass to my Controller and i can manually call state changes from the create and destroy methods. Is there some prebuilt object in essenty that I can use for this or how would i implement one?
a

Arkadii Ivanov

03/29/2024, 2:25 PM
Hmm, maybe LifecycleRegistry resume/destroy?
r

Robert Munro

03/29/2024, 3:41 PM
Something like this?
Copy code
class RemotesUiComponent:UiComponent{

    // the mvi controller
    private val controller: RemotesController by scope.inject()
    private val lifecycle = LifecycleRegistry()
    override fun create() {
        lifecycle.onCreate()
        lifecycle.onStart()
        lifecycle.onResume()
    }

    override fun destroy() {
        lifecycle.onPause()
        lifecycle.onStop()
        lifecycle.onDestroy()
        scope.close()
    }
}
and i just pass in
lifecycle
to my remotes controller via injection?
a

Arkadii Ivanov

03/29/2024, 4:31 PM
I would rather pass Lifecycle and avoid create/destroy methods. But this way is also fine. Also, you can call resume/destroy extensions for simplicity.
r

Robert Munro

03/29/2024, 4:32 PM
Yes good point I will inject it. Thanks heaps for the help!
👍 1
2 Views