Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 committed Oct 19, 2019
2 parents 1cddf40 + 95f92e3 commit a355d61
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion MultiPlatformLibraryMvvm.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'MultiPlatformLibraryMvvm'
spec.version = '0.1.0'
spec.version = '0.3.0'
spec.homepage = 'https://github.com/icerockdev/moko-mvvm'
spec.source = { :git => "https://github.com/icerockdev/moko-mvvm.git", :tag => "release/#{spec.version}" }
spec.authors = 'IceRock Development'
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This is a Kotlin Multiplatform library that provides architecture components of
- kotlin 1.3.50
- 0.1.0
- 0.2.0
- 0.3.0

## Installation
root build.gradle
Expand All @@ -44,7 +45,7 @@ allprojects {
project build.gradle
```groovy
dependencies {
commonMainApi("dev.icerock.moko:mvvm:0.2.0")
commonMainApi("dev.icerock.moko:mvvm:0.3.0")
}
```

Expand All @@ -55,7 +56,7 @@ enableFeaturePreview("GRADLE_METADATA")

On iOS, in addition to the Kotlin library add in Podfile
```ruby
pod 'MultiPlatformLibraryMvvm', :git => 'https://github.com/icerockdev/moko-mvvm.git', :tag => 'release/0.2.0'
pod 'MultiPlatformLibraryMvvm', :git => 'https://github.com/icerockdev/moko-mvvm.git', :tag => 'release/0.3.0'
```
**`MultiPlatformLibraryMvvm` CocoaPod requires that the framework compiled from Kotlin be named
`MultiPlatformLibrary` and be connected as a CocoaPod `MultiPlatformLibrary`.
Expand Down Expand Up @@ -322,7 +323,7 @@ class LoginViewModel(
val emailValue = email.value
val passwordValue = password.value

coroutineScope.launch {
viewModelScope.launch {
_isLoading.value = true

try {
Expand All @@ -346,7 +347,7 @@ class LoginViewModel(
}
}
```
`coroutineScope` is a field of the `ViewModel` class with a default Dispatcher - `UI` on both platforms.
`viewModelScope` is a `CoroutineScope` field of the `ViewModel` class with a default Dispatcher - `UI` on both platforms.
All coroutines will be canceled in `onCleared` automatically.
#### Android
`LoginActivity.kt`:
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object Versions {
const val coroutines = "1.3.0"
const val mokoCore: String = "0.1.0"
const val mokoResources: String = "0.3.0"
const val mokoMvvm: String = "0.2.0"
const val mokoMvvm: String = "0.3.0"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import kotlinx.coroutines.cancel

actual open class ViewModel actual constructor() : ViewModel() {
// for now dispatcher fixed on Main. after implementing multithread coroutines on native - we can change it
protected actual val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Main)
protected actual val viewModelScope: CoroutineScope = CoroutineScope(Dispatchers.Main)

actual override fun onCleared() {
super.onCleared()

coroutineScope.cancel()
viewModelScope.cancel()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.livedata

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow

@UseExperimental(ExperimentalCoroutinesApi::class)
fun <T> LiveData<T>.asFlow(): Flow<T> = channelFlow {
val observer: (T) -> Unit = { offer(it) }

addObserver(observer)

awaitClose { removeObserver(observer) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package dev.icerock.moko.mvvm.viewmodel
import kotlinx.coroutines.CoroutineScope

expect open class ViewModel() {
protected val coroutineScope: CoroutineScope
protected val viewModelScope: CoroutineScope

protected open fun onCleared()
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,24 @@ internal class UIDispatcher : CoroutineDispatcher(), Delay {
}
}
}

override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle {
var disposed = false
dispatch_after(
`when` = dispatch_time(
DISPATCH_TIME_NOW,
timeMillis * NSEC_PER_MSEC.toLong()
),
queue = mQueue
) {
if (disposed) return@dispatch_after

block.run()
}
return object : DisposableHandle {
override fun dispose() {
disposed = true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import kotlinx.coroutines.cancel

actual open class ViewModel actual constructor() {
// for now dispatcher fixed on Main. after implementing multithread coroutines on native - we can change it
protected actual val coroutineScope: CoroutineScope = CoroutineScope(UIDispatcher())
protected actual val viewModelScope: CoroutineScope = CoroutineScope(UIDispatcher())

protected actual open fun onCleared() {
coroutineScope.cancel()
viewModelScope.cancel()
}
}
2 changes: 1 addition & 1 deletion sample/ios-app/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ target 'TestProj' do
# MultiPlatformLibrary
# для корректной установки фреймворка нужно сначала скомпилировать котлин библиотеку - compile-kotlin-framework.sh (в корневой директории репозитория)
pod 'MultiPlatformLibrary', :path => '../mpp-library'
pod 'MultiPlatformLibraryMvvm', :git => 'https://github.com/icerockdev/moko-mvvm.git', :tag => 'release/0.2.0'
pod 'MultiPlatformLibraryMvvm', :git => 'https://github.com/icerockdev/moko-mvvm.git', :tag => 'release/0.3.0'
pod 'MultiPlatformLibraryResources', :git => 'https://github.com/icerockdev/moko-resources.git', :tag => 'release/0.3.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LoginViewModel(
val emailValue = email.value
val passwordValue = password.value

coroutineScope.launch {
viewModelScope.launch {
_isLoading.value = true

try {
Expand Down

0 comments on commit a355d61

Please sign in to comment.