Add swipe action to rename cap

This commit is contained in:
Christoph Hagen 2022-12-11 19:26:11 +01:00
parent b767301aa0
commit 25aadffa51
2 changed files with 56 additions and 0 deletions

View File

@ -43,6 +43,12 @@ struct ContentView: View {
@State
var showNewClassifierAlert = false
@State
var showUpdateCapNameAlert = false
@State
var updatedCapName = ""
@State
var isEnteringNewCapName = false
@ -119,6 +125,14 @@ struct ContentView: View {
.onTapGesture {
didTap(cap: cap)
}
.swipeActions() {
Button {
showRenameWindow(for: cap)
} label: {
Label("Rename", systemSymbol: .pencil)
}
.tint(.purple)
}
}
.refreshable {
refresh()
@ -252,6 +266,13 @@ struct ContentView: View {
primaryButton: .default(Text("Download"), action: downloadClassifier),
secondaryButton: .cancel())
}
.alert("Update name", isPresented: $showUpdateCapNameAlert, actions: {
TextField("Name", text: $updatedCapName)
Button("Update", action: saveNewCapName)
Button("Cancel", role: .cancel, action: {})
}, message: {
Text("Please enter the new name for the cap")
})
.onChange(of: database.image) { newImage in
if newImage != nil {
sortType = .id
@ -332,6 +353,30 @@ struct ContentView: View {
removeCapturedImage()
isEnteringNewCapName = false
}
private func showRenameWindow(for cap: Cap) {
updatedCapName = cap.name
capIdOfNextPhoto = cap.id
showUpdateCapNameAlert = true
}
private func saveNewCapName() {
defer {
capIdOfNextPhoto = nil
updatedCapName = ""
}
guard let capId = capIdOfNextPhoto else {
return
}
let name = updatedCapName.trimmingCharacters(in: .whitespacesAndNewlines)
guard name != "" else {
return
}
guard database.update(name: name, for: capId) else {
return
}
}
}
struct ContentView_Previews: PreviewProvider {

View File

@ -395,6 +395,17 @@ final class Database: ObservableObject {
return true
}
func update(name: String, for capId: Int) -> Bool {
guard var cap = caps[capId] else {
log("Failed to update name for missing cap \(capId)")
return false
}
cap.name = name
caps[capId] = cap
changedCaps.insert(capId)
return true
}
// MARK: Uploads
func startRegularUploads() {