An implementation of the Bloc pattern for Kotlin and Jetpack Compose

Overview
Comments
  • Bloc state survival after configuration change

    Bloc state survival after configuration change

    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)?

    opened by sebaslogen 1
  • mapEventToState blocks future event handling

    mapEventToState blocks future event handling

    Issue Description

    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 added to a NumberListBloc in the following order, then the second event will never be handled:

    1. NumberListInitialized
    2. 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.

    Expected Behaviour

    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.

    bug help wanted 
    opened by ptrbrynt 0
  • BLoC freezes when receiving an event that does not emit a new state

    BLoC freezes when receiving an event that does not emit a new state

    Specs

    com.github.ptrbrynt.KotlinBloc:core 3.0.0 org.jetbrains.compose 1.1.0

    Introduction

    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.

    The issue

    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

    image

    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:

    image

    The coroutine

    image 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

    opened by PedroStrabeli 0
  • Jetbrains compose and KMM

    Jetbrains compose and KMM

    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.

    opened by jasin 0
  • BlocViewModel?

    BlocViewModel?

    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?

    opened by firefinchdev 4
Owner
Peter Bryant
Flutter developer.
Peter Bryant
Android App made by Jetpack Compose Components with Kotlin, MVVM Pattern, Multi Module, Navigation, Hilt, Coroutines, Retrofit and cached data by Room

Mobile Banking Android App made by Jetpack Compose Components with Kotlin, MVVM Pattern, Multi Module, Navigation, Hilt, Coroutines, Retrofit and cach

Yogi Dewansyah 13 Aug 31, 2022
A sample project in Kotlin to demonstrate Jetpack Compose, MVVM, Coroutines, Hilt, Room, Coil, Retrofit, Moshi, Leak Canary and Repository pattern

Jetpack-Compose-Boilerplate This repository contains a sample project in Kotlin to demonstrate Jetpack Compose, MVVM, Coroutines, Hilt, Room, Coil, Re

Areg Petrosyan 14 Dec 12, 2022
FullMangement - an application that helps you manage your tasks effectively. built with the latest tachs like Compose UI, Jetpack libraries, and MVVM design pattern.

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.

Amr algnyat 4 Nov 1, 2022
WPatternLock: Android jetpack compose pattern lock library

WPatternLock android jetpack compose pattern lock library easy to use customizab

amirhossin 9 Jul 8, 2022
WPatternLock: Android jetpack compose pattern lock library

WPatternLock android jetpack compose pattern lock library easy to use customizab

amirhossin 1 Jan 2, 2022
Luis David Orellana 3 Jun 20, 2022
Carol 12 Sep 25, 2022
Compose-Ratingbar-library - A simple implementation for rating bar in Jetpack Compose

Compose-Ratingbar-library - A simple implementation for rating bar in Jetpack Compose

Mahmoud Hussein 14 Dec 21, 2022
A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose

Authentication A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose Scree

Felix Kariuki 5 Dec 29, 2022
ComposeImageBlurhash is a Jetpack Compose component with the necessary implementation to display a blurred image while the real image is loaded from the internet. Use blurhash and coil to ensure good performance.

compose-image-blurhash ComposeImageBlurhash is a Jetpack Compose component with the necessary implementation to display a blurred image while the real

Orlando Novas Rodriguez 24 Nov 18, 2022
Luis David Orellana 11 Jan 1, 2023
A simple implementation of collapsing toolbar for Jetpack Compose

compose-collapsing-toolbar A simple implementation of CollapsingToolbarLayout for Jetpack Compose Installation You should add mavenCentral() repositor

onebone 321 Dec 31, 2022
Jetpack Compose implementation of Discord's Overlapping Panels

OverlappingPanelsCompose Jetpack Compose implementation of Discord's Overlapping Panels Installation Groovy Add JitPack repository to root build.gradl

Xinto 9 Jul 11, 2022
A jetpack compose form builder implementation.

Jetpack Compose FormBuilder A highly android library used to provide an abstraction layer over form elements as well as provide a DRY code implementat

DSC-JKUAT 116 Dec 22, 2022
A Jetpack Compose implementation of Owl, a Material Design study

Owl sample This sample is a Jetpack Compose implementation of Owl, a Material De

Alexander 1 Feb 26, 2022
Android Jetpack Compose implementation of SpinKit with additionals

ComposeLoading Android Jetpack Compose implementation of SpinKit with additionals. How it looks Preview Setup Open the file settings.gradle (it looks

Emir Demirli 8 Dec 18, 2022
Compose-navigation - Set of utils to help with integrating Jetpack Compose and Jetpack's Navigation

Jetpack Compose Navigation Set of utils to help with integrating Jetpack Compose

Adam Kobus 5 Apr 5, 2022
Jetpack Compose Boids | Flocking Insect 🐜. bird or Fish simulation using Jetpack Compose Desktop 🚀, using Canvas API 🎨

?? ?? ?? Compose flocking Ants(boids) ?? ?? ?? Jetpack compose Boids | Flocking Insect. bird or Fish simulation using Jetpack Compose Desktop ?? , usi

Chetan Gupta 38 Sep 25, 2022
A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Why Not Compose! A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Md. Mahmudul Hasan Shohag 186 Jan 1, 2023