diff --git a/LeCountdown.xcodeproj/project.pbxproj b/LeCountdown.xcodeproj/project.pbxproj index ddbaae6..7fb48d4 100644 --- a/LeCountdown.xcodeproj/project.pbxproj +++ b/LeCountdown.xcodeproj/project.pbxproj @@ -136,7 +136,7 @@ C4BA2B3A299F838000CB4FBA /* Model+SharedExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B39299F838000CB4FBA /* Model+SharedExtensions.swift */; }; C4BA2B3B299F838000CB4FBA /* Model+SharedExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B39299F838000CB4FBA /* Model+SharedExtensions.swift */; }; C4BA2B3C299F838000CB4FBA /* Model+SharedExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B39299F838000CB4FBA /* Model+SharedExtensions.swift */; }; - C4BA2B3E299FC86800CB4FBA /* StatsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B3D299FC86800CB4FBA /* StatsView.swift */; }; + C4BA2B3E299FC86800CB4FBA /* GraphsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B3D299FC86800CB4FBA /* GraphsView.swift */; }; C4BA2B43299FCB2B00CB4FBA /* RecordsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B42299FCB2B00CB4FBA /* RecordsView.swift */; }; C4BA2B49299FCE0C00CB4FBA /* IntervalGroup+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B47299FCE0C00CB4FBA /* IntervalGroup+CoreDataProperties.swift */; }; C4BA2B4A299FCE0C00CB4FBA /* IntervalGroup+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B47299FCE0C00CB4FBA /* IntervalGroup+CoreDataProperties.swift */; }; @@ -357,7 +357,7 @@ C4BA2B30299F759700CB4FBA /* DefaultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultView.swift; sourceTree = ""; }; C4BA2B35299F82FB00CB4FBA /* Fakes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Fakes.swift; sourceTree = ""; }; C4BA2B39299F838000CB4FBA /* Model+SharedExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Model+SharedExtensions.swift"; sourceTree = ""; }; - C4BA2B3D299FC86800CB4FBA /* StatsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsView.swift; sourceTree = ""; }; + C4BA2B3D299FC86800CB4FBA /* GraphsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GraphsView.swift; sourceTree = ""; }; C4BA2B42299FCB2B00CB4FBA /* RecordsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordsView.swift; sourceTree = ""; }; C4BA2B46299FCD8B00CB4FBA /* LeCountdown.0.6.1.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = LeCountdown.0.6.1.xcdatamodel; sourceTree = ""; }; C4BA2B47299FCE0C00CB4FBA /* IntervalGroup+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IntervalGroup+CoreDataProperties.swift"; sourceTree = ""; }; @@ -675,7 +675,7 @@ C438C80E29828B8600BF3EF9 /* ActivitiesView.swift */, C4BA2B6029A3C02400CB4FBA /* ActivityView.swift */, C4BA2B42299FCB2B00CB4FBA /* RecordsView.swift */, - C4BA2B3D299FC86800CB4FBA /* StatsView.swift */, + C4BA2B3D299FC86800CB4FBA /* GraphsView.swift */, ); path = Stats; sourceTree = ""; @@ -1028,7 +1028,7 @@ C4E5D66629B73AED008E7465 /* StartTimerIntent.swift in Sources */, C4BA2AD62993F62700CB4FBA /* SoundSelectionView.swift in Sources */, C498E5A5299152B400E90DE0 /* GreenCheckmarkView.swift in Sources */, - C4BA2B3E299FC86800CB4FBA /* StatsView.swift in Sources */, + C4BA2B3E299FC86800CB4FBA /* GraphsView.swift in Sources */, C4F8B1B8298AC81D005C86A5 /* CountdownDialView.swift in Sources */, C473C2F929A8DC0A0056B38A /* LaunchWidgetAttributes.swift in Sources */, C445FA922987CC8A0054D761 /* Sound.swift in Sources */, diff --git a/LeCountdown/Model/NSManagedContext+Extensions.swift b/LeCountdown/Model/NSManagedContext+Extensions.swift index 285fa24..ad1c790 100644 --- a/LeCountdown/Model/NSManagedContext+Extensions.swift +++ b/LeCountdown/Model/NSManagedContext+Extensions.swift @@ -16,13 +16,14 @@ extension NSManagedObjectContext { return self.object(with: objectId) } - func distinct(entityName: String, attributes: [String]) throws -> [Any] { + func distinct(entityName: String, attributes: [String], predicate: NSPredicate? = nil) throws -> [Any] { let request = NSFetchRequest(entityName: entityName) request.entity = NSEntityDescription.entity(forEntityName: entityName, in: self) request.returnsDistinctResults = true request.resultType = .dictionaryResultType + request.predicate = predicate if let entity = request.entity { let entityProperties = entity.propertiesByName var properties = [NSPropertyDescription]() diff --git a/LeCountdown/Views/Stats/ActivityView.swift b/LeCountdown/Views/Stats/ActivityView.swift index 2b31842..fa2eeaa 100644 --- a/LeCountdown/Views/Stats/ActivityView.swift +++ b/LeCountdown/Views/Stats/ActivityView.swift @@ -29,11 +29,11 @@ struct ActivityView: View { List { - StatsView(activity: self.activity, + GraphsView(activity: self.activity, timeFrame: self.selectedTimeFrame) .environment(\.managedObjectContext, viewContext) - let filters = self.selectedTimeFrame.filters(context: viewContext) + let filters = self.selectedTimeFrame.filters(context: viewContext, activity: activity) RecordsView(activity: self.activity, filters: filters) .environment(\.managedObjectContext, viewContext) } @@ -59,14 +59,16 @@ enum TimeFrame: Int, Identifiable, CaseIterable { } } - func filters(context: NSManagedObjectContext) -> [Filter] { + 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"]) + 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) } @@ -74,7 +76,7 @@ enum TimeFrame: Int, Identifiable, CaseIterable { Logger.w("Could not cast \(distinct) as [Int]") } case .month: - let distinct = try context.distinct(entityName: "Record", attributes: ["year", "month"]) + let distinct = try context.distinct(entityName: "Record", attributes: ["year", "month"], predicate: predicate) if let distinctMonths = distinct as? [[String : Int]] { let months = distinctMonths.compactMap { @@ -95,7 +97,6 @@ enum TimeFrame: Int, Identifiable, CaseIterable { Logger.error(error) } return [] - } } diff --git a/LeCountdown/Views/Stats/StatsView.swift b/LeCountdown/Views/Stats/GraphsView.swift similarity index 91% rename from LeCountdown/Views/Stats/StatsView.swift rename to LeCountdown/Views/Stats/GraphsView.swift index 632074a..0697bde 100644 --- a/LeCountdown/Views/Stats/StatsView.swift +++ b/LeCountdown/Views/Stats/GraphsView.swift @@ -43,7 +43,7 @@ class StatModel: ObservableObject { } -struct StatsView: View { +struct GraphsView: View { var activity: Activity var timeFrame: TimeFrame @@ -62,7 +62,7 @@ struct StatsView: View { } else { VStack(alignment: .leading) { ForEach(self.model.statValues) { statValue in - StatGraphView(statValue: statValue, timeFrame: self.timeFrame) + GraphView(statValue: statValue, timeFrame: self.timeFrame) // .padding(.vertical) } } @@ -92,7 +92,7 @@ struct StatView: View { } -struct StatGraphView: View { +struct GraphView: View { var statValue: StatValue var timeFrame: TimeFrame @@ -130,7 +130,7 @@ struct StatGraphView_Previews: PreviewProvider { Point(date: Date(timeIntervalSince1970: 100000), value: 1)] static var previews: some View { - StatGraphView(statValue: StatValue(stat: .count, value: NSDecimalNumber(integerLiteral: 3), points: points), timeFrame: .all) + GraphView(statValue: StatValue(stat: .count, value: NSDecimalNumber(integerLiteral: 3), points: points), timeFrame: .all) } } @@ -142,7 +142,7 @@ struct StatView_Previews: PreviewProvider { struct StatsView_Previews: PreviewProvider { static var previews: some View { - StatsView(activity: Activity.fake(context: PersistenceController.preview.container.viewContext), + GraphsView(activity: Activity.fake(context: PersistenceController.preview.container.viewContext), timeFrame: .all) } } diff --git a/LeCountdown/Views/Stats/RecordsView.swift b/LeCountdown/Views/Stats/RecordsView.swift index 33defd4..5588dac 100644 --- a/LeCountdown/Views/Stats/RecordsView.swift +++ b/LeCountdown/Views/Stats/RecordsView.swift @@ -13,7 +13,7 @@ class RecordsModel: ObservableObject { @Published var filters: [Filter] = [] func loadFilters(activity: Activity, timeFrame: TimeFrame, context: NSManagedObjectContext) { - self.filters = timeFrame.filters(context: context) + self.filters = timeFrame.filters(context: context, activity: activity) } } @@ -27,12 +27,12 @@ struct RecordsView: View { var body: some View { - Section { +// Section { ForEach(self.filters) { filter in RecordsSectionView(activity: self.activity, filter: filter) .environment(\.managedObjectContext, viewContext) } - } +// } } }