forked from slackhq/circuit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
521d989
commit b23b7ef
Showing
12 changed files
with
152 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (C) 2022 Slack Technologies, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
plugins { | ||
kotlin("multiplatform") | ||
alias(libs.plugins.compose) | ||
} | ||
|
||
kotlin { | ||
// region KMP Targets | ||
ios() | ||
iosSimulatorArm64() | ||
// endregion | ||
|
||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
api(projects.circuitRuntimePresenter) | ||
api(libs.kmmviewmodel.core) | ||
implementation(libs.molecule.runtime) | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
circuit-swiftui/src/commonMain/kotlin/com/slack/circuit/swiftui/SwiftUIPresenter.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,26 @@ | ||
package com.slack.circuit.swiftui | ||
|
||
import androidx.compose.runtime.Composable | ||
import app.cash.molecule.RecompositionClock | ||
import com.rickclephas.kmm.viewmodel.KMMViewModel | ||
import com.slack.circuit.runtime.CircuitUiState | ||
import com.slack.circuit.runtime.Navigator | ||
import com.slack.circuit.runtime.presenter.Presenter | ||
import com.slack.circuit.runtime.presenter.presenterOf | ||
|
||
public class SwiftUIPresenter<UiState: CircuitUiState> internal constructor( | ||
private val presenter: Presenter<UiState> | ||
): KMMViewModel() { | ||
|
||
private val stateFlow = viewModelScope.launchMolecule(RecompositionClock.Immediate) { | ||
presenter.present() | ||
} | ||
|
||
public val state: UiState get() = stateFlow.value | ||
} | ||
|
||
public fun <UiState : CircuitUiState> swiftUIPresenterOf( | ||
body: @Composable (Navigator) -> UiState | ||
): SwiftUIPresenter<UiState> { | ||
return SwiftUIPresenter(presenterOf { body(Navigator.NoOp) }) | ||
} |
34 changes: 34 additions & 0 deletions
34
circuit-swiftui/src/commonMain/kotlin/com/slack/circuit/swiftui/molecule.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,34 @@ | ||
package com.slack.circuit.swiftui | ||
|
||
import androidx.compose.runtime.Composable | ||
import app.cash.molecule.RecompositionClock | ||
import app.cash.molecule.launchMolecule | ||
import com.rickclephas.kmm.viewmodel.MutableStateFlow | ||
import com.rickclephas.kmm.viewmodel.ViewModelScope | ||
import com.rickclephas.kmm.viewmodel.coroutineScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
/** | ||
* Identical to the molecule implementation, but with a KMM-ViewModel [MutableStateFlow]. | ||
* https://github.com/cashapp/molecule/blob/c902f7f60022911bf0cc6940cf86f3ff07c76591/molecule-runtime/src/commonMain/kotlin/app/cash/molecule/molecule.kt#L102 | ||
*/ | ||
internal fun <T> ViewModelScope.launchMolecule( | ||
clock: RecompositionClock, | ||
body: @Composable () -> T, | ||
): StateFlow<T> { | ||
var flow: MutableStateFlow<T>? = null | ||
coroutineScope.launchMolecule( | ||
clock = clock, | ||
emitter = { value -> | ||
val outputFlow = flow | ||
if (outputFlow != null) { | ||
outputFlow.value = value | ||
} else { | ||
flow = MutableStateFlow(this, value) | ||
} | ||
}, | ||
body = body, | ||
) | ||
return flow!! | ||
} |
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
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,19 @@ | ||
// | ||
// KMMViewModel.swift | ||
// Counter | ||
// | ||
// Created by Rick Clephas on 26/05/2023. | ||
// Copyright © 2023 orgName. All rights reserved. | ||
// | ||
|
||
import KMMViewModelCore | ||
import KMMViewModelState | ||
import counter | ||
|
||
extension Kmm_viewmodel_coreKMMViewModel: KMMViewModel { } | ||
|
||
extension ObservedViewModelState { | ||
init(wrappedValue: ViewModel) where ViewModel: Circuit_swiftuiSwiftUIPresenter<State> { | ||
self.init(wrappedValue: wrappedValue, \.state) | ||
} | ||
} |
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
39 changes: 0 additions & 39 deletions
39
samples/counter/src/commonMain/kotlin/com/slack/circuit/sample/counter/SwiftSupport.kt
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
samples/counter/src/iosMain/kotlin/com.slack.circuit.sample.counter/PresenterFactory.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,8 @@ | ||
package com.slack.circuit.sample.counter | ||
|
||
import com.slack.circuit.swiftui.swiftUIPresenterOf | ||
|
||
object PresenterFactory { | ||
fun counterPresenter() = swiftUIPresenterOf { CounterPresenter(it) } | ||
fun primePresenter(number: Int) = swiftUIPresenterOf { PrimePresenter(it, number) } | ||
} |
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