Skip to content

Commit

Permalink
Fixes for undo logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mfikes committed Jan 16, 2024
1 parent a3833d8 commit a5e4dd5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
20 changes: 13 additions & 7 deletions Impedance Converter/Impedance Converter/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,19 @@ struct ContentView: View {
}
}
.alert(isPresented: $showUndoConfirmation) {
Alert(
title: Text("Undo Action"),
primaryButton: .destructive(Text("Undo")) {
viewModel.undo()
},
secondaryButton: .cancel()
)
if viewModel.canUndo {
return Alert(
title: Text("Undo Action"),
primaryButton: .destructive(Text("Undo")) {
viewModel.undo()
},
secondaryButton: .cancel()
)
} else {
return Alert(
title: Text("Cannot Undo")
)
}
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
viewModel.appDidBecomeActive()
Expand Down
15 changes: 14 additions & 1 deletion Impedance Converter/Impedance Converter/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class ViewModel: ObservableObject, Codable {
}

func performHold(held: (type: Hold, value: Double)) {
let isUndoCheckpointEnabledPrev = isUndoCheckpointEnabled
isUndoCheckpointEnabled = false
switch held.type {
case .none:
Expand All @@ -91,7 +92,7 @@ class ViewModel: ObservableObject, Codable {
case .capacitance:
capacitance = held.value
}
isUndoCheckpointEnabled = true
isUndoCheckpointEnabled = isUndoCheckpointEnabledPrev
hold = held.type
}

Expand Down Expand Up @@ -617,6 +618,8 @@ class ViewModel: ObservableObject, Codable {

required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let isUndoCheckpointEnabledPrev = isUndoCheckpointEnabled
isUndoCheckpointEnabled = false
displayMode = try container.decode(DisplayMode.self, forKey: .displayMode)
circuitMode = try container.decode(CircuitMode.self, forKey: .circuitMode)
immittance = try container.decode(Immittance.self, forKey: .immittance)
Expand All @@ -626,6 +629,7 @@ class ViewModel: ObservableObject, Codable {
let angleRadians = try container.decode(Double.self, forKey: .refAngle)
refAngle = Angle(radians:angleRadians)
angleOrientation = try container.decode(AngleOrientation.self, forKey: .measureOrientation)
isUndoCheckpointEnabled = isUndoCheckpointEnabledPrev
}

func encode(to encoder: Encoder) throws {
Expand All @@ -641,6 +645,8 @@ class ViewModel: ObservableObject, Codable {
}

func update(from other: ViewModel) {
let isUndoCheckpointEnabledPrev = isUndoCheckpointEnabled
isUndoCheckpointEnabled = false
self.displayMode = other.displayMode
self.circuitMode = other.circuitMode
self.immittance = other.immittance
Expand All @@ -649,6 +655,7 @@ class ViewModel: ObservableObject, Codable {
self.velocityFactor = other.velocityFactor
self.refAngle = other.refAngle
self.angleOrientation = other.angleOrientation
isUndoCheckpointEnabled = isUndoCheckpointEnabledPrev
}

func encodeToJSON() -> String? {
Expand Down Expand Up @@ -689,6 +696,12 @@ class ViewModel: ObservableObject, Codable {
}
}

var canUndo: Bool {
get {
checkpoints.count > 1
}
}

func undo() {
// Ensure there's more than one checkpoint
guard checkpoints.count > 1 else { return }
Expand Down

0 comments on commit a5e4dd5

Please # to comment.