Git Product home page Git Product logo

compose-undo's Introduction

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)
}

compose-undo's People

Contributors

zach-klippenstein avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.