molecule-util

Extension library enabling Compose-style presenters with Flow or State actions, event channels, nested presenters, ViewModel-managed lifecycles, and resolution of native integration conflicts.

Android JVMJVMKotlin/NativeWasm
GitHub stars0
Authorsaleyn97
Open issues0
Creation date4 months ago

Last activity4 months ago
Latest release1.0.0 (4 months ago)

Molecule

Molecule is a library from cashapp, which can write business logic in Compose, and it's also Kotlin Multiplatform project. For more information: https://github.com/cashapp/molecule

https://github.com/cashapp/molecule

Molecule Util

Maven Central

implementation("io.github.aleyn97:molecule-util:x.y.z")

MoleculeUtil is an extension library for Molecule. It has the same functionality as PreCompose, but integrating the org.jetbrains.kotlin.native.cocoapods plugin with the IOS platform may cause conflicts. MoleculeUtil handles conflict issues

Usage

Flow Action

You can write a Presenter like this:

@Composable
fun CounterPresenter(
    action: Flow<CounterAction>,
): CounterState {
    var count by remember { mutableStateOf(0) }

    action.collectAction {
        when (this) {
            CounterAction.Increment -> count++
            CounterAction.Decrement -> count--
        }
    }

    return CounterState("Clicked $count times")
}

in your Compose UI, you can use this CounterPresenter with rememberPresenter

val (state, channel) = rememberPresenter { CounterPresenter(it) }

state is the instance of CounterState, which return by CounterPresenter, and channel is the instance of Channel<CounterEvent>, you can send event to CounterPresenter by channel.trySend(CounterEvent.Increment)

The molecule scope and the Event Channel will be managed by the ViewModel, so it has the same lifecycle as the ViewModel.

You can nest your Presenter by using rememberNestedPresenter

State Action

If you prefer using State Action, you can write a Presenter like this:

@Composable
fun CounterPresenter(): CounterState {
    var count by remember { mutableStateOf(0) }
    return CounterState("Clicked $count times") {
        when (it) {
            CounterAction.Increment -> count++
            CounterAction.Decrement -> count--
        }
    }
}

in your Compose UI, you can use this CounterPresenter with producePresenter

val state by producePresenter { CounterPresenter() }
Android JVMJVMKotlin/NativeWasm
GitHub stars0
Authorsaleyn97
Open issues0
Creation date4 months ago

Last activity4 months ago
Latest release1.0.0 (4 months ago)

Molecule

Molecule is a library from cashapp, which can write business logic in Compose, and it's also Kotlin Multiplatform project. For more information: https://github.com/cashapp/molecule

https://github.com/cashapp/molecule

Molecule Util

Maven Central

implementation("io.github.aleyn97:molecule-util:x.y.z")

MoleculeUtil is an extension library for Molecule. It has the same functionality as PreCompose, but integrating the org.jetbrains.kotlin.native.cocoapods plugin with the IOS platform may cause conflicts. MoleculeUtil handles conflict issues

Usage

Flow Action

You can write a Presenter like this:

@Composable
fun CounterPresenter(
    action: Flow<CounterAction>,
): CounterState {
    var count by remember { mutableStateOf(0) }

    action.collectAction {
        when (this) {
            CounterAction.Increment -> count++
            CounterAction.Decrement -> count--
        }
    }

    return CounterState("Clicked $count times")
}

in your Compose UI, you can use this CounterPresenter with rememberPresenter

val (state, channel) = rememberPresenter { CounterPresenter(it) }

state is the instance of CounterState, which return by CounterPresenter, and channel is the instance of Channel<CounterEvent>, you can send event to CounterPresenter by channel.trySend(CounterEvent.Increment)

The molecule scope and the Event Channel will be managed by the ViewModel, so it has the same lifecycle as the ViewModel.

You can nest your Presenter by using rememberNestedPresenter

State Action

If you prefer using State Action, you can write a Presenter like this:

@Composable
fun CounterPresenter(): CounterState {
    var count by remember { mutableStateOf(0) }
    return CounterState("Clicked $count times") {
        when (it) {
            CounterAction.Increment -> count++
            CounterAction.Decrement -> count--
        }
    }
}

in your Compose UI, you can use this CounterPresenter with producePresenter

val state by producePresenter { CounterPresenter() }