Coder Social home page Coder Social logo

hoc081098 / kmp-viewmodel Goto Github PK

View Code? Open in Web Editor NEW
103.0 3.0 4.0 4.23 MB

🔆 Kotlin Multiplatform ViewModel. Kotlin Multiplatform MVVM. Common/Shared ViewModel in Kotlin Multiplatform - A Kotlin Multiplatform library that provides shared MVVM for UI applications. Components are lifecycle-aware on Android. Supports Android Parcelable, Kotlin Parcelize, AndroidX SavedStateHandle for restoring state after process death.

Home Page: https://hoc081098.github.io/kmp-viewmodel/docs/0.x/

License: MIT License

Kotlin 99.22% Shell 0.13% Swift 0.55% HTML 0.10%
kotlin-multiplatform kmm kmm-example kmm-library kmm-mvvm kmm-sample kmp-redux kmp-viewmodel kotlin-multiplaform kotlin-multiplatform-library

kmp-viewmodel's Introduction

kmp-viewmodel 🔆

Kotlin Multiplatform ViewModel - Kotlin Multiplatform MVVM - Compose Multiplatform ViewModel

Common/ Shared ViewModel in Kotlin Multiplatform - A Kotlin Multiplatform library that provides shared MVVM for UI applications. Components are lifecycle-aware on Android. Supports Android Parcelable, Kotlin Parcelize, AndroidX SavedStateHandle for restoring state after process death. Easy interoperability with Swift/Objective-C and SwiftUI. Supports Compose Multiplatform Framework (Android, Desktop, Web, iOS, macOS, tvOS, watchOS).

The ViewModel class is a business logic or screen level state holder. It exposes state to the UI and encapsulates related business logic. Its principal advantage is that it caches state and persists it through configuration changes (on Android).

kmp-viewmodel and Navigation for JetBrains Compose Multiplatform 👉 https://github.com/hoc081098/solivagant

maven-central codecov Build and publish snapshot Build sample Publish Release License: MIT Kotlin version KotlinX Coroutines version Compose Multiplatform version Hits badge badge badge badge badge badge badge badge badge

Liked some of my work? Buy me a coffee (or more likely a beer)

Buy Me A Coffee

Supported targets

  • android.
  • jvm (must add kotlinx-coroutines-swing/kotlinx-coroutines-javafx to your dependencies to make sure Dispatchers.Main available).

Note

If you are targeting Desktop and:

  • not using JetBrains Compose Multiplatform, you should provide the dependency org.jetbrains.kotlinx:kotlinx-coroutines-swing or org.jetbrains.kotlinx:kotlinx-coroutines-javafx.
  • using JetBrains Compose Multiplatform, you should provide org.jetbrains.kotlinx:kotlinx-coroutines-swing.

Because the ViewModel.viewModelScope depends on Dispatchers.Main provided by that libraries on Desktop.

  • js (IR).
  • wasmJs.
  • Darwin targets:
    • iosArm64, iosX64, iosSimulatorArm64.
    • watchosArm32, watchosArm64, watchosX64, watchosSimulatorArm64.
    • tvosX64, tvosSimulatorArm64, tvosArm64.
    • macosX64, macosArm64.

Docs

Getting started

1. kmp-viewmodel library

For more information check out the docs.

2. kmp-viewmodel-savedstate library

For more information check out the docs.

3. kmp-viewmodel and Swift interoperability

For more information check out the docs.

4. kmp-viewmodel-compose library

For more information check out the docs.

5. kmp-viewmodel-koin-compose library

For more information check out the docs.

6. kmp-viewmodel-koject-compose library

For more information check out the docs.

7. kmp-viewmodel and Navigation for JetBrains Compose Multiplatform.

For more information check out https://github.com/hoc081098/solivagant library.

