Skip to content

Latest commit

 

History

History
79 lines (59 loc) · 2.15 KB

File metadata and controls

79 lines (59 loc) · 2.15 KB

SwiftUIUndo

A quick and easy way to add undo/redo functionality to SwiftUI apps.

On iOS, this enables undo/redo via a shake gesture. On macOS, this enables undo/redo via menu commands and their keyboard shortcuts.

Examples

Call withUndoRedo on a visible SwiftUI view and provide a binding to an equatable value.

struct SimpleExample: View {
    @State private var int = 0
    @State private var bool = false

    var body: some View {
        VStack {
            Stepper(int.formatted(), value: $int)
                .withUndoRedo("Change Int", for: $int)
            Toggle("Toggle", isOn: $bool)
                .withUndoRedo("Change Bool", for: $bool)
        }
    }
}

You can dynamically decide the name of the action displayed to the user based on the changed values.

struct AdvancedExample: View {
    struct Values: Equatable {
        var int: Int
        var bool: Bool
    }

    @State private var values = Values(int: 0, bool: false)

    var body: some View {
        VStack {
            Stepper(values.int.formatted(), value: $values.int)
            Toggle("Toggle", isOn: $values.bool)
        }
        .withUndoRedo(for: $values) { oldValues, newValues in
            if oldValues.int < newValues.int {
                return "Increment"
            }
            if oldValues.int > newValues.int {
                return "Decrement"
            }
            return "Toggle"
        }
    }
}

If you do not want undo to activate via the shake gesture, set the enableShakeToUndo parameter to false.

Installation

Swift Package Manager

  1. Add the following to the dependencies array in your Package.swift file:

    .package(url: "https://github.com/Tunous/SwiftUIUndo.git", .upToNextMajor(from: "1.0.0")),
  2. Add SwiftUIUndo as a dependency for your target:

    .target(name: "MyApp", dependencies: ["SwiftUIUndo"]),
  3. Add import SwiftUIUndo in your source code.

Xcode

Add https://github.com/Tunous/SwiftUIUndo.git to the list of Swift packages for your project in Xcode and include it as a dependency for your target.