parent
13e09d2163
commit
5f4c995fb8
@ -0,0 +1,188 @@ |
|||||||
|
// |
||||||
|
// CallMessageCustomizationView.swift |
||||||
|
// Padel Tournament |
||||||
|
// |
||||||
|
// Created by Razmig Sarkissian on 02/11/2023. |
||||||
|
// |
||||||
|
|
||||||
|
import SwiftUI |
||||||
|
|
||||||
|
struct CallMessageCustomizationView: View { |
||||||
|
@EnvironmentObject var dataStore: DataStore |
||||||
|
var tournament: Tournament |
||||||
|
var user: User |
||||||
|
|
||||||
|
@FocusState private var textEditor: Bool |
||||||
|
@State private var customClubName: String = "" |
||||||
|
@State private var customCallMessageBody: String = "" |
||||||
|
@State private var customCallMessageSignature: String = "" |
||||||
|
|
||||||
|
init(tournament: Tournament, user: User) { |
||||||
|
self.tournament = tournament |
||||||
|
self.user = user |
||||||
|
_customCallMessageBody = State(wrappedValue: user.callMessageBody ?? "") |
||||||
|
_customCallMessageSignature = State(wrappedValue: user.callMessageSignature ?? "") |
||||||
|
_customClubName = State(wrappedValue: tournament.clubName ?? "") |
||||||
|
} |
||||||
|
|
||||||
|
var clubName: String { |
||||||
|
customClubName |
||||||
|
} |
||||||
|
|
||||||
|
var formatMessage: String? { |
||||||
|
user.callDisplayFormat ? tournament.matchFormat.computedLongLabel + "." : nil |
||||||
|
} |
||||||
|
|
||||||
|
var entryFeeMessage: String? { |
||||||
|
user.callDisplayEntryFee ? tournament.entryFeeMessage : nil |
||||||
|
} |
||||||
|
|
||||||
|
var computedMessage: String { |
||||||
|
[formatMessage, entryFeeMessage, customCallMessageBody].compacted().map { $0.trimmed }.joined(separator: "\n") |
||||||
|
} |
||||||
|
|
||||||
|
var finalMessage: String? { |
||||||
|
let localizedCalled = "convoqué" + (tournament.tournamentCategory == .women ? "e" : "") + "s" |
||||||
|
return "Bonjour,\n\nVous êtes \(localizedCalled) pour jouer en \(RoundRule.roundName(fromRoundIndex: 2).lowercased()) du \(tournament.tournamentTitle()) au \(clubName) le \(Date().formatted(Date.FormatStyle().weekday(.wide).day().month(.wide))) à \(Date().formatted(Date.FormatStyle().hour().minute())).\n\n" + computedMessage + "\n\n\(customCallMessageSignature)" |
||||||
|
} |
||||||
|
|
||||||
|
var body: some View { |
||||||
|
@Bindable var user = user |
||||||
|
List { |
||||||
|
Section { |
||||||
|
ZStack { |
||||||
|
Text(customCallMessageBody).hidden() |
||||||
|
.padding(.vertical, 20) |
||||||
|
TextEditor(text: $customCallMessageBody) |
||||||
|
.autocorrectionDisabled() |
||||||
|
.focused($textEditor) |
||||||
|
} |
||||||
|
} header: { |
||||||
|
Text("Personnalisation du message de convocation") |
||||||
|
} |
||||||
|
|
||||||
|
Section { |
||||||
|
ZStack { |
||||||
|
Text(customCallMessageSignature).hidden() |
||||||
|
TextEditor(text: $customCallMessageSignature) |
||||||
|
.autocorrectionDisabled() |
||||||
|
.focused($textEditor) |
||||||
|
} |
||||||
|
} header: { |
||||||
|
Text("Signature du message") |
||||||
|
} |
||||||
|
|
||||||
|
Section { |
||||||
|
TextField("Nom du club", text: $customClubName) |
||||||
|
.autocorrectionDisabled() |
||||||
|
.onSubmit { |
||||||
|
if let eventClub = tournament.eventObject?.clubObject { |
||||||
|
eventClub.name = customClubName |
||||||
|
try? dataStore.clubs.addOrUpdate(instance: eventClub) |
||||||
|
} |
||||||
|
} |
||||||
|
} header: { |
||||||
|
Text("Nom du club") |
||||||
|
} |
||||||
|
|
||||||
|
Section { |
||||||
|
if user.callUseFullCustomMessage { |
||||||
|
Text(self.computedFullCustomMessage()) |
||||||
|
.contextMenu { |
||||||
|
Button("Coller dans le presse-papier") { |
||||||
|
UIPasteboard.general.string = self.computedFullCustomMessage() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
else if let finalMessage { |
||||||
|
Text(finalMessage) |
||||||
|
.contextMenu { |
||||||
|
Button("Coller dans le presse-papier") { |
||||||
|
UIPasteboard.general.string = finalMessage |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} header: { |
||||||
|
Text("Exemple") |
||||||
|
} |
||||||
|
|
||||||
|
Section { |
||||||
|
LabeledContent { |
||||||
|
Toggle(isOn: $user.callUseFullCustomMessage) { |
||||||
|
|
||||||
|
} |
||||||
|
} label: { |
||||||
|
Text("contrôle complet du message") |
||||||
|
} |
||||||
|
} header: { |
||||||
|
Text("Personnalisation complète") |
||||||
|
} footer: { |
||||||
|
Text("Utilisez ces balises dans votre texte : #titre, #jour, #horaire, #club, #signature") |
||||||
|
} |
||||||
|
} |
||||||
|
.navigationTitle("Message de convocation") |
||||||
|
.toolbar { |
||||||
|
ToolbarItem(placement: .topBarTrailing) { |
||||||
|
Menu { |
||||||
|
Picker(selection: $user.callDisplayFormat) { |
||||||
|
Text("Afficher le format").tag(true) |
||||||
|
Text("Masquer le format").tag(false) |
||||||
|
} label: { |
||||||
|
|
||||||
|
} |
||||||
|
Picker(selection: $user.callDisplayEntryFee) { |
||||||
|
Text("Afficher le prix d'inscription").tag(true) |
||||||
|
Text("Masquer le prix d'inscription").tag(false) |
||||||
|
} label: { |
||||||
|
|
||||||
|
} |
||||||
|
} label: { |
||||||
|
LabelOptions() |
||||||
|
} |
||||||
|
} |
||||||
|
ToolbarItemGroup(placement: .keyboard) { |
||||||
|
if textEditor { |
||||||
|
Spacer() |
||||||
|
Button { |
||||||
|
textEditor = false |
||||||
|
} label: { |
||||||
|
Label("Fermer", systemImage: "xmark") |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
.onChange(of: user.callUseFullCustomMessage) { |
||||||
|
if user.callUseFullCustomMessage == false { |
||||||
|
user.callMessageBody = ContactType.defaultCustomMessage |
||||||
|
} |
||||||
|
_save() |
||||||
|
} |
||||||
|
.onChange(of: customCallMessageBody) { |
||||||
|
user.callMessageBody = customCallMessageBody |
||||||
|
_save() |
||||||
|
} |
||||||
|
.onChange(of: customCallMessageSignature) { |
||||||
|
user.callMessageSignature = customCallMessageSignature |
||||||
|
_save() |
||||||
|
} |
||||||
|
.onChange(of: user.callDisplayEntryFee) { |
||||||
|
_save() |
||||||
|
} |
||||||
|
.onChange(of: user.callDisplayFormat) { |
||||||
|
_save() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private func _save() { |
||||||
|
try? dataStore.setUser(user) |
||||||
|
} |
||||||
|
|
||||||
|
func computedFullCustomMessage() -> String { |
||||||
|
var text = customCallMessageBody.replacingOccurrences(of: "#titre", with: tournament.tournamentTitle()) |
||||||
|
text = text.replacingOccurrences(of: "#club", with: clubName) |
||||||
|
text = text.replacingOccurrences(of: "#jour", with: "\(Date().formatted(Date.FormatStyle().weekday(.wide).day().month(.wide)))") |
||||||
|
text = text.replacingOccurrences(of: "#horaire", with: "\(Date().formatted(Date.FormatStyle().hour().minute()))") |
||||||
|
text = text.replacingOccurrences(of: "#signature", with: customCallMessageSignature) |
||||||
|
return text |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue