icerockdev / moko-mvvm

Model-View-ViewModel architecture components for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
994 stars 95 forks source link

Clarify SwiftUI ViewModel state null semantics #268

Closed darronschall closed 3 months ago

darronschall commented 3 months ago

This simplifies code and cleans up call-site use, and makes it explicit when to use each helper. When your Kotlin ViewModel StateFlow has non-optional values, use the state helper in SwiftUI. When your StateFlow has an optional type and might have null values, use stateNullable in SwiftUI.

Note: There isn't compiler help when deciding between state and stateNullable in SwiftUI. Choosing state where stateNullable is required will cause a runtime crash due to force unwrapping. Take care to use the correct helper.

This PR also introduces state and stateNullable helpers for KotlinBase. When Kotlin/Native exports to Objective-C, all custom classes share a common KotlinBase interface (which implements NSObject and has isEqual available).

This simplification makes the library slightly easier to use and perhaps a little more intuitive. Instead of forcing callers through state(_:equals:mapper:) for custom types and making them define their own equals and mapper, it's now possible to just use the simpler state(_) helper.