简明易用框架,解决 MVI 实战痛点

Overview

 

🌏 English README

研发故事:《解决 MVI 架构实战痛点》

 

上期《Jetpack 架构组件设计拆解及改善建议》侧重拆解 “领域层” 设计误区,并给出改善建议 —— 通过 MVI-Dispatcher 承担 Event-Handler,

然有小伙伴表示,不仅要 MVI-Dispatcher,还想看看 Kotlin 版 MVI 实践

故这期,我们肝个 MVI-Dispatcher-KTX 示例项目,相信查阅后你会耳目一新。

 

收藏或置顶 顺滑转场 删除笔记

 

项目简介

“单向数据流” 是近年公认 “图形化客户端开发” 领域推荐方式,

MVI-Dispatcher 抹除 “单向数据流” 学习成本,使开发者在不熟 mutable、MVI 情况下,仅根据简明易懂 input-output 设计亦可自动实现 “单向数据流” 开发,

KTX 版接口与 MVI-Dispatcher 保持一致,可彻底消除 mutable 样板代码;杜绝 setValue/emit 误用滥用;且可无缝整至 Jetpack MVVM 项目中。

 

implementation 'com.kunminx.arch:mvi-dispatch-ktx:7.0.2-beta'

 

 

What‘s More

本项目由 100% Java MVI-Dispatcher 项目改造而来,

如您正在学习 Kotlin,通过横向对比 MVI-Dispatcher 项目,可快速了解 Android Studio 一键转换后,为因地制宜遵循 Kotlin 特性/风格/思维,我们还可手动完成哪些调整修缮。

区别于避重就轻实验性示例,MVI-Dispatcher 及 MVI-Dispatcher-KTX 提供完成一款记事本软件最少必要源码实现。

故通过该示例您还可获得内容包括:

1.整洁代码风格 & 标准命名规范

2.对 “视图控制器” 知识点深入理解 & 正确使用

3.AndroidX 和 Material Design 全面使用

4.ConstraintLayout 约束布局使用

5.十六进制复合状态管理最佳实践

6.优秀用户体验 & 交互设计

 

Thanks to

感谢小伙伴苏旗的测试反馈和协助排查

AndroidX

Jetpack

SwipeDelMenuLayout

项目中图标素材来自 iconfinder 提供 免费授权图片

 

Copyright

本项目场景案例及 MVI-Dispatcher-KTX 框架,均属本人独立原创设计,本人对此享有最终解释权。

任何个人或组织,未经与作者本人当面沟通许可,不得将本项目代码设计及本人对 “唯一可信源” 及 MVI 独家理解用于 "打包贩卖、出书、卖课" 等商业用途。

如需引用借鉴 “本项目框架设计背景及思路” 写作发行,请注明链接出处

 

License

Copyright 2019-present KunMinX

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
A simple MVI framework for Kotlin Multiplatform and Android
A simple MVI framework for Kotlin Multiplatform and Android

Orbit Multiplatform Get in touch What is Orbit Orbit is a Redux/MVI-like library - but without the baggage. It's so simple we think of it as MVVM+. Si

Architecture Study with MVVM and MVI

Android-Architecture-Sample Android Architecture Study with MVVM and MVI Using DI(Hilt) + Retrofit + Coroutine + Moshi + AAC Stack 100% Kotlin + Corou

Kata to practice Clean Architecture & MVI by building TodoList Application

Mvi Todo Kata Application Features TODO specification Title Description Due Date Priority Supported Operations Create a new TODO Remove a TODO Select

An example of a test task for creating a simple currency converter application for the Android platform. The app is developed using Kotlin, MVI, Dagger Hilt, Retrofit, Jetpack Compose.
An example of a test task for creating a simple currency converter application for the Android platform. The app is developed using Kotlin, MVI, Dagger Hilt, Retrofit, Jetpack Compose.

Simple Currency Converter Simple Currency Converter Android App by Isaev Semyon An example of a test task for creating a simple currency converter app

MVI Architecture Android Beginners: Sample App

MVI Architecture Android Beginners: Sample App This repository contains a sample app that implements MVI architecture using Kotlin, ViewModel, LiveDat

Android Bitcoin market app base on Jetpack Compose and MVI. The app displays current bitcoin market price and history price k-line charts.

compose-bitcoin Android Bitcoin market app base on Jetpack Compose and MVVM & MVI. Features Current bitcoin market price. K-line charts of history pri

MVI Architecture Android Beginners: Sample App
MVI Architecture Android Beginners: Sample App

MVI Architecture Android Beginners: Sample App This repository contains a sample app that implements MVI architecture using Kotlin, ViewModel, LiveDat

This project shows trending github repositories using MVI (Model View Intent) using kotlin flows and multi module clean architecture

