Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/secret.properties
/local.properties
/build/
/.gradle/
/.idea/
/mobk-compose/build/
/mobk-core/build/
build/
107 changes: 106 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ kotlin {

### SwiftUI ###

You need add the following file [Observer.kt](./mobk-swift/Observer.kt) to your
You need add the following file [Observer.swift](./mobk-swift/Observer.swift) to your
Xcode project. Be sure to replace the import line with the name of your Kotlin
framework.

Expand Down Expand Up @@ -133,3 +133,108 @@ Observer {
}
}
```

### MobK ViewModel ###

#### Dependency ####
Add the mobk-viewmodel dependency to your commonMain dependencies :

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

#### Usage ####

The ViewModel class is a simple class that can be used to store state and logic for your application.
It is designed to be used with SwiftUI and Jetpack Compose, but can be used with any UI toolkit.

It provides reactions, that respect lifecycle of the ViewModel, and can be used to perform side effects.

``` kotlin
class CounterViewModel: MobKViewModel {
var counter by observable(0)

val counterWatch = reaction(
delay = 5.seconds,
trackingFn = { counter }) { counter ->
if (counter != null && counter > 10) {
message = "Counter is too high"
}
}

val warningWatch = whenReaction(predicate = { counter > 12 }) {
message = "Counter is really too high"
}

fun increment() {
counter++
}

@override
fun onCleared() {
super.onCleared()
// Do some cleanup here
}
}
```

#### SwiftUI ####

You need add the following file [MobKViewModel.swift](./mobk-swift/MobKViewModel.swift) to your Xcode project. Be sure to replace the import line with the name of your Kotlin framework.

After that, you can use the @VM property wrapper to manage the lifecycle of your ViewModel. When component is destroyed, the ViewModel will be cleared.
Usually, you may also use the @StateObject property wrapper to manage the persistence of your ViewModel.

``` swift
struct ContentView: View {
@StateObject @VM var counterViewModel: CounterViewModel = CounterViewModel()

var body: some View {
VStack {
Text(verbatim: self.counterViewModel.counter)

HStack {
Button(action: {
self.counterViewModel.increment()
}) {
Text("Increment")
}
}
}
}
}
```

If your view model has a dependency on a parameter, you have to use the following syntax:

``` swift
struct ContentView: View {
@StateObject @VM var counterViewModel: CounterViewModel

init(counter: Int) {
_counterViewModel = asStateObject(CounterViewModel(counter: counter))
}

var body: some View {
VStack {
Text(verbatim: self.counterViewModel.counter)

HStack {
Button(action: {
self.counterViewModel.increment()
}) {
Text("Increment")
}
}
}
}
}
```
32 changes: 0 additions & 32 deletions build.gradle

This file was deleted.

27 changes: 27 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
buildscript {
repositories {
google()
mavenCentral()
}

dependencies {
classpath("com.android.tools.build:gradle:8.1.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21")
classpath("org.jetbrains.compose:compose-gradle-plugin:1.5.1")
}
}

plugins {
kotlin("multiplatform") version "1.9.10"
}

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

kotlin {
jvm()
}
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
android.useAndroidX=true
android.useAndroidX=true
org.jetbrains.compose.experimental.uikit.enabled=true
6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Aug 11 10:48:24 CEST 2021
#Wed Sep 20 20:55:00 CEST 2023
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
73 changes: 0 additions & 73 deletions mobk-compose/build.gradle

This file was deleted.

57 changes: 0 additions & 57 deletions mobk-core/build.gradle

This file was deleted.

Loading