-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ViewModel State In BaseViewModel With Generic Type Compiles As Any
#15
Comments
This is an unfortunate limitation of the KMP-NativeCoroutines implementation combined with the Kotlin Objective-C interop. While it can't be completely fix, there might be a way to add the correct type on the Swift side again. |
Alright, that makes sense. Thank you |
I just had the same issue and realized that in Swift you have access to I don't know if it's new and I am not sure of the side effects in using this solution. |
@matthiaslao it sounds like you somehow have two properties with the name |
@rickclephas abstract class BaseViewModel<State : Reducer.ViewState> : KMMViewModel() {
@NativeCoroutinesState
abstract val state: StateFlow<State>
} class Store<State : Reducer.ViewState>(
viewModelScope: ViewModelScope,
initialState: State,
) {
private val _state: MutableStateFlow<State> = MutableStateFlow(viewModelScope, initialState)
val state: StateFlow<State>
get() = _state.asStateFlow()
} class MovieDetailViewModel : BaseViewModel<MovieDetailViewState>() {
private val store = Store(
viewModelScope = viewModelScope,
initialState = MovieDetailViewState.initial()
)
@NativeCoroutinesState
override val state: StateFlow<MovieDetailViewState>
get() = store.state
} |
Ok, I realized that I have set |
@matthiaslao that's correct. You can either specify the name explicitly with e.g. |
@rickclephas
I found another workaround by putting the abstract class BaseViewModel<State : Reducer.ViewState> : KMMViewModel(), ViewModelInterface<State>
interface ViewModelInterface<State : Reducer.ViewState> {
@NativeCoroutinesState // if deleted here, we have a compilation error
val state: StateFlow<State>
}
and we have to leave it also in the implementation class MovieDetailViewModel : BaseViewModel<MovieDetailViewState>() {
private val store = Store(
viewModelScope = viewModelScope,
initialState = MovieDetailViewState.initial()
)
@NativeCoroutinesState
override val state: StateFlow<MovieDetailViewState>
get() = store.state
} In that case we won't have any duplicated Thank you a lot for your help and the awesome library! |
Ah you are right. You can satisfy this requirement with the
Correct, although that is only a side effect of the Kotlin-ObjC interop regarding interfaces. It is still creating an extension property/function. It's just not directly accessible (in Swift) on the view model class. |
I'm not sure if this is an issue with
Kotlin/Native
itself but I have a BaseViewModel with generic typeT
which is used to construct a stateflow:All viewmodels in the project subclass this like so:
When using this in Xcode,
viewModel.state
in Swift has a type ofAny
, which means i have to cast it:Any idea why?
Thanks in advance
The text was updated successfully, but these errors were encountered: