icerockdev / moko-mvvm

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

Add support of using @State attribute of SwiftUI with moko-mvvm State class #47

Closed michaelbrainvu closed 3 years ago

michaelbrainvu commented 4 years ago

When importing the MultiPlatformLibrary to a SwiftUI view I get an error "Unknown attribute @State". I think that the State class defined in moko-mvvm somehow causes this issue.

Alex009 commented 4 years ago

just try to use import struct SwiftUI.State. it force import of SwiftUI State, but you can't use State from mpp in this case. Only with fullname: MultiPlatformLibrary.State

DmyMi commented 3 years ago

Maybe it's possible to rename State to something like MokoState or ViewState, and Data & Error to something like Success & Fail (for now there are no such types in either SwiftUI or Combine frameworks). As more people are going to use SwiftUI it will remove some confusion.

Alex009 commented 3 years ago

@DmyMi agree. i think about ResourceState name, or ViewState yes.

sealed class ViewState<out T, out E> {
    data class Success<out T, out E>(val data: T) : ViewState<T, E>()
    data class Fail<out T, out E>(val error: E) : ViewState<T, E>()
    class Loading<out T, out E> : ViewState<T, E>()
    class Empty<out T, out E> : ViewState<T, E>()
}

seems good i think.

but required automatically refactoring tool for migration https://readyset.build/kotlin-deprecation-goodies-a35a397aa9b5 i think replaceWith will help us. to implement it i think we should move current State class into mvvm-deprecated module, and in this module will be available old State class with @Deprecated mark and replaceWith - to smooth update of current apps. in core mvvm module we will not contain State - but inly ViewState

Alex009 commented 3 years ago

start from 0.9.0 we have two modules:

  1. mvvm-state-deprecated with State class that used now
  2. mvvm-state with ResourceState class (renamed State) - this will allow the class to be used with SwiftUI

by default mvvm umbrella artifact depends on mvvm-state-deprecated now, to support smooth migration to new version.