|
|
|
|
@ -14,12 +14,15 @@ struct ClubDetailView: View { |
|
|
|
|
@EnvironmentObject var dataStore: DataStore |
|
|
|
|
@FocusState var focusedField: Club.CodingKeys? |
|
|
|
|
@State private var acronymMode: Club.AcronymMode = .automatic |
|
|
|
|
@State private var updateClubData: Bool = false |
|
|
|
|
|
|
|
|
|
init(club: Club, displayContext: DisplayContext = .edition) { |
|
|
|
|
@State private var city: String |
|
|
|
|
@State private var zipCode: String |
|
|
|
|
|
|
|
|
|
init(club: Club, displayContext: DisplayContext) { |
|
|
|
|
_club = Bindable(club) |
|
|
|
|
self.displayContext = displayContext |
|
|
|
|
_acronymMode = State(wrappedValue: club.shortNameMode()) |
|
|
|
|
_city = State(wrappedValue: club.city ?? "") |
|
|
|
|
_zipCode = State(wrappedValue: club.zipCode ?? "") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var body: some View { |
|
|
|
|
@ -87,31 +90,51 @@ struct ClubDetailView: View { |
|
|
|
|
club.acronym = "" |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} footer: { |
|
|
|
|
Text("Vous pouvez personaliser le nom court ou laisser celui généré par défaut. Le nom court est utile au niveau des liens de diffusions.") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if club.code == nil || updateClubData { |
|
|
|
|
Section { |
|
|
|
|
NavigationLink { |
|
|
|
|
ClubSearchView(displayContext: .edition, club: club) |
|
|
|
|
} label: { |
|
|
|
|
Label("Chercher dans la base fédérale", systemImage: "magnifyingglass") |
|
|
|
|
|
|
|
|
|
if club.code == nil { |
|
|
|
|
VStack(alignment: .leading, spacing: 0) { |
|
|
|
|
Text("Ville").foregroundStyle(.secondary).font(.caption) |
|
|
|
|
TextField("Ville", text: $city) |
|
|
|
|
.fixedSize() |
|
|
|
|
.focused($focusedField, equals: ._city) |
|
|
|
|
.submitLabel( displayContext == .addition ? .next : .done) |
|
|
|
|
.onSubmit { |
|
|
|
|
if displayContext == .addition { |
|
|
|
|
focusedField = ._zipCode |
|
|
|
|
} |
|
|
|
|
club.city = city |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} footer: { |
|
|
|
|
if club.code != nil { |
|
|
|
|
HStack { |
|
|
|
|
Spacer() |
|
|
|
|
Button("annuler", role: .cancel) { |
|
|
|
|
updateClubData = false |
|
|
|
|
.onTapGesture { |
|
|
|
|
focusedField = ._city |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
VStack(alignment: .leading, spacing: 0) { |
|
|
|
|
Text("Code Postal").foregroundStyle(.secondary).font(.caption) |
|
|
|
|
TextField("Code Postal", text: $zipCode) |
|
|
|
|
.fixedSize() |
|
|
|
|
.focused($focusedField, equals: ._zipCode) |
|
|
|
|
.submitLabel( displayContext == .addition ? .next : .done) |
|
|
|
|
.onSubmit { |
|
|
|
|
club.zipCode = zipCode |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
Text("Vous pouvez chercher un club dans la base fédérale et importer les informations directement.") |
|
|
|
|
} |
|
|
|
|
.onTapGesture { |
|
|
|
|
focusedField = ._zipCode |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} footer: { |
|
|
|
|
if displayContext == .lockedForEditing { |
|
|
|
|
Text("Édition impossible, vous n'êtes pas le créateur de ce club.").foregroundStyle(.logoRed) |
|
|
|
|
} else { |
|
|
|
|
Text("Vous pouvez personaliser le nom court ou laisser celui généré par défaut.") |
|
|
|
|
} |
|
|
|
|
} else if let federalLink = club.federalLink() { |
|
|
|
|
} |
|
|
|
|
.disabled(displayContext == .lockedForEditing) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let federalLink = club.federalLink() { |
|
|
|
|
Section { |
|
|
|
|
LabeledContent("Code Club") { |
|
|
|
|
Text(club.code ?? "") |
|
|
|
|
@ -119,14 +142,24 @@ struct ClubDetailView: View { |
|
|
|
|
LabeledContent("Ville") { |
|
|
|
|
Text(club.city ?? "") |
|
|
|
|
} |
|
|
|
|
LabeledContent("Code Postal") { |
|
|
|
|
Text(club.zipCode ?? "") |
|
|
|
|
} |
|
|
|
|
Link(destination: federalLink) { |
|
|
|
|
Text("Fiche du club sur tenup") |
|
|
|
|
} |
|
|
|
|
} footer: { |
|
|
|
|
HStack { |
|
|
|
|
Spacer() |
|
|
|
|
Button("modifier", role: .destructive) { |
|
|
|
|
updateClubData = true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if displayContext == .edition { |
|
|
|
|
Section { |
|
|
|
|
RowButtonView("Supprimer ce club", role: .destructive) { |
|
|
|
|
do { |
|
|
|
|
try dataStore.clubs.deleteById(club.id) |
|
|
|
|
dataStore.user?.clubs?.removeAll(where: { $0 == club.id }) |
|
|
|
|
try dataStore.userStorage.update() |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -135,27 +168,36 @@ struct ClubDetailView: View { |
|
|
|
|
.keyboardType(.alphabet) |
|
|
|
|
.autocorrectionDisabled() |
|
|
|
|
.defaultFocus($focusedField, ._name, priority: .automatic) |
|
|
|
|
.navigationTitle(displayContext == .edition ? club.name : "Nouveau club") |
|
|
|
|
.navigationTitle(displayContext == .addition ? "Nouveau club" : club.name) |
|
|
|
|
.navigationBarTitleDisplayMode(.inline) |
|
|
|
|
.toolbar(.visible, for: .navigationBar) |
|
|
|
|
.toolbarBackground(.visible, for: .navigationBar) |
|
|
|
|
.toolbar { |
|
|
|
|
ToolbarItem(placement: .topBarTrailing) { |
|
|
|
|
Button { |
|
|
|
|
do { |
|
|
|
|
dataStore.user?.clubs?.removeAll(where: { $0.id == club.id }) |
|
|
|
|
try dataStore.userStorage.update() |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
if displayContext == .edition || displayContext == .lockedForEditing { |
|
|
|
|
let isFavorite = club.isFavorite() |
|
|
|
|
ToolbarItem(placement: .topBarTrailing) { |
|
|
|
|
BarButtonView("Favori", icon: isFavorite ? "start" : "star.fill") { |
|
|
|
|
do { |
|
|
|
|
if isFavorite { |
|
|
|
|
dataStore.user?.clubs?.removeAll(where: { $0 == club.id }) |
|
|
|
|
} else { |
|
|
|
|
dataStore.user?.clubs?.append(club.id) |
|
|
|
|
} |
|
|
|
|
try dataStore.userStorage.update() |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} label: { |
|
|
|
|
LabelDelete() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.onDisappear { |
|
|
|
|
if displayContext == .edition { |
|
|
|
|
try? dataStore.clubs.addOrUpdate(instance: club) |
|
|
|
|
do { |
|
|
|
|
try dataStore.clubs.addOrUpdate(instance: club) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.onAppear { |
|
|
|
|
@ -169,5 +211,5 @@ struct ClubDetailView: View { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#Preview { |
|
|
|
|
ClubDetailView(club: Club.mock()) |
|
|
|
|
ClubDetailView(club: Club.mock(), displayContext: .edition) |
|
|
|
|
} |
|
|
|
|
|