Sample

  • KMM sample: shares business logic and ViewHolders, using Jetpack Compose for Android and SwiftUi for iOS.
  • Compose Multiplatform Koin sample: shares ViewModels and integrates with Navigation in Compose Multiplatform. It uses Koin for DI.
  • Compose Multiplatform Koject sample: shares ViewModels and integrates with Navigation in Compose Multiplatform. It uses Koject for DI.
  • Compose Multiplatform KmpViewModel KMM Unsplash Sample: A KMP template of the Unsplash App using Compose multiplatform for Android, Desktop, iOS. Share everything including data, domain, presentation, and UI.
  • 🍭 GithubSearchKMM: Github Repos Search KMM for Android and iOS. Kotlin Multiplatform Mobile using Jetpack Compose, SwiftUI, FlowRedux, Coroutines Flow, Dagger Hilt, Koin Dependency Injection, shared KMP ViewModel, Clean Architecture.

Roadmap

  • support SaveStateHandle (since 0.2.0).
  • add extensions for Flow/StateFlow, to use the ViewModel easily on ios/macOS/tvOS/watchOS platforms (since 0.3.0).
  • support Compose Multiplatform Framework (since 0.5.0).
  • support Koin integration with ViewModel and JetBrains Compose Multiplatform (since 0.6.2).

License

MIT License
Copyright (c) 2023-2024 Petrus Nguyễn Thái Học

kmp-viewmodel's People

Contributors

dependabot[bot] avatar hoc081098 avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

kmp-viewmodel's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: renovate.json5
Error type: Invalid JSON5 (parsing failed)
Message: JSON5.parse error: JSON5: invalid character '{' at 40:5

Placeholder

Screen.Recording.2023-09-25.at.10.49.05.PM.mov
Screen.Recording.2023-09-25.at.10.41.59.PM.mov
Simulator.Screen.Recording.-.iPhone.11.Pro.-.2023-09-25.at.23.34.25.mp4

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Use matchDepPatterns instead of matchPackagePatterns
  • WARN: Error updating branch: update failure

Errored

These updates encountered an error and will be retried. Click on a checkbox below to force a retry now.

  • Update dependency androidx.lifecycle:lifecycle-runtime-compose to v2
  • Update dependency androidx.lifecycle:lifecycle-viewmodel-compose to v2

Detected dependencies

cocoapods
sample/iosApp/Podfile
  • Kingfisher ~> 7.0
  • Kingfisher ~> 7.0
  • RxSwift ~> 6.7.0
  • RxCocoa ~> 6.7.0
  • RxRelay ~> 6.7.0
  • NSObject+Rx ~> 5.2.2
  • RxDataSources ~> 5.0
  • MBProgressHUD ~> 1.2.0
github-actions
.github/workflows/build.yml
  • actions/checkout v4
  • actions/setup-java v4
  • actions/cache v4
  • actions/cache v4
  • codecov/codecov-action v4.3.1
  • actions/upload-artifact v4
  • actions/checkout v4
  • actions/setup-java v4
  • actions/cache v4
  • actions/cache v4
  • actions/setup-python v5
  • JamesIves/github-pages-deploy-action v4.6.0
  • actions/checkout v4
  • actions/setup-java v4
  • actions/cache v4
  • actions/cache v4
  • actions/cache v4
  • reactivecircus/android-emulator-runner v2.30.1
  • reactivecircus/android-emulator-runner v2.30.1
.github/workflows/deploy-release-docs.yml
  • actions/checkout v4
  • actions/setup-java v4
  • actions/cache v4
  • actions/cache v4
  • actions/setup-python v5
  • JamesIves/github-pages-deploy-action v4.6.0
.github/workflows/gradle-wrapper-validation.yml
  • actions/checkout v4
  • gradle/wrapper-validation-action v3
.github/workflows/publish-release.yml
  • actions/checkout v4
  • ffurrer2/extract-release-notes v2
  • softprops/action-gh-release v2
  • actions/checkout v4
  • actions/setup-java v4
  • actions/cache v4
  • actions/cache v4
  • actions/setup-python v5
  • JamesIves/github-pages-deploy-action v4.6.0
