Android MVI 框架搭建
时间: 2023-11-22 07:48:03 浏览: 170
Android MVI示例,升级版(注: 该版本只提供'lib-base'使用方式和Android MVI示例类似)
为了搭建Android MVI框架,可以按照以下步骤进行:
1. 首先,需要在项目的build.gradle文件中添加以下依赖项:
```gradle
dependencies {
implementation 'io.reactivex.rxjava2:rxjava:2.2.19'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.2.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.2.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-design:2.2.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-recyclerview-v7:2.2.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-swiperefreshlayout:2.2.0'
implementation 'com.jakewharton.rxrelay2:rxrelay:2.1.0'
implementation 'com.google.dagger:dagger:2.27'
kapt 'com.google.dagger:dagger-compiler:2.27'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
}
```
2. 接下来,需要创建一个基础的MVI架构,包括以下几个部分:
- Model:数据模型,用于存储应用程序的数据。
- View:用户界面,用于显示数据和接收用户输入。
- Intent:用户意图,用于描述用户的操作。
3. 在MVI架构中,还需要使用以下几个类:
- Action:表示用户的操作,例如点击按钮或滑动屏幕。
- Result:表示操作的结果,例如从服务器获取的数据。
- ViewState:表示View的状态,例如显示数据或显示加载中的进度条。
4. 接下来,需要创建一个基础的MVI架构类,例如:
```kotlin
abstract class MviViewModel<I : MviIntent, S : MviViewState, A : MviAction, R : MviResult> : ViewModel() {
private val disposables = CompositeDisposable()
abstract fun bindIntents()
abstract fun actionFromIntent(intent: I): A
abstract fun reducer(previousState: S, result: R): S
abstract fun initialState(): S
private val intentSubject: PublishSubject<I> = PublishSubject.create()
private val stateObservable: Observable<S> by lazy {
intentSubject
.map { actionFromIntent(it) }
.compose(actionProcessor)
.scan(initialState(), ::reducer)
.distinctUntilChanged()
.replay(1)
.autoConnect(0)
}
private val actionProcessor = ObservableTransformer<A, R> { actions ->
actions.flatMap { action ->
handleAction(action)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError { throwable ->
Log.e("MviViewModel", "Error handling action: $action", throwable)
}
}
}
private fun handleAction(action: A): Observable<R> {
return Observable.empty()
}
fun processIntents(intents: Observable<I>) {
intents.subscribe(intentSubject)
.let(disposables::add)
}
fun states(): Observable<S> = stateObservable
override fun onCleared() {
super.onCleared()
disposables.dispose()
}
}
```
5. 最后,需要创建一个具体的ViewModel类,例如:
```kotlin
class MyViewModel : MviViewModel<MyIntent, MyViewState, MyAction, MyResult>() {
override fun bindIntents() {
// TODO: Implement binding of intents to actions
}
override fun actionFromIntent(intent: MyIntent): MyAction {
// TODO: Implement conversion of intent to action
}
override fun reducer(previousState: MyViewState, result: MyResult): MyViewState {
// TODO: Implement state reducer
}
override fun initialState(): MyViewState {
// TODO: Implement initial state
}
}
```
阅读全文