1.0.2 - Widget and Photos fixes
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import PhotosUI
|
||||
import SwiftUI
|
||||
|
||||
struct AddAttemptView: View {
|
||||
@@ -19,6 +20,8 @@ struct AddAttemptView: View {
|
||||
@State private var newProblemGrade = ""
|
||||
@State private var selectedClimbType: ClimbType = .boulder
|
||||
@State private var selectedDifficultySystem: DifficultySystem = .vScale
|
||||
@State private var selectedPhotos: [PhotosPickerItem] = []
|
||||
@State private var imageData: [Data] = []
|
||||
|
||||
private var activeProblems: [Problem] {
|
||||
dataManager.activeProblems(forGym: gym.id)
|
||||
@@ -126,6 +129,8 @@ struct AddAttemptView: View {
|
||||
|
||||
Button("Back") {
|
||||
showingCreateProblem = false
|
||||
selectedPhotos = []
|
||||
imageData = []
|
||||
}
|
||||
.foregroundColor(.blue)
|
||||
}
|
||||
@@ -209,6 +214,74 @@ struct AddAttemptView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Section("Photos (Optional)") {
|
||||
PhotosPicker(
|
||||
selection: $selectedPhotos,
|
||||
maxSelectionCount: 5,
|
||||
matching: .images
|
||||
) {
|
||||
HStack {
|
||||
Image(systemName: "camera.fill")
|
||||
.foregroundColor(.blue)
|
||||
.font(.title2)
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text("Add Photos")
|
||||
.font(.headline)
|
||||
.foregroundColor(.blue)
|
||||
Text("\(imageData.count) of 5 photos added")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
Spacer()
|
||||
Image(systemName: "chevron.right")
|
||||
.foregroundColor(.secondary)
|
||||
.font(.caption)
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
.onChange(of: selectedPhotos) { _, _ in
|
||||
Task {
|
||||
await loadSelectedPhotos()
|
||||
}
|
||||
}
|
||||
|
||||
if !imageData.isEmpty {
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
HStack(spacing: 12) {
|
||||
ForEach(imageData.indices, id: \.self) { index in
|
||||
if let uiImage = UIImage(data: imageData[index]) {
|
||||
Image(uiImage: uiImage)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fill)
|
||||
.frame(width: 80, height: 80)
|
||||
.clipped()
|
||||
.cornerRadius(8)
|
||||
.overlay(alignment: .topTrailing) {
|
||||
Button(action: {
|
||||
imageData.remove(at: index)
|
||||
}) {
|
||||
Image(systemName: "xmark.circle.fill")
|
||||
.foregroundColor(.red)
|
||||
.background(Circle().fill(.white))
|
||||
}
|
||||
.offset(x: 8, y: -8)
|
||||
}
|
||||
} else {
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.fill(.gray.opacity(0.3))
|
||||
.frame(width: 80, height: 80)
|
||||
.overlay {
|
||||
Image(systemName: "photo")
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
@@ -310,11 +383,20 @@ struct AddAttemptView: View {
|
||||
let difficulty = DifficultyGrade(
|
||||
system: selectedDifficultySystem, grade: newProblemGrade)
|
||||
|
||||
// Save images and get paths
|
||||
var imagePaths: [String] = []
|
||||
for data in imageData {
|
||||
if let relativePath = ImageManager.shared.saveImageData(data) {
|
||||
imagePaths.append(relativePath)
|
||||
}
|
||||
}
|
||||
|
||||
let newProblem = Problem(
|
||||
gymId: gym.id,
|
||||
name: newProblemName.isEmpty ? nil : newProblemName,
|
||||
climbType: selectedClimbType,
|
||||
difficulty: difficulty
|
||||
difficulty: difficulty,
|
||||
imagePaths: imagePaths
|
||||
)
|
||||
|
||||
dataManager.addProblem(newProblem)
|
||||
@@ -347,8 +429,26 @@ struct AddAttemptView: View {
|
||||
dataManager.addAttempt(attempt)
|
||||
}
|
||||
|
||||
// Clear photo states after saving
|
||||
selectedPhotos = []
|
||||
imageData = []
|
||||
|
||||
dismiss()
|
||||
}
|
||||
|
||||
private func loadSelectedPhotos() async {
|
||||
var newImageData: [Data] = []
|
||||
|
||||
for item in selectedPhotos {
|
||||
if let data = try? await item.loadTransferable(type: Data.self) {
|
||||
newImageData.append(data)
|
||||
}
|
||||
}
|
||||
|
||||
await MainActor.run {
|
||||
imageData = newImageData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ProblemSelectionRow: View {
|
||||
@@ -599,6 +699,8 @@ struct EditAttemptView: View {
|
||||
@State private var newProblemGrade = ""
|
||||
@State private var selectedClimbType: ClimbType = .boulder
|
||||
@State private var selectedDifficultySystem: DifficultySystem = .vScale
|
||||
@State private var selectedPhotos: [PhotosPickerItem] = []
|
||||
@State private var imageData: [Data] = []
|
||||
|
||||
private var availableProblems: [Problem] {
|
||||
guard let session = dataManager.session(withId: attempt.sessionId) else {
|
||||
@@ -727,6 +829,8 @@ struct EditAttemptView: View {
|
||||
|
||||
Button("Back") {
|
||||
showingCreateProblem = false
|
||||
selectedPhotos = []
|
||||
imageData = []
|
||||
}
|
||||
.foregroundColor(.blue)
|
||||
}
|
||||
@@ -810,6 +914,74 @@ struct EditAttemptView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Section("Photos (Optional)") {
|
||||
PhotosPicker(
|
||||
selection: $selectedPhotos,
|
||||
maxSelectionCount: 5,
|
||||
matching: .images
|
||||
) {
|
||||
HStack {
|
||||
Image(systemName: "camera.fill")
|
||||
.foregroundColor(.blue)
|
||||
.font(.title2)
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text("Add Photos")
|
||||
.font(.headline)
|
||||
.foregroundColor(.blue)
|
||||
Text("\(imageData.count) of 5 photos added")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
Spacer()
|
||||
Image(systemName: "chevron.right")
|
||||
.foregroundColor(.secondary)
|
||||
.font(.caption)
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
.onChange(of: selectedPhotos) { _, _ in
|
||||
Task {
|
||||
await loadSelectedPhotos()
|
||||
}
|
||||
}
|
||||
|
||||
if !imageData.isEmpty {
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
HStack(spacing: 12) {
|
||||
ForEach(imageData.indices, id: \.self) { index in
|
||||
if let uiImage = UIImage(data: imageData[index]) {
|
||||
Image(uiImage: uiImage)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fill)
|
||||
.frame(width: 80, height: 80)
|
||||
.clipped()
|
||||
.cornerRadius(8)
|
||||
.overlay(alignment: .topTrailing) {
|
||||
Button(action: {
|
||||
imageData.remove(at: index)
|
||||
}) {
|
||||
Image(systemName: "xmark.circle.fill")
|
||||
.foregroundColor(.red)
|
||||
.background(Circle().fill(.white))
|
||||
}
|
||||
.offset(x: 8, y: -8)
|
||||
}
|
||||
} else {
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.fill(.gray.opacity(0.3))
|
||||
.frame(width: 80, height: 80)
|
||||
.overlay {
|
||||
Image(systemName: "photo")
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
@@ -915,11 +1087,20 @@ struct EditAttemptView: View {
|
||||
let difficulty = DifficultyGrade(
|
||||
system: selectedDifficultySystem, grade: newProblemGrade)
|
||||
|
||||
// Save images and get paths
|
||||
var imagePaths: [String] = []
|
||||
for data in imageData {
|
||||
if let relativePath = ImageManager.shared.saveImageData(data) {
|
||||
imagePaths.append(relativePath)
|
||||
}
|
||||
}
|
||||
|
||||
let newProblem = Problem(
|
||||
gymId: gym.id,
|
||||
name: newProblemName.isEmpty ? nil : newProblemName,
|
||||
climbType: selectedClimbType,
|
||||
difficulty: difficulty
|
||||
difficulty: difficulty,
|
||||
imagePaths: imagePaths
|
||||
)
|
||||
|
||||
dataManager.addProblem(newProblem)
|
||||
@@ -949,8 +1130,26 @@ struct EditAttemptView: View {
|
||||
dataManager.updateAttempt(updatedAttempt)
|
||||
}
|
||||
|
||||
// Clear photo states after saving
|
||||
selectedPhotos = []
|
||||
imageData = []
|
||||
|
||||
dismiss()
|
||||
}
|
||||
|
||||
private func loadSelectedPhotos() async {
|
||||
var newImageData: [Data] = []
|
||||
|
||||
for item in selectedPhotos {
|
||||
if let data = try? await item.loadTransferable(type: Data.self) {
|
||||
newImageData.append(data)
|
||||
}
|
||||
}
|
||||
|
||||
await MainActor.run {
|
||||
imageData = newImageData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
@@ -997,6 +1196,7 @@ struct ProblemSelectionImageView: View {
|
||||
ProgressView()
|
||||
.scaleEffect(0.8)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
import PhotosUI
|
||||
import SwiftUI
|
||||
|
||||
@@ -61,11 +60,11 @@ struct AddEditProblemView: View {
|
||||
Form {
|
||||
GymSelectionSection()
|
||||
BasicInfoSection()
|
||||
PhotosSection()
|
||||
ClimbTypeSection()
|
||||
DifficultySection()
|
||||
LocationAndSetterSection()
|
||||
TagsSection()
|
||||
PhotosSection()
|
||||
AdditionalInfoSection()
|
||||
}
|
||||
.navigationTitle(isEditing ? "Edit Problem" : "Add Problem")
|
||||
@@ -304,18 +303,30 @@ struct AddEditProblemView: View {
|
||||
|
||||
@ViewBuilder
|
||||
private func PhotosSection() -> some View {
|
||||
Section("Photos") {
|
||||
Section("Photos (Optional)") {
|
||||
PhotosPicker(
|
||||
selection: $selectedPhotos,
|
||||
maxSelectionCount: 5,
|
||||
matching: .images
|
||||
) {
|
||||
HStack {
|
||||
Image(systemName: "photo.on.rectangle.angled")
|
||||
Image(systemName: "camera.fill")
|
||||
.foregroundColor(.blue)
|
||||
Text("Add Photos (\(imageData.count)/5)")
|
||||
.font(.title2)
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text("Add Photos")
|
||||
.font(.headline)
|
||||
.foregroundColor(.blue)
|
||||
Text("\(imageData.count) of 5 photos added")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
Spacer()
|
||||
Image(systemName: "chevron.right")
|
||||
.foregroundColor(.secondary)
|
||||
.font(.caption)
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
|
||||
if !imageData.isEmpty {
|
||||
|
||||
Reference in New Issue
Block a user