feat: 添加 compose-mvi 模块

This commit is contained in:
2025-10-02 16:55:08 +08:00
parent 5a605f1d12
commit cd679f0a3c
10 changed files with 184 additions and 0 deletions

1
compose-mvi/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,49 @@
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
}
android {
namespace = "moe.tabidachi.compose.mvi"
compileSdk = 36
defaultConfig {
minSdk = 28
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
explicitApi = ExplicitApiMode.Strict
}
dependencies {
api(libs.lifecycle.runtime.compose)
api(libs.lifecycle.viewmodel)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.espresso.core)
}

View File

21
compose-mvi/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,22 @@
package moe.tabidachi.compose.mvi
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("moe.tabidachi.compose.mvi.test", appContext.packageName)
}
}

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>

View File

@@ -0,0 +1,41 @@
package moe.tabidachi.compose.mvi
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
public abstract class BaseViewModel<STATE, EVENT, EFFECT>(
initialState: STATE
) : ViewModel(), UnidirectionalViewModel<STATE, EVENT, EFFECT> {
private val _state: MutableStateFlow<STATE> = MutableStateFlow(initialState)
override val state: StateFlow<STATE> = _state.asStateFlow()
private val _effect: MutableSharedFlow<EFFECT> = MutableSharedFlow()
override val effect: SharedFlow<EFFECT> = _effect.asSharedFlow()
private val handledOneTimeEvents: MutableSet<EVENT> = mutableSetOf()
protected fun updateState(block: (STATE) -> STATE) {
_state.update(block)
}
protected fun emitEffect(effect: EFFECT) {
viewModelScope.launch {
_effect.emit(effect)
}
}
protected fun handleOneTimeEvent(event: EVENT, block: () -> Unit) {
if (event !in handledOneTimeEvents) {
handledOneTimeEvents.add(event)
block()
}
}
}

View File

@@ -0,0 +1,29 @@
package moe.tabidachi.compose.mvi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
public interface UnidirectionalViewModel<STATE, EVENT, EFFECT> {
public val state: StateFlow<STATE>
public val effect: SharedFlow<EFFECT>
public fun event(event: EVENT)
@Composable
public operator fun component1(): State<STATE> = state.collectAsStateWithLifecycle()
public operator fun component2(): (EVENT) -> Unit = ::event
}
@Composable
public inline fun <reified STATE, EVENT, EFFECT> UnidirectionalViewModel<STATE, EVENT, EFFECT>.observe(
crossinline handleEffect: (EFFECT) -> Unit
): UnidirectionalViewModel<STATE, EVENT, EFFECT> = apply {
LaunchedEffect(key1 = effect) {
effect.collect {
handleEffect(it)
}
}
}

View File

@@ -0,0 +1,16 @@
package moe.tabidachi.compose.mvi
import org.junit.Assert.*
import org.junit.Test
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}

View File

@@ -17,3 +17,4 @@ dependencyResolutionManagement {
}
rootProject.name = "Electro"
include(":app")
include(":compose-mvi")