You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
PadelClub/PadelClub/Views/Calling/CallView.swift

174 lines
6.7 KiB

//
// CallView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 16/04/2024.
//
import SwiftUI
struct CallView: View {
struct CallStatusView: View {
let count: Int
let total: Int
let startDate: Date?
var body: some View {
VStack(spacing: 0) {
HStack {
if let startDate {
Text(startDate.formatted(.dateTime.hour().minute()))
} else {
Text("Aucun horaire")
}
Spacer()
Text(count.formatted() + "/" + total.formatted())
}
.font(.largeTitle)
HStack {
if let startDate {
Text(startDate.formatted(.dateTime.weekday().day(.twoDigits).month().year()))
}
Spacer()
Text("paires convoquées")
}
.font(.caption)
.foregroundColor(.secondary)
}
}
}
struct TeamView: View {
let team: TeamRegistration
var body: some View {
TeamRowView(team: team, displayCallDate: true)
}
}
@EnvironmentObject var dataStore: DataStore
@EnvironmentObject var networkMonitor: NetworkMonitor
@Environment(Tournament.self) var tournament: Tournament
var teams: [TeamRegistration]
let callDate: Date
let matchFormat: MatchFormat
let roundLabel: String
@State private var contactType: ContactType? = nil
@State private var sentError: ContactManagerError? = nil
var messageSentFailed: Binding<Bool> {
Binding {
sentError != nil
} set: { newValue in
if newValue == false {
sentError = nil
}
}
}
private func _called(_ success: Bool) {
if success {
teams.forEach { team in
team.callDate = callDate
}
try? dataStore.teamRegistrations.addOrUpdate(contentOfs: teams)
}
}
var finalMessage: String {
ContactType.callingGroupStageMessage(tournament: tournament, startDate: callDate, roundLabel: roundLabel, matchFormat: matchFormat)
}
var body: some View {
let callWord = teams.allSatisfy({ $0.called() }) ? "Reconvoquer" : "Convoquer"
HStack(spacing: 0.0) {
if teams.count == 1 {
if let previousCallDate = teams.first?.callDate, Calendar.current.compare(previousCallDate, to: callDate, toGranularity: .minute) != .orderedSame {
Text("Reconvoquer " + callDate.localizedDate() + " par")
} else {
Text(callWord + " cette paire par")
}
} else {
Text(callWord + " ces \(teams.count) paires par")
}
Button {
contactType = .message(date: callDate, recipients: teams.flatMap { $0.getPhoneNumbers() }, body: finalMessage, tournamentBuild: nil)
} label: {
Text("sms")
.underline()
}
Text("ou")
Button {
contactType = .mail(date: callDate, recipients: tournament.umpireMail(), bccRecipients: teams.flatMap { $0.getMail() }, body: finalMessage, subject: tournament.tournamentTitle(), tournamentBuild: nil)
} label: {
Text("mail")
.underline()
}
}
.font(.subheadline)
.buttonStyle(.borderless)
.alert("Un problème est survenu", isPresented: messageSentFailed) {
Button("OK") {
}
} message: {
let message = [networkMonitor.connected == false ? "L'appareil n'est pas connecté à internet." as String? : nil, 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." as String? : nil, (sentError == .messageFailed || sentError == .messageNotSent) ? "Le SMS n'a pas été envoyé" as String? : nil, sentError == .mailFailed ? "Le mail n'a pas été envoyé" as String? : nil].compacted().joined(separator: "\n")
Text(message)
}
.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:
_called(true)
break
case .failed:
self.sentError = .messageFailed
case .sent:
if networkMonitor.connected == false {
self.sentError = .messageNotSent
} else {
_called(true)
}
@unknown default:
break
}
}
} else {
SubscriptionView(showLackOfPlanMessage: true)
}
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
_called(true)
case .failed:
self.contactType = nil
self.sentError = .mailFailed
case .sent:
if networkMonitor.connected == false {
self.contactType = nil
self.sentError = .mailNotSent
} else {
_called(true)
}
@unknown default:
break
}
}
} else {
SubscriptionView(showLackOfPlanMessage: true)
}
}
}
.tint(.master)
}
}
}