wip html pdf landscape

refresh loser bracket match name update when ending edition
multistore
Razmig Sarkissian 1 year ago
parent a0b6c41c86
commit 9979824072
  1. 36
      PadelClub/Utils/HtmlGenerator.swift
  2. 40
      PadelClub/Utils/HtmlService.swift
  3. 13
      PadelClub/Views/Round/LoserRoundView.swift
  4. 34
      PadelClub/Views/Tournament/Screen/PrintSettingsView.swift

@ -31,10 +31,19 @@ class HtmlGenerator: ObservableObject {
@Published var height: CGFloat = 0
private var webView: WKWebView = WKWebView()
private var groupStageDone: Int = 0
@Published var landscape: Bool = false
var baseWidth: CGFloat {
landscape ? 842 : 595
}
var baseHeight: CGFloat {
landscape ? 595 : 842
}
var estimatedPageCount: Int {
if let zoomLevel {
let pageSize = CGSize(width: 595 * (1 + zoomLevel), height: 812 * (1 + zoomLevel))
let pageSize = CGSize(width: baseWidth * (1 + zoomLevel), height: baseHeight * (1 + zoomLevel))
let numberOfPageInWidth = Int(width / pageSize.width) + 1
let numberOfPageInHeight = Int(height / pageSize.height) + 1
return numberOfPageInWidth * numberOfPageInHeight
@ -50,17 +59,19 @@ class HtmlGenerator: ObservableObject {
func generateWebView(webView: WKWebView) {
self.webView = webView
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
print("evaluateJavaScript", "readystage", complete, error)
if complete != nil {
self.webView.evaluateJavaScript("document.documentElement.scrollHeight", completionHandler: { (height, error) in
print("evaluateJavaScript", "height", height, error)
self.height = height as! CGFloat
self.webView.evaluateJavaScript("document.documentElement.scrollWidth", completionHandler: { (width, error) in
print("evaluateJavaScript", "width", width, error)
self.width = width as! CGFloat
if self.completionHandler != nil {
self.buildPDF()
}
})
})
self.webView.evaluateJavaScript("document.documentElement.scrollWidth", completionHandler: { (width, error) in
self.width = width as! CGFloat
})
}
if self.completionHandler != nil {
self.buildPDF()
}
})
}
@ -75,7 +86,7 @@ class HtmlGenerator: ObservableObject {
print("bracket", width, height)
let config = WKPDFConfiguration()
config.rect = CGRect(origin: .zero, size: CGSize(width: Int(width), height: Int(width)))
config.rect = CGRect(origin: .zero, size: CGSize(width: Int(width), height: Int(height)))
webView.createPDF(configuration: config){ result in
switch result{
case .success(let data):
@ -111,7 +122,7 @@ class HtmlGenerator: ObservableObject {
try? FileManager.default.removeItem(at: pdfURL!)
print("buildPDF", width, height, zoomLevel ?? 0)
if let zoomLevel {
let pageSize = CGSize(width: 595 * (1 + zoomLevel), height: 812 * (1 + zoomLevel))
let pageSize = CGSize(width: baseWidth * (1 + zoomLevel), height: baseHeight * (1 + zoomLevel))
let numberOfPageInWidth = Int(width / pageSize.width) + 1
let numberOfPageInHeight = Int(height / pageSize.height) + 1
for w in 0..<numberOfPageInWidth {
@ -159,6 +170,11 @@ class HtmlGenerator: ObservableObject {
HtmlService.template(tournament: tournament).html(headName: displayHeads, withRank: displayRank, withScore: false)
}
func generateLoserBracketHtml(upperRound: Round) -> String {
//HtmlService.groupstage(bracket: tournament.orderedBrackets.first!).html()
HtmlService.loserBracket(upperRound: upperRound).html(headName: displayHeads, withRank: displayRank, withScore: false)
}
var pdfURL: URL? {
guard let pdfFolderURL = getFilePath() else {
return nil

@ -10,7 +10,8 @@ import Foundation
enum HtmlService {
case template(tournament: Tournament)
case bracket(tournament: Tournament, roundIndex: Int)
case bracket(round: Round)
case loserBracket(upperRound: Round)
case match(match: Match)
case player(entrant: TeamRegistration)
case hiddenPlayer
@ -26,7 +27,7 @@ enum HtmlService {
var fileName: String {
switch self {
case .template:
case .template, .loserBracket:
return "tournament-template"
case .bracket:
return "bracket-template"
@ -191,23 +192,42 @@ enum HtmlService {
}
template = template.replacingOccurrences(of: "{{matchDescription}}", with: "")
return template
case .bracket(let tournament, let roundIndex):
case .bracket(let round):
var template = ""
var bracket = ""
if let round = tournament.rounds().first(where: { $0.index == roundIndex }) {
for (_, match) in round._matches().enumerated() {
template = template.appending(HtmlService.match(match: match).html(headName: headName, withRank: withRank, withScore: withScore))
}
bracket = html.replacingOccurrences(of: "{{match-template}}", with: template)
bracket = bracket.replacingOccurrences(of: "{{roundLabel}}", with: round.roundTitle())
for (_, match) in round._matches().enumerated() {
template = template.appending(HtmlService.match(match: match).html(headName: headName, withRank: withRank, withScore: withScore))
}
bracket = html.replacingOccurrences(of: "{{match-template}}", with: template)
bracket = bracket.replacingOccurrences(of: "{{roundLabel}}", with: round.roundTitle())
return bracket
case .loserBracket(let upperRound):
var template = html
template = template.replacingOccurrences(of: "{{tournamentTitle}}", with: upperRound.correspondingLoserRoundTitle())
var brackets = ""
for round in upperRound.loserRounds() {
brackets = brackets.appending(HtmlService.bracket(round: round).html(headName: headName, withRank: withRank, withScore: withScore))
}
var winnerName = ""
let winner = """
<ul class="round" scope="last">
<li class="spacer">&nbsp;</li>
<li class="game game-top winner">\(winnerName)</li>
<li class="spacer">&nbsp;</li>
</ul>
<ul class="main" style="visibility:hidden">
</ul>
"""
brackets = brackets.appending(winner)
template = template.replacingOccurrences(of: "{{brackets}}", with: brackets)
return template
case .template(let tournament):
var template = html
template = template.replacingOccurrences(of: "{{tournamentTitle}}", with: tournament.tournamentTitle(.short))
var brackets = ""
for round in tournament.rounds() {
brackets = brackets.appending(HtmlService.bracket(tournament: tournament, roundIndex: round.index).html(headName: headName, withRank: withRank, withScore: withScore))
brackets = brackets.appending(HtmlService.bracket(round: round).html(headName: headName, withRank: withRank, withScore: withScore))
}
var winnerName = ""

@ -6,6 +6,7 @@
//
import SwiftUI
import LeStorage
struct LoserRoundView: View {
@EnvironmentObject var dataStore: DataStore
@ -22,7 +23,7 @@ struct LoserRoundView: View {
print("func _roundDisabled", duration.formatted(.units(allowed: [.seconds, .milliseconds])))
}
#endif
return loserBracket.allMatches.allSatisfy({ $0.disabled == false })
return loserBracket.allMatches.allSatisfy({ $0.disabled == true })
}
private func _matches(loserRoundId: String?) -> [Match] {
@ -87,6 +88,16 @@ struct LoserRoundView: View {
ToolbarItem(placement: .topBarTrailing) {
Button(isEditingTournamentSeed.wrappedValue == true ? "Valider" : "Modifier") {
isEditingTournamentSeed.wrappedValue.toggle()
if isEditingTournamentSeed.wrappedValue == false {
let allRoundMatches = loserBracket.allMatches
allRoundMatches.forEach({ $0.name = $0.roundTitle() })
do {
try DataStore.shared.matches.addOrUpdate(contentOfs: allRoundMatches)
} catch {
Logger.error(error)
}
}
}
}
}

@ -28,7 +28,6 @@ struct PrintSettingsView: View {
// Toggle(isOn: $generator.displayHeads, label: {
// Text("Afficher les têtes de séries")
// })
Toggle(isOn: $generator.displayRank, label: {
Text("Afficher le classement du joueur")
})
@ -57,6 +56,17 @@ struct PrintSettingsView: View {
} label: {
Text("Zoom")
}
.onChange(of: generator.zoomLevel) {
if generator.zoomLevel == nil {
generator.landscape = false
}
}
if generator.zoomLevel != nil {
Toggle(isOn: $generator.landscape, label: {
Text("Format paysage")
})
}
HStack {
Text("Nombre de page A4 à imprimer")
@ -106,11 +116,23 @@ struct PrintSettingsView: View {
Section {
NavigationLink {
WebViewPreview(bracket: true)
WebViewPreview()
.environmentObject(generator)
} label: {
Text("Aperçu du tableau")
}
//
// ForEach(tournament.rounds()) { round in
// if round.index > 0 {
// NavigationLink {
// WebViewPreview(round: round)
// .environmentObject(generator)
// } label: {
// Text("Aperçu \(round.correspondingLoserRoundTitle())")
// }
// }
// }
//
ForEach(tournament.groupStages()) { groupStage in
NavigationLink {
WebViewPreview(groupStage: groupStage)
@ -260,13 +282,13 @@ struct WebView: UIViewRepresentable {
struct WebViewPreview: View {
@EnvironmentObject var generator: HtmlGenerator
let bracket: Bool
let groupStage: GroupStage?
let round: Round?
@State private var html: String?
init(bracket: Bool = false, groupStage: GroupStage? = nil) {
self.bracket = bracket
init(groupStage: GroupStage? = nil, round: Round? = nil) {
self.round = round
self.groupStage = groupStage
}
@ -280,6 +302,8 @@ struct WebViewPreview: View {
.onAppear {
if let groupStage {
html = HtmlService.groupstage(groupStage: groupStage).html(headName: generator.displayHeads, withRank: generator.displayRank, withScore: false)
} else if let round {
html = generator.generateLoserBracketHtml(upperRound: round)
} else {
html = generator.generateHtml()
}

Loading…
Cancel
Save