Add swipe action to rename cap
This commit is contained in:
parent
b767301aa0
commit
25aadffa51
@ -43,6 +43,12 @@ struct ContentView: View {
|
|||||||
@State
|
@State
|
||||||
var showNewClassifierAlert = false
|
var showNewClassifierAlert = false
|
||||||
|
|
||||||
|
@State
|
||||||
|
var showUpdateCapNameAlert = false
|
||||||
|
|
||||||
|
@State
|
||||||
|
var updatedCapName = ""
|
||||||
|
|
||||||
@State
|
@State
|
||||||
var isEnteringNewCapName = false
|
var isEnteringNewCapName = false
|
||||||
|
|
||||||
@ -119,6 +125,14 @@ struct ContentView: View {
|
|||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
didTap(cap: cap)
|
didTap(cap: cap)
|
||||||
}
|
}
|
||||||
|
.swipeActions() {
|
||||||
|
Button {
|
||||||
|
showRenameWindow(for: cap)
|
||||||
|
} label: {
|
||||||
|
Label("Rename", systemSymbol: .pencil)
|
||||||
|
}
|
||||||
|
.tint(.purple)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.refreshable {
|
.refreshable {
|
||||||
refresh()
|
refresh()
|
||||||
@ -252,6 +266,13 @@ struct ContentView: View {
|
|||||||
primaryButton: .default(Text("Download"), action: downloadClassifier),
|
primaryButton: .default(Text("Download"), action: downloadClassifier),
|
||||||
secondaryButton: .cancel())
|
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
|
.onChange(of: database.image) { newImage in
|
||||||
if newImage != nil {
|
if newImage != nil {
|
||||||
sortType = .id
|
sortType = .id
|
||||||
@ -332,6 +353,30 @@ struct ContentView: View {
|
|||||||
removeCapturedImage()
|
removeCapturedImage()
|
||||||
isEnteringNewCapName = false
|
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 {
|
struct ContentView_Previews: PreviewProvider {
|
||||||
|
@ -395,6 +395,17 @@ final class Database: ObservableObject {
|
|||||||
return true
|
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
|
// MARK: Uploads
|
||||||
|
|
||||||
func startRegularUploads() {
|
func startRegularUploads() {
|
||||||
|
Loading…
Reference in New Issue
Block a user