This is a sample app to use [Molecule library] and solve some scoping problems in Android development.

Overview

Android sample app

Problem

It's started when P.Y. in Twitter shared this tweet and his post,

py

which is a correct complain about introducing a new set of race conditions to Android apps by using WhileSubscribed(). He wrote a post, which is basically a reply to a post from the Android Developers blog by Manuel Vivo. The problem in the core is that by using AAC ViewModel we don't know the scope of the logic related to each of composable functions in the view! Notice the scope of each composable function in the view is not reliable as the configuration changes would end the scope, where it should not be, because the logic should survive the configuration change. But on the other hand, the scope of ViewModel itself is too long for the logic of each composable function!

The next tweet that is related to this repository is

Jake

As you can see my first reaction was that we need to keep the ViewModels, but Jake is right.

Solution

In this sample I tried Jake's solution to the problem, which I think is the Molecule library. It's a very simple app that have just one page, but by adding a number on it, we'll use it to make this app a multiple page app.

Record

Before this, we separated the logic of composable functions by using state hoisting, where you can find more details in Jetpack Compose basics. The tree of the views was looking like this.

View Tree

But as we mentioned before lifetime of the logic is wider than the lifetime of the view, so we can separate the hoist composable functions.

View Logic Tree

Where the logic composable functions live longer than the view composable functions. But it cannot happen unless we share Action and State object between them to be able to decouple them and put them on different trees. Also we rename hoists to presenters, as it's suggested in the Molecule library.

View State Logic Tree

So in the end, we have four trees, where two of them, the view and logic trees, are trees that is built with Jetpack Compose, and the other two trees, the state and action trees, is built by simple object references. You can find the root of logic, state, and action trees in the RootLogic.kt file. Also the root of the view tree is in the Main.kt file.

Please feel free to give me your feedback in Twitter, or make an issue, or a PR on this repository. Hope it solves some problems and makes our life easier.

You might also like...
Provides Kotlin libs and some features for building Kotlin plugins
Provides Kotlin libs and some features for building Kotlin plugins

Kotlin Plugin Provides Kotlin libs and some features for building awesome Kotlin plugins. Can be used instead of CreeperFace's KotlinLib (don't use to

Some basic samples of Kotlin for Android
Some basic samples of Kotlin for Android

android-kotlin-samples Some basic samples of Kotlin for Android Samples HttpUrlConnection and AsyncTask ListView with BaseAdapter and Data Object OkHt

A collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android.

requireKTX requireKTX is a collection of small utility functions to make it easier to deal with some otherwise nullable APIs on Android, using the sam

A mobile application developed for *Android* devices, aimed at 11th grade students in which they can take some basic training tests for presentation of external tests.

ApliKTest11 Application with Kit of questions and Knowledge Test for the preparation of the Saber Test. Description A mobile application developed for

Same as an Outlined text fields presented in Material Design page but with some dynamic changes
Same as an Outlined text fields presented in Material Design page but with some dynamic changes

README SSCustomEditTextOutlineBorder Getting Started SSCustomEditTextOutLineBorder is a small kotlin library for android to support outlined (stroked)

Some fancy custom views for kotlin
Some fancy custom views for kotlin

CoolCustomViews 1. Neruromorphic ProgressBar Resources Documentaion and Other So

WolfxPaper - A Paper fork designed for Wolfx Survial, may useful for some Semi-Vanilla Server

WolfxPaper A Paper fork designed for Wolfx Survial, may useful for some "Semi-Va

This Project for how to use  MVVM , state flow, Retrofit, dagger hit, coroutine , use cases with Clean architecture.
This Project for how to use MVVM , state flow, Retrofit, dagger hit, coroutine , use cases with Clean architecture.

Clean-architecture This Project for how to use MVVM , state flow, Retrofit, dagger hit, coroutine , use cases with Clean architecture. Why i should us

Owner
Hadi
Hadi
AbstractMvp 0.8 0.0 Kotlin is a library that provides abstract components for MVP architecture realization, with problems solutions that are exist in classic MVP.

MinSDK 14+ AbstractMvp AbstractMvp is a library that provides abstract components for MVP architecture realization, with problems solutions that are e

Robert 12 Apr 5, 2022
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.

Mr.Sanchez 176 Jan 4, 2023
ATH Sample is a sample Authentication and Authorization Application with Kotlin Language and MVVM architecture.

ATH Sample ATH Sample is a sample Authentication and Authorization Application with Kotlin Language and MVVM architecture. Overview ATH Sample is a sa

AbolfaZl RezaEi 4 Jun 8, 2021
CleanArchitecture is a sample project that presents a modern, 2021 approach to Android application development.

CleanArchitecture is a sample project that presents a modern, 2021 approach to Android application development. The goal of the pro

Shushant tiwari 0 Nov 13, 2021
WordMasterKMP - WIP Kotlin Multiplatform sample inspired by Wordle and also Word Master web sample

WordMasterKMP WIP Kotlin Multiplatform sample inspired by Wordle and also Word M

John O'Reilly 56 Oct 4, 2022
ViewModel LiveData Sample - Sample of using ViewModel, LiveData and Data Binding

ViewModel_LiveData_Sample Sample Code for Lesson 8 using ViewModel, LiveData and

null 0 Mar 18, 2022
Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

Chris Russell 1 Feb 11, 2022
📒 NotyKT is a complete 💎Kotlin-stack (Backend + Android) 📱 application built to demonstrate the use of Modern development tools with best practices implementation🦸.

NotyKT ??️ NotyKT is the complete Kotlin-stack note taking ??️ application ?? built to demonstrate a use of Kotlin programming language in server-side

Shreyas Patil 1.4k Jan 4, 2023
A sample application that build with combine use Clean Architecture framework and Github API

The Github Example Introduction This is a sample application that build with combine use Clean Architecture framework and Github API (https://develope

Nguyễn Hùng An 2 Aug 12, 2022
A library provides some useful kotlin extension functions

ktext ?? A library provides some useful kotlin extension functions. Including in your project Gradle Add below codes to your root build.gradle file (n

热心市民苏苏仔 76 Oct 26, 2022