Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

The evolution of writing modern mobile apps. React | Flutter | Jetpack compose | Swift-UI = f(state)

 

 
Gaurav Bhola
Android Geek | Eng @ Google Pay
Published: April 02, 2021
Tweet
Share
 

 

Goal

  • To understand how we arrived at the current state of the mobile app development.
  • To also understand what the modern mobile app development is headed towards?

 

`Web` in this article refers to single page web apps made for mobile. They may or may not be progressive (PWA).

 

What is a general Mobile [App| Frontend | Client] composed of?

  • The underlying platform/toolkit (android/IOS/flutter/react)
  • Main() methods
  • App logic (X in MVX)
  • UI logic (V in MVX)
  • Data layer (M in MVX)

Main() methods:
Here we have the UI components and the entry points of the app:

  • A UI component is anything that enables you to draw on the device screen. 
    In Android it would be an Activity, Window,Fragment, View, layout xml etc. 
    In Ios, it would be UIViewController, UIView, StoryBoard etc.
    In Web, it would be the DOM Api, html etc.
    In React/Flutter, it would be your composable components and widgets.
    UI Components are very closely tied to the underlying framework and hence they vary a lot in how you use them.
  • An entry point is anything that enables your code to start getting executed by the system. Basically the main() methods of your apps.
    In Android it could be Application, Service, Activity, Alarm Manager, Broadcast receiver, AndroidManifest.xml
    In Ios, it could be UIApplication, UIViewController
    In web & ReactJs, it could be onload() and similar DOM events. Or any javascript running on the page.
    In Flutter, it could be your main() method or a custom entry point method.

 

While it depends on how the app is architected but most of the apps use the navigation framework provided by the underlying platform/toolkit.

 

App logic & UI Logic
App logic & UI Logic 
is any logic that is triggered by the main() methods.

  • In Android & Ios, AppLogic could be a jetpack’s ViewModel class or the X in MVX frameworks and UI-logic could be the entire View layer in MVX frameworks.
  • In Web, it could be the logic triggered by onInput(), onChange(), onBlur() etc
    In React/Flutter, App-logic could be X (in MVX), if you are indeed using an architecture. UI-logic could be referred to as the content defined via render() in react components, build() in flutter widgets or the composable functions in jetpack compose.

 

I personally see a thin difference between App-logic and UI-logic. 
A single App-logic module generally abstracts out a logical part in the Data layer. 
UI-logic builds on top of App-logic and decides what needs to be shown on the screen.

 

Data layer

For a poorly architected app, this layer generally lies together with the App Logic.

  • In Android/Ios, it basically represents the repositories/service classes that expose various kinds of datasources and also manipulate the data with some embedded business logic.
  • In Web, this is generally absent, but when doing React/Flutter, it generally represents Reducer functions, Stores, Repositories etc.

 

App-logic and data-layer atleast in theory can be platform/franework independent. UI-logic is something that needs to be platform dependent to call the platform specific UI components.

 

Evolution

I will try to cover my journey around building mobile apps across the platforms.

 

1. Vanilla way of doing UI

Lets take an example of fetching some data from the network and showing it on the UI

 

Android

XML:

 

 

Activity:

 

 

Things look pretty simple in our vanilla style but a bit verbose when we look at the Activity. 
Our UI + App Logic is kind of shared between the Xml and Activity. The data-layer however seems to be pretty separate & connected with the Activity using a callback.

  • Now if we imagine this kind of vanilla structure across a very large scale android app, things might become very messy on the UI side.
  • Even the services in the data-layer might become problematic because of various callback classes.
  • And if we ever think of adding a manual caching layer for the data we are fetching and reporting the cached/cache-updated status to the UI; things will go crazy and a design overhaul might be required.

 

IOS

Follows almost the exact same path as android. The threading constructs will differ in the service.

 

Web

HTML + JS

 

 

Imagining web based mobile app without a framework seems like a total nightmare. While this may work for very simple apps but as the app scales, the code might become very inconsistent across various screens/pages.

 

2. state driven [UI = fn(state)]

For most of the experienced developers reading this article, the spot-on development over the vanilla way would be to add a state.

 

 

We can have more state vars in the ActivityState class to represent the state of the network result in the case where it is cached in the sqlite db and then returned to the UI. Which means that onNetworkFetchResult() can be called N number of times with some valid permutation of the state vars.

This is something almost all of us have done in the past. One interesting fact about setState() method is that on every state change, we update all the UI elements regardless of the previous state of the UI.

 

