Book Image

Clean Android Architecture

By : Alexandru Dumbravan
Book Image

Clean Android Architecture

By: Alexandru Dumbravan

Overview of this book

As an application’s code base increases, it becomes harder for developers to maintain existing features and introduce new ones. In this clean architecture book, you'll learn to identify when and how this problem emerges and how to structure your code to overcome it. The book starts by explaining clean architecture principles and Android architecture components and then explores the tools, frameworks, and libraries involved. You’ll learn how to structure your application in the data and domain layers, the technologies that go in each layer, and the role that each layer plays in keeping your application clean. You’ll understand how to arrange the code into these two layers and the components involved in assembling them. Finally, you'll cover the presentation layer and the patterns that can be applied to have a decoupled and testable code base. By the end of this architecture book, you'll be able to build an application following clean architecture principles and have the knowledge you need to maintain and test the application easily.
Table of Contents (15 chapters)
1
Part 1 – Introduction
6
Part 2 – Domain and Data Layers
10
Part 3 – Presentation Layer

Implementing MVI with Kotlin flows

In this section, we will look at how we can implement the MVI architecture pattern using Kotlin flows and the benefits and pitfalls of this approach.

In the previous section, we defined an MVI approach using StateFlow and SharedFlow, as in the following example:

    private val _myStateFlow = MutableStateFlow<MyState>(MyState())
    val myStateFlow: StateFlow<MyState> = _myDataFlow
    private val actionFlow: MutableSharedFlow<MyAction> = MutableSharedFlow()

The different types of flows used here serve different purposes. MutableStateFlow will emit the last value held, which is good for the user interface because we want it to display the last data loaded, like how LiveData works. SharedFlow doesn't have this feature, which is useful for actions because we do not want the last action to be emitted twice. Another aspect we will need to consider is one-shot events...