fix file update

clubs
Raz 1 year ago
parent 5d7a81f7ef
commit cd38b33a6c
  1. 19
      PadelClub/Extensions/URL+Extensions.swift
  2. 5
      PadelClub/Utils/Network/NetworkManager.swift
  3. 33
      PadelClub/Utils/SourceFileManager.swift
  4. 4
      PadelClub/Views/Navigation/MainView.swift
  5. 20
      PadelClub/Views/Navigation/Umpire/PadelClubView.swift
  6. 37
      PadelClub/Views/Tournament/Screen/AddTeamView.swift

@ -70,6 +70,25 @@ extension URL {
return nil 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? { func fftImportingMaleUnrankValue() -> Int? {
// Read the contents of the file // Read the contents of the file
guard let fileContents = try? String(contentsOfFile: path(), encoding: .utf8) else { guard let fileContents = try? String(contentsOfFile: path(), encoding: .utf8) else {

@ -44,7 +44,8 @@ class NetworkManager {
return formatter.string(from: date) 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" let dateString = ["CLASSEMENT-PADEL", fileName, lastDateString].joined(separator: "-") + ".csv"
@ -77,6 +78,7 @@ class NetworkManager {
try? FileManager.default.removeItem(at: destinationFileUrl) try? FileManager.default.removeItem(at: destinationFileUrl)
try FileManager.default.copyItem(at: task.0, to: destinationFileUrl) try FileManager.default.copyItem(at: task.0, to: destinationFileUrl)
print("dl rank data ok", lastDateString, fileName) print("dl rank data ok", lastDateString, fileName)
return destinationFileUrl.fftImportingStatus() ?? 1
} else if urlResponse.statusCode == 404 && fileName == "MESSIEURS" { } else if urlResponse.statusCode == 404 && fileName == "MESSIEURS" {
print("dl rank data failedm fileNotYetAvailable", lastDateString, fileName) print("dl rank data failedm fileNotYetAvailable", lastDateString, fileName)
throw NetworkManagerError.fileNotYetAvailable throw NetworkManagerError.fileNotYetAvailable
@ -88,6 +90,7 @@ class NetworkManager {
throw NetworkManagerError.fileNotDownloaded(urlResponse.statusCode) throw NetworkManagerError.fileNotDownloaded(urlResponse.statusCode)
} }
} }
return nil
} }
func checkFileCreationDate(filePath: String) throws -> Date? { func checkFileCreationDate(filePath: String) throws -> Date? {

@ -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 @discardableResult
func fetchData(fromDate current: Date) async -> Bool { func fetchData(fromDate current: Date) async -> Int? {
let lastStringDate = URL.importDateFormatter.string(from: current) let lastStringDate = URL.importDateFormatter.string(from: current)
let files = ["MESSIEURS", "MESSIEURS-2", "MESSIEURS-3", "MESSIEURS-4", "DAMES"] let files = ["MESSIEURS", "MESSIEURS-2", "MESSIEURS-3", "MESSIEURS-4", "DAMES"]
let sourceFileDownloadTracker = SourceFileDownloadTracker()
do { do {
try await withThrowingTaskGroup(of: Void.self) { group in // Mark 1 try await withThrowingTaskGroup(of: Void.self) { group in // Mark 1
for file in files { for file in files {
group.addTask { group.addTask { [sourceFileDownloadTracker] in
try await NetworkManager.shared.downloadRankingData(lastDateString: lastStringDate, fileName: file) 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) // await fetchData(fromDate: nextCurrent)
// } // }
// } // }
return true
} catch { } catch {
print("downloadRankingData", error) print("downloadRankingData", error)
@ -111,10 +132,10 @@ class SourceFileManager {
await fetchData(fromDate: previousDate) await fetchData(fromDate: previousDate)
} }
} }
return false
} }
let downloadedFileStatus = await sourceFileDownloadTracker.getDownloadedFileStatus()
return downloadedFileStatus
} }
func getAllFiles(initialDate: String = "08-2022") async { func getAllFiles(initialDate: String = "08-2022") async {

@ -255,9 +255,9 @@ struct MainView: View {
Task { Task {
let updated = await SourceFileManager.shared.fetchData(fromDate: mostRecentDateImported) let updated = await SourceFileManager.shared.fetchData(fromDate: mostRecentDateImported)
print("file updated", updated) print("file updated", updated)
if updated { if let updated, updated == 1 {
await _startImporting(importingDate: mostRecentDateImported) await _startImporting(importingDate: mostRecentDateImported)
} else if current.incompleteMode == false { } else if current.incompleteMode == false || updated == 0 {
await _calculateMonthData(dataSource: current.monthKey) await _calculateMonthData(dataSource: current.monthKey)
} }
} }

@ -202,14 +202,6 @@ struct PadelClubView: View {
} }
} }
.id(uuid) .id(uuid)
.task {
await self._checkSourceFileAvailability()
}
.refreshable {
Task {
await self._checkSourceFileAvailability()
}
}
.headerProminence(.increased) .headerProminence(.increased)
.navigationTitle("Données fédérales") .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() { private func _startImporting() {
let importingDate = SourceFileManager.shared.mostRecentDateAvailable let importingDate = SourceFileManager.shared.mostRecentDateAvailable
importObserver.currentImportDate = importingDate importObserver.currentImportDate = importingDate

@ -149,7 +149,7 @@ struct AddTeamView: View {
@ViewBuilder @ViewBuilder
private func _managementView() -> some View { private func _managementView() -> some View {
Section { Section {
RowButtonView("Rechercher dans la base fédérale") { RowButtonView("Ajouter via la base fédérale") {
presentPlayerSearch = true presentPlayerSearch = true
} }
} footer: { } 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: { } header: {
let _currentSelection = _currentSelection() let _currentSelection = _currentSelection()
let selectedSortedTeams = tournament.selectedSortedTeams() 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 let pasteString {
if fetchPlayers.isEmpty { if fetchPlayers.isEmpty {
ContentUnavailableView { ContentUnavailableView {

Loading…
Cancel
Save