Shared ViewModel in Kotlin Multiplatform

Overview

multiplatform-viewmodel 🗳

Create shared ViewModel's for shared business logic using our ViewModel base class.

Features

  • Uses Jetpack ViewModel on Android for lifecycled
  • Exposes a CoroutineScope to be used in your methods

Overview of Package

This package has 1 component to it: A ViewModel class you can base your view models on.

Getting Started

Add Dependency

commonMain {
    dependencies {
        // ...
        api("com.doublesymmetry:multiplatform-viewmodel:0.0.1")
    }
}

Expose it to iOS native side

ios {
    binaries {
        framework {
            baseName = "shared"
            export(Deps.viewmodel) // required to expose the class to iOS
        }
    }
}

Using the ViewModel

class ExampleViewModel: ViewModel() {
    private val _viewState = MutableStateFlow(UIViewState())
    val viewState: StateFlow<UIViewState> = _viewState
 
    fun onLaunched() {
        scope.launch {
            // fetch some data
            _viewState.emit(newState)
        }
    }
}

Using your ViewModel on iOS with a UIViewController

When using it on iOS you'll want to make sure that you call clear() on your ViewModel on deinit to properly kill the CoroutineScope

You might also like...
[Android-Kotlin] MVVM, ViewModel, LiveData, Observer, DataBinding, Repository, Retrofit, Dagger example

SimpleMvvmDaggerKotlin [Android-Kotlin] MVVM, ViewModel, LiveData, Observer, DataBinding, Repository, Retrofit, Dagger example [Image1 : User informat

A simple project that describes the relationship between the view and it's viewmodel in android development
A simple project that describes the relationship between the view and it's viewmodel in android development

View-ViewModel-Communication A simple project that describes the relationship between the view and it's viewmodel in android development In MVVM archi

ViewModel ve DataBindig ile LiveData kullanım örneği.
ViewModel ve DataBindig ile LiveData kullanım örneği.

#Android Kotlin ViewModel ve Livedata Görsel Kaynak: https://www.youtube.com/watch?v=suC0OM5gGAA Temiz kod mantığı ve bu mantık doğrultusunda geliştir

A minimal notes application in Jetpack Compose with MVVM architecture. Built with components like DataStore, Coroutines, ViewModel, LiveData, Room, Navigation-Compose, Coil, koin etc.
A minimal notes application in Jetpack Compose with MVVM architecture. Built with components like DataStore, Coroutines, ViewModel, LiveData, Room, Navigation-Compose, Coil, koin etc.

Paper - A Minimal Notes App A minimal notes application in Jetpack Compose with MVVM architecture. Built with components like DataStore, Coroutines, V

LifecycleAwareGitHubSearch - The Activity Lifecycle and the ViewModel Architecture

Lifecycle-Aware GitHub Search In this project, we'll modify our GitHub search ap

🪐 Modern Android development with Hilt, Coroutines, Flow, JetPack(ViewModel) based on MVVM architecture.

Ceres 🪐 Modern Android development with Hilt, Coroutines, Flow, JetPack(ViewModel) based on MVVM architecture. Download Gradle Add the dependency bel

Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

Opinionated Redux-like implementation backed by Kotlin Coroutines and Kotlin Multiplatform Mobile

CoRed CoRed is Redux-like implementation that maintains the benefits of Redux's core idea without the boilerplate. No more action types, action creato

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.
An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile. 项目架构主要分为原生系统层、Android/iOS业务SDK层、KMM SDK层、KMM业务逻辑SDK层、iOS sdkfra

Releases(0.0.3)
  • 0.0.3(Apr 26, 2022)

    What's Changed

    • Add better instantiation and synchronization of scope by @dcvz in https://github.com/doublesymmetry/multiplatform-viewmodel/commit/4bb4c72c494c9f7f4447efebfcefdc348fc3c1b7

    Breaking

    • Expose CoroutineScope as viewModelScope by @dcvz in https://github.com/doublesymmetry/multiplatform-viewmodel/commit/5604b999ad9fa7f70322ff98b36836ad1e0f83df

    New Contributors

    • @dcvz made their first contribution in https://github.com/doublesymmetry/multiplatform-viewmodel/pull/1

    Full Changelog: https://github.com/doublesymmetry/multiplatform-viewmodel/compare/0.0.2...0.0.3

    Source code(tar.gz)
    Source code(zip)
  • 0.0.2(Apr 16, 2022)

  • 0.0.1(Apr 7, 2022)

Owner
Double Symmetry
Double Symmetry
Plugin-shared-preferences - Pluto plugin to manage your Shared Preferences

Pluto Shared Preferences Plugin Pluto Shared Preferences is a Pluto plugin to in

Pluto 1 Feb 14, 2022
D-KMP Architecture official sample: it uses a shared KMP ViewModel and Navigation for Compose and SwiftUI apps.

D-KMP architecture - official sample This is the official sample of the D-KMP architecture, presenting a simple master/detail app, for Android, iOS an

null 594 Jan 3, 2023
ViewModel LiveData Sample - Sample of using ViewModel, LiveData and Data Binding

ViewModel_LiveData_Sample Sample Code for Lesson 8 using ViewModel, LiveData and

null 0 Mar 18, 2022
Clean MVVM with eliminating the usage of context from view models by introducing hilt for DI and sealed classes for displaying Errors in views using shared flows (one time event), and Stateflow for data

Clean ViewModel with Sealed Classes Following are the purposes of this repo Showing how you can remove the need of context in ViewModels. I. By using

Kashif Mehmood 22 Oct 26, 2022
Astha Nayak 4 Oct 10, 2022
Android Ptrace Inject for all ABIs and all APIs. Help you inject Shared Library on Android.

Android Ptrace Inject 中文可以参考我的注释内容进行理解 我写的注释相对来说比较全面了 How to build Make sure you have CMake and Ninja in your PATH Edit CMakeLists.txt. Set ANDROID_ND

SsageParuders 65 Dec 19, 2022
TinyDB Android Shared Preferences Turbo Turkce

TinyDB--Android-Shared-Preferences-Turbo-Turkce Bu sınıf, bir satır kod ile SharedPreferences çağrılarını ve daha fazlasını gerçekleştirmenizi sağlar.

Mehmet Abak 2 Nov 6, 2022
Android To-Do MVVM Architecture App written in Kotlin.(ViewModel, ROOM, Livedata, Coroutines)

MVVM-To-Do-App A To-Do application written in kotlin using Android Architectural components What's new? Room + Coroutines - Upgraded Room to v2.1. Roo

Naveen T P 77 Dec 8, 2022
Sample app that implements MVVM architecture using Kotlin, ViewModel, LiveData, and etc.

TheShard is a project to showcase different architectural approaches to developing Android apps. In its different branches you will find the same app (A movie Listing and detail page) implemented with small differences.

null 17 Aug 19, 2021
ViewModel e LiveData - Android Studio - Kotlin

Unscramble App Starter code for Android Basics codelab - Store the data in a ViewModel Unscramble is a single player game app that displays scrambled

Ademir Oliveira 1 Nov 23, 2021