@ -12,6 +12,7 @@ struct FollowUpMatchView: View {
@ Environment ( \ . dismiss ) private var dismiss
@ Environment ( \ . dismiss ) private var dismiss
let match : Match
let match : Match
let readyMatches : [ Match ]
let readyMatches : [ Match ]
let matchesLeft : [ Match ]
let isFree : Bool
let isFree : Bool
@ State private var sortingMode : SortingMode = . index
@ State private var sortingMode : SortingMode = . index
@ -21,6 +22,8 @@ struct FollowUpMatchView: View {
enum SortingMode : Int , Identifiable , CaseIterable {
enum SortingMode : Int , Identifiable , CaseIterable {
var id : Int { self . rawValue }
var id : Int { self . rawValue }
case winner
case loser
case index
case index
case restingTime
case restingTime
case court
case court
@ -32,7 +35,11 @@ struct FollowUpMatchView: View {
case . court :
case . court :
return " Terrain "
return " Terrain "
case . restingTime :
case . restingTime :
return " Temps de repos "
return " Repos "
case . winner :
return " Gagnant "
case . loser :
return " Perdant "
}
}
}
}
}
}
@ -43,12 +50,44 @@ struct FollowUpMatchView: View {
_selectedCourt = . init ( wrappedValue : match . courtIndex )
_selectedCourt = . init ( wrappedValue : match . courtIndex )
let currentTournament = match . currentTournament ( )
let currentTournament = match . currentTournament ( )
let allMatches = currentTournament ? . allMatches ( ) ? ? [ ]
let allMatches = currentTournament ? . allMatches ( ) ? ? [ ]
self . matchesLeft = currentTournament ? . matchesLeft ( allMatches ) ? ? [ ]
let runningMatches = currentTournament ? . runningMatches ( allMatches ) ? ? [ ]
let runningMatches = currentTournament ? . runningMatches ( allMatches ) ? ? [ ]
let readyMatches = currentTournament ? . readyMatches ( allMatches ) ? ? [ ]
let readyMatches = currentTournament ? . readyMatches ( allMatches ) ? ? [ ]
self . readyMatches = currentTournament ? . availableToStart ( readyMatches , in : runningMatches , checkCanPlay : false ) ? ? [ ]
self . readyMatches = currentTournament ? . availableToStart ( readyMatches , in : runningMatches , checkCanPlay : false ) ? ? [ ]
self . isFree = currentTournament ? . isFree ( ) ? ? true
self . isFree = currentTournament ? . isFree ( ) ? ? true
}
}
var winningTeam : TeamRegistration ? {
match . winner ( )
}
var losingTeam : TeamRegistration ? {
match . loser ( )
}
func contentUnavailableDescriptionLabel ( ) -> String {
switch sortingMode {
case . winner :
if let winningTeam {
return " Aucun match à suivre pour \( winningTeam . teamLabel ( ) ) "
} else {
return " La paire gagnante n'a pas été décidé "
}
case . loser :
if let losingTeam {
return " Aucun match à suivre pour \( losingTeam . teamLabel ( ) ) "
} else {
return " La paire perdante n'a pas été décidé "
}
case . index :
return " Ce tournoi n'a aucun match prêt à démarrer "
case . restingTime :
return " Ce tournoi n'a aucun match prêt à démarrer "
case . court :
return " Ce tournoi n'a aucun match prêt à démarrer "
}
}
var sortedMatches : [ Match ] {
var sortedMatches : [ Match ] {
switch sortingMode {
switch sortingMode {
case . index :
case . index :
@ -57,6 +96,18 @@ struct FollowUpMatchView: View {
return readyMatches . sorted ( by : \ . restingTimeForSorting )
return readyMatches . sorted ( by : \ . restingTimeForSorting )
case . court :
case . court :
return readyMatches . sorted ( using : [ . keyPath ( \ . courtIndexForSorting ) , . keyPath ( \ . restingTimeForSorting ) ] , order : . ascending )
return readyMatches . sorted ( using : [ . keyPath ( \ . courtIndexForSorting ) , . keyPath ( \ . restingTimeForSorting ) ] , order : . ascending )
case . winner :
if let winningTeam , let followUpMatch = matchesLeft . first ( where : { $0 . containsTeamId ( winningTeam . id ) } ) {
return [ followUpMatch ]
} else {
return [ ]
}
case . loser :
if let losingTeam , let followUpMatch = matchesLeft . first ( where : { $0 . containsTeamId ( losingTeam . id ) } ) {
return [ followUpMatch ]
} else {
return [ ]
}
}
}
}
}
@ -95,8 +146,12 @@ struct FollowUpMatchView: View {
}
}
Section {
Section {
ForEach ( sortedMatches ) { match in
if sortedMatches . isEmpty = = false {
MatchRowView ( match : match , matchViewStyle : . followUpStyle , updatedField : selectedCourt )
ForEach ( sortedMatches ) { match in
MatchRowView ( match : match , matchViewStyle : . followUpStyle , updatedField : selectedCourt )
}
} else {
ContentUnavailableView ( " Aucun match à venir " , systemImage : " xmark.circle " , description : Text ( contentUnavailableDescriptionLabel ( ) ) )
}
}
} header : {
} header : {
Picker ( selection : $ sortingMode ) {
Picker ( selection : $ sortingMode ) {
@ -112,7 +167,6 @@ struct FollowUpMatchView: View {
}
}
. headerProminence ( . increased )
. headerProminence ( . increased )
. textCase ( nil )
. textCase ( nil )
}
}
. toolbarBackground ( . visible , for : . navigationBar )
. toolbarBackground ( . visible , for : . navigationBar )
. navigationTitle ( " Match à suivre " )
. navigationTitle ( " Match à suivre " )
@ -120,6 +174,9 @@ struct FollowUpMatchView: View {
. toolbar {
. toolbar {
ToolbarItem ( placement : . topBarLeading ) {
ToolbarItem ( placement : . topBarLeading ) {
Button ( " Retour " , role : . cancel ) {
Button ( " Retour " , role : . cancel ) {
if readyMatches . isEmpty && matchesLeft . isEmpty {
dismissWhenPresentFollowUpMatchIsDismissed = true
}
dismiss ( )
dismiss ( )
}
}
}
}