pedroql / mvflow Goto Github PK
View Code? Open in Web Editor NEWSimple Android MVI architecture using Kotlin Flows
License: MIT License
Simple Android MVI architecture using Kotlin Flows
License: MIT License
any example to unit test counter
samples?
There are a couple ways I can see to fix this, neither of which is backwards compatible.
StateFlow<State>
as parameter instead of State
in Handlertypealias Handler<State, Action, Mutation> = (StateFlow<State>, Action) -> Flow<Mutation>
//Flow<Action>.collectIntoHandler changes to
handler.invoke(state, action, LoggingEffectSender(logger))
//Inside handler functions the current state can be accessed using
{ state, action ->
state.value
}
() -> State
as parameter instead of State
in handlertypealias Handler<State, Action, Mutation> = (() -> State, Action) -> Flow<Mutation>
//Flow<Action>.collectIntoHandler changes to
handler.invoke({state.value}, action, LoggingEffectSender(logger))
//Inside handler functions the current state can be accessed using
{ state, action ->
state()
}
Example: Notice that MyState(count=1) and MyState(count=2) are printed when it should print all the way up to count=6
data class MyState(val count: Int = 1)
sealed class MyAction {
object Start : MyAction()
}
sealed class MyMutation {
data class IncrementCount(val newCount: Int) : MyMutation()
}
@Test
fun testStaleState() {
runBlocking {
val handler: Handler<MyState, MyAction, MyMutation> = { state, action ->
when (action) {
MyAction.Start -> flow {
emit(MyMutation.IncrementCount(state.count + 1))
kotlinx.coroutines.delay(100)
emit(MyMutation.IncrementCount(state.count + 1))
kotlinx.coroutines.delay(100)
emit(MyMutation.IncrementCount(state.count + 1))
kotlinx.coroutines.delay(100)
emit(MyMutation.IncrementCount(state.count + 1))
kotlinx.coroutines.delay(100)
emit(MyMutation.IncrementCount(state.count + 1))
}
}
}
val reducer: Reducer<MyState, MyMutation> = { state, mutation ->
when(mutation){
is MyMutation.IncrementCount -> state.copy(count = mutation.newCount)
}
}
MVFlow(MyState(), handler, reducer, this).takeView(this, object : MVFlow.View<MyState, MyAction>{
override fun actions(): Flow<MyAction> {
return flowOf(MyAction.Start)
}
override fun render(state: MyState) {
println(state)
}
})
}
}
I have a serch that each time that you type a character it launches a request. The problem is that if I don't cancel I could have this race condition:
At the end the user sees the results of "mv" instead of "mvf".
If I can cancel the collection of the current old flow I would fix it. Maybe there is other way to avoid this problem. I tried out this one: badoo/MVICore#68 (comment) but Flow doesn't have a takeUntil
.
MVFlow uses a SharedFlow
to handle the Effects, then launchWhenX {}
in the View to collect the effects.
However this raises some good points about not using SharedFlow
and instead use a Channel(Channel.BUFFERED)
. Because some Effects can be lost due to configuration changes. And even using launchWhen(Started/Resumed) {}
is not immune to this, and he proposes a simple observer class to handle it.
the method MVFlow#takeView
is requires lifecycleScope
, but Jetpack Compose doesn't have one, any suggestion to implement MVFlow to use Jetpack Compose?
Ref:
based on an article here
tl;dr: https://dev.to/feresr/comment/1ddi3
does the ideas of MVI was at minimal was requires both reducer and handler, therefore the above discussion was indeed the actual drawback of MVI (because it's not a silver bullet)? cmiiw ๐
Can't get the sample app to compile or even Gradle sync, opened the sample_app folder in Android Studio Iguana and no matter what jdk I select I get:
BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 61
We're implementing our click events (e.g: buttonA
), but unable to (re)click the buttonA
unless we're resetting the events/click another events
The buttonA
is just a simple showing bottom dialog, we're providing an ID to its intent then fetch API and display the data retrieved from server
Is it possible to allow same flow emission for particular/specific events? or should we implement reset state instead manually?
This might be out of scope because the intention of this library is Minimalistic yet complete, but i'm curious is it possible to add time travel debug tools same like badoo/MVICore provides?
sealed class Error : Action {
object MaxChar : Error()
object NPE : Error()
object UPA : Error()
}
private val actionsChannel = Channel<Action>()
// somewhere in the code
// e.g: 1
viewBinding.editText.doAfterTextChanged {
if (it.length > 200) actionsChannel.offer(Action.Error.MaxChar)
}
// e.g: 2
try {
doSomething(null)
} catch (e: NullPointerException) {
actionsChannel.offer(Action.Error.NPE)
}
fun doSomething(nonNull: String) {
// do something
}
// e.g: 3
private var lateinit abc: String
// we forgot to initialized,
// therefore will throw UninitializedPropertyAccessException: lateinit property `abc`
try {
doSomething(abc)
} catch (e: UninitializedPropertyAccessException) {
actionsChannel.offer(Action.Error.UPA)
}
or is there any alternative to distinguish between throwing errors in our view (e.g: without an action), maybe using Effects
#30?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.