Isaac Udy
06/18/2025, 10:31 AMJonathan
06/18/2025, 12:56 PMViewController
subclass that extends ComposeHostingViewController
and implements LifecycleOwner?
Andrei Salavei
06/18/2025, 2:11 PMIsaac Udy
06/18/2025, 10:46 PMCan you make a custom ViewController subclass that extends ComposeHostingViewController and implements LifecycleOwner?No; ComposeHostingViewController is an internal class and can't be subclassed. IOSLifecycleOwner is also internal, and so is ViewControllerLifecycleDelegate. It's possible to re-implement all of this myself, based on the library code, but that doesn't feel ideal.
What actually you want to achieve by accessing the LifecycleOwner/ViewModelStoreOwner ?I want to be able to externally observe the ViewController's lifecycle, and access objects in the ViewModelStore. Contrived example: I have a UINavigationController; when some external event happens, I want to take a quick look through the UIControllers hosted by the UINavigationController, and if the top-most Controller is a ComposeHostingViewController, grab a ViewModel from that ComposeHostingViewController's ViewModelStore, and then call some function on the ViewModel. For example, if I intercept a deep link that's targeting a screen that's in the currently active ComposeHostingViewController, I might want to tell the ViewModel to reset a scroll state, so that a scrollable column on the screen scrolls back up to the top. At the moment to achieve what I want to achieve, I need to create my own wrapper for
ComposeUIViewController
, and grab the LocalLifecycleOwner/LocalViewModelStoreOwner inside composition, and keep a reference to these on the UIViewController with objc_getAssociatedObject. It's all possible to do, but it feels a little like jumping through hoops to achieve something that would otherwise be easily possible if the ComposeHostingViewController made lifecycleOwner
public, or implemented the appropriate interfaces.