.github/workflows/sample.yml
  • actions/checkout v4
  • actions/setup-java v4
  • actions/cache v4
  • actions/cache v4
  • actions/checkout v4
  • actions/setup-java v4
  • maxim-lobanov/setup-xcode v1
  • actions/cache v4
  • actions/cache v4
  • sersoft-gmbh/xcodebuild-action v3
  • sersoft-gmbh/xcodebuild-action v3
.github/workflows/standalone-sample.yml
  • actions/checkout v4
  • actions/setup-java v4
  • actions/cache v4
  • actions/cache v4
  • actions/checkout v4
  • actions/setup-java v4
  • maxim-lobanov/setup-xcode v1
  • actions/cache v4
  • actions/cache v4
  • sersoft-gmbh/xcodebuild-action v3
standalone-sample/kmpviewmodel_compose_koject_sample/.github/workflows/gradle.yml
  • actions/checkout v4
  • actions/setup-java v4
  • gradle/actions v3.3.2@db19848a5fa7950289d3668fb053140cf3028d43
  • actions/checkout v4
  • actions/setup-java v4
  • gradle/actions v3.3.2@db19848a5fa7950289d3668fb053140cf3028d43
gradle
gradle.properties
settings.gradle.kts
  • org.gradle.toolchains.foojay-resolver-convention 0.8.0
build.gradle.kts
gradle/libs.versions.toml
  • org.jetbrains.kotlinx:kotlinx-coroutines-core 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-android 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-test 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-jdk8 1.8.1-Beta
  • junit:junit 4.13.2
  • org.jetbrains.kotlinx:kotlinx-serialization-json 1.6.3
  • org.jetbrains.kotlinx:kotlinx-collections-immutable 0.3.7
  • org.robolectric:robolectric 4.12.1
  • co.touchlab:stately-concurrency 2.0.7
  • io.github.aakira:napier 2.7.1
  • io.github.hoc081098:FlowExt 0.8.1-Beta
  • io.insert-koin:koin-core 3.5.6
  • io.insert-koin:koin-android 3.5.6
  • io.insert-koin:koin-androidx-compose 3.5.6
  • io.insert-koin:koin-compose 1.1.5
  • com.moriatsushi.koject:koject-core 1.3.0
  • com.moriatsushi.koject:koject-processor-lib 1.3.0
  • io.coil-kt:coil-compose 2.6.0
  • io.nlopez.compose.rules:detekt 0.3.20
  • androidx.lifecycle:lifecycle-viewmodel-ktx 2.8.0-rc01
  • androidx.lifecycle:lifecycle-viewmodel-savedstate 2.8.0-rc01
  • androidx.lifecycle:lifecycle-runtime-compose androidx-lifecycle
  • androidx.lifecycle:lifecycle-viewmodel-compose androidx-lifecycle
  • androidx.lifecycle:lifecycle-livedata-ktx 2.8.0-rc01
  • androidx.annotation:annotation 1.7.1
  • androidx.activity:activity-compose 1.9.0
  • androidx.compose:compose-bom 2024.05.00
  • androidx.navigation:navigation-compose 2.7.7
  • org.jetbrains.kotlin.multiplatform 1.9.23
  • org.jetbrains.kotlin.android 1.9.23
  • org.jetbrains.kotlin.native.cocoapods 1.9.23
  • org.jetbrains.kotlin.plugin.serialization 1.9.23
  • org.jetbrains.kotlin.plugin.parcelize 1.9.23
  • org.jetbrains.compose 1.6.2
  • com.android.application 8.4.0
  • com.android.library 8.4.0
  • com.diffplug.gradle.spotless 6.25.0
  • io.gitlab.arturbosch.detekt 1.23.6
  • org.jetbrains.kotlinx.kover 0.7.6
  • org.jetbrains.dokka 1.9.20
  • org.jetbrains.kotlinx.binary-compatibility-validator 0.14.0
  • com.google.devtools.ksp 1.9.23-1.0.20
  • dev.drewhamilton.poko 0.15.2
  • com.vanniktech.maven.publish 0.28.0
