Fixed add/edit problems
This commit is contained in:
Binary file not shown.
@@ -19,6 +19,7 @@ struct AddEditProblemView: View {
|
|||||||
@State private var tags = ""
|
@State private var tags = ""
|
||||||
@State private var notes = ""
|
@State private var notes = ""
|
||||||
@State private var isActive = true
|
@State private var isActive = true
|
||||||
|
@State private var dateSet = Date()
|
||||||
@State private var imagePaths: [String] = []
|
@State private var imagePaths: [String] = []
|
||||||
@State private var imageData: [Data] = []
|
@State private var imageData: [Data] = []
|
||||||
@State private var showingPhotoOptions = false
|
@State private var showingPhotoOptions = false
|
||||||
@@ -26,6 +27,9 @@ struct AddEditProblemView: View {
|
|||||||
@State private var showingImagePicker = false
|
@State private var showingImagePicker = false
|
||||||
@State private var imageSource: UIImagePickerController.SourceType = .photoLibrary
|
@State private var imageSource: UIImagePickerController.SourceType = .photoLibrary
|
||||||
@State private var isEditing = false
|
@State private var isEditing = false
|
||||||
|
@State private var showingGradeError = false
|
||||||
|
@State private var isLoaded = false
|
||||||
|
@State private var originalImages: [(path: String, data: Data)] = []
|
||||||
|
|
||||||
private var existingProblem: Problem? {
|
private var existingProblem: Problem? {
|
||||||
guard let problemId = problemId else { return nil }
|
guard let problemId = problemId else { return nil }
|
||||||
@@ -52,6 +56,10 @@ struct AddEditProblemView: View {
|
|||||||
Form {
|
Form {
|
||||||
GymSelectionSection()
|
GymSelectionSection()
|
||||||
BasicInfoSection()
|
BasicInfoSection()
|
||||||
|
PhotosSection()
|
||||||
|
ClimbTypeSection()
|
||||||
|
DifficultySection()
|
||||||
|
LocationDetailsSection()
|
||||||
AdditionalInfoSection()
|
AdditionalInfoSection()
|
||||||
}
|
}
|
||||||
.navigationTitle(isEditing ? "Edit Problem" : "New Problem")
|
.navigationTitle(isEditing ? "Edit Problem" : "New Problem")
|
||||||
@@ -65,15 +73,22 @@ struct AddEditProblemView: View {
|
|||||||
|
|
||||||
ToolbarItem(placement: .navigationBarTrailing) {
|
ToolbarItem(placement: .navigationBarTrailing) {
|
||||||
Button("Save") {
|
Button("Save") {
|
||||||
|
if canSave {
|
||||||
saveProblem()
|
saveProblem()
|
||||||
|
} else {
|
||||||
|
showingGradeError = true
|
||||||
}
|
}
|
||||||
.disabled(!canSave)
|
}
|
||||||
|
.disabled(!canSave && !showingGradeError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
setupInitialClimbType()
|
setupInitialClimbType()
|
||||||
loadExistingProblem()
|
loadExistingProblem()
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
isLoaded = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.sheet(isPresented: $showingPhotoOptions) {
|
.sheet(isPresented: $showingPhotoOptions) {
|
||||||
PhotoOptionSheet(
|
PhotoOptionSheet(
|
||||||
@@ -155,43 +170,158 @@ struct AddEditProblemView: View {
|
|||||||
Section("Problem Details") {
|
Section("Problem Details") {
|
||||||
TextField("Problem Name (Optional)", text: $name)
|
TextField("Problem Name (Optional)", text: $name)
|
||||||
|
|
||||||
VStack(alignment: .leading, spacing: 8) {
|
TextField("Description (Optional)", text: $description, axis: .vertical)
|
||||||
Text("Description (Optional)")
|
.lineLimit(3...6)
|
||||||
.font(.headline)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TextEditor(text: $description)
|
@ViewBuilder
|
||||||
.frame(minHeight: 80)
|
private func PhotosSection() -> some View {
|
||||||
.padding(8)
|
Section("Photos (Optional)") {
|
||||||
.background(
|
Button(action: {
|
||||||
RoundedRectangle(cornerRadius: 8)
|
showingPhotoOptions = true
|
||||||
.fill(.quaternary)
|
}) {
|
||||||
)
|
HStack {
|
||||||
|
Image(systemName: "camera.fill")
|
||||||
|
.foregroundColor(themeManager.accentColor)
|
||||||
|
|
||||||
|
VStack(alignment: .leading) {
|
||||||
|
Text("Add Photos")
|
||||||
|
.foregroundColor(themeManager.accentColor)
|
||||||
|
Text("\(imageData.count) of 5 photos added")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer()
|
||||||
|
|
||||||
|
Image(systemName: "chevron.right")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !imageData.isEmpty {
|
||||||
|
ScrollView(.horizontal, showsIndicators: false) {
|
||||||
|
HStack(spacing: 8) {
|
||||||
|
ForEach(imageData.indices, id: \.self) { index in
|
||||||
|
if let uiImage = UIImage(data: imageData[index]) {
|
||||||
|
ZStack(alignment: .topTrailing) {
|
||||||
|
Image(uiImage: uiImage)
|
||||||
|
.resizable()
|
||||||
|
.scaledToFill()
|
||||||
|
.frame(width: 80, height: 80)
|
||||||
|
.clipShape(RoundedRectangle(cornerRadius: 8))
|
||||||
|
|
||||||
|
Button(action: {
|
||||||
|
imageData.remove(at: index)
|
||||||
|
}) {
|
||||||
|
Image(systemName: "xmark.circle.fill")
|
||||||
|
.foregroundColor(.white)
|
||||||
|
.background(Circle().fill(Color.black.opacity(0.5)))
|
||||||
|
}
|
||||||
|
.padding(4)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding(.vertical, 4)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ViewBuilder
|
@ViewBuilder
|
||||||
private func AdditionalInfoSection() -> some View {
|
private func ClimbTypeSection() -> some View {
|
||||||
Section("Additional Information") {
|
Section("Climb Type") {
|
||||||
VStack(alignment: .leading, spacing: 8) {
|
ForEach(ClimbType.allCases, id: \.self) { type in
|
||||||
Text("Notes (Optional)")
|
HStack {
|
||||||
.font(.headline)
|
Text(type.displayName)
|
||||||
|
Spacer()
|
||||||
TextEditor(text: $notes)
|
if selectedClimbType == type {
|
||||||
.frame(minHeight: 80)
|
Image(systemName: "checkmark.circle.fill")
|
||||||
.padding(8)
|
.foregroundColor(themeManager.accentColor)
|
||||||
.background(
|
} else {
|
||||||
RoundedRectangle(cornerRadius: 8)
|
Image(systemName: "circle")
|
||||||
.fill(.quaternary)
|
.foregroundColor(.secondary)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
.contentShape(Rectangle())
|
||||||
|
.onTapGesture {
|
||||||
|
selectedClimbType = type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewBuilder
|
||||||
|
private func DifficultySection() -> some View {
|
||||||
|
Section("Difficulty") {
|
||||||
|
Picker("Difficulty System", selection: $selectedDifficultySystem) {
|
||||||
|
ForEach(DifficultySystem.systemsForClimbType(selectedClimbType), id: \.self) { system in
|
||||||
|
Text(system.displayName).tag(system)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onChange(of: selectedDifficultySystem) { _ in
|
||||||
|
if isLoaded {
|
||||||
|
difficultyGrade = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if selectedDifficultySystem == .custom {
|
||||||
|
HStack {
|
||||||
|
Text("Grade")
|
||||||
|
Spacer()
|
||||||
|
TextField("Numbers only", text: $difficultyGrade)
|
||||||
|
.multilineTextAlignment(.trailing)
|
||||||
|
.keyboardType(.numberPad)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Picker("Grade", selection: $difficultyGrade) {
|
||||||
|
if difficultyGrade.isEmpty {
|
||||||
|
Text("Select Grade").tag("")
|
||||||
|
}
|
||||||
|
ForEach(selectedDifficultySystem.availableGrades, id: \.self) { grade in
|
||||||
|
Text(grade).tag(grade)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.pickerStyle(.menu)
|
||||||
|
}
|
||||||
|
|
||||||
|
if showingGradeError && difficultyGrade.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||||
|
Text("Please select a grade to continue")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.red)
|
||||||
|
.italic()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewBuilder
|
||||||
|
private func LocationDetailsSection() -> some View {
|
||||||
|
Section("Location & Details") {
|
||||||
|
TextField("e.g., 'Cave area', 'Wall 3'", text: $location)
|
||||||
|
|
||||||
|
DatePicker("Date Set", selection: $dateSet, displayedComponents: .date)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewBuilder
|
||||||
|
private func AdditionalInfoSection() -> some View {
|
||||||
|
Section("Tags (Optional)") {
|
||||||
|
TextField("e.g., crimpy, dynamic (comma-separated)", text: $tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
Section("Additional Information") {
|
||||||
|
TextField("Notes (Optional)", text: $notes, axis: .vertical)
|
||||||
|
.lineLimit(3...6)
|
||||||
|
|
||||||
Toggle("Problem is currently active", isOn: $isActive)
|
Toggle("Problem is currently active", isOn: $isActive)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var canSave: Bool {
|
private var canSave: Bool {
|
||||||
selectedGym != nil && difficultyGrade.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == false
|
selectedGym != nil && !difficultyGrade.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupInitialClimbType() {
|
private func setupInitialClimbType() {
|
||||||
@@ -199,7 +329,6 @@ struct AddEditProblemView: View {
|
|||||||
selectedGym = dataManager.gym(withId: gymId)
|
selectedGym = dataManager.gym(withId: gymId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always ensure a gym is selected if available and none is currently selected
|
|
||||||
if selectedGym == nil && !dataManager.gyms.isEmpty {
|
if selectedGym == nil && !dataManager.gyms.isEmpty {
|
||||||
selectedGym = dataManager.gyms.first
|
selectedGym = dataManager.gyms.first
|
||||||
}
|
}
|
||||||
@@ -219,13 +348,17 @@ struct AddEditProblemView: View {
|
|||||||
tags = problem.tags.joined(separator: ", ")
|
tags = problem.tags.joined(separator: ", ")
|
||||||
notes = problem.notes ?? ""
|
notes = problem.notes ?? ""
|
||||||
isActive = problem.isActive
|
isActive = problem.isActive
|
||||||
|
if let date = problem.dateSet {
|
||||||
|
dateSet = date
|
||||||
|
}
|
||||||
imagePaths = problem.imagePaths
|
imagePaths = problem.imagePaths
|
||||||
|
|
||||||
// Load image data for preview
|
|
||||||
imageData = []
|
imageData = []
|
||||||
|
originalImages = []
|
||||||
for imagePath in problem.imagePaths {
|
for imagePath in problem.imagePaths {
|
||||||
if let data = ImageManager.shared.loadImageData(fromPath: imagePath) {
|
if let data = ImageManager.shared.loadImageData(fromPath: imagePath) {
|
||||||
imageData.append(data)
|
imageData.append(data)
|
||||||
|
originalImages.append((path: imagePath, data: data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -242,12 +375,25 @@ struct AddEditProblemView: View {
|
|||||||
$0.trimmingCharacters(in: .whitespacesAndNewlines)
|
$0.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
}.filter { !$0.isEmpty }
|
}.filter { !$0.isEmpty }
|
||||||
|
|
||||||
let tempImagePaths = imagePaths.filter { !$0.isEmpty && !imagePaths.contains($0) }
|
var finalPaths: [String] = []
|
||||||
for imagePath in tempImagePaths {
|
var preservedPaths: Set<String> = []
|
||||||
_ = ImageManager.shared.deleteImage(atPath: imagePath)
|
|
||||||
|
for data in imageData {
|
||||||
|
if let existing = originalImages.first(where: { $0.data == data }) {
|
||||||
|
finalPaths.append(existing.path)
|
||||||
|
preservedPaths.insert(existing.path)
|
||||||
|
} else {
|
||||||
|
if let newPath = ImageManager.shared.saveImageData(data) {
|
||||||
|
finalPaths.append(newPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let newImagePaths = imagePaths.filter { !$0.isEmpty }
|
for original in originalImages {
|
||||||
|
if !preservedPaths.contains(original.path) {
|
||||||
|
_ = ImageManager.shared.deleteImage(atPath: original.path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if isEditing, let problem = existingProblem {
|
if isEditing, let problem = existingProblem {
|
||||||
let updatedProblem = problem.updated(
|
let updatedProblem = problem.updated(
|
||||||
@@ -257,8 +403,9 @@ struct AddEditProblemView: View {
|
|||||||
difficulty: DifficultyGrade(system: selectedDifficultySystem, grade: difficultyGrade),
|
difficulty: DifficultyGrade(system: selectedDifficultySystem, grade: difficultyGrade),
|
||||||
tags: trimmedTags,
|
tags: trimmedTags,
|
||||||
location: trimmedLocation.isEmpty ? nil : trimmedLocation,
|
location: trimmedLocation.isEmpty ? nil : trimmedLocation,
|
||||||
imagePaths: newImagePaths.isEmpty ? [] : newImagePaths,
|
imagePaths: finalPaths,
|
||||||
isActive: isActive,
|
isActive: isActive,
|
||||||
|
dateSet: dateSet,
|
||||||
notes: trimmedNotes.isEmpty ? nil : trimmedNotes
|
notes: trimmedNotes.isEmpty ? nil : trimmedNotes
|
||||||
)
|
)
|
||||||
dataManager.updateProblem(updatedProblem)
|
dataManager.updateProblem(updatedProblem)
|
||||||
@@ -272,8 +419,8 @@ struct AddEditProblemView: View {
|
|||||||
difficulty: DifficultyGrade(system: selectedDifficultySystem, grade: difficultyGrade),
|
difficulty: DifficultyGrade(system: selectedDifficultySystem, grade: difficultyGrade),
|
||||||
tags: trimmedTags,
|
tags: trimmedTags,
|
||||||
location: trimmedLocation.isEmpty ? nil : trimmedLocation,
|
location: trimmedLocation.isEmpty ? nil : trimmedLocation,
|
||||||
imagePaths: newImagePaths.isEmpty ? [] : newImagePaths,
|
imagePaths: finalPaths,
|
||||||
dateSet: Date(),
|
dateSet: dateSet,
|
||||||
notes: trimmedNotes.isEmpty ? nil : trimmedNotes
|
notes: trimmedNotes.isEmpty ? nil : trimmedNotes
|
||||||
)
|
)
|
||||||
dataManager.addProblem(problem)
|
dataManager.addProblem(problem)
|
||||||
|
|||||||
Reference in New Issue
Block a user