Skip to content

Commit

Permalink
CubeLabsNZ#231 - adds a preference and fixes race condition
Browse files Browse the repository at this point in the history
Found that there was a race condition on the timelistview. Added a preference (default false, to stay as it currently exists) that now prompts on the 3 places to delete a solve.
  • Loading branch information
jmlumpkin committed Oct 21, 2024
1 parent 1abc2d0 commit 388a3ef
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CubeTime/Settings/AppearanceSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct AppearanceSettingsView: View {
// system settings (appearance)
@Preference(\.overrideDM) private var overrideSystemAppearance
@Preference(\.dmBool) private var darkMode
@Preference(\.promptDelete) private var promptDelete


@Preference(\.scrambleSize) private var scrambleSize
Expand Down Expand Up @@ -126,6 +127,8 @@ struct AppearanceSettingsView: View {
if overrideSystemAppearance {
SettingsToggle(String(localized: "Dark Mode"), $darkMode)
}

SettingsToggle(String(localized: "Prompt before deleting solves"), $promptDelete)
}


Expand Down
4 changes: 4 additions & 0 deletions CubeTime/Settings/SettingsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ final class SettingsManager {
fontCasual = 0.0
fontCursive = false
showZenMode = false
promptDelete = false
}

init(userDefaults: NSUbiquitousKeyValueStore) {
Expand Down Expand Up @@ -162,6 +163,9 @@ final class SettingsManager {

@UserDefault("showZenMode")
var showZenMode: Bool = false

@UserDefault("promptDelete")
var promptDelete: Bool = false
}

@propertyWrapper
Expand Down
31 changes: 21 additions & 10 deletions CubeTime/TimeList/TimeDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct TimeDetailView: View {

@FocusState private var commentFocus: Bool

@Preference(\.promptDelete) private var promptDelete


init(for solve: Solve, currentSolve: Binding<Solve?>?, sessionsCanMoveTo: Binding<[Session]?>? = nil, sessionsCanMoveTo_playground: Binding<[Session]?>? = nil) {
self.solve = solve
Expand Down Expand Up @@ -207,7 +209,12 @@ struct TimeDetailView: View {


CTButton(type: .coloured(Color("red")), size: .large, square: true, onTapRun: {
showAlert = true
if(promptDelete){
showAlert = true
}
else{
deleteSolve()
}
}) {
Image(systemName: "trash")
}
Expand All @@ -222,16 +229,9 @@ struct TimeDetailView: View {
action: { }
),
secondaryButton: .destructive(
Text("Delete Time"),
Text("Delete Solve"),
action: {
if currentSolve == nil {
dismiss()
}
currentSolve = nil

withAnimation {
stopwatchManager.delete(solve: solve)
}
deleteSolve()
}
)
)
Expand Down Expand Up @@ -370,5 +370,16 @@ struct TimeDetailView: View {
}
}
}

func deleteSolve() -> Void{
if currentSolve == nil {
dismiss()
}
currentSolve = nil

withAnimation {
stopwatchManager.delete(solve: solve)
}
}
}

38 changes: 25 additions & 13 deletions CubeTime/TimeList/TimeListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ struct TimeListView: View {

@State var showAlert = false

@Preference(\.promptDelete) private var promptDelete

var body: some View {
let sessionType = stopwatchManager.currentSession.sessionType
NavigationView {
Expand Down Expand Up @@ -383,7 +385,12 @@ struct TimeListView: View {
Divider()

Button(role: .destructive, action: {
showAlert = true
if(promptDelete){
showAlert = true
}
else{
deleteSolves()
}
}) {
Label("Delete", systemImage: "trash")
}
Expand All @@ -397,18 +404,6 @@ struct TimeListView: View {
.frame(width: 28, height: 28)
}
.frame(width: 28, height: 28)

.confirmationDialog(String(localized: "Are you sure you want to delete the selected solves? This cannot be undone."), isPresented: $showAlert, titleVisibility: .visible) {
Button("Confirm", role: .destructive) {
isSelectMode = false
for object in stopwatchManager.timeListSolvesSelected {
stopwatchManager.delete(solve: object)
}

stopwatchManager.timeListSolvesSelected.removeAll()
}
Button("Cancel", role: .cancel) {}
}
}
}

Expand Down Expand Up @@ -454,6 +449,13 @@ struct TimeListView: View {
Text("Are you sure you want to continue? This will delete every solve in this session!")
}

.confirmationDialog(String(localized: "Are you sure you want to delete the selected solves? This cannot be undone."), isPresented: $showAlert, titleVisibility: .visible) {
Button("Confirm", role: .destructive) {
deleteSolves()
}
Button("Cancel", role: .cancel) {}
}

.sheet(item: $solve) { item in
TimeDetailView(for: item, currentSolve: $solve)
.tint(Color("accent"))
Expand Down Expand Up @@ -484,4 +486,14 @@ struct TimeListView: View {
}
}
}

func deleteSolves() -> Void{
isSelectMode = false
for object in stopwatchManager.timeListSolvesSelected {
stopwatchManager.delete(solve: object)
}

stopwatchManager.timeListSolvesSelected.removeAll()
}
}

25 changes: 16 additions & 9 deletions CubeTime/TimeList/TimeListViewInner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ final class TimeListViewController: UICollectionViewController, UICollectionView
typealias DataSource = UICollectionViewDiffableDataSource<Int, Solve>
typealias Snapshot = NSDiffableDataSourceSnapshot<Int, Solve>
private let timeResuseIdentifier = "TimeCard"
@Preference(\.promptDelete) private var promptDelete


#warning("TODO: subscribe to changes of dynamic type?")
Expand Down Expand Up @@ -360,17 +361,23 @@ final class TimeListViewController: UICollectionViewController, UICollectionView

let delete = UIMenu(options: [.destructive, .displayInline], children: [ // For empty divide line
UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) {_ in
let alert = UIAlertController(title: "Confirm Delete", message: "Are you sure you want to delete this solve?", preferredStyle: .actionSheet)

alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { _ in
// Perform the delete action if confirmed
if(self.promptDelete){
let alert = UIAlertController(title: "Confirm Delete", message: "Are you sure you want to delete this solve?", preferredStyle: .actionSheet)

alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { _ in
// Perform the delete action if confirmed
self.stopwatchManager.delete(solve: solve)
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

if let viewController = self.navigationController?.visibleViewController{
viewController.present(alert, animated: true, completion: nil)
}
}
else{
self.stopwatchManager.delete(solve: solve)
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

if let viewController = self.navigationController?.visibleViewController{
viewController.present(alert, animated: true, completion: nil)
}
}
])
Expand Down
12 changes: 6 additions & 6 deletions Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,6 @@
}
}
}
},
"Are you sure you want to delete this time?" : {

},
"Are you sure you want to reset all appearance settings? Your solves and sessions will be kept." : {
"localizations" : {
Expand Down Expand Up @@ -964,6 +961,9 @@
}
}
}
},
"Delete Solve" : {

},
"Delete Solve Group" : {
"localizations" : {
Expand All @@ -974,9 +974,6 @@
}
}
}
},
"Delete Time" : {

},
"Descending" : {
"localizations" : {
Expand Down Expand Up @@ -1867,6 +1864,9 @@
}
}
}
},
"Prompt before deleting solves" : {

},
"Puzzle Type" : {
"localizations" : {
Expand Down

0 comments on commit 388a3ef

Please sign in to comment.