sample/app/build.gradle.kts
sample/shared/build.gradle.kts
standalone-sample/kmpviewmodel_compose_koin_sample/gradle.properties
standalone-sample/kmpviewmodel_compose_koin_sample/settings.gradle.kts
  • org.gradle.toolchains.foojay-resolver-convention 0.8.0
standalone-sample/kmpviewmodel_compose_koin_sample/build.gradle.kts
standalone-sample/kmpviewmodel_compose_koin_sample/android/build.gradle.kts
standalone-sample/kmpviewmodel_compose_koin_sample/common/build.gradle.kts
  • junit:junit 4.13.2
standalone-sample/kmpviewmodel_compose_koin_sample/desktop/build.gradle.kts
standalone-sample/kmpviewmodel_compose_koin_sample/gradle/libs.versions.toml
  • org.jetbrains.kotlinx:kotlinx-coroutines-core 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-android 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-swing 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-test 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-jdk8 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-serialization-json 1.6.3
  • org.jetbrains.kotlinx:kotlinx-collections-immutable 0.3.7
  • org.jetbrains.compose.runtime:runtime 1.6.2
  • co.touchlab:stately-concurrency 2.0.7
  • io.github.aakira:napier 2.7.1
  • io.github.hoc081098:FlowExt 0.8.1-Beta
  • io.insert-koin:koin-core 3.5.6
  • io.insert-koin:koin-android 3.5.6
  • io.insert-koin:koin-androidx-compose 3.5.6
  • io.insert-koin:koin-compose 1.1.5
  • io.coil-kt:coil-compose 2.6.0
  • io.nlopez.compose.rules:detekt 0.3.20
  • androidx.lifecycle:lifecycle-viewmodel-ktx 2.7.0
  • androidx.lifecycle:lifecycle-viewmodel-savedstate 2.7.0
  • androidx.lifecycle:lifecycle-runtime-compose androidx-lifecycle
  • androidx.lifecycle:lifecycle-viewmodel-compose androidx-lifecycle
  • androidx.annotation:annotation 1.7.1
  • androidx.activity:activity-compose 1.9.0
  • androidx.appcompat:appcompat 1.6.1
  • androidx.compose:compose-bom 2024.05.00
  • androidx.core:core-ktx 1.13.1
  • androidx.navigation:navigation-compose 2.7.7
  • com.benasher44:uuid 0.8.4
  • io.github.hoc081098:solivagant-navigation 0.3.0
  • org.jetbrains.kotlin.multiplatform 1.9.23
  • org.jetbrains.kotlin.android 1.9.23
  • org.jetbrains.kotlin.native.cocoapods 1.9.23
  • org.jetbrains.kotlin.plugin.serialization 1.9.23
  • org.jetbrains.kotlin.plugin.parcelize 1.9.23
  • org.jetbrains.compose 1.6.2
  • com.android.application 8.4.0
  • com.android.library 8.4.0
  • com.diffplug.gradle.spotless 6.25.0
  • io.gitlab.arturbosch.detekt 1.23.6
  • org.jetbrains.kotlinx.kover 0.7.6
  • org.jetbrains.dokka 1.9.20
  • org.jetbrains.kotlinx.binary-compatibility-validator 0.14.0
  • com.vanniktech.maven.publish 0.28.0
