• Stars
    star
    1,458
  • Rank 30,981 (Top 0.7 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated 3 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Multiplatform reactive UI for Android, iOS, and web using Kotlin and Jetpack Compose

Redwood

Redwood is a library for building reactive Android, iOS, and web UIs using Kotlin.

Redwood is currently under development and not ready for use by anyone.

Reactive UIs

Android and iOS UI frameworks model the user interface as a β€˜mutable view tree’ or document object model (DOM). To build an application using the mutable view tree abstraction, the programmer performs two discrete steps:

  • Build the static view tree. In Android the conventional tool for this is layout XML, though we've done some cool work with Contour to build view trees with Kotlin lambdas.

  • Make it dance. The view tree should change in response to user actions (like pushing buttons) and external events (like data loading). The program mutates the view tree to represent the current application state. Some mutations change the on-screen UI instantly; others animate smoothly from the old state to the new state.

React popularized a new programming model, reactive UIs. With reactive UIs, the programmer writes a render() function that accepts the application state and returns a view tree. The framework calls this function with the initial application state and again each time the application state changes. The framework analyzes the differences between pairs of view trees and updates the display, including animating transitions where appropriate.

In React the view tree returned by the render function is called a virtual DOM, and it has an on-screen counterpart called the real DOM. The virtual DOM is a tree of simple JavaScript value objects; the real DOM is a tree of live browser HTML components. Creating and traversing thousands of virtual DOM objects is fast; creating thousands of HTML components is not! Therefore, the virtual DOM optimization is the magic that makes React work.

Compose

Jetpack Compose is an implementation of the reactive UI model for Android. It uses an implementation trick to further optimize the reactive programming model. It is implemented in two complementary modules:

  • The Compose compiler is a Kotlin compiler plugin that supports partial re-evaluation of a function. The programmer still writes render functions to transform application state into a view tree. The compiler rewrites this function to track which inputs yield which outputs. When the input application state changes, it evaluates only what is necessary to generate the corresponding view tree changes.

  • Compose UI is a new set of Android UI components designed to work with the Compose compiler. It addresses longstanding technical debt with Android's view system.

A Kotlin function that is rewritten by the Compose compiler is called a composable function. Partial re-evaluation of a composable function is called recomposing.

Note that the Compose compiler can be used without Compose UI. For example, compose-server-side renders HTML components on a server that are sent to a browser over a WebSocket.

Design Systems

In Cash App we use a design system. It specifies our UI in detail and names its elements:

  • Names for our standard colors, fonts, icons, dimensions
  • Named text blocks, specified using the names above
  • Named controls, such as our standard checkboxes, buttons, and dialogs

The design system helps with collaboration between programmers and designers. It also increases uniformity within the application and across platforms.

What Is Redwood?

Redwood integrates the Compose compiler, a design system, and a set of platform-specific displays. Each Redwood project is implemented in three parts:

  • A design system. Redwood includes a sample design system called β€˜Sunspot’. Most applications should customize this to match their product needs.

  • Displays for UI platforms. The display draws the pixels of the design system on-screen. Displays can be implemented for any UI platform. Redwood includes sample displays for Sunspot for Android, iOS, and web.

  • Composable Functions. This is client logic that accepts application state and returns elements of the design system. These have similar responsibilities to presenters in an MVP system.

Why Redwood?

We're eager to start writing reactive UIs! But we're reluctant to continue duplicating code across iOS, Android, and web platforms. In particular, we don't like how supporting multiple platforms reduces our overall agility.

We'd like to shortcut the slow native UI development process. Iterating on UIs for Android requires a slow compile step and a slow adb install step. With Redwood, we hope to use the web as our development target while we iterate on composable function changes.

We want the option to change application behavior without waiting for users to update their apps. With Kotlin/JS we may be able to update our composable functions at application launch time, and run them in a JavaScript VM. We may even be able to use WebAssembly to accomplish this with little performance penalty.

Redwood is a library, not a framework. It is designed to be adopted incrementally, and to be low-risk to integrate in an existing Android project. Using Redwood in an iOS or web application is riskier! We've had good experiences with Kotlin Multiplatform Mobile, and expect a similar outcome with Redwood.

Code Sample

We start by expressing our design system as a set of Kotlin data classes. Redwood will use these classes to generate type-safe APIs for the displays and composable functions.

@Widget(1)
data class Text(
  @Property(1) val text: String?,
  @Property(2) @Default("\"black\"") val color: String,
)

@Widget(2)
data class Button(
  @Property(1) val text: String?,
  @Property(2) @Default("true") val enabled: Boolean,
  @Property(3) val onClick: () -> Unit,
)

Displays implement the design system using native UI components.

class AndroidText(
  override val value: TextView,
) : Text<View> {
  override fun text(text: String?) {
    value.text = text
  }

  override fun color(color: String) {
    value.setTextColor(Color.parseColor(color))
  }
}

Composable functions render application state into the design system. These will make use of Compose API features like remember().

@Composable
fun Counter(value: Int = 0) {
  var count by remember { mutableStateOf(value) }

  Button("-1", onClick = { count-- })
  Text(count.toString())
  Button("+1", onClick = { count++ })
}

More Repositories

1

sqldelight

SQLDelight - Generates typesafe Kotlin APIs from SQL
Kotlin
5,918
star
2

turbine

A small testing library for kotlinx.coroutines Flow
Kotlin
2,323
star
3

paparazzi

Render your Android screens without a physical device or emulator
Kotlin
2,164
star
4

zipline

Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs
C
1,914
star
5

molecule

Build a StateFlow stream using Jetpack Compose
Kotlin
1,710
star
6

contour

Layouts with lambdas 😎
Kotlin
1,522
star
7

InflationInject

Constructor-inject views during XML layout inflation
Kotlin
908
star
8

pranadb

Go
615
star
9

licensee

Gradle plugin which validates the licenses of your dependency graph match what you expect
Kotlin
599
star
10

hermit

🐚 Hermit manages isolated, self-bootstrapping sets of tools in software projects.
Go
557
star
11

AccessibilitySnapshot

Easy regression testing for iOS accessibility
Swift
522
star
12

exhaustive

An annotation and Kotlin compiler plugin for enforcing a when statement is exhaustive
Kotlin
465
star
13

multiplatform-paging

A library that packages AndroidX Paging for Kotlin/Multiplatform.
Kotlin
452
star
14

misk

Microservice Kontainer
Kotlin
386
star
15

copper

A content provider wrapper for reactive queries
Kotlin
300
star
16

barber

Barber πŸ’ˆ A type safe Kotlin JVM library for building up localized, fillable, themed documents using Mustache templating
Kotlin
157
star
17

paraphrase

A Gradle plugin that generates type-safe formatters for Android string resources in the ICU message format.
Kotlin
157
star
18

stagehand

Modern, type-safe API for building animations on iOS
Swift
126
star
19

hermit-packages

Hermit manages isolated, self-bootstrapping sets of tools in software projects.
HCL
111
star
20

quiver

Quiver is a collection of extension methods and handy functions to make the wonderful functional programming Kotlin library, Arrow, even better.
Kotlin
95
star
21

spirit

Online Schema Change Tool for MySQL 8.0
Go
87
star
22

pivit

Go
82
star
23

tempest

Typesafe DynamoDB for Kotlin and Java.
Kotlin
80
star
24

better-dynamic-features

Making dynamic feature modules better
Kotlin
70
star
25

misk-web

Micro-Frontends React + Redux + Typescript Framework
TypeScript
65
star
26

blip

Sublime MySQL monitoring
Go
59
star
27

logquacious

Logquacious (lq) is a fast and simple log viewer.
TypeScript
58
star
28

wisp

Wisp is a collection of kotlin modules providing various features and utilities, including config, logging, feature flags and more.
Kotlin
58
star
29

certifikit

Kotlin Certificate processing library.
Kotlin
40
star
30

backfila

Service that manages backfill state, calling into other services to do batched work
Kotlin
30
star
31

cash-app-pay-android-sdk

Cash Android PayKit SDK for merchant integrations with Cash App Pay
Kotlin
29
star
32

nostrino

A Kotlin SDK for Nostr
Kotlin
28
star
33

transflect

Kubernetes operator using Istio to set up Envoy's gRPC-JSON transcoding.
Go
24
star
34

cmmc

K8S ConfigMap Merging Controller
Go
20
star
35

cloner

Go
20
star
36

kfsm

Finite state machinery in Kotlin
Kotlin
19
star
37

yet-another-aws-exporter

A Prometheus metrics exporter for AWS that fills in gaps CloudWatch doesn't cover
Go
17
star
38

cash-app-pay-ios-sdk

Swift
14
star
39

protosync

ProtoSync synchronises remote .proto files to a local directory
Go
13
star
40

cash-pay-pay-sdk-android-sample-app

Cash App Pay Kit SDK Sample app for Android.
Kotlin
12
star
41

trifle

Security functionality for interoperability/interaction with core services.
Swift
11
star
42

AardvarkReveal

Generate and attach a Reveal file to your Aardvark bug reports
Swift
10
star
43

ln-invoice

Parse lightning network payment requests (invoices) in Kotlin.
Kotlin
10
star
44

kfactories

Set of factories and utils to create effective and lightweight property-based testing strategies.
Kotlin
9
star
45

jooq-encryption

Kotlin
9
star
46

hermit-ij-plugin

Kotlin
8
star
47

cash-app-pay-sdk-ios-sample-app

Swift
7
star
48

AardvarkCrashReport

AardvarkCrashReport makes it easy to provide high quality data about crashes in your bug reports
Swift
7
star
49

awsu

su for aws roles
Go
7
star
50

check-signature-action

Shell
6
star
51

csop

Go
6
star
52

activate-hermit

Github Action to activate a Hermit environment.
Shell
6
star
53

kruit

TypeScript
4
star
54

chronicler

Kotlin
3
star
55

hermit-build

Relocatable/static builds for Hermit packages
Makefile
3
star
56

s3-copy-gradle-plugin

1
star
57

.github

1
star
58

hermit-package-version

Shell
1
star
59

cash-app-pay-sandbox-releases

1
star