-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from icerockdev/develop
- Loading branch information
Showing
38 changed files
with
324 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip | ||
# can't update to gradle 8 with kotlin 1.8 | ||
# https://youtrack.jetbrains.com/issue/KT-55751 | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
plugins { | ||
id("kmp-library-convention") | ||
id("detekt-convention") | ||
id("org.jetbrains.compose") | ||
id("javadoc-stub-convention") | ||
id("publication-convention") | ||
} | ||
|
||
android { | ||
namespace = "dev.icerock.moko.mvvm.compose" | ||
defaultConfig { | ||
minSdk = 21 | ||
} | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(11)) | ||
} | ||
} | ||
|
||
kotlin { | ||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
api(projects.mvvmCore) | ||
api(compose.runtime) | ||
} | ||
} | ||
|
||
androidMain { | ||
dependencies { | ||
implementation(libs.composeFoundation) | ||
implementation(libs.androidxCore) | ||
} | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
mvvm-compose/src/androidMain/kotlin/dev/icerock/moko/mvvm/compose/getViewModel.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.icerock.moko.mvvm.compose | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.core.app.ComponentActivity | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewModelStore | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
import dev.icerock.moko.mvvm.getViewModel | ||
import dev.icerock.moko.mvvm.viewmodel.ViewModel | ||
import kotlin.reflect.KClass | ||
|
||
@Composable | ||
actual fun <T : ViewModel> getViewModel( | ||
key: Any, | ||
klass: KClass<T>, | ||
viewModelBlock: () -> T | ||
): T { | ||
val context: Context = LocalContext.current | ||
val storeHolder: ViewModelStoreHolder = remember(context, key) { | ||
val viewModelStore: ViewModelStoreOwner = context as? ViewModelStoreOwner | ||
?: error("context not implement ViewModelStoreOwner") | ||
|
||
val storeViewModel: StoreViewModel = viewModelStore.getViewModel { StoreViewModel() } | ||
storeViewModel.get(key) | ||
} | ||
val viewModel: T = remember(storeHolder) { | ||
ViewModelProvider( | ||
store = storeHolder.viewModelStore, | ||
factory = viewModelFactory { addInitializer(klass) { viewModelBlock() } } | ||
)[klass.java] | ||
} | ||
|
||
DisposableEffect(context, storeHolder) { | ||
onDispose { | ||
val componentActivity: ComponentActivity = context as? ComponentActivity | ||
?: error("context should be ComponentActivity") | ||
|
||
if (!componentActivity.isChangingConfigurations) { | ||
storeHolder.viewModelStore.clear() | ||
storeHolder.disposeStore() | ||
} | ||
} | ||
} | ||
|
||
return viewModel | ||
} | ||
|
||
private class StoreViewModel : androidx.lifecycle.ViewModel() { | ||
private val stores: MutableMap<Any, ViewModelStore> = mutableMapOf() | ||
|
||
fun get(key: Any): ViewModelStoreHolder { | ||
val store: ViewModelStore = stores[key] ?: ViewModelStore() | ||
|
||
if (stores.containsKey(key).not()) { | ||
stores[key] = store | ||
} | ||
|
||
return ViewModelStoreHolder( | ||
viewModelStore = store, | ||
disposeStore = { stores.remove(key) } | ||
) | ||
} | ||
} | ||
|
||
private data class ViewModelStoreHolder( | ||
val viewModelStore: ViewModelStore, | ||
val disposeStore: () -> Unit | ||
) |
23 changes: 23 additions & 0 deletions
23
mvvm-compose/src/commonMain/kotlin/dev/icerock/moko/mvvm/compose/ViewModelFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.icerock.moko.mvvm.compose | ||
|
||
import dev.icerock.moko.mvvm.viewmodel.ViewModel | ||
import kotlin.reflect.KClass | ||
|
||
interface ViewModelFactory<T : ViewModel> { | ||
val kClass: KClass<T> | ||
|
||
fun createViewModel(): T | ||
} | ||
|
||
inline fun <reified T : ViewModel> viewModelFactory( | ||
crossinline builder: () -> T | ||
): ViewModelFactory<T> { | ||
return object : ViewModelFactory<T> { | ||
override val kClass: KClass<T> = T::class | ||
override fun createViewModel(): T = builder() | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
mvvm-compose/src/commonMain/kotlin/dev/icerock/moko/mvvm/compose/getViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.icerock.moko.mvvm.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import dev.icerock.moko.mvvm.viewmodel.ViewModel | ||
import kotlin.reflect.KClass | ||
|
||
@Composable | ||
expect fun <T : ViewModel> getViewModel( | ||
key: Any, | ||
klass: KClass<T>, | ||
viewModelBlock: () -> T | ||
): T | ||
|
||
// at now function can't be used on iOS because of https://youtrack.jetbrains.com/issue/KT-57727 | ||
//@Composable | ||
//inline fun <reified T> getViewModel( | ||
// key: Any, | ||
// noinline viewModelBlock: @DisallowComposableCalls () -> T | ||
//): T = remember(key, T::class, viewModelBlock) | ||
|
||
/** | ||
* While https://youtrack.jetbrains.com/issue/KT-57727 it's best API that i can do. | ||
* | ||
* Created ViewModel will be saved during configuration change on Android and will call | ||
* `onCleared` when composition will destroyed. | ||
* | ||
* Usage: | ||
* ``` | ||
* getViewModel( | ||
* key = "some-key-of-current-navigation-screen", | ||
* factory = viewModelFactory { SimpleViewModel() } | ||
* ) | ||
* ``` | ||
* @param key if key have same value with last recomposition - function return same ViewModel instance | ||
* @param factory ViewModel factory, used when key changed or ViewModel not created at all | ||
* | ||
* @return new created, or already exist ViewModel instance | ||
*/ | ||
@Composable | ||
fun <T : ViewModel> getViewModel( | ||
key: Any, | ||
factory: ViewModelFactory<T> | ||
): T { | ||
return getViewModel( | ||
key = key, | ||
klass = factory.kClass, | ||
viewModelBlock = factory::createViewModel | ||
) | ||
} |
Oops, something went wrong.