standalone-sample/kmpviewmodel_compose_koject_sample/gradle.properties
standalone-sample/kmpviewmodel_compose_koject_sample/settings.gradle.kts
standalone-sample/kmpviewmodel_compose_koject_sample/build.gradle.kts
standalone-sample/kmpviewmodel_compose_koject_sample/composeApp/build.gradle.kts
standalone-sample/kmpviewmodel_compose_koject_sample/gradle/libs.versions.toml
  • com.moriatsushi.koject:koject-android-core 1.3.0
  • com.moriatsushi.koject:koject-core 1.3.0
  • com.moriatsushi.koject:koject-compose-core 1.3.0
  • com.moriatsushi.koject:koject-processor-app 1.3.0
  • org.jetbrains.kotlin:kotlin-test 1.9.23
  • org.jetbrains.kotlin:kotlin-test-junit 1.9.23
  • junit:junit 4.13.2
  • androidx.core:core-ktx 1.13.1
  • androidx.test.ext:junit 1.1.5
  • androidx.test.espresso:espresso-core 3.5.1
  • androidx.appcompat:appcompat 1.6.1
  • com.google.android.material:material 1.12.0
  • androidx.constraintlayout:constraintlayout 2.1.4
  • androidx.activity:activity-compose 1.9.0
  • androidx.compose.ui:ui-tooling 1.6.7
  • androidx.compose.ui:ui-tooling-preview 1.6.7
  • io.github.hoc081098:FlowExt 0.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-collections-immutable 0.3.7
  • org.jetbrains.kotlinx:kotlinx-coroutines-android 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-core 1.8.1-Beta
  • org.jetbrains.kotlinx:kotlinx-coroutines-swing 1.8.1-Beta
  • io.github.hoc081098:solivagant-navigation 0.3.0
  • com.android.application 8.4.0
  • com.android.library 8.4.0
  • org.jetbrains.compose 1.6.2
  • org.jetbrains.kotlin.multiplatform 1.9.23
  • org.jetbrains.kotlin.plugin.parcelize 1.9.23
  • com.google.devtools.ksp 1.9.23-1.0.20
viewmodel/gradle.properties
viewmodel/build.gradle.kts
viewmodel-compose/gradle.properties
viewmodel-compose/build.gradle.kts
  • androidx.test.ext:junit 1.1.5
viewmodel-koin/gradle.properties
viewmodel-koin/build.gradle.kts
viewmodel-koin-compose/gradle.properties
viewmodel-koin-compose/build.gradle.kts
viewmodel-koject/gradle.properties
viewmodel-koject/build.gradle.kts
viewmodel-koject-compose/gradle.properties
viewmodel-koject-compose/build.gradle.kts
viewmodel-savedstate/gradle.properties
viewmodel-savedstate/build.gradle.kts
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 8.7
standalone-sample/kmpviewmodel_compose_koin_sample/gradle/wrapper/gradle-wrapper.properties
  • gradle 8.7
standalone-sample/kmpviewmodel_compose_koject_sample/gradle/wrapper/gradle-wrapper.properties
  • gradle 8.7
pip_requirements
docs/requirements.txt
  • mkdocs ==1.6.0
  • mkdocs-material ==9.5.21
  • pymdown-extensions ==10.8.1
  • mdx_truly_sane_lists ==1.3

  • Check this box to trigger a request for Renovate to run again on this repository

Fix unit test

com.hoc081098.kmp.viewmodel.ViewModelTest > addCloseablesThatWillBeClosedWhenClear_multiThreads FAILED
    java.lang.IllegalStateException: Already closed!
        at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:42)
        at com.hoc081098.kmp.viewmodel.ViewModelTest$addCloseablesThatWillBeClosedWhenClear_multiThreads$1$2$1.invokeSuspend(ViewModelTest.kt:125)

        Caused by:
        java.lang.IllegalStateException: Already closed!
            at com.hoc081098.kmp.viewmodel.utils.TestCloseable.close(TestCloseable.kt:10)
            at androidx.lifecycle.ViewModel.closeWithRuntimeException(ViewModel.java:252)
            at androidx.lifecycle.ViewModel.clear(ViewModel.java:197)
            at androidx.lifecycle.ViewModelStore.clear(ViewModelStore.kt:69)
            at com.hoc081098.kmp.viewmodel.ViewModelStore.clear(ViewModelStore.android.kt:46)
            at com.hoc081098.kmp.viewmodel.ViewModelTestKt$createTestViewModel$2.invoke(ViewModelTest.kt:53)
            at com.hoc081098.kmp.viewmodel.ViewModelTestKt$createTestViewModel$2.invoke(ViewModelTest.kt:53)
            at com.hoc081098.kmp.viewmodel.utils.RunBlockInNewThread_androidKt$runBlockInNewThread$2$2.run(runBlockInNewThread.android.kt:13)
            at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
            at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
            at java.base/java.lang.Thread.run(Thread.java:840)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.