The UI logic is still shared among the activity and the xml (js and html in case of web) where xml defines the UI and activity manipulates the UI in setState().

 

  • Lets say the progressBar is already hidden and we call setState with isLoading=false. progressBar.setVisibility(Hidden) is still called which might be a wasted cycle if lets say we have a lot of state vars and UI components.
  • Which means, for the UI to be most optimal we need some kind of UI diffing before the updates are dispatched over to the UI. This is exactly what lies at the heart of all the modern UI technologies: Jetpack compose, Flutter, React etc.

This now brings us to the ultimate Declarative UI paradigm. 
Before we get there, a quick note on the App logic:

 

For the vanilla , state driven or declarative way, App Logic can either reside along with the UI logic or separately when there is a proper architecture in place like MVX.

 

3. Declarative

The most important question that Declarative paradigm addresses is: 
Why does the UI logic has to be separate from the UI definition?
The answer is ‘No’, there is not much benefit in having them separate. Having them together and depending upon a common state unlocks a host of new possibilities.

 

Android [Jetpack Compose]

 

 

Declarative UI naturally leads to composable UI components where one component can wrap another one(s) and provide some defaults for the state. Example:

 

 

The SquareButton and CircularPinkButton are nothing but components wrapping/composing the BasicButton with some default state.

 

The data layer is still plain-old imperative. But with declarative UI, its generally better if the data layer is reactive in nature, so that UI state manipulations can depend on that. There are a ton of ways on how to structure the data flow in apps that use state driven Ui frameworks like redux, flux, custom MVX. The choice entirely depends on the kind of the app, underlying framework, expertise of the team, etc.

 

Benefits:

Loc--
Looks like its obvious from the code example shared above. The line of code has reduced by quite a bit.
Flutter & React native here provide extra benefit of being cross-platform, hence the reduction in loc with these will be a lot more.

Readability++
The UI code is certainly more readable and easy to reason about.

Composability
The very fact that all the UI components are based on some state permutations and/or combinations of smaller basic components makes the code ultra reusable.

UI Replication
This benefit actually comes from the State driven UI. Where given a permutation of state, one can easily figure out the UI. Now this is actually the most underrated benefit of state driven UI. Using this concept one can practically come up with a UI documentation (aka component library) for an entire software/app. And anybody can visualise how a particular component will look given this state. React storybook is one such tool that is built around this philosophy.

Better UI testing
Composability & UI replication brings us to the next obvious point: Better UI testing. 
Writing Unit tests become super easy for the declarative UI components. One just needs to manipulate the state and query the UI accordingly.

 

Downsides:

Not many, but for the sake of it:
Learning Curve
For imperative programmers, declarative UI code might be not-so-obvious to begin with. Understanding the internals of declarative UI frameworks might also take a lot of brain cycles.
Performance
One has to be very careful when dealing with endless scrolling, dynamic content in lists etc. Because if the UI state is changing very frequently and the code is poorly optimised, it can give some serious UI janks. 
On top of it, every declarative UI framework comes with its own implementation of how UI diffing is done which leads to framework specific nuances and different kinds of optimization that these frameworks demand.

 

Closing the entire loop!

 

 

  • Jetpack compose uses a gap buffer datastructure to calculate the minim possible UI updates that are required. (https://medium.com/androiddevelopers/under-the-hood-of-jetpack-compose-part-2-of-2-37b2c20c6cdd)
  • React creates a Virtual DOM tree. (https://reactjs.org/docs/faq-internals.html)
  • Flutter uses Element Tree and Render tree to do the same. (https://flutter.dev/docs/resources/architectural-overview)

 

WHOA!

That was quite a ride through the modern mobile UI and its evolution. This article doesn’t even touch the surface of the modern UI frameworks but can give a general sense of what is happening on the UI development. With Apple pushing swift UI, Google coming up with jetpack compose and flutter and react being here for quite some time; all of this is a testimony to the fact that Declarative UI is the future.

Advanced topics [Stories coming in the future]

  • Comparing UI diff internals of Flutter | Jetpack Compose | React
  • Comparing performance of Flutter | Jetpack Compose | React
  • Dataflow [data layer in declarative UIs]
  • Primary and secondary components, MVXs

 

Thanks to Tavishi Jain and Omolara Adejuwon. 

 

 

 

Tags: Android App Development, Reactive Programming, React, Jetpack Compose, Flutter

 

View original article at: 


 

Originally published: March 30, 2021

Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code 🗣 With Kotlin Scopes
Noisy Code 🗣 With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
Android News
KMM QuickStart Guide
KMM QuickStart Guide

By Mayank Kharbanda

Kotlin Multiplatform (KMP) is a code-sharing technology that allows us to use the same code for different platforms whether it’s JVM, Android, iOS, watchOS, tvOS, Web, Desktop, or WebAssembly. In this article, we will use Kotlin Multiplatform Mobile (KMM) which is a subset of KMP with the focus on providing better tooling and support for sharing code on mobile platforms i.e. Android and iOS.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One