An example of using The Elm Architecture in Android

Related tags

Kotlin GreenTea
Overview

GreenTea

[Work in progress] An example of using The Elm Architecture in Android. There is a working example and the most important base clases. Still, the project will be updated with better testing and more advanced features.

TEA

The Elm Architecture is a pattern of building an app.

Why?

Here are the meain features of TEA:

  • Unidirectional dataflow
  • Immutable state
  • Pure functions for business / presentation logic
  • Managed side effects

These features lead to the next system qualities:

  • It is easy to navigate the code
  • It is easy to reason about what is the current state
  • It is easy to test We'll go through a few examples below.

How?

The architecture suggests splitting your code into 3 main parts:

  • State (Model) - literally, what is the state that should be displayed
  • View - mapping of the state to something that a user can see. Eg populating Android Views or Jetpack Compose functions
  • Update - some messages that come from a user or from "outside world" that lead to changes in the state

In addition, there is actually one more important piece:

  • Side Effects - anything that makes our pure and testable functions into something less predictable, for example IO calls

So, why is it good?

First, you always know what is the state of your feature. For simplicity, let's say we have a feature per screen. Then, just knowing the current State you know if the user is going to see what's expected or not.

Ideally, everything goes right. And you can always write pure unit tests without any mocks to assure it.

If something still goes wrong, you always know where to find the issue. If a proper state came to the View, then the problem is somewhere in the View. If the state is not the one that you expect - you just check what was the last Message that changed the state - and you can understand, what went wrong, what brings us to...

Time travel. As all the State changes and Messages that led to the State change are logged, it's possible to go through all the changes from a Feature init to the moment where you are at the moment. This feature makes debugging much easier.

How it works?

All you need to create a new Feature is to define the next parts:

  1. State. Everything that you need to display to a user.
  2. Messages. What are the things that can change the State? For example "OnRefreshClicked".
  3. Update function. Pure function that gets current State, incoming Message, and returns updated State and, potentially, a set of Side Effects.
  4. Side Effects. Do you need to make a network request? Declare it here. Result arrived? Send it back to Update as a Message - and the flow is looped! Navigation should also be treated as a Side Effect.
  5. Initial Update. How does your Feature start? What is the default State? Should there be any Effects happening right on Start?

And that's how you define a full-blown Feature. What's left?

  1. Create a View. Most often it will be a Fragment with some render(state) function that needs to be implemented.
  2. Connect Feature and View with help of a tea-runtime. This projects presents a Coroutine-based runtime, but implementations may vary.

This project

This project shows you and example of how a feature, written in MVVM may be migrated to MVU. It contains the Tea Runtime class and a few base classes that make creation of Features very easy and straightforward process.

Readings

The Official Guide on The Elm Architecture A series of blog posts about ELM on Android

You might also like...
Native android app made with Kotlin & Compose with example usage of Ktor, SqlDelight.
Native android app made with Kotlin & Compose with example usage of Ktor, SqlDelight.

Delight-Playground 🎉 Native Android application built with Kotlin and Jetpack Compose. This app also illustrates the usage of advance libraries such

Monorepo - An android monorepo example

A mono-repository containing multiple Android apps sharing library modules. http

[Android-Kotlin] MVVM, ViewModel, LiveData, Observer, DataBinding, Repository, Retrofit, Dagger example

SimpleMvvmDaggerKotlin [Android-Kotlin] MVVM, ViewModel, LiveData, Observer, DataBinding, Repository, Retrofit, Dagger example [Image1 : User informat

Example mod with Mixin to help you to get started with creating a mod with mixins.

ExampleMixinMod Example mod with Mixin to help you to get started with creating a mod with mixins. For usage of mixins, see here. Also, remember to tu

This repository contains the article describing my attempt to implement a simple state reducer based on Kotlin Flow and an example app that uses it.
This repository contains the article describing my attempt to implement a simple state reducer based on Kotlin Flow and an example app that uses it.

This repository contains the article describing my attempt to implement a simple state reducer based on Kotlin Flow and an example app that uses it.

Example code from coroutines testing talk.

Coroutines Testing This repo contains example code used to build my (Bill Phillips) talk on coroutines testing. Use it however you please, but note so

A simple example of kotlim compiler plugin with FIR and IR.

A simple Kotlin compiler plugin example This Kotlin compiler plugin generates a top level class: public final class foo.bar.MyClass { fun foo(): S

Kotlin/JVM Lambda example

kotlin-lambda-example-jvm Kotlin Lambda example on JVM contains four CDK stacks of Kotlin Lambda on JVM: JVM on x86, JVM on Arm64, JVM with C1 compile

Android Clean Architecture in Rorty is a sample project that presents modern, approach to Android application development using Kotlin and latest tech-stack.
Android Clean Architecture in Rorty is a sample project that presents modern, approach to Android application development using Kotlin and latest tech-stack.

Android Clean Architecture in Rorty is a sample project that presents modern, approach to Android application development using Kotlin and latest tech-stack.

Owner
Artur Badretdinov
Artur Badretdinov
Example of migrating from Dagger to Hilt with a real service/repository example

DaggerToHilt Overview This repo provides a real example of using Hilt for dependency injection. It hits endpoints provided by the Movie Database, and

null 0 Nov 29, 2021
Example Multi module architecture Android project using MVVM, Dynamic Features, Dagger-Hilt, Coroutines and Navigation Components

ModularDynamicFeatureHilt An Android template project following a multi module approach with clean architecture. It has been built following Clean Arc

Mbuodile Obiosio 25 Nov 23, 2022
Cleanarchitecture MVVM Hilt base - Android Clean Architecture example project

Android Clean Architecture example project Note: This is project based on MVVM c

Mohsin Javed 4 Aug 30, 2022
Example KMM app for showing about layered architecture

Layered Architecture in a Kotlin Multiplatform project This project was created by a series of posts you can find on my blog https://jflavio.com The d

Jose Flavio Quispe Irrazábal 13 Oct 10, 2022
A fork of our clean architecture boilerplate, this time using the Android Architecture Components

Android Clean Architecture Components Boilerplate Note: This is a fork of our original Clean Architecture Boilerplate, except in this repo we have swi

Buffer 1.3k Jan 3, 2023
Skeleton project for show the architecture of Android project using MVVM, Clean Architecture and Kotlin coroutine Flow

ClearScoreDemo Skeleton project for showing the architecture of Android project using MVVM, Clean architecture and Kotlin coroutine Flow App Architect

Plabon Modak 1 Mar 6, 2022
FaceTimeClone app that implements Coroutines , mvvm architecture , clean architecture , navigation component , hilt , etc.... using kotlin language

This repository contains a FaceTimeClone app that implements Coroutines , mvvm architecture , clean architecture , navigation component , hilt , etc.... using kotlin language

null 17 Dec 13, 2022
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

Semyon Isaev 1 Nov 8, 2021
LifecycleMvp 1.2 0.0 Kotlin is MVP architecture implementation with Android Architecture Components and Kotlin language features

MinSDK 14+ Download Gradle Add to project level build.gradle allprojects { repositories { ... maven { url 'https://jitpack.io' }

Robert 20 Nov 9, 2021
An example for who are all going to start learning Kotlin programming language to develop Android application.

Kotlin Example Here is an example for who are all going to start learning Kotlin programming language to develop Android application. First check this

Prabhakar Thota 56 Sep 16, 2022