add multi import

multistore
Razmig Sarkissian 1 year ago
parent 6e26222300
commit 63f5b29d5e
  1. 5
      PadelClub/Data/Tournament.swift
  2. 4
      PadelClub/Views/Components/ButtonValidateView.swift
  3. 6
      PadelClub/Views/Components/GenericDestinationPickerView.swift
  4. 77
      PadelClub/Views/Tournament/FileImportView.swift

@ -463,7 +463,10 @@ class Tournament : ModelObject, Storable {
func shareURL(_ pageLink: PageLink = .matches) -> URL? { func shareURL(_ pageLink: PageLink = .matches) -> URL? {
if pageLink == .clubBroadcast { if pageLink == .clubBroadcast {
if let club = club(), let broadcastCode = club.broadcastCode { let club = club()
print("club", club)
print("club broadcast code", club?.broadcastCode)
if let club, let broadcastCode = club.broadcastCode {
return URLs.main.url.appending(path: "c/\(broadcastCode)") return URLs.main.url.appending(path: "c/\(broadcastCode)")
} else { } else {
return nil return nil

@ -9,11 +9,11 @@ import SwiftUI
struct ButtonValidateView: View { struct ButtonValidateView: View {
var role: ButtonRole? var role: ButtonRole?
var title: String = "Valider"
let action: () -> () let action: () -> ()
var body: some View { var body: some View {
Button("Valider", role: role) { Button(title, role: role) {
action() action()
} }
.clipShape(Capsule()) .clipShape(Capsule())

@ -49,7 +49,7 @@ struct GenericDestinationPickerView<T: Identifiable & Selectable & Equatable >:
.buttonStyle(.plain) .buttonStyle(.plain)
.overlay(alignment: .bottomTrailing) { .overlay(alignment: .bottomTrailing) {
if destination.displayImageIfValueZero() { if destination.displayImageIfValueZero() {
let count = destination.badgeValue() let count : Int? = destination.badgeValue()
if let count, count == 0, let badge = destination.badgeImage() { if let count, count == 0, let badge = destination.badgeImage() {
Image(systemName: badge.systemName()) Image(systemName: badge.systemName())
.foregroundColor(badge.color()) .foregroundColor(badge.color())
@ -60,7 +60,7 @@ struct GenericDestinationPickerView<T: Identifiable & Selectable & Equatable >:
) )
.offset(x: 3, y: 3) .offset(x: 3, y: 3)
} else if let count, count > 0 { } else if let count, count > 0 {
Image(systemName: count <= 50 ? "\(count).circle.fill" : "plus.circle.fill") Image(systemName: count <= 50 ? "\(String(count)).circle.fill" : "plus.circle.fill")
.foregroundColor(destination.badgeValueColor() ?? .red) .foregroundColor(destination.badgeValueColor() ?? .red)
.imageScale(.medium) .imageScale(.medium)
.background ( .background (
@ -80,7 +80,7 @@ struct GenericDestinationPickerView<T: Identifiable & Selectable & Equatable >:
) )
.offset(x: 3, y: 3) .offset(x: 3, y: 3)
} else if let count = destination.badgeValue(), count > 0 { } else if let count = destination.badgeValue(), count > 0 {
Image(systemName: count <= 50 ? "\(count).circle.fill" : "plus.circle.fill") Image(systemName: count <= 50 ? "\(String(count)).circle.fill" : "plus.circle.fill")
.foregroundColor(destination.badgeValueColor() ?? .red) .foregroundColor(destination.badgeValueColor() ?? .red)
.imageScale(.medium) .imageScale(.medium)
.background ( .background (

@ -28,8 +28,9 @@ struct FileImportView: View {
@State private var fileProvider: FileImportManager.FileProvider = .frenchFederation @State private var fileProvider: FileImportManager.FileProvider = .frenchFederation
@State private var validationInProgress: Bool = false @State private var validationInProgress: Bool = false
@State private var multiImport: Bool = false
private var filteredTeams: [FileImportManager.TeamHolder] { private func filteredTeams(tournament: Tournament) -> [FileImportManager.TeamHolder] {
return teams.filter { $0.tournamentCategory == tournament.tournamentCategory }.sorted(by: \.weight) return teams.filter { $0.tournamentCategory == tournament.tournamentCategory }.sorted(by: \.weight)
} }
@ -68,6 +69,24 @@ struct FileImportView: View {
Text(.init(footerString)) Text(.init(footerString))
} }
} }
if let tournaments = tournament.eventObject()?.tournaments, tournaments.count > 1, fileProvider == .frenchFederation {
Section {
RowButtonView("Importer toutes les épreuves") {
multiImport = true
if let fileContent {
do {
try await _startImport(fileContent: fileContent)
} catch {
errorMessage = error.localizedDescription
}
}
}
} footer: {
Text("Ce tournoi possède plusieurs épreuves. Vous pouvez importer les équipes du fichier pour toutes les épreuves d'un coup.")
}
.disabled(fileContent == nil || convertingFile)
}
} }
// if filteredTeams.isEmpty == false && tournament.unsortedTeams().isEmpty == false { // if filteredTeams.isEmpty == false && tournament.unsortedTeams().isEmpty == false {
@ -128,7 +147,8 @@ struct FileImportView: View {
// } // }
// } // }
if filteredTeams.isEmpty && teams.isEmpty == false { let filteredTeams = filteredTeams(tournament: tournament)
if filteredTeams.isEmpty && teams.isEmpty == false && multiImport == false {
@Bindable var tournament = tournament @Bindable var tournament = tournament
Section { Section {
Text("Aucune équipe \(tournament.tournamentCategory.importingRawValue) détectée mais \(teams.count) équipes sont dans le fichier") Text("Aucune équipe \(tournament.tournamentCategory.importingRawValue) détectée mais \(teams.count) équipes sont dans le fichier")
@ -157,6 +177,23 @@ struct FileImportView: View {
.tipStyle(tint: nil) .tipStyle(tint: nil)
} }
} }
if multiImport, fileProvider == .frenchFederation, let tournaments = tournament.eventObject()?.tournaments, tournaments.count > 1 {
ForEach(tournaments) { tournament in
let tournamentFilteredTeams = self.filteredTeams(tournament: tournament)
Section {
RowButtonView("Valider") {
await _validate(tournament: tournament)
}
} header: {
Text("\(tournamentFilteredTeams.count.formatted()) équipe\(tournamentFilteredTeams.count.pluralSuffix)")
} footer: {
Text(tournament.tournamentTitle())
}
}
.headerProminence(.increased)
} else {
Section { Section {
LabeledContent { LabeledContent {
Text(_filteredTeams.count.formatted()) Text(_filteredTeams.count.formatted())
@ -174,6 +211,7 @@ struct FileImportView: View {
} }
} }
} }
}
.fileImporter(isPresented: $isShowing, allowedContentTypes: [.spreadsheet, .commaSeparatedText, .text], allowsMultipleSelection: false, onCompletion: { results in .fileImporter(isPresented: $isShowing, allowedContentTypes: [.spreadsheet, .commaSeparatedText, .text], allowsMultipleSelection: false, onCompletion: { results in
switch results { switch results {
@ -215,13 +253,13 @@ struct FileImportView: View {
.navigationTitle("Importation") .navigationTitle("Importation")
.navigationBarTitleDisplayMode(.large) .navigationBarTitleDisplayMode(.large)
.toolbar { .toolbar {
ToolbarItem(placement: .bottomBar) { // ToolbarItem(placement: .bottomBar) {
PasteButton(payloadType: String.self) { strings in // PasteButton(payloadType: String.self) { strings in
guard let string = strings.first else { return } // guard let string = strings.first else { return }
fileContent = string // fileContent = string
fileProvider = .padelClub // fileProvider = .padelClub
} // }
} // }
ToolbarItem(placement: .cancellationAction) { ToolbarItem(placement: .cancellationAction) {
Button("Annuler", role: .cancel) { Button("Annuler", role: .cancel) {
dismiss() dismiss()
@ -232,8 +270,19 @@ struct FileImportView: View {
if validationInProgress { if validationInProgress {
ProgressView() ProgressView()
} else { } else {
ButtonValidateView { ButtonValidateView(title: (multiImport ? "Tout Valider" : "Valider")) {
validationInProgress = true validationInProgress = true
Task {
if let tournaments = tournament.eventObject()?.tournaments, tournaments.count > 1, multiImport {
for tournament in tournaments {
await _validate(tournament: tournament)
}
dismiss()
} else {
await _validate(tournament: tournament)
}
}
} }
.disabled(teams.isEmpty) .disabled(teams.isEmpty)
} }
@ -241,13 +290,10 @@ struct FileImportView: View {
} }
.interactiveDismissDisabled(validationInProgress) .interactiveDismissDisabled(validationInProgress)
.disabled(validationInProgress) .disabled(validationInProgress)
.onChange(of: validationInProgress) {
_validate()
}
} }
private func _validate() { private func _validate(tournament: Tournament) async {
Task { let filteredTeams = filteredTeams(tournament: tournament)
let previousTeams = filteredTeams.compactMap({ $0.previousTeam }) let previousTeams = filteredTeams.compactMap({ $0.previousTeam })
let unfound = Set(tournament.unsortedTeams()).subtracting(Set(previousTeams)) let unfound = Set(tournament.unsortedTeams()).subtracting(Set(previousTeams))
@ -265,6 +311,7 @@ struct FileImportView: View {
} }
tournament.importTeams(filteredTeams) tournament.importTeams(filteredTeams)
if multiImport == false {
dismiss() dismiss()
} }
} }

Loading…
Cancel
Save