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.
141 lines
4.8 KiB
141 lines
4.8 KiB
//
|
|
// ActivityView.swift
|
|
// LeCountdown
|
|
//
|
|
// Created by Laurent Morvillier on 20/02/2023.
|
|
//
|
|
|
|
import SwiftUI
|
|
import CoreData
|
|
|
|
struct ActivityStatsView: View {
|
|
|
|
@Environment(\.managedObjectContext) private var viewContext
|
|
|
|
let activity: Activity
|
|
var filter: Filter? = nil
|
|
|
|
@State var subFilters: [Filter] = []
|
|
|
|
var body: some View {
|
|
|
|
List {
|
|
Section {
|
|
StatisticsView(activity: self.activity, filter: self.filter) { statValues in
|
|
ForEach(statValues) { statValue in
|
|
StatView(statValue: statValue)
|
|
}
|
|
}.environment(\.managedObjectContext, viewContext)
|
|
}
|
|
|
|
// Picker("Time", selection: $selectedTimeFrame) {
|
|
// ForEach(TimeFrame.allCases) { timeFrame in
|
|
// Text(timeFrame.localizedString).tag(timeFrame)
|
|
// }
|
|
// }
|
|
// .pickerStyle(.segmented)
|
|
// .padding(.horizontal)
|
|
|
|
|
|
StatisticsSubView(activity: self.activity, subFilters: self.subFilters)
|
|
.environment(\.managedObjectContext, viewContext)
|
|
|
|
}
|
|
.navigationTitle(self.filter?.localizedString ?? self.activity.name ?? "no name")
|
|
.onAppear {
|
|
self._load()
|
|
}
|
|
|
|
}
|
|
|
|
fileprivate func _load() {
|
|
|
|
do {
|
|
switch self.filter {
|
|
case nil:
|
|
let years = try CoreDataRequests.years(context: self.viewContext, activity: self.activity)
|
|
self.subFilters = years.map { Filter.year($0) }
|
|
case .some(let filter):
|
|
switch filter {
|
|
case .year(let year):
|
|
let months = try CoreDataRequests.months(context: self.viewContext, activity: self.activity, year: year)
|
|
self.subFilters = months.map { Filter.month(Month(month: $0, year: year)) }
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
enum TimeFrame: Int, Identifiable, CaseIterable {
|
|
|
|
var id: Int { return self.rawValue }
|
|
|
|
// case all
|
|
case year
|
|
case month
|
|
|
|
var localizedString: String {
|
|
switch self {
|
|
// case .all: return NSLocalizedString("All", comment: "")
|
|
case .year: return NSLocalizedString("Year", comment: "")
|
|
case .month: return NSLocalizedString("Month", comment: "")
|
|
}
|
|
}
|
|
|
|
func filters(context: NSManagedObjectContext, activity: Activity) -> [Filter] {
|
|
|
|
let predicate = NSPredicate(format: "activity = %@", activity)
|
|
|
|
do {
|
|
switch self {
|
|
// case .all:
|
|
// return [.none]
|
|
case .year:
|
|
let distinct = try context.distinct(entityName: "Record", attributes: ["year"], predicate: predicate)
|
|
if let yearsMap = distinct as? [[String : Int]] {
|
|
let years = yearsMap.compactMap { $0.first?.value }
|
|
return years.map { Filter.year($0) }
|
|
} else {
|
|
Logger.w("Could not cast \(distinct) as [Int]")
|
|
}
|
|
case .month:
|
|
|
|
let monthes = try CoreDataRequests.months(context: context, activity: activity)
|
|
return monthes.map { Filter.month($0) }
|
|
|
|
// let distinct = try context.distinct(entityName: "Record", attributes: ["year", "month"], predicate: predicate)
|
|
// if let distinctMonths = distinct as? [[String : Int]] {
|
|
//
|
|
// let months = distinctMonths.compactMap {
|
|
// if let month = $0["month"],
|
|
// let year = $0["year"] {
|
|
// return Month(month: month, year: year)
|
|
// } else {
|
|
// Logger.w("issue with dictionary \($0)")
|
|
// return nil
|
|
// }
|
|
// }
|
|
// return months.map { Filter.month($0) }
|
|
// } else {
|
|
// Logger.w("Could not cast \(distinct) as [Int]")
|
|
// }
|
|
}
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
return []
|
|
}
|
|
|
|
}
|
|
|
|
struct ActivityStatsView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
ActivityStatsView(activity: Activity.fake(context: PersistenceController.preview.container.viewContext))
|
|
.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
|
|
}
|
|
}
|
|
|