Kotlin Bloc
An implementation of the Bloc pattern for Kotlin and Jetpack Compose.
Documentation
Documentation is available here: https://ptrbrynt.github.io/KotlinBloc
An implementation of the Bloc pattern for Kotlin and Jetpack Compose.
Documentation is available here: https://ptrbrynt.github.io/KotlinBloc
Will the current state of a Bloc survive orientation changes?
I assume the examples with remember
only survive recomposition but not configuration change (nor state restoration from process death)
Could you please provide an example of how to implement a Bloc for which the state (e.g. Loading) survives configuration changes (e.g. screen rotation)?
Consider the following Bloc implementation:
sealed class NumberListEvent
object NumberListInitialized: NumberListEvent()
data class NumberListNumberAdded(val number: Int): NumberListEvent()
class NumberListBloc: Bloc<NumberListEvent, List<Int>>(emptyList()) {
private val numbers = MutableStateFlow(emptyList<Int>())
override fun mapEventToState(event: NumberListEvent): Flow<List<Int>> = flow {
when (event) {
is NumberListInitialized → {
emitAll(numbers)
}
is NumberListNumberAdded → {
numbers.emit(numbers.value + event.number)
}
}
}
}
If events are add
ed to a NumberListBloc
in the following order, then the second event will never be handled:
NumberListInitialized
NumberListNumberAdded(10)
I can only assume this is because emitAll
blocks the mapEventToState
method indefinitely, since it's emitting a Flow
which doesn't terminate.
The above code should allow the current value of numbers
to always be emitted as the state of NumberListBloc
, whilst not blocking subsequent events from being handled.
com.github.ptrbrynt.KotlinBloc:core 3.0.0 org.jetbrains.compose 1.1.0
Hi. I'm using BLoC in my Kotlin-Compose (desktop) to manage the connection status of a peripheral device in my application.
Each screen checks the device's connection status, kept by the BLoC and requests connection if the BLoC is not in Connected
state.
Since the connection is asynchronous, I have a situation where a connection event comes through and at the same time that the Connected state change occurs. In this case, the connection event received by the BLoC should be ignored.
That being said, the event handling of my BLoC is:
init {
on<HdmEvent> {event ->
when(event) {
is HdmEvent.Connect -> {
when (state) {
is HdmState.Disconnected -> {
emit(HdmState.Connecting())
}
is HdmState.Disconnecting -> {
emit(HdmState.Connecting())
}
else -> { }
}
}
...
}
If I receive a Connect event
and it's in Connected state
, the event should be ignored falling into the else clause, but that causes the BLoC to stop responding to any future events.
I believe if an event is received but doesn't emit a state, we reach an inconsistent state where the on
function is not triggered anymore.
The "solution" I found here was to add:
else -> {
emit(state)
}
Thinking of a state machine, it kinda makes sense, because every transition (event) causes a state change, and it's an event that circulates to the state it was like in the image below
but I don't think this should trigger a state emission, because this makes the listeners of the event flow act on the state change twice, and now I have to manage the current state and the new state of the BLoC, just to avoid this issue... That should not be the listener's responsibility, since the state transition is spurious, to avoid the freezing of the BLoC.
To illustrate the freeze, I created a coroutine that sends Connect
events each 200ms AFTER it's connected, and the else clause above in the BLoC is empty:
The coroutine
The states and events.
The first 2 events are received, and then, not anymore. It looks to me like erratic behaviour that should not happen.
Can you assist me in finding a better solution? Thanks
I figured I could use this with Jetbrains compose, but when trying to add it to my gradle build file I am getting a
No matching variant error
on the compose library. Is there no way we can get this to work? I plan on developing the desktop app first and then move to android and iOS later.
I really liked your implementation of Bloc in kotlin.
ViewModel is finely integrated with Activities (retention on activity recreate, etc) and other Android stuff in general. Also ViewModel is responsible for holding the ui state, and reacting to ui actions (just like Bloc).
So, a BlocViewModel
based on AndroidViewModel
/ViewModel
would be great and really useful.
BlocViewModel
would be a AndroidViewModel
/ViewModel
with all capabilities of Bloc
.
Your thoughts?
Mobile Banking Android App made by Jetpack Compose Components with Kotlin, MVVM Pattern, Multi Module, Navigation, Hilt, Coroutines, Retrofit and cach
Jetpack-Compose-Boilerplate This repository contains a sample project in Kotlin to demonstrate Jetpack Compose, MVVM, Coroutines, Hilt, Room, Coil, Re
Full Management is an application that helps you manage your tasks effectively. built with the latest tachs like Compose UI, Jetpack libraries and MVVM design pattern.
WPatternLock android jetpack compose pattern lock library easy to use customizab
WPatternLock android jetpack compose pattern lock library easy to use customizab
Lista - Lenguajes de programación - Jetpack Compose Screenshots ?? Referencias ?
Git App An Android application consuming the GitHub API to search for users on Github, display their followers, following and repositories. The projec
Compose-Ratingbar-library - A simple implementation for rating bar in Jetpack Compose
Authentication A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose Scree
compose-image-blurhash ComposeImageBlurhash is a Jetpack Compose component with the necessary implementation to display a blurred image while the real
Simple Login/Auth + Jetpack Compose + Retrofit Demo ?? Json Object Request ?? Re
compose-collapsing-toolbar A simple implementation of CollapsingToolbarLayout for Jetpack Compose Installation You should add mavenCentral() repositor
OverlappingPanelsCompose Jetpack Compose implementation of Discord's Overlapping Panels Installation Groovy Add JitPack repository to root build.gradl
Jetpack Compose FormBuilder A highly android library used to provide an abstraction layer over form elements as well as provide a DRY code implementat
Owl sample This sample is a Jetpack Compose implementation of Owl, a Material De
ComposeLoading Android Jetpack Compose implementation of SpinKit with additionals. How it looks Preview Setup Open the file settings.gradle (it looks
Jetpack Compose Navigation Set of utils to help with integrating Jetpack Compose
?? ?? ?? Compose flocking Ants(boids) ?? ?? ?? Jetpack compose Boids | Flocking Insect. bird or Fish simulation using Jetpack Compose Desktop ?? , usi
Why Not Compose! A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!