rickclephas / KMP-NativeCoroutines

Library to use Kotlin Coroutines from Swift code in KMP apps
MIT License
1.06k stars 31 forks source link

StateFlows not generating properties for Swift #156

Closed jollygreenegiant closed 9 months ago

jollygreenegiant commented 9 months ago

KMP-Coroutines version 1.0.0-ALPHA-24 KMM-ViewModel version 1.0.0-ALPHA-17

I have a ViewModel defined in my shared layer as follows:

import com.rickclephas.kmm.viewmodel.KMMViewModel
import com.rickclephas.kmm.viewmodel.MutableStateFlow
import com.rickclephas.kmm.viewmodel.coroutineScope
import com.rickclephas.kmm.viewmodel.stateIn
import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState

open class HomeViewModel(): KMMViewModel(
    // constructor parameters
) {
    private val _state: MutableStateFlow<HomeScreenState> = MutableStateFlow(viewModelScope, HomeScreenState())

    @NativeCoroutinesState
    val state = _state.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), HomeScreenState())

    // rest of the ViewModel
}

The generated class in Xcode looks like this:

__attribute__((swift_name("HomeViewModel")))
@interface SharedHomeViewModel : SharedKmm_viewmodel_coreKMMViewModel
- (instancetype)initWithUserBooksRepository:(SharedUserBooksRepository *)userBooksRepository goalsRepository:(SharedGoalsRepository *)goalsRepository authRepository:(SharedAuthRepository *)authRepository preferences:(SharedKMMPreferences *)preferences supabaseMigrator:(SharedSupabaseMigrator *)supabaseMigrator __attribute__((swift_name("init(userBooksRepository:goalsRepository:authRepository:preferences:supabaseMigrator:)"))) __attribute__((objc_designated_initializer));
- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer)) __attribute__((unavailable));
+ (instancetype)new __attribute__((unavailable));
- (void)dismissGoodreadsPrompt __attribute__((swift_name("dismissGoodreadsPrompt()")));
- (void)fetchScreenData __attribute__((swift_name("fetchScreenData()")));
- (void)getYearlyGoal __attribute__((swift_name("getYearlyGoal()")));
- (void)onCleared __attribute__((swift_name("onCleared()")));
- (void)refresh __attribute__((swift_name("refresh()")));
- (void)setYearlyGoalGoal:(int32_t)goal __attribute__((swift_name("setYearlyGoal(goal:)")));
@end

You can see that the state property isn't there - I'm assuming that I'm just missing something here, how can I get access to that property in Swift?

rickclephas commented 9 months ago

Hi! KMP-NativeCoroutines generates extensions properties (and functions) so it is to be expected that those don't show up in the SharedHomeViewModel class. However there should be a state property in a SharedHomeViewModel category.

In your example the following Swift code should work:

let viewModel = HomeViewModel()
let state: HomeScreenState = viewModel.state

If that isn't the case I can think of a couple possible causes:

jollygreenegiant commented 9 months ago

Okay sweet, got it building - KSP wasn't applied properly and I had to update from 1.9.21-1.0.16 to 1.9.22-1.0.16. Thank you!