tunous/swiftuiundo
A quick and easy way to add undo/redo functionality to SwiftUI apps.
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
- Add the following to the dependencies array in your
Package.swiftfile:
``swift .package(url: "https://github.com/Tunous/SwiftUIUndo.git", .upToNextMajor(from: "1.0.0")), ``
- Add
SwiftUIUndoas a dependency for your target:
``swift .target(name: "MyApp", dependencies: ["SwiftUIUndo"]), ``
- Add
import SwiftUIUndoin 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.
Package Metadata
Repository: tunous/swiftuiundo
Default branch: main
README: README.md