@ -10,14 +10,28 @@ import LeStorage
struct EditingTeamView : View {
struct EditingTeamView : View {
@ EnvironmentObject var dataStore : DataStore
@ EnvironmentObject var dataStore : DataStore
@ EnvironmentObject var networkMonitor : NetworkMonitor
@ Environment ( Tournament . self ) var tournament : Tournament
var team : TeamRegistration
var team : TeamRegistration
@ State private var editedTeam : TeamRegistration ?
@ State private var contactType : ContactType ? = nil
@ State private var sentError : ContactManagerError ? = nil
@ State private var showSubscriptionView : Bool = false
@ State private var registrationDate : Date
@ State private var registrationDate : Date
@ State private var callDate : Date
@ State private var callDate : Date
@ State private var name : String
@ State private var name : String
@ Environment ( Tournament . self ) var tournament : Tournament
var messageSentFailed : Binding < Bool > {
Binding {
sentError != nil
} set : { newValue in
if newValue = = false {
sentError = nil
}
}
}
var tournamentStore : TournamentStore {
var tournamentStore : TournamentStore {
return self . tournament . tournamentStore
return self . tournament . tournamentStore
}
}
@ -32,52 +46,111 @@ struct EditingTeamView: View {
var body : some View {
var body : some View {
List {
List {
Section {
Section {
TextField ( " Nom de l'équipe " , text : $ name )
RowButtonView ( " Modifier la composition de l'équipe " ) {
. autocorrectionDisabled ( )
editedTeam = team
. keyboardType ( . alphabet )
}
. frame ( maxWidth : . infinity )
. submitLabel ( . done )
. onSubmit ( of : . text ) {
let trimmed = name . trimmed
if trimmed . isEmpty {
team . name = nil
} else {
team . name = trimmed
}
_save ( )
}
TeamDetailView ( team : team )
} footer : {
HStack {
CopyPasteButtonView ( pasteValue : team . playersPasteData ( ) )
Spacer ( )
NavigationLink {
GroupStageTeamReplacementView ( team : team )
. environment ( tournament )
} label : {
Text ( " Chercher à remplacer " )
}
}
}
}
Section {
Section {
DatePicker ( registrationDate . localizedWeekDay ( ) , selection : $ registrationDate )
DatePicker ( selection : $ registrationDate ) {
} header : {
Text ( " Date d'inscription " )
Text ( " Date d'inscription " )
Text ( registrationDate . localizedWeekDay ( ) )
}
}
Section {
if let callDate = team . callDate {
if let callDate = team . callDate {
LabeledContent ( ) {
LabeledContent ( ) {
Text ( callDate . localizedDate ( ) )
Text ( callDate . localizedDate ( ) )
} label : {
} label : {
Text ( " OK " )
Text ( " Convocation " )
}
}
} else {
} else {
Text ( " Cette équipe n'a pas été convoquée " )
Text ( " Cette équipe n'a pas été convoquée " )
}
}
} header : {
Text ( " Statut de la convocation " )
}
}
Section {
Section {
Toggle ( isOn : hasArrived ) {
Toggle ( isOn : hasArrived ) {
Text ( " Équipe sur place " )
Text ( " Équipe sur place " )
}
}
/*
Toggle ( isOn : $ team . confirmedCall ) {
Toggle ( isOn : . init ( get : {
Text ( " Équipe sur place " )
return team . wildCardBracket
} , set : { value in
team . resetPositions ( )
team . wildCardGroupStage = false
team . walkOut = false
team . wildCardBracket = value
do {
try tournamentStore . teamRegistrations . addOrUpdate ( instance : team )
} catch {
Logger . error ( error )
}
} ) ) {
Text ( " Wildcard Tableau " )
}
Toggle ( isOn : . init ( get : {
return team . wildCardGroupStage
} , set : { value in
team . resetPositions ( )
team . wildCardBracket = false
team . walkOut = false
team . wildCardGroupStage = value
do {
try tournamentStore . teamRegistrations . addOrUpdate ( instance : team )
} catch {
Logger . error ( error )
}
} ) ) {
Text ( " Wildcard Poule " )
}
Toggle ( isOn : . init ( get : {
return team . walkOut
} , set : { value in
team . resetPositions ( )
team . wildCardBracket = false
team . wildCardGroupStage = false
team . walkOut = value
do {
try tournamentStore . teamRegistrations . addOrUpdate ( instance : team )
} catch {
Logger . error ( error )
}
} ) ) {
Text ( " Forfait " )
}
}
*/
}
Section {
TextField ( " Nom de l'équipe " , text : $ name )
. autocorrectionDisabled ( )
. keyboardType ( . alphabet )
. frame ( maxWidth : . infinity )
. submitLabel ( . done )
. onSubmit ( of : . text ) {
let trimmed = name . trimmed
if trimmed . isEmpty {
team . name = nil
} else {
team . name = trimmed
}
_save ( )
}
}
}
Section {
Section {
@ -95,15 +168,101 @@ struct EditingTeamView: View {
}
}
. disabled ( team . inRound ( ) = = false )
. disabled ( team . inRound ( ) = = false )
}
}
Section {
RowButtonView ( " Effacer l'équipe " , role : . destructive , systemImage : " trash " ) {
team . deleteTeamScores ( )
do {
try tournamentStore . teamRegistrations . delete ( instance : team )
} catch {
Logger . error ( error )
}
}
}
}
. alert ( " Un problème est survenu " , isPresented : messageSentFailed ) {
Button ( " OK " ) {
}
} message : {
Text ( _getErrorMessage ( ) )
}
. sheet ( item : $ contactType ) { contactType in
Group {
switch contactType {
case . message ( _ , let recipients , let body , _ ) :
if Guard . main . paymentForNewTournament ( ) != nil {
MessageComposeView ( recipients : recipients , body : body ) { result in
switch result {
case . cancelled :
break
case . failed :
self . sentError = . messageFailed
case . sent :
if networkMonitor . connected = = false {
self . sentError = . messageNotSent
}
@ unknown default :
break
}
}
} else {
SubscriptionView ( isPresented : self . $ showSubscriptionView , showLackOfPlanMessage : true )
. environment ( \ . colorScheme , . light )
}
case . mail ( _ , let recipients , let bccRecipients , let body , let subject , _ ) :
if Guard . main . paymentForNewTournament ( ) != nil {
MailComposeView ( recipients : recipients , bccRecipients : bccRecipients , body : body , subject : subject ) { result in
switch result {
case . cancelled , . saved :
self . contactType = nil
case . failed :
self . contactType = nil
self . sentError = . mailFailed
case . sent :
if networkMonitor . connected = = false {
self . contactType = nil
self . sentError = . mailNotSent
}
@ unknown default :
break
}
}
} else {
SubscriptionView ( isPresented : self . $ showSubscriptionView , showLackOfPlanMessage : true )
. environment ( \ . colorScheme , . light )
}
}
}
. tint ( . master )
}
. sheet ( item : $ editedTeam ) { editedTeam in
NavigationStack {
AddTeamView ( tournament : tournament , editedTeam : editedTeam )
}
. tint ( . master )
}
. toolbar {
ToolbarItem ( placement : . topBarTrailing ) {
MenuWarningView ( tournament : tournament , teams : [ team ] , contactType : $ contactType )
}
}
}
. onChange ( of : registrationDate ) {
. onChange ( of : registrationDate ) {
team . registrationDate = registrationDate
team . registrationDate = registrationDate
_save ( )
_save ( )
}
}
. headerProminence ( . increased )
. navigationTitle ( " Statut de l'équipe " )
. navigationBarTitleDisplayMode ( . inline )
. toolbarBackground ( . visible , for : . navigationBar )
. toolbarBackground ( . visible , for : . navigationBar )
. navigationTitle ( " Édition de l'équipe " )
. navigationBarTitleDisplayMode ( . inline )
}
private func _getErrorMessage ( ) -> String {
let m1 : String ? = ( networkMonitor . connected = = false ? " L'appareil n'est pas connecté à internet. " : nil )
let m2 : String ? = ( sentError = = . mailNotSent ? " Le mail est dans la boîte d'envoi de l'app Mail. Vérifiez son état dans l'app Mail avant d'essayer de le renvoyer. " : nil )
let m3 : String ? = ( ( sentError = = . messageFailed || sentError = = . messageNotSent ) ? " Le SMS n'a pas été envoyé " : nil )
let m4 : String ? = ( sentError = = . mailFailed ? " Le mail n'a pas été envoyé " : nil )
let message : String = [ m1 , m2 , m3 , m4 ] . compacted ( ) . joined ( separator : " \n " )
return message
}
}
private var hasArrived : Binding < Bool > {
private var hasArrived : Binding < Bool > {