Alex009 / moko-mvvm-compose-swiftui

35 stars 6 forks source link

LoginViewModelActionKs is not generated ? #2

Open RageshAntony opened 1 year ago

RageshAntony commented 1 year ago

Why LoginViewModelActionKs is not generated ? I am getting

Cannot find 'LoginViewModelActionKs' in scope

My gradle files:

project gradle

buildscript {
    val compose_version by extra("1.1.0-beta01")
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
        classpath("com.android.tools.build:gradle:7.2.1")
        classpath("dev.icerock.moko:kswift-gradle-plugin:0.5.0")
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

shared module gradle

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("dev.icerock.moko.kswift")
}

version = "1.0"
val mokoMvvmVersion = "0.13.0"

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        ios.deploymentTarget = "14.1"
        podfile = project.file("../iosApp/Podfile")
        framework {
            baseName = "MultiPlatformLibrary"
            export("dev.icerock.moko:mvvm-core:$mokoMvvmVersion")
            export("dev.icerock.moko:mvvm-flow:$mokoMvvmVersion")
        }
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation ("com.badoo.reaktive:reaktive:1.2.2")
                implementation ("com.badoo.reaktive:reaktive-annotations:1.2.2")
                api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1-native-mt")
                api("dev.icerock.moko:mvvm-core:$mokoMvvmVersion")
                api("dev.icerock.moko:mvvm-flow:$mokoMvvmVersion")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting {
            dependencies {
                api("dev.icerock.moko:mvvm-flow-compose:$mokoMvvmVersion")
            }
        }
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    compileSdk = 32
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdk = 21
        targetSdk = 32
    }
}

kswift {
    install(dev.icerock.moko.kswift.plugin.feature.SealedToSwiftEnumFeature)
}
class LoginViewModel : ViewModel() {
    val login: CMutableStateFlow<String> = MutableStateFlow("").cMutableStateFlow()
    val password: CMutableStateFlow<String> = MutableStateFlow("").cMutableStateFlow()

    private val _isLoading: MutableStateFlow<Boolean> = MutableStateFlow(false)
    val isLoading: CStateFlow<Boolean> = _isLoading.cStateFlow()
    val isButtonEnabled: CStateFlow<Boolean> =
        combine(isLoading, login, password) { isLoading, login, password ->
            isLoading.not() && login.isNotBlank() && password.isNotBlank()
        }.stateIn(viewModelScope, SharingStarted.Eagerly, false).cStateFlow()
    private val _actions = Channel<Action>()
    val actions: CFlow<Action> get() = _actions.receiveAsFlow().cFlow()
    fun onLoginPressed() {
        _isLoading.value = true
        viewModelScope.launch {
            delay(1000)
            _isLoading.value = false
            _actions.send(Action.LoginSuccess)
        }
    }
    sealed interface Action {
        object LoginSuccess : Action
    }
}

please help me

kxxb commented 1 year ago

When you run build in xCode, LoginViewModelActionKs generated, but you need add it in xCode project manually. This step not described in article, and for newbies it might be confusion.

1) Build project in xCode and getting error Cannot find 'LoginViewModelActionKs' in scope, at this step xCode generate all necessary code from Kotlin

2) Add new group to xCode project an call it kswift 2022-08-16_07-19-40

3) In new group add files to project 2022-08-16_07-23-21

4) Find generated swift file in this route (In your case name of file will be < projectName >_shared.swift) 2022-08-16_07-22-14

5) Your project tree should be look like this 2022-08-16_07-22-52

Now build your project again and your application will be stated in emulator, don't forget choose right form the list.

WarBorg commented 1 year ago

@kxxb

I took the latest version of this repo and I am getting two errors in xcode:

error: Build input file cannot be found: '/moko-mvvm-compose-swiftui/shared/build/cocoapods/framework/MultiPlatformLibrarySwift/mvvm-compose-swiftui_shared.swift' (in target 'iosApp' from project 'iosApp')

/moko-mvvm-compose-swiftui/iosApp/iosApp/LoginScreen.swift:11:8: No such module 'mokoMvvmFlowSwiftUI'

and I see no file called kswift_shared.swift in the folder you mentioned above so I don't really understand what is going on here :|

this is how my project looks in xcode:

Screenshot 2022-08-22 at 00 19 46
RageshAntony commented 1 year ago

@kxxb

Awesome. Thanks. Working now.

Thanks for your detailed answer with images 💕💕

WarBorg commented 1 year ago

ok, I think I managed to fix it by opening the iosApp.xcworkspace in XCode instead of the iosApp.xcodeproj, building one time -> failed but all the files are generated and then the second time the app starts

hoc081098 commented 1 year ago

See my comment https://github.com/icerockdev/moko-kswift/issues/55#issuecomment-1269254912