Skip to content

Commit ff5045d

Browse files
committed
make molecule not depends on precompose
1 parent 7506448 commit ff5045d

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

precompose-molecule/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ kotlin {
4040
val commonMain by getting {
4141
dependencies {
4242
compileOnly(compose.foundation)
43-
implementation(project(":precompose"))
4443
compileOnly(libs.molecule.runtime)
44+
compileOnly(libs.jetbrains.viewmodel)
4545
}
4646
}
4747
val commonTest by getting {

precompose-molecule/src/commonMain/kotlin/moe/tlaster/precompose/molecule/Molecule.kt

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import kotlinx.coroutines.channels.Channel
1818
import kotlinx.coroutines.flow.Flow
1919
import kotlinx.coroutines.flow.StateFlow
2020
import kotlinx.coroutines.flow.consumeAsFlow
21-
import moe.tlaster.precompose.reflect.canonicalName
2221
import kotlin.coroutines.CoroutineContext
2322

2423
internal expect fun providePlatformDispatcher(): CoroutineContext
@@ -51,31 +50,25 @@ private class ActionViewHolder<T> : ViewModel() {
5150

5251
@Composable
5352
private fun <E> rememberAction(
54-
keys: List<Any?>,
53+
key: String? = null,
5554
): Pair<Channel<E>, Flow<E>> {
56-
val key = remember(keys) {
57-
(keys.map { it.hashCode().toString() } + ActionViewHolder::class.canonicalName).joinToString()
58-
}
5955
return viewModel<ActionViewHolder<E>>(key = key).pair
6056
}
6157

6258
/**
6359
* Return StateFlow, use it in your Compose UI
6460
* The molecule scope will be managed by the [ViewModel], so it has the same lifecycle as the [ViewModel]
65-
* @param keys The keys to use to identify the Presenter
61+
* @param key The key to use to identify the Presenter
6662
* @param body The body of the molecule presenter
6763
* @return StateFlow
6864
*/
6965
@Composable
7066
private fun <T> rememberPresenterState(
71-
keys: List<Any?> = emptyList(),
67+
key: String? = null,
7268
useImmediateClock: Boolean,
7369
body: @Composable () -> T,
7470
): StateFlow<T> {
75-
val key = remember(keys) {
76-
(keys.map { it.hashCode().toString() } + PresenterHolder::class.canonicalName).joinToString()
77-
}
78-
val factory = remember {
71+
val factory = remember(key) {
7972
viewModelFactory {
8073
addInitializer(PresenterHolder::class) {
8174
PresenterHolder(useImmediateClock, body)
@@ -88,38 +81,38 @@ private fun <T> rememberPresenterState(
8881
/**
8982
* Return State, use it in your Compose UI
9083
* The molecule scope will be managed by the [ViewModel], so it has the same lifecycle as the [ViewModel]
91-
* @param keys The keys to use to identify the Presenter
84+
* @param key The key to use to identify the Presenter
9285
* @param useImmediateClock Use immediate clock or not, for text input, you should set it to true
9386
* @param body The body of the molecule presenter
9487
* @return State
9588
*/
9689
@Composable
9790
fun <T> producePresenter(
98-
keys: List<Any?> = emptyList(),
91+
key: String? = null,
9992
useImmediateClock: Boolean = false,
10093
body: @Composable () -> T,
10194
): State<T> {
102-
val presenter = rememberPresenterState(keys = keys, useImmediateClock = useImmediateClock) { body() }
95+
val presenter = rememberPresenterState(key = key, useImmediateClock = useImmediateClock) { body() }
10396
return presenter.collectAsState()
10497
}
10598

10699
/**
107100
* Return pair of State and Action Channel, use it in your Compose UI
108101
* The molecule scope and the Action Channel will be managed by the [ViewModel], so it has the same lifecycle as the [ViewModel]
109102
*
110-
* @param keys The keys to use to identify the Presenter
103+
* @param key The key to use to identify the Presenter
111104
* @param useImmediateClock Use immediate clock or not, for text input, you should set it to true
112105
* @param body The body of the molecule presenter, the flow parameter is the flow of the action channel
113106
* @return Pair of State and Action channel
114107
*/
115108
@Composable
116109
fun <T, E> rememberPresenter(
117-
keys: List<Any?> = emptyList(),
110+
key: String? = null,
118111
useImmediateClock: Boolean = false,
119112
body: @Composable (flow: Flow<E>) -> T,
120113
): Pair<T, Channel<E>> {
121-
val (channel, action) = rememberAction<E>(keys = keys)
122-
val presenter = rememberPresenterState(keys = keys, useImmediateClock = useImmediateClock) { body(action) }
114+
val (channel, action) = rememberAction<E>(key = key)
115+
val presenter = rememberPresenterState(key = key, useImmediateClock = useImmediateClock) { body(action) }
123116
val state by presenter.collectAsState()
124117
return state to channel
125118
}

0 commit comments

Comments
 (0)