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.
 
 
PadelClub/PadelClub/Views/Navigation/MainView.swift

160 lines
5.3 KiB

//
// MainView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 29/02/2024.
//
import SwiftUI
struct MainView: View {
@StateObject var dataStore = DataStore.shared
@AppStorage("importingFiles") var importingFiles: Bool = false
@State private var checkingFilesAttempt: Int = 0
@State private var checkingFiles: Bool = false
@AppStorage("lastDataSource") var lastDataSource: String?
@AppStorage("lastDataSourceMaleUnranked") var lastDataSourceMaleUnranked: Int?
@AppStorage("lastDataSourceFemaleUnranked") var lastDataSourceFemaleUnranked: Int?
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [],
animation: .default)
private var players: FetchedResults<ImportedPlayer>
var body: some View {
TabView {
if dataStore.tournaments.isEmpty {
EmptyActivityView()
.tabItem(for: .activity)
} else {
ActivityView()
.tabItem(for: .activity)
}
TournamentOrganizerView()
.tabItem(for: .tournamentOrganizer)
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(5))
}
}
}
func _activityStatusBoxView() -> some View {
_activityStatus()
.font(.title3)
.frame(height: 32)
.padding()
.background {
RoundedRectangle(cornerRadius: 20, style: .continuous)
.fill(.white)
}
.shadow(radius: 2)
.offset(y: -64)
}
@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("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 {
lastDataSource = await FileImportManager.shared.importDataFromFFT()
if let lastDataSource, let mostRecentDate = URL.importDateFormatter.date(from: lastDataSource) {
await _calculateCurrentUnrankedValues(mostRecentDateAvailable: mostRecentDate)
}
importingFiles = false
_downloadPreviousDate()
}
}
private func _calculateCurrentUnrankedValues(mostRecentDateAvailable: Date) async {
lastDataSourceMaleUnranked = await FederalPlayer.lastRank(mostRecentDateAvailable: mostRecentDateAvailable, man: true)
lastDataSourceFemaleUnranked = await FederalPlayer.lastRank(mostRecentDateAvailable: mostRecentDateAvailable, man: false)
}
private func _downloadPreviousDate() {
Task {
await SourceFileManager.shared.getAllFiles()
}
}
}
fileprivate extension View {
func tabItem(for tabDestination: TabDestination) -> some View {
modifier(TabItemModifier(tabDestination: tabDestination))
}
}
fileprivate struct TabItemModifier: ViewModifier {
let tabDestination: TabDestination
func body(content: Content) -> some View {
content
.tabItem {
Label(tabDestination.title, systemImage: tabDestination.image)
}
}
}
#Preview {
MainView()
}