From c089e05c1a736e9fa5d27db56c5a911822ad600f Mon Sep 17 00:00:00 2001 From: Raz Date: Wed, 2 Oct 2024 11:58:44 +0200 Subject: [PATCH] fix courtIndex not save --- PadelClub/Data/Match.swift | 11 +++++++++++ PadelClub/Views/Match/MatchDetailView.swift | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/PadelClub/Data/Match.swift b/PadelClub/Data/Match.swift index 8b109da..1e5f5e0 100644 --- a/PadelClub/Data/Match.swift +++ b/PadelClub/Data/Match.swift @@ -993,5 +993,16 @@ enum MatchFieldSetup: Hashable, Identifiable { // case firstAvailable case field(Int) + var courtIndex: Int? { + switch self { + case .random: + return nil + case .fullRandom: + return nil + case .field(let int): + return int + } + } + var id: Int { hashValue } } diff --git a/PadelClub/Views/Match/MatchDetailView.swift b/PadelClub/Views/Match/MatchDetailView.swift index f8766d1..0e61b10 100644 --- a/PadelClub/Views/Match/MatchDetailView.swift +++ b/PadelClub/Views/Match/MatchDetailView.swift @@ -478,6 +478,14 @@ struct MatchDetailView: View { } label: { Text("Terrain") } + .onChange(of: fieldSetup) { + if let courtIndex = fieldSetup.courtIndex { + match.setCourt(courtIndex) + } else { + match.removeCourt() + } + _saveMatch() + } RowButtonView("Valider") { match.validateMatch(fromStartDate: startDateSetup == .now ? Date() : startDate, toEndDate: endDate, fieldSetup: fieldSetup) @@ -540,6 +548,10 @@ struct MatchDetailView: View { } } + _saveMatch() + } + + private func _saveMatch() { do { try tournamentStore.matches.addOrUpdate(instance: match) } catch {