Hi, I’m using moko-mvvm, and I have a view model l...
# moko
s
Hi, I’m using moko-mvvm, and I have a view model like this:
Copy code
private val _products = MutableStateFlow<List<Product>>(emptyList())
val products = _products.asStateFlow().cStateFlow()
Now, in iOS, I’m trying to create a list, but this doesn’t work; I’m not sure how to write it or if I’m doing it right:
Copy code
List {
        ForEach(homeViewModel.state(\.products), id: \.id) { product in
          Text(product.title)
        }
      }
Is there a way to do something simple like this, or do I need to do something like this (mirror the BookList functionality)? https://github.com/icerockdev/moko-mvvm/blob/master/sample-declarative-ui/shared/src/commonMain/kotlin/dev/icerock/moko/mvvm/sample/declarativeui/BookListViewModel.kt I also found this pattern, where the flow is collected: https://github.com/rvenky125/NoteKMM/blob/master/iosApp/iosApp/NoteIOSViewModel.swift I guess my question is: what is the easiest way to loop through these values in iOS? In compose, I just do this, and access the value:
Copy code
val products = viewModel.products.collectAsStateWithLifecycle()
1
Ah ha, I tried something like this & it worked:
Copy code
List {
                ForEach(homeViewModel.state(\.products, equals: { $0 === $1 },
                                             mapper: { ($0 as! [Product]?)! }), id: \.id) { product in
                    Text(product.title)
                }
            }
I am still curious about best practice though, and if anyone has some examples, I’d love to check them out.
a
hello. in your case you have not predefined type of StateFlow, so you should use
state(:equals:mapper:)
function. to improve usability we create extensions with this calls - https://github.com/icerockdev/moko-mvvm/blob/80b6e45d1d007188087ad0bdd8aa15b032e50[…]1/sample-declarative-ui/iosApp/iosApp/BookListViewBinding.swift
s
Hi, thanks for the link - I have learned a lot more after studying the examples this weekend…so far, everything is working great now that I have a better understanding of how this works; I’m hoping to finish up my code tomorrow; I finished up the compose side & i’m just about done w/the iOS side….these are really great libraries - thank you!
a
thanks 🙂
120 Views