From cd38b33a6c048309ed31d02970c65016da1b9cde Mon Sep 17 00:00:00 2001 From: Raz Date: Mon, 19 Aug 2024 10:49:04 +0200 Subject: [PATCH] fix file update --- PadelClub/Extensions/URL+Extensions.swift | 19 ++++++++++ PadelClub/Utils/Network/NetworkManager.swift | 5 ++- PadelClub/Utils/SourceFileManager.swift | 33 ++++++++++++++--- PadelClub/Views/Navigation/MainView.swift | 4 +- .../Navigation/Umpire/PadelClubView.swift | 20 ---------- .../Views/Tournament/Screen/AddTeamView.swift | 37 +++++++++---------- 6 files changed, 69 insertions(+), 49 deletions(-) diff --git a/PadelClub/Extensions/URL+Extensions.swift b/PadelClub/Extensions/URL+Extensions.swift index 5cddc1b..3fb7746 100644 --- a/PadelClub/Extensions/URL+Extensions.swift +++ b/PadelClub/Extensions/URL+Extensions.swift @@ -70,6 +70,25 @@ extension URL { return nil } + func fftImportingStatus() -> Int? { + // Read the contents of the file + guard let fileContents = try? String(contentsOfFile: path(), encoding: .utf8) else { + return nil + } + + // Split the contents by newline characters + let lines = fileContents.components(separatedBy: .newlines) + //0 means no need to reimport, just recalc + //1 or missing means re-import + if let line = lines.first(where: { + $0.hasPrefix("import-status:") + }) { + return Int(line.replacingOccurrences(of: "import-status:", with: "")) + } + + return nil + } + func fftImportingMaleUnrankValue() -> Int? { // Read the contents of the file guard let fileContents = try? String(contentsOfFile: path(), encoding: .utf8) else { diff --git a/PadelClub/Utils/Network/NetworkManager.swift b/PadelClub/Utils/Network/NetworkManager.swift index 914adb1..6a7a7e6 100644 --- a/PadelClub/Utils/Network/NetworkManager.swift +++ b/PadelClub/Utils/Network/NetworkManager.swift @@ -44,7 +44,8 @@ class NetworkManager { return formatter.string(from: date) } - func downloadRankingData(lastDateString: String, fileName: String) async throws { + @discardableResult + func downloadRankingData(lastDateString: String, fileName: String) async throws -> Int? { let dateString = ["CLASSEMENT-PADEL", fileName, lastDateString].joined(separator: "-") + ".csv" @@ -77,6 +78,7 @@ class NetworkManager { try? FileManager.default.removeItem(at: destinationFileUrl) try FileManager.default.copyItem(at: task.0, to: destinationFileUrl) print("dl rank data ok", lastDateString, fileName) + return destinationFileUrl.fftImportingStatus() ?? 1 } else if urlResponse.statusCode == 404 && fileName == "MESSIEURS" { print("dl rank data failedm fileNotYetAvailable", lastDateString, fileName) throw NetworkManagerError.fileNotYetAvailable @@ -88,6 +90,7 @@ class NetworkManager { throw NetworkManagerError.fileNotDownloaded(urlResponse.statusCode) } } + return nil } func checkFileCreationDate(filePath: String) throws -> Date? { diff --git a/PadelClub/Utils/SourceFileManager.swift b/PadelClub/Utils/SourceFileManager.swift index 0f7bf05..a04a2b7 100644 --- a/PadelClub/Utils/SourceFileManager.swift +++ b/PadelClub/Utils/SourceFileManager.swift @@ -80,17 +80,39 @@ class SourceFileManager { } } + actor SourceFileDownloadTracker { + var _downloadedFileStatus : Int? = nil + + func updateIfNecessary(with successState: Int?) { + if successState != nil && (_downloadedFileStatus == nil || _downloadedFileStatus == 0) { + _downloadedFileStatus = successState + } + } + + func getDownloadedFileStatus() -> Int? { + return _downloadedFileStatus + } + + } + + //return nil if no new files + //return 1 if new file to import + //return 0 if new file just to re-calc static data, no need to re-import @discardableResult - func fetchData(fromDate current: Date) async -> Bool { + func fetchData(fromDate current: Date) async -> Int? { let lastStringDate = URL.importDateFormatter.string(from: current) let files = ["MESSIEURS", "MESSIEURS-2", "MESSIEURS-3", "MESSIEURS-4", "DAMES"] + + let sourceFileDownloadTracker = SourceFileDownloadTracker() + do { try await withThrowingTaskGroup(of: Void.self) { group in // Mark 1 for file in files { - group.addTask { - try await NetworkManager.shared.downloadRankingData(lastDateString: lastStringDate, fileName: file) + group.addTask { [sourceFileDownloadTracker] in + let success = try await NetworkManager.shared.downloadRankingData(lastDateString: lastStringDate, fileName: file) + await sourceFileDownloadTracker.updateIfNecessary(with: success) } } @@ -102,7 +124,6 @@ class SourceFileManager { // await fetchData(fromDate: nextCurrent) // } // } - return true } catch { print("downloadRankingData", error) @@ -111,10 +132,10 @@ class SourceFileManager { await fetchData(fromDate: previousDate) } } - - return false } + let downloadedFileStatus = await sourceFileDownloadTracker.getDownloadedFileStatus() + return downloadedFileStatus } func getAllFiles(initialDate: String = "08-2022") async { diff --git a/PadelClub/Views/Navigation/MainView.swift b/PadelClub/Views/Navigation/MainView.swift index 5db1726..dda58d7 100644 --- a/PadelClub/Views/Navigation/MainView.swift +++ b/PadelClub/Views/Navigation/MainView.swift @@ -255,9 +255,9 @@ struct MainView: View { Task { let updated = await SourceFileManager.shared.fetchData(fromDate: mostRecentDateImported) print("file updated", updated) - if updated { + if let updated, updated == 1 { await _startImporting(importingDate: mostRecentDateImported) - } else if current.incompleteMode == false { + } else if current.incompleteMode == false || updated == 0 { await _calculateMonthData(dataSource: current.monthKey) } } diff --git a/PadelClub/Views/Navigation/Umpire/PadelClubView.swift b/PadelClub/Views/Navigation/Umpire/PadelClubView.swift index ea4cb32..5de7806 100644 --- a/PadelClub/Views/Navigation/Umpire/PadelClubView.swift +++ b/PadelClub/Views/Navigation/Umpire/PadelClubView.swift @@ -202,14 +202,6 @@ struct PadelClubView: View { } } .id(uuid) - .task { - await self._checkSourceFileAvailability() - } - .refreshable { - Task { - await self._checkSourceFileAvailability() - } - } .headerProminence(.increased) .navigationTitle("Données fédérales") } @@ -232,18 +224,6 @@ struct PadelClubView: View { } } - private func _checkSourceFileAvailability() async { - - print("check internet") - print("check files on internet") - print("check if any files on internet are more recent than here") - importObserver.checkingFiles = true - await SourceFileManager.shared.fetchData() - importObserver.checkingFilesAttempt += 1 - importObserver.checkingFiles = false - //uuid = UUID() - } - private func _startImporting() { let importingDate = SourceFileManager.shared.mostRecentDateAvailable importObserver.currentImportDate = importingDate diff --git a/PadelClub/Views/Tournament/Screen/AddTeamView.swift b/PadelClub/Views/Tournament/Screen/AddTeamView.swift index 59d03d9..75246aa 100644 --- a/PadelClub/Views/Tournament/Screen/AddTeamView.swift +++ b/PadelClub/Views/Tournament/Screen/AddTeamView.swift @@ -149,7 +149,7 @@ struct AddTeamView: View { @ViewBuilder private func _managementView() -> some View { Section { - RowButtonView("Rechercher dans la base fédérale") { + RowButtonView("Ajouter via la base fédérale") { presentPlayerSearch = true } } footer: { @@ -365,6 +365,22 @@ struct AddTeamView: View { } } } + if editedTeam == nil { + if createdPlayerIds.isEmpty { + RowButtonView("Bloquer une place") { + _createTeam(checkDuplicates: false) + } + } else { + RowButtonView("Ajouter l'équipe") { + _createTeam(checkDuplicates: true) + } + } + } else { + RowButtonView("Confirmer") { + _updateTeam(checkDuplicates: false) + editedTeam = nil + } + } } header: { let _currentSelection = _currentSelection() let selectedSortedTeams = tournament.selectedSortedTeams() @@ -393,25 +409,6 @@ struct AddTeamView: View { } - Section { - if editedTeam == nil { - if createdPlayerIds.isEmpty { - RowButtonView("Bloquer une place") { - _createTeam(checkDuplicates: false) - } - } else { - RowButtonView("Ajouter l'équipe") { - _createTeam(checkDuplicates: true) - } - } - } else { - RowButtonView("Modifier l'équipe") { - _updateTeam(checkDuplicates: false) - editedTeam = nil - } - } - } - if let pasteString { if fetchPlayers.isEmpty { ContentUnavailableView {