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.
130 lines
4.4 KiB
130 lines
4.4 KiB
//
|
|
// MainView.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Razmig Sarkissian on 29/02/2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LeStorage
|
|
|
|
struct MainView: View {
|
|
@StateObject var dataStore = DataStore.shared
|
|
@AppStorage("importingFiles") var importingFiles: Bool = false
|
|
@Environment(NavigationViewModel.self) private var navigation: NavigationViewModel
|
|
|
|
@State private var checkingFilesAttempt: Int = 0
|
|
@State private var checkingFiles: Bool = false
|
|
|
|
var lastDataSource: String? {
|
|
dataStore.appSettings.lastDataSource
|
|
}
|
|
|
|
@Environment(\.managedObjectContext) private var viewContext
|
|
|
|
@FetchRequest(
|
|
sortDescriptors: [],
|
|
animation: .default)
|
|
private var players: FetchedResults<ImportedPlayer>
|
|
|
|
var body: some View {
|
|
@Bindable var navigation = navigation
|
|
TabView(selection: $navigation.selectedTab) {
|
|
ActivityView()
|
|
.tabItem(for: .activity)
|
|
TournamentOrganizerView()
|
|
.tabItem(for: .tournamentOrganizer)
|
|
OngoingView()
|
|
.tabItem(for: .ongoing)
|
|
ToolboxView()
|
|
.tabItem(for: .toolbox)
|
|
UmpireView()
|
|
.tabItem(for: .umpire)
|
|
// PadelClubView()
|
|
// .tabItem(for: .padelClub)
|
|
}
|
|
.environmentObject(dataStore)
|
|
.task {
|
|
await self._checkSourceFileAvailability()
|
|
await self._downloadPreviousDate()
|
|
}
|
|
// .refreshable {
|
|
// Task {
|
|
// await self._checkSourceFileAvailability()
|
|
// }
|
|
// }
|
|
.overlay(alignment: .bottom) {
|
|
if importingFiles {
|
|
_activityStatusBoxView()
|
|
} else {
|
|
_activityStatusBoxView()
|
|
.deferredRendering(for: .seconds(3))
|
|
}
|
|
}
|
|
}
|
|
|
|
func _activityStatusBoxView() -> some View {
|
|
_activityStatus()
|
|
.toastFormatted()
|
|
}
|
|
|
|
@ViewBuilder
|
|
func _activityStatus() -> some View {
|
|
if importingFiles {
|
|
HStack(spacing: 20) {
|
|
ProgressView()
|
|
if let mostRecentDateAvailable = SourceFileManager.shared.mostRecentDateAvailable {
|
|
if mostRecentDateAvailable > SourceFileManager.shared.lastDataSourceDate() ?? .distantPast {
|
|
Text("import " + mostRecentDateAvailable.monthYearFormatted)
|
|
}
|
|
}
|
|
}
|
|
} else if let mostRecentDateAvailable = SourceFileManager.shared.mostRecentDateAvailable {
|
|
if mostRecentDateAvailable > SourceFileManager.shared.lastDataSourceDate() ?? .distantPast {
|
|
Label(mostRecentDateAvailable.monthYearFormatted + " disponible", systemImage: "exclamationmark.triangle")
|
|
.labelStyle(.titleAndIcon)
|
|
} else {
|
|
Label(mostRecentDateAvailable.monthYearFormatted, systemImage: "checkmark")
|
|
.labelStyle(.titleAndIcon)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func _checkSourceFileAvailability() async {
|
|
print("dataStore.appSettings.lastDataSource :", dataStore.appSettings.lastDataSource ?? "none")
|
|
print("check internet")
|
|
print("check files on internet")
|
|
print("check if any files on internet are more recent than here")
|
|
checkingFiles = true
|
|
await SourceFileManager.shared.fetchData()
|
|
checkingFilesAttempt += 1
|
|
checkingFiles = false
|
|
|
|
if let mostRecentDateAvailable = SourceFileManager.shared.mostRecentDateAvailable, mostRecentDateAvailable > SourceFileManager.shared.lastDataSourceDate() ?? .distantPast {
|
|
//_startImporting()
|
|
}
|
|
}
|
|
|
|
private func _startImporting() {
|
|
importingFiles = true
|
|
Task {
|
|
let lastDataSource = await FileImportManager.shared.importDataFromFFT()
|
|
dataStore.appSettings.lastDataSource = lastDataSource
|
|
dataStore.updateSettings()
|
|
if let lastDataSource, let mostRecentDate = URL.importDateFormatter.date(from: lastDataSource) {
|
|
await MonthData.calculateCurrentUnrankedValues(mostRecentDateAvailable: mostRecentDate)
|
|
}
|
|
importingFiles = false
|
|
|
|
await _downloadPreviousDate()
|
|
}
|
|
}
|
|
|
|
private func _downloadPreviousDate() async {
|
|
await SourceFileManager.shared.getAllFiles()
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
MainView()
|
|
}
|
|
|