Giter VIP home page Giter VIP logo

mobk's Introduction

MobK: MobX for Kotlin Mobile Multiplatform

This is a port of the core Mobx API for Kotlin Multiplatform. Mobx is a simple and successful JS library for state management.

This library bring the benefits of transparent reactive programming to your mobile, while leveraging modern UI toolkit such as SwiftUI and Jetpack Compose.

Getting started

Add the following repository to your multiplatform project build.gradle.

repositories {
    maven {
        url "TODO URL""
    }
}

Add mobk-core as an api dependency to your commonMain dependencies :

kotlin {
    ...
    sourceSets {
        commonMain {
            dependencies {
                ...
                api "io.monkeypatch:mobk-core:$mobk_version"
            }
        }

For the iOS part, you need to export mobx API as a KMP framework. This is needed so that you can consume Observable values from SwiftUI views.

kotlin {
    android()
    ios {
        binaries {
            framework {
                baseName = "..."
                export("io.monkeypatch:mobk-core-iosx64:$mobk_version")
            }
        }
    }

If you are using the multiplatform cocoapods plugin, which will create a framework for you to consume in your Xcode project, your configuration should look like this:

kotlin {
    ...
    ios {
        def iosArch = System.getenv('SDK_NAME')?.startsWith("iphoneos") ? "iosarm64" : "iosx64"
        binaries.forEach {
            if (it instanceof org.jetbrains.kotlin.gradle.plugin.mpp.Framework) {
                it.export("io.monkeypatch:mobk-$iosArch:$mobk_version")
            }
        }
    }

SwiftUI

You need add the following file Observer.kt to your Xcode project. Be sure to replace the import line with the name of your Kotlin framework.

After that, you can use the Observer view anywhere in the hierarchy. The observable values used in the Observer block will be tracked, and the view will be automatically rebuilded whenever one of those values changes.

struct ContentView: View {
    let counterStore: CounterStore
    
    var body: some View {
        Observer {
            VStack {
                Text(verbatim: self.counterStore.stateView)
                
                HStack {
                    Button(action: {
                        self.counterStore.increment()
                    }) {
                        Text("Increment")
                    }
                    Button(action: {
                        self.counterStore.decrement()
                    }) {
                        Text("Decrement")
                    }.disabled(!self.counterStore.decrementAvailable)
                }
            }
        }
    }
}

Jetpack Compose

Add the mobk-compose depenency to your Android application. Due to limitation of the Compose, the API is slightly different from SwiftUI

Inside your composables, you use Observer and pass a lambda that:

  • Get the value of all the observables/computed you wish to observe
  • Return a Render block containing the composable you want to display, based on the current values.
Observer {
    // All observations should happen here, before Render
    val stateView = counterStore.stateView
    val decrementAvailable = counterStore.decrementAvailable

    Render  {
        Column(modifier = Modifier.padding(16.dp)) {
            Text(text = stateView)

            Button(onClick = { counterStore.increment() }) {
                Text(text = "Increment")
            }
            Button(onClick = { counterStore.decrement() }, enabled = decrementAvailable) {
                Text(text = "Decrement")
            }
        }
    }
}

mobk's People

Stargazers

Thomas Salvetat avatar

Watchers

igor avatar Guillaume ANDRIEU avatar Arnaud Bos avatar James Cloos avatar Alexandre Delattre avatar Emmanuel Vinas avatar Florent Le Gac avatar Logan Mzz avatar Thomas Salvetat avatar

mobk's Issues

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.