GithubTrendingMVIFlow Build Architecture: This project shows trending github repositories using MVI (Model View Intent) using kotlin flows and multi m

MVIExample - A sample app showing how to build an app using the MVI architecture pattern
MVIExample - A sample app showing how to build an app using the MVI architecture pattern

MVIExample A sample app showing how to build an app using the MVI architecture p

Android Sample Kotlin+ MVI + Jetpack compose + Coroutines + Retrofit + Hilt  + Room + Navigation component
Android Sample Kotlin+ MVI + Jetpack compose + Coroutines + Retrofit + Hilt + Room + Navigation component

MVIComposeSample Android Sample app to show user latest movies implementing MVI + Clean Architecture using kotlin & Jetpack compose following solid an

Searchbook - SearchBooks is an app built with Jetpack Compose and architecture of MVVM + MVI
Searchbook - SearchBooks is an app built with Jetpack Compose and architecture of MVVM + MVI

SearchBooks SearchBooks is an app built with Jetpack Compose and architecture of

A simple, classic Kotlin MVI implementation based on coroutines with Android support, clean DSL and easy to understand logic

A simple, classic Kotlin MVI implementation based on coroutines with Android support, clean DSL and easy to understand logic

PokeCard Compose is a demo app 100% write in Compose, Flow and Koin based on MVI Clean Architecture 🐱⚡️
PokeCard Compose is a demo app 100% write in Compose, Flow and Koin based on MVI Clean Architecture 🐱⚡️

A Pokemon Card demo app using Jetpack Compose and Koin based on MVI architecture. Fetching data from the network with Ktor and integrating persisted data in Room database with usecase/repository pattern.

Clean Android multi-module offline-first scalable app in 2022. Including Jetpack Compose, MVI, Kotlin coroutines/Flow, Kotlin serialization, Hilt and Room.

Android Kotlin starter project - 2022 edition Android starter project, described precisely in this article. Purpose To show good practices using Kotli

Architecture With MVI using Kotlin, Coroutines, Retrofit and Unit test
Architecture With MVI using Kotlin, Coroutines, Retrofit and Unit test

Architecture With MVI using Kotlin, Coroutines, Retrofit and Unit test MVI (Model-View-Intent) streamlines the process of creating and developing appl

Clean Architecture Modular Project: MVI + Jetpack Compose + Coroutines + Flows + Hilt + UnitTests
Clean Architecture Modular Project: MVI + Jetpack Compose + Coroutines + Flows + Hilt + UnitTests

NyTimes for Clean Architecture The purpose of this project is to consolidate some of the learned insights throughout the years about the Clean Archite

Kotlin multi platform project template and sample app with everything shared except the UI. Built with clean architecture + MVI
Kotlin multi platform project template and sample app with everything shared except the UI. Built with clean architecture + MVI

KMMNewsAPP There are two branches Main News App Main The main branch is a complete template that you can clone and use to build the awesome app that y

Comments
  • 集成Paging3时,paging3 不生效,无法进行网络请求操作

    集成Paging3时,paging3 不生效,无法进行网络请求操作

    在项目中集成 Paging 3,但用在这个框架里发现并不能触发请求操作,参考代码如下:

    // interface apiService
    @GET("/xxx/xxx")
    fun getList(@Query("page") page: Int,@Query("num") num: Int): Call<List<TestBean>>
    
    // PagingSource
    class TestPagingSource(private val apiService: ApiService) : PagingSource<Int, TestBean>() {
        override fun getRefreshKey(state: PagingState<Int, FaultLogBean>): Int? = null
    
        override suspend fun load(params: LoadParams<Int>): LoadResult<Int, TestBean> {
            return try {
                LogUtils.e("paging load")  // 2. 并没有走这里的方法
                val page = params.key ?: 1
                val pageSize = params.loadSize
                val testList= apiService.getList(page, pageSize).await()
                val prevKey = if (page > 1) page - 1 else null
                val nextKey = if (testList.isNotEmpty()) page + 1 else null
                LoadResult.Page(testList, prevKey, nextKey)
            } catch (e: Exception) {
                LoadResult.Error(e)
            }
        }
    }
    
    // TestRepository
    val pageSize = 10
    fun getTestList(): Flow<PagingData<TestBean>> {
        return Pager(
            config = PagingConfig(pageSize),
            pagingSourceFactory = { TestPagingSource(apiService) }
        ).flow
    }
    
    // intent or event
    sealed class TestEvent {
        data class GetTestList(val pagingData: Flow<PagingData<TestBean>>? = null) : TestEvent()
    }
    
    //  dispatcher 
    class TestRequester : MviDispatcherKTX<TestEvent>() {
        ...
        is TestEvent.GetTestList -> sendResult(event.copy(testRepo.getTestList()))
    }
    
    // ui
    class TestFragment {
        override fun onInput() {
            testRequest.input(TestEvent.GetTestList()) // 1. 触发请求
        }
    
        override fun onOutput() {
            // 3. 这里收不到消息
            testRequest.output(this) { testEvent ->
                // ...
            }
        }
    }
    

    如果用传统的 ViewModel 测试是没问题,可以触发,参考代码如下:

    // viewModel
    @HiltViewModel
    class TestViewModel @Inject constructor(
        private val testRepo: TestRepository,
    ) : ViewModel() {
    
        fun getTestList(): Flow<PagingData<TestBean>> {
            return testRepo.getTestList().cachedIn(viewModelScope)
        }
    }
    
    // ui
    class TestFragment {
        override fun onInitView() {
            lifecycleScope.launch {
                testViewModel.getTestList().collect { adapter.submitData(it) }
            } // 这边是ok的
        }
    }
    
    opened by KXwonderful 3
  • 【 提问须知 】

    【 提问须知 】

    如有 bug,请另外 new 一个 issue ⚠️⚠️⚠️

    本项目开 issue 规范:

    1. 有任何 bug 都欢迎及时开 issue,我看到后则予以处理。
    2. 如有使用疑问,请先认真阅读 Readme 和源码 samples,在没有找到答案后,另外开 issue。
    3. 如开 issue 是为发表个人见解,请务必 客观、具体、严谨;严禁草率、乱入、带节奏:

    务必注明观点所对应场景,并附上完整可复现代码,

    不然很难构成有效交流。

    任何缺乏实证依据和因果逻辑的泛泛而谈,都可能对其他使用者造成困扰。

    在发表个人见解前,请先确保已认真阅读过源码。这是对自己、对作者、对其他读者最起码尊重。

    提问须知 
    opened by KunMinX 0
Releases(7.0.2-beta)
Owner
KunMinX
大物始于小 - Big Things Have Small Beginnings.
KunMinX
Clean Android multi-module offline-first scalable app in 2022. Including Jetpack Compose, MVI, Kotlin coroutines/Flow, Kotlin serialization, Hilt and Room.

Android Kotlin starter project - 2022 edition Android starter project, described precisely in this article. Purpose To show good practices using Kotli

Krzysztof Dąbrowski 176 Jan 3, 2023
Kotlin multi platform project template and sample app with everything shared except the UI. Built with clean architecture + MVI

KMMNewsAPP There are two branches Main News App Main The main branch is a complete template that you can clone and use to build the awesome app that y

Kashif Mehmood 188 Dec 30, 2022
An MVI project setup using Jetpack compose. This would be a good place to start learning Model View Intent (MVI) architecture for Android.

Compose-MVI An MVI project setup using Jetpack compose. This would be a good place to start learning Model View Intent (MVI) architecture for Android.

null 6 Jul 28, 2022
A sample app showing how to build an app using the MVI architecture pattern.

MVI Example This application was streamed live on Twitch to demonstrate how to build an application using MVI. You can find the VOD here for now: http

Adam McNeilly 46 Jan 2, 2023
Android playground project with modularization by feature (android libraries), unit tests, MVVM & MVI.

Movies Movies is a simple project to study and play with some android components, architecture and tools for Android development. Tech Stack This proj

Christopher Elias 333 Dec 30, 2022
Android sample app following best practices: Kotlin, Compose, Coroutines and Flow, Hilt, JetPack Navigation, ViewModel, MVVM and MVI, Retrofit, Coil

Foodies - Modern Android Architecture Foodies is a sample project that presents a modern 2021 approach to Android app development. The project tries t

null 362 Jan 2, 2023
Extendable MVI framework for Kotlin Multiplatform with powerful debugging tools (logging and time travel), inspired by Badoo MVICore library

Should you have any questions or ideas please welcome to the Slack channel: #mvikotlin Inspiration This project is inspired by Badoo MVICore library.

Arkadii Ivanov 460 Dec 31, 2022
Multi-module, Kotlin, MVI, Compose, Hilt, Navigation Component, Use-cases, Room, Retrofit

Work in progress Multi-module demo app that gets data from dota2 api. API https://docs.opendota.com/ Players by rank (GET) https://api.opendota.com/ap

Mitch Tabian 321 Dec 27, 2022
Wallpaper app made using Hilt, Retrofit, Room, Navigation Components, MVI, Coroutines, Flows, ViewModel, LiveData, Datastore Preference.

Android Picture Engine Wallpaper app made using Hilt, Retrofit, Room, Navigation Components, MVI, Coroutines, Flows, ViewModel, LiveData, Datastore Pr

Simone Conigliaro 59 Sep 27, 2022
A sample application demonstrating the differences between MVVM and MVI.

MVI Case Study This is a sample application to demonstrate the differences between MVVM and MVI architecture. The sample is split into three helpful m

Adam McNeilly 37 Nov 19, 2022