rickclephas / KMP-ObservableViewModel

Library to use AndroidX/Kotlin ViewModels with SwiftUI
MIT License
569 stars 29 forks source link

Support child ViewModels #41

Closed rickclephas closed 1 year ago

rickclephas commented 1 year ago

Fixes #39

Usage:

extension MyParentViewModel {
    var myChildViewModel: MyChildViewModel {
        childViewModel(__myChildViewModel, at: \.__myChildViewModel)
    }
}
rickclephas commented 1 year ago

Hmm this would bring some other challenges with more complex hierarchies, like when MyChildViewModel has another child view model.

humblehacker commented 1 year ago

Yes, which is not uncommon in our codebase. Also consider lists of child view models.

rickclephas commented 1 year ago

Actually we could fix that by using @ShouldRefineInSwift and an extension property instead of a subclass.

rickclephas commented 1 year ago

@humblehacker do you have an example of such lists? I mean is it just a regular list containing KMMViewModels?

humblehacker commented 1 year ago

We have this, for example:

    @NativeCoroutinesState
    val tasks: StateFlow<List<TaskViewModel>> = mutableTasks.asStateFlow()
rickclephas commented 1 year ago

Alright, that should be doable. Do you see any other potential issues or challenges?

humblehacker commented 1 year ago

We also have maps of view models like this:

    @NativeCoroutinesState
    val things: StateFlow<Map<Item, ThingViewModel>?> = mutableThings.asStateFlow()
ldobransky commented 11 months ago

Fixes #39

Usage:

extension MyParentViewModel {
    var myChildViewModel: MyChildViewModel {
        childViewModel(__myChildViewModel, at: \.__myChildViewModel)
    }
}

hello, I am facing with the issue regarding child viewmodels, but when I am trying to write extension, I am getting error like Cannot find 'childViewModels' in scope. Something was changed in new versions? I changed in parent viewmodel annotation for inner viewmodel and I wanna write that block of the code above, but I am getting issue. can you help me?

rickclephas commented 11 months ago

@ldobransky the childViewModel functions should be available in v1.0.0-ALPHA-10 and above. If for some reason they aren't please open an issue with some reproduction code.