• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated about 2 years ago

Reviews

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

Repository Details

Undo snapshot state changes in Compose.

compose-undo Maven Central

Track changes to any snapshot state object and restore state from any point in the past.

Usage

implementation 'com.zachklipp.compose-undo:statehistory:{version}'

The simplest way to get started is to use the WithStateHistory composable:

@Composable
fun App() {
    WithStateHistory { history ->
        var text by remember { mutableStateOf(TextFieldValue("")) }.trackStateChanges()
        TextField(text, onValueChange = { text = it })

        Button(onClick { history.undo() }) {
            Text("Undo")
        }
    }
}

The key is to call trackStateChanges on every snapshot state object you want to track. If you're creating state objects outside a composition, call StateHistory.startTrackingState yourself.

Advanced usage

The main API is the StateHistory class. See its kdoc for more detailed information.

Demo

This repo includes a demo app you can run and tinker with if you fork the repo. Here's a little preview:

compose-undo-demo.mp4

How it works

StateHistory keeps a set of all the state objects that were registered on it. It registers an apply listener to the snapshot system, and any time a snapshot is applied to the global snapshot it checks if any of the objects changed by that snapshot are being tracked. For every tracked changed object, it makes a copy of its latest state record. It collects all changes to tracked objects in a map (called a "frame"), then when saveFrame is called, it pushes that map onto the list of frames that represents the history.

When asked to restore states to a particular frame, it goes through every tracked state object and searches the frame list from the requested frame to find the latest frame that captured a change to that object. It then asks the snapshot system for a writable record for that object and copies the saved record back into the writable record, effectively setting the state object's value.

This is a very unconventional and probably unsupported use case of the StateObject and StateRecord APIs, but it allows the library to support any type of state object, even custom third-party ones. The actual implementation for saving and restoring state values looks something like this (stateObject is a StateObject):

// Save a state object's current value
val savedRecord = stateObject.firstStateRecord.create()
stateObject.firstStateRecord.withCurrent { currentRecord ->
    savedRecord.assign(currentRecord)
}

// Restore the value
stateObject.firstStateRecord.writable(stateObject) {
    assign(savedRecord)
}

More Repositories

1

compose-fractal-nav

A proof-of-concept of a fractal/recursive navigation system.
Kotlin
335
star
2

goadb

A Golang library for interacting with adb.
Go
229
star
3

adbfs

A FUSE filesystem that uses goadb to expose Android devices' filesystems.
Go
150
star
4

goregen

randexp for Go.
Go
90
star
5

compose-autotransition

A simple library for automatically animating between Compose states.
Kotlin
77
star
6

compose-seqdiag

Sequence diagram renderer for Jetpack Compose.
Kotlin
60
star
7

compose-revealer

Kotlin
47
star
8

compose-model

Kotlin
36
star
9

compose-touchbar

Kotlin
16
star
10

go-typedregexp

typedregexp matches regular expressions into structs.
Go
15
star
11

statedatastore

A snapshot state wrapper for the Jetpack Datastore library.
Kotlin
13
star
12

compose-worksheet-blog

Code for a blog post about derivedStateOf.
Kotlin
12
star
13

captivate

A simple service for detecting wifi hotspots that ask for information (captive portals), and automatically showing the login page.
Java
9
star
14

flow-operators

Some additional operators for Kotlin's cold stream library Flow
Kotlin
8
star
15

snapshot-state-swiftui-bridge

An experiment to consume Jetpack Compose's snapshot state change tracking from SwiftUI
Kotlin
7
star
16

web-adb

A Chrome extension and accompanying native adb proxy for working with Android devices over the Android Debug Bridge.
Go
7
star
17

regen

Simple command-line utility for generating strings from regular expression patterns.
Go
6
star
18

coroutine-workflows-poc

This is a proof-of-concept for entirely coroutine-based reactor + workflows.
Kotlin
6
star
19

rxkotlin

Pretending to write a cold streams library for Kotlin using coroutines.
Kotlin
5
star
20

textbuffer-sandbox

A repo for benchmarking different text buffer implementations.
Kotlin
5
star
21

go-pcomb

Parser Combinators for Golang
Go
4
star
22

gofontviewer

Render TTF fonts to an HTML5 Canvas in Go.
Go
3
star
23

zach-klippenstein.github.com

My personal website.
HTML
2
star
24

fraknums

Kotlin port of https://github.com/pyricau/fragnums
Kotlin
2
star
25

afpfs-ng

afpfs-ng is an Apple Filing Protocol client that will allow BSD, Linux and Mac OS X systems to access files exported from a Mac OS system with AFP over TCP.
Shell
2
star
26

with-intent

With Intent is a utility app for developers that lets you build and send intents without writing up a script or a test app.
Kotlin
2
star
27

ReproComposeAndroidViewTreeOwnersBugs

Kotlin
1
star
28

workflow-react-native-experiment

Experimenting with wiring up React Native to Workflow
Kotlin
1
star
29

snapshot-linked-list-blog

Kotlin
1
star
30

phrase-styling-example

Demonstrates a couple of different ways to style text with Phrase
Java
1
star