split all the logic out of filter view into protocols and other shit
This commit is contained in:
@@ -8,6 +8,9 @@
|
|||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
1C2618FA2795E41D00FDC148 /* Charts in Frameworks */ = {isa = PBXBuildFile; productRef = 1C2618F92795E41D00FDC148 /* Charts */; };
|
1C2618FA2795E41D00FDC148 /* Charts in Frameworks */ = {isa = PBXBuildFile; productRef = 1C2618F92795E41D00FDC148 /* Charts */; };
|
||||||
|
1C2618FE27960A4F00FDC148 /* FilterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C2618FD27960A4F00FDC148 /* FilterViewModel.swift */; };
|
||||||
|
1C26190327960CE500FDC148 /* ChartDataBuildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C26190227960CE500FDC148 /* ChartDataBuildable.swift */; };
|
||||||
|
1C26190727960DC900FDC148 /* ChartViewItemBuildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C26190627960DC900FDC148 /* ChartViewItemBuildable.swift */; };
|
||||||
1C412082278F2B8800D9153A /* FilterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C412081278F2B8800D9153A /* FilterView.swift */; };
|
1C412082278F2B8800D9153A /* FilterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C412081278F2B8800D9153A /* FilterView.swift */; };
|
||||||
1C412083278F2B8800D9153A /* FilterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C412081278F2B8800D9153A /* FilterView.swift */; };
|
1C412083278F2B8800D9153A /* FilterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C412081278F2B8800D9153A /* FilterView.swift */; };
|
||||||
1C683FCA2792281400745862 /* Stats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C683FC92792281400745862 /* Stats.swift */; };
|
1C683FCA2792281400745862 /* Stats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C683FC92792281400745862 /* Stats.swift */; };
|
||||||
@@ -16,7 +19,7 @@
|
|||||||
1C744F2C278CE15600953A57 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C744F2B278CE15600953A57 /* AppDelegate.swift */; };
|
1C744F2C278CE15600953A57 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C744F2B278CE15600953A57 /* AppDelegate.swift */; };
|
||||||
1CA2662D2793908700C0E12C /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90AEF278C7DDF001C4FEA /* Persistence.swift */; };
|
1CA2662D2793908700C0E12C /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90AEF278C7DDF001C4FEA /* Persistence.swift */; };
|
||||||
1CC469AA278F30A0003E0C6E /* BGTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC469A9278F30A0003E0C6E /* BGTask.swift */; };
|
1CC469AA278F30A0003E0C6E /* BGTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC469A9278F30A0003E0C6E /* BGTask.swift */; };
|
||||||
1CC469AC27907D48003E0C6E /* CircleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC469AB27907D48003E0C6E /* CircleView.swift */; };
|
1CC469AC27907D48003E0C6E /* DayChartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC469AB27907D48003E0C6E /* DayChartView.swift */; };
|
||||||
1CD90B07278C7DE0001C4FEA /* Tests_iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90B06278C7DE0001C4FEA /* Tests_iOS.swift */; };
|
1CD90B07278C7DE0001C4FEA /* Tests_iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90B06278C7DE0001C4FEA /* Tests_iOS.swift */; };
|
||||||
1CD90B09278C7DE0001C4FEA /* Tests_iOSLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90B08278C7DE0001C4FEA /* Tests_iOSLaunchTests.swift */; };
|
1CD90B09278C7DE0001C4FEA /* Tests_iOSLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90B08278C7DE0001C4FEA /* Tests_iOSLaunchTests.swift */; };
|
||||||
1CD90B13278C7DE0001C4FEA /* Tests_macOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90B12278C7DE0001C4FEA /* Tests_macOS.swift */; };
|
1CD90B13278C7DE0001C4FEA /* Tests_macOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90B12278C7DE0001C4FEA /* Tests_macOS.swift */; };
|
||||||
@@ -100,11 +103,14 @@
|
|||||||
/* End PBXCopyFilesBuildPhase section */
|
/* End PBXCopyFilesBuildPhase section */
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
|
1C2618FD27960A4F00FDC148 /* FilterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterViewModel.swift; sourceTree = "<group>"; };
|
||||||
|
1C26190227960CE500FDC148 /* ChartDataBuildable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChartDataBuildable.swift; sourceTree = "<group>"; };
|
||||||
|
1C26190627960DC900FDC148 /* ChartViewItemBuildable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChartViewItemBuildable.swift; sourceTree = "<group>"; };
|
||||||
1C412081278F2B8800D9153A /* FilterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterView.swift; sourceTree = "<group>"; };
|
1C412081278F2B8800D9153A /* FilterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterView.swift; sourceTree = "<group>"; };
|
||||||
1C683FC92792281400745862 /* Stats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Stats.swift; sourceTree = "<group>"; };
|
1C683FC92792281400745862 /* Stats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Stats.swift; sourceTree = "<group>"; };
|
||||||
1C744F2B278CE15600953A57 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
1C744F2B278CE15600953A57 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
||||||
1CC469A9278F30A0003E0C6E /* BGTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BGTask.swift; sourceTree = "<group>"; };
|
1CC469A9278F30A0003E0C6E /* BGTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BGTask.swift; sourceTree = "<group>"; };
|
||||||
1CC469AB27907D48003E0C6E /* CircleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CircleView.swift; sourceTree = "<group>"; };
|
1CC469AB27907D48003E0C6E /* DayChartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DayChartView.swift; sourceTree = "<group>"; };
|
||||||
1CD90AEC278C7DDF001C4FEA /* Shared.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Shared.xcdatamodel; sourceTree = "<group>"; };
|
1CD90AEC278C7DDF001C4FEA /* Shared.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Shared.xcdatamodel; sourceTree = "<group>"; };
|
||||||
1CD90AED278C7DDF001C4FEA /* FeelsApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeelsApp.swift; sourceTree = "<group>"; };
|
1CD90AED278C7DDF001C4FEA /* FeelsApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeelsApp.swift; sourceTree = "<group>"; };
|
||||||
1CD90AEF278C7DDF001C4FEA /* Persistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = "<group>"; };
|
1CD90AEF278C7DDF001C4FEA /* Persistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = "<group>"; };
|
||||||
@@ -186,6 +192,15 @@
|
|||||||
/* End PBXFrameworksBuildPhase section */
|
/* End PBXFrameworksBuildPhase section */
|
||||||
|
|
||||||
/* Begin PBXGroup section */
|
/* Begin PBXGroup section */
|
||||||
|
1C26190127960CDA00FDC148 /* Protocol */ = {
|
||||||
|
isa = PBXGroup;
|
||||||
|
children = (
|
||||||
|
1C26190227960CE500FDC148 /* ChartDataBuildable.swift */,
|
||||||
|
1C26190627960DC900FDC148 /* ChartViewItemBuildable.swift */,
|
||||||
|
);
|
||||||
|
path = Protocol;
|
||||||
|
sourceTree = "<group>";
|
||||||
|
};
|
||||||
1CD90AE5278C7DDF001C4FEA = {
|
1CD90AE5278C7DDF001C4FEA = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
@@ -207,6 +222,7 @@
|
|||||||
1CD90AEA278C7DDF001C4FEA /* Shared */ = {
|
1CD90AEA278C7DDF001C4FEA /* Shared */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
1C26190127960CDA00FDC148 /* Protocol */,
|
||||||
1CD90AED278C7DDF001C4FEA /* FeelsApp.swift */,
|
1CD90AED278C7DDF001C4FEA /* FeelsApp.swift */,
|
||||||
1C744F2B278CE15600953A57 /* AppDelegate.swift */,
|
1C744F2B278CE15600953A57 /* AppDelegate.swift */,
|
||||||
1CC469A9278F30A0003E0C6E /* BGTask.swift */,
|
1CC469A9278F30A0003E0C6E /* BGTask.swift */,
|
||||||
@@ -269,7 +285,6 @@
|
|||||||
1CD90B33278C7E38001C4FEA /* GraphView.swift */,
|
1CD90B33278C7E38001C4FEA /* GraphView.swift */,
|
||||||
1CD90B36278C7E38001C4FEA /* HeaderStatsView.swift */,
|
1CD90B36278C7E38001C4FEA /* HeaderStatsView.swift */,
|
||||||
1CD90B32278C7E38001C4FEA /* SettingsView.swift */,
|
1CD90B32278C7E38001C4FEA /* SettingsView.swift */,
|
||||||
1CC469AB27907D48003E0C6E /* CircleView.swift */,
|
|
||||||
);
|
);
|
||||||
path = views;
|
path = views;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@@ -298,8 +313,10 @@
|
|||||||
1CD90B60278C7EBA001C4FEA /* Models */ = {
|
1CD90B60278C7EBA001C4FEA /* Models */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
1CC469AB27907D48003E0C6E /* DayChartView.swift */,
|
||||||
1CD90B61278C7EBA001C4FEA /* Mood.swift */,
|
1CD90B61278C7EBA001C4FEA /* Mood.swift */,
|
||||||
1CD90B62278C7EBA001C4FEA /* MoodEntryExtension.swift */,
|
1CD90B62278C7EBA001C4FEA /* MoodEntryExtension.swift */,
|
||||||
|
1C2618FD27960A4F00FDC148 /* FilterViewModel.swift */,
|
||||||
);
|
);
|
||||||
path = Models;
|
path = Models;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@@ -504,15 +521,18 @@
|
|||||||
1CD90B76278C8119001C4FEA /* LocalNotification.swift in Sources */,
|
1CD90B76278C8119001C4FEA /* LocalNotification.swift in Sources */,
|
||||||
1CD90B16278C7DE0001C4FEA /* Feels.xcdatamodeld in Sources */,
|
1CD90B16278C7DE0001C4FEA /* Feels.xcdatamodeld in Sources */,
|
||||||
1CC469AA278F30A0003E0C6E /* BGTask.swift in Sources */,
|
1CC469AA278F30A0003E0C6E /* BGTask.swift in Sources */,
|
||||||
|
1C26190727960DC900FDC148 /* ChartViewItemBuildable.swift in Sources */,
|
||||||
1CD90B5D278C7EAD001C4FEA /* Random.swift in Sources */,
|
1CD90B5D278C7EAD001C4FEA /* Random.swift in Sources */,
|
||||||
|
1C2618FE27960A4F00FDC148 /* FilterViewModel.swift in Sources */,
|
||||||
1C744F2C278CE15600953A57 /* AppDelegate.swift in Sources */,
|
1C744F2C278CE15600953A57 /* AppDelegate.swift in Sources */,
|
||||||
1CD90B63278C7EBA001C4FEA /* Mood.swift in Sources */,
|
1CD90B63278C7EBA001C4FEA /* Mood.swift in Sources */,
|
||||||
1CD90B53278C7E7A001C4FEA /* FeelsWidget.intentdefinition in Sources */,
|
1CD90B53278C7E7A001C4FEA /* FeelsWidget.intentdefinition in Sources */,
|
||||||
1CD90B3D278C7E38001C4FEA /* ContentView.swift in Sources */,
|
1CD90B3D278C7E38001C4FEA /* ContentView.swift in Sources */,
|
||||||
1CD90B3F278C7E38001C4FEA /* HeaderStatsView.swift in Sources */,
|
1CD90B3F278C7E38001C4FEA /* HeaderStatsView.swift in Sources */,
|
||||||
1CD90B3B278C7E38001C4FEA /* AddMoodHeaderView.swift in Sources */,
|
1CD90B3B278C7E38001C4FEA /* AddMoodHeaderView.swift in Sources */,
|
||||||
1CC469AC27907D48003E0C6E /* CircleView.swift in Sources */,
|
1CC469AC27907D48003E0C6E /* DayChartView.swift in Sources */,
|
||||||
1CD90B37278C7E38001C4FEA /* SettingsView.swift in Sources */,
|
1CD90B37278C7E38001C4FEA /* SettingsView.swift in Sources */,
|
||||||
|
1C26190327960CE500FDC148 /* ChartDataBuildable.swift in Sources */,
|
||||||
1CD90B66278C7EBA001C4FEA /* MoodEntryExtension.swift in Sources */,
|
1CD90B66278C7EBA001C4FEA /* MoodEntryExtension.swift in Sources */,
|
||||||
1CD90B1C278C7DE0001C4FEA /* Persistence.swift in Sources */,
|
1CD90B1C278C7DE0001C4FEA /* Persistence.swift in Sources */,
|
||||||
1C412082278F2B8800D9153A /* FilterView.swift in Sources */,
|
1C412082278F2B8800D9153A /* FilterView.swift in Sources */,
|
||||||
|
|||||||
@@ -8,17 +8,10 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct DayChartView: View, Hashable {
|
struct DayChartView: ChartViewItemBuildable, View, Hashable {
|
||||||
|
var color: Color
|
||||||
enum ViewType: Hashable {
|
var weekDay: Int
|
||||||
case cicle
|
var viewType: ViewType
|
||||||
case square
|
|
||||||
case text(String)
|
|
||||||
}
|
|
||||||
|
|
||||||
let color: Color
|
|
||||||
let weekDay: Int
|
|
||||||
let viewType: ViewType
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
switch viewType {
|
switch viewType {
|
||||||
@@ -36,6 +29,5 @@ struct DayChartView: View, Hashable {
|
|||||||
.font(.footnote)
|
.font(.footnote)
|
||||||
.frame(minWidth: 5, idealWidth: 50, maxWidth: 50, minHeight: 5, idealHeight: 20, maxHeight: 50, alignment: .center)
|
.frame(minWidth: 5, idealWidth: 50, maxWidth: 50, minHeight: 5, idealHeight: 20, maxHeight: 50, alignment: .center)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
35
Shared/Models/FilterViewModel.swift
Normal file
35
Shared/Models/FilterViewModel.swift
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
//
|
||||||
|
// FilterViewModel.swift
|
||||||
|
// Feels
|
||||||
|
//
|
||||||
|
// Created by Trey Tartt on 1/17/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class FilterViewModel: ObservableObject {
|
||||||
|
@Published public var entryStartDate: Date = Date()
|
||||||
|
@Published public var entryEndDate: Date = Date()
|
||||||
|
@Published var selectedDays = [Int]()
|
||||||
|
|
||||||
|
// year, month, items
|
||||||
|
@Published public private(set) var data = [Int: [Int: [DayChartView]]]()
|
||||||
|
@Published public private(set) var numberOfRatings: Int = 0
|
||||||
|
public private(set) var uncategorizedData = [MoodEntry]() {
|
||||||
|
didSet {
|
||||||
|
self.numberOfRatings = uncategorizedData.count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private let chartViewBuilder = DayChartViewChartBuilder()
|
||||||
|
|
||||||
|
public func filterEntries(startDate: Date, endDate: Date) {
|
||||||
|
let filteredEntries = PersistenceController.shared.getData(startDate: startDate,
|
||||||
|
endDate: endDate,
|
||||||
|
includedDays: selectedDays)
|
||||||
|
data.removeAll()
|
||||||
|
let filledOutData = chartViewBuilder.buildGridData(withData: filteredEntries)
|
||||||
|
data = filledOutData
|
||||||
|
uncategorizedData = filteredEntries
|
||||||
|
}
|
||||||
|
}
|
||||||
106
Shared/Protocol/ChartDataBuildable.swift
Normal file
106
Shared/Protocol/ChartDataBuildable.swift
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
//
|
||||||
|
// ChartDataBuildable.swift
|
||||||
|
// Feels
|
||||||
|
//
|
||||||
|
// Created by Trey Tartt on 1/17/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
typealias Year = Int
|
||||||
|
typealias Month = Int
|
||||||
|
|
||||||
|
protocol ChartDataBuildable {
|
||||||
|
associatedtype ChartType: ChartViewItemBuildable
|
||||||
|
// [Year: [Month: [View]]
|
||||||
|
func buildGridData(withData data: [MoodEntry]) -> [Year: [Month: [ChartType]]]
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ChartDataBuildable {
|
||||||
|
public func buildGridData(withData data: [MoodEntry]) -> [Year: [Month: [ChartType]]] {
|
||||||
|
var returnData = [Int: [Int: [ChartType]]]()
|
||||||
|
|
||||||
|
if let earliestEntry = data.first,
|
||||||
|
let lastEntry = data.last {
|
||||||
|
|
||||||
|
let calendar = Calendar.current
|
||||||
|
let components = calendar.dateComponents([.year], from: earliestEntry.forDate!)
|
||||||
|
let earliestYear = components.year!
|
||||||
|
|
||||||
|
let latestComponents = calendar.dateComponents([.year], from: lastEntry.forDate!)
|
||||||
|
let latestYear = latestComponents.year!
|
||||||
|
|
||||||
|
for year in earliestYear...latestYear {
|
||||||
|
var allMonths = [Int: [ChartType]]()
|
||||||
|
|
||||||
|
// add back in if months header has leading (-1, ""),
|
||||||
|
// and add back gridItem
|
||||||
|
// var dayViews = [DayChartView]()
|
||||||
|
// for day in 0...32 {
|
||||||
|
// let view = DayChartView(color: Mood.missing.color,
|
||||||
|
// weekDay: 2,
|
||||||
|
// viewType: .text(String(day+1)))
|
||||||
|
// dayViews.append(view)
|
||||||
|
// }
|
||||||
|
// allMonths[0] = dayViews
|
||||||
|
|
||||||
|
for month in (1...12) {
|
||||||
|
var components = DateComponents()
|
||||||
|
components.month = month
|
||||||
|
components.year = year
|
||||||
|
let startDateOfMonth = Calendar.current.date(from: components)!
|
||||||
|
|
||||||
|
let items = data.filter({ entry in
|
||||||
|
let components = calendar.dateComponents([.month, .year], from: startDateOfMonth)
|
||||||
|
let entryComponents = calendar.dateComponents([.month, .year], from: entry.forDate!)
|
||||||
|
return (components.month == entryComponents.month && components.year == entryComponents.year)
|
||||||
|
})
|
||||||
|
|
||||||
|
allMonths[month] = createViewFor(monthEntries: items, forMonth: startDateOfMonth)
|
||||||
|
}
|
||||||
|
returnData[year] = allMonths
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return returnData
|
||||||
|
}
|
||||||
|
|
||||||
|
private func createViewFor(monthEntries: [MoodEntry], forMonth month: Date) -> [ChartType] {
|
||||||
|
var filledOutArray = [ChartType]()
|
||||||
|
|
||||||
|
let calendar = Calendar.current
|
||||||
|
let range = calendar.range(of: .day, in: .month, for: month)!
|
||||||
|
let numDays = range.count
|
||||||
|
|
||||||
|
for day in 1...numDays {
|
||||||
|
if let item = monthEntries.filter({ entry in
|
||||||
|
let components = calendar.dateComponents([.day], from: entry.forDate!)
|
||||||
|
let date = components.day
|
||||||
|
return day == date
|
||||||
|
}).first {
|
||||||
|
let view = ChartType(color: item.mood.color,
|
||||||
|
weekDay: Int(item.weekDay),
|
||||||
|
viewType: .square)
|
||||||
|
filledOutArray.append(view)
|
||||||
|
} else {
|
||||||
|
let thisDate = Calendar.current.date(bySetting: .day, value: day, of: month)!
|
||||||
|
let view = ChartType(color: Mood.missing.color,
|
||||||
|
weekDay: Calendar.current.component(.weekday, from: thisDate),
|
||||||
|
viewType: .square)
|
||||||
|
filledOutArray.append(view)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in filledOutArray.count...32 {
|
||||||
|
let view = ChartType(color: Mood.missing.color,
|
||||||
|
weekDay: 2,
|
||||||
|
viewType: .cicle)
|
||||||
|
filledOutArray.append(view)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filledOutArray
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DayChartViewChartBuilder: ChartDataBuildable {
|
||||||
|
typealias ChartType = DayChartView
|
||||||
|
}
|
||||||
22
Shared/Protocol/ChartViewItemBuildable.swift
Normal file
22
Shared/Protocol/ChartViewItemBuildable.swift
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
//
|
||||||
|
// ChartViewItemBuildable.swift
|
||||||
|
// Feels
|
||||||
|
//
|
||||||
|
// Created by Trey Tartt on 1/17/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
enum ViewType: Hashable {
|
||||||
|
case cicle
|
||||||
|
case square
|
||||||
|
case text(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
protocol ChartViewItemBuildable: View {
|
||||||
|
var color: Color { get }
|
||||||
|
var weekDay: Int { get }
|
||||||
|
var viewType: ViewType { get }
|
||||||
|
|
||||||
|
init(color: Color, weekDay: Int, viewType: ViewType)
|
||||||
|
}
|
||||||
@@ -8,29 +8,12 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
import CoreData
|
import CoreData
|
||||||
|
|
||||||
class DataHolder: ObservableObject {
|
|
||||||
// year, month, items
|
|
||||||
@Published var data = [Int: [Int: [DayChartView]]]()
|
|
||||||
@Published var numberOfRatings: Int = 0
|
|
||||||
var uncategorizedData = [MoodEntry]() {
|
|
||||||
didSet {
|
|
||||||
self.numberOfRatings = uncategorizedData.count
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct FilterView: View {
|
struct FilterView: View {
|
||||||
typealias Year = Int
|
|
||||||
typealias Month = Int
|
|
||||||
|
|
||||||
let weekdays = [("Sun", 1), ("mon", 2), ("tue", 3), ("wed", 4), ("thur", 5), ("fri", 6), ("sat", 7)]
|
let weekdays = [("Sun", 1), ("mon", 2), ("tue", 3), ("wed", 4), ("thur", 5), ("fri", 6), ("sat", 7)]
|
||||||
let months = [(0, "J"), (1, "F"), (2,"M"), (3,"A"), (4,"M"), (5, "J"), (6,"J"), (7,"A"), (8,"S"), (9,"O"), (10, "N"), (11,"D")]
|
let months = [(0, "J"), (1, "F"), (2,"M"), (3,"A"), (4,"M"), (5, "J"), (6,"J"), (7,"A"), (8,"S"), (9,"O"), (10, "N"), (11,"D")]
|
||||||
|
|
||||||
@State private var toggle = true
|
@State private var toggle = true
|
||||||
@State var selectedDays = [Int]()
|
|
||||||
@State private var entryStartDate: Date = Date()
|
|
||||||
@State private var entryEndDate: Date = Date()
|
|
||||||
@State private var showFilter = false
|
@State private var showFilter = false
|
||||||
|
|
||||||
@FetchRequest(
|
@FetchRequest(
|
||||||
@@ -38,7 +21,7 @@ struct FilterView: View {
|
|||||||
animation: .spring())
|
animation: .spring())
|
||||||
private var items: FetchedResults<MoodEntry>
|
private var items: FetchedResults<MoodEntry>
|
||||||
|
|
||||||
@StateObject private var dataHolder = DataHolder()
|
@StateObject private var viewModel = FilterViewModel()
|
||||||
//[
|
//[
|
||||||
// 2001: [0: [], 1: [], 2: []],
|
// 2001: [0: [], 1: [], 2: []],
|
||||||
// 2002: [0: [], 1: [], 2: []]
|
// 2002: [0: [], 1: [], 2: []]
|
||||||
@@ -58,97 +41,6 @@ struct FilterView: View {
|
|||||||
GridItem(.flexible(minimum: 5, maximum: 50)),
|
GridItem(.flexible(minimum: 5, maximum: 50)),
|
||||||
]
|
]
|
||||||
|
|
||||||
private func filterEntries(startDate: Date, endDate: Date) {
|
|
||||||
let filteredEntries = PersistenceController.shared.getData(startDate: startDate, endDate: endDate, includedDays: selectedDays)
|
|
||||||
self.dataHolder.data.removeAll()
|
|
||||||
let filledOutData = buildGridData(withData: filteredEntries)
|
|
||||||
self.dataHolder.data = filledOutData
|
|
||||||
self.dataHolder.uncategorizedData = filteredEntries
|
|
||||||
}
|
|
||||||
|
|
||||||
private func buildGridData(withData data: [MoodEntry]) -> [Year: [Month: [DayChartView]]] {
|
|
||||||
var returnData = [Year: [Month: [DayChartView]]]()
|
|
||||||
|
|
||||||
if let earliestEntry = data.first,
|
|
||||||
let lastEntry = data.last {
|
|
||||||
|
|
||||||
let calendar = Calendar.current
|
|
||||||
let components = calendar.dateComponents([.year], from: earliestEntry.forDate!)
|
|
||||||
let earliestYear = components.year!
|
|
||||||
|
|
||||||
let latestComponents = calendar.dateComponents([.year], from: lastEntry.forDate!)
|
|
||||||
let latestYear = latestComponents.year!
|
|
||||||
|
|
||||||
for year in earliestYear...latestYear {
|
|
||||||
var allMonths = [Int: [DayChartView]]()
|
|
||||||
|
|
||||||
// add back in if months header has leading (-1, ""),
|
|
||||||
// and add back gridItem
|
|
||||||
// var dayViews = [DayChartView]()
|
|
||||||
// for day in 0...32 {
|
|
||||||
// let view = DayChartView(color: Mood.missing.color,
|
|
||||||
// weekDay: 2,
|
|
||||||
// viewType: .text(String(day+1)))
|
|
||||||
// dayViews.append(view)
|
|
||||||
// }
|
|
||||||
// allMonths[0] = dayViews
|
|
||||||
|
|
||||||
for month in (1...12) {
|
|
||||||
var components = DateComponents()
|
|
||||||
components.month = month
|
|
||||||
components.year = year
|
|
||||||
let startDateOfMonth = Calendar.current.date(from: components)!
|
|
||||||
|
|
||||||
let items = data.filter({ entry in
|
|
||||||
let components = calendar.dateComponents([.month, .year], from: startDateOfMonth)
|
|
||||||
let entryComponents = calendar.dateComponents([.month, .year], from: entry.forDate!)
|
|
||||||
return (components.month == entryComponents.month && components.year == entryComponents.year)
|
|
||||||
})
|
|
||||||
|
|
||||||
allMonths[month] = createViewFor(monthEntries: items, forMonth: startDateOfMonth)
|
|
||||||
}
|
|
||||||
returnData[year] = allMonths
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return returnData
|
|
||||||
}
|
|
||||||
|
|
||||||
private func createViewFor(monthEntries: [MoodEntry], forMonth month: Date) -> [DayChartView] {
|
|
||||||
var filledOutArray = [DayChartView]()
|
|
||||||
|
|
||||||
let calendar = Calendar.current
|
|
||||||
let range = calendar.range(of: .day, in: .month, for: month)!
|
|
||||||
let numDays = range.count
|
|
||||||
|
|
||||||
for day in 1...numDays {
|
|
||||||
if let item = monthEntries.filter({ entry in
|
|
||||||
let components = calendar.dateComponents([.day], from: entry.forDate!)
|
|
||||||
let date = components.day
|
|
||||||
return day == date
|
|
||||||
}).first {
|
|
||||||
let view = DayChartView(color: item.mood.color,
|
|
||||||
weekDay: Int(item.weekDay),
|
|
||||||
viewType: .cicle)
|
|
||||||
filledOutArray.append(view)
|
|
||||||
} else {
|
|
||||||
let thisDate = Calendar.current.date(bySetting: .day, value: day, of: month)!
|
|
||||||
let view = DayChartView(color: Mood.missing.color,
|
|
||||||
weekDay: Calendar.current.component(.weekday, from: thisDate),
|
|
||||||
viewType: .cicle)
|
|
||||||
filledOutArray.append(view)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _ in filledOutArray.count...32 {
|
|
||||||
let view = DayChartView(color: Mood.missing.color,
|
|
||||||
weekDay: 2,
|
|
||||||
viewType: .cicle)
|
|
||||||
filledOutArray.append(view)
|
|
||||||
}
|
|
||||||
|
|
||||||
return filledOutArray
|
|
||||||
}
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
filterButon
|
filterButon
|
||||||
@@ -159,7 +51,7 @@ struct FilterView: View {
|
|||||||
.cornerRadius(25)
|
.cornerRadius(25)
|
||||||
.padding([.leading, .trailing])
|
.padding([.leading, .trailing])
|
||||||
|
|
||||||
Text("Total: \(self.dataHolder.numberOfRatings)")
|
Text("Total: \(self.viewModel.numberOfRatings)")
|
||||||
.font(.title2)
|
.font(.title2)
|
||||||
|
|
||||||
if showFilter {
|
if showFilter {
|
||||||
@@ -168,12 +60,7 @@ struct FilterView: View {
|
|||||||
|
|
||||||
gridView
|
gridView
|
||||||
.onAppear(perform: {
|
.onAppear(perform: {
|
||||||
let monthEntries = PersistenceController.shared.getData(startDate: Date(timeIntervalSince1970: 0), endDate: Date(), includedDays: selectedDays)
|
self.viewModel.filterEntries(startDate: Date(timeIntervalSince1970: 0), endDate: Date())
|
||||||
entryStartDate = monthEntries.first!.forDate!
|
|
||||||
entryEndDate = monthEntries.last!.forDate!
|
|
||||||
self.dataHolder.data = buildGridData(withData: monthEntries)
|
|
||||||
self.dataHolder.uncategorizedData = monthEntries
|
|
||||||
// filterEntries(startDate: entryStartDate, endDate: entryEndDate)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -215,7 +102,7 @@ struct FilterView: View {
|
|||||||
HStack {
|
HStack {
|
||||||
Spacer()
|
Spacer()
|
||||||
ForEach(Mood.allValues, id: \.self) { mood in
|
ForEach(Mood.allValues, id: \.self) { mood in
|
||||||
StatsSubView(data: self.dataHolder.uncategorizedData, mood: mood)
|
StatsSubView(data: self.viewModel.uncategorizedData, mood: mood)
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,10 +118,10 @@ struct FilterView: View {
|
|||||||
Color(UIColor.secondarySystemBackground)
|
Color(UIColor.secondarySystemBackground)
|
||||||
DatePicker(
|
DatePicker(
|
||||||
"Start Date",
|
"Start Date",
|
||||||
selection: $entryStartDate,
|
selection: $viewModel.entryStartDate,
|
||||||
displayedComponents: [.date]
|
displayedComponents: [.date]
|
||||||
).onChange(of: entryStartDate, perform: { value in
|
).onChange(of: viewModel.entryStartDate, perform: { value in
|
||||||
filterEntries(startDate: self.entryStartDate, endDate: self.entryEndDate)
|
viewModel.filterEntries(startDate: viewModel.entryStartDate, endDate: viewModel.entryEndDate)
|
||||||
})
|
})
|
||||||
.padding()
|
.padding()
|
||||||
}
|
}
|
||||||
@@ -246,10 +133,10 @@ struct FilterView: View {
|
|||||||
Color(UIColor.secondarySystemBackground)
|
Color(UIColor.secondarySystemBackground)
|
||||||
DatePicker(
|
DatePicker(
|
||||||
"End Date",
|
"End Date",
|
||||||
selection: $entryEndDate,
|
selection: $viewModel.entryEndDate,
|
||||||
displayedComponents: [.date]
|
displayedComponents: [.date]
|
||||||
).onChange(of: entryStartDate, perform: { value in
|
).onChange(of: viewModel.entryStartDate, perform: { value in
|
||||||
filterEntries(startDate: self.entryStartDate, endDate: self.entryEndDate)
|
viewModel.filterEntries(startDate: viewModel.entryStartDate, endDate: viewModel.entryEndDate)
|
||||||
})
|
})
|
||||||
.padding()
|
.padding()
|
||||||
}
|
}
|
||||||
@@ -266,15 +153,15 @@ struct FilterView: View {
|
|||||||
let value = weekdays[dayIdx].1
|
let value = weekdays[dayIdx].1
|
||||||
|
|
||||||
Button(day.capitalized, action: {
|
Button(day.capitalized, action: {
|
||||||
if let index = selectedDays.firstIndex(of: value) {
|
if let index = viewModel.selectedDays.firstIndex(of: value) {
|
||||||
selectedDays.remove(at: index)
|
viewModel.selectedDays.remove(at: index)
|
||||||
} else {
|
} else {
|
||||||
selectedDays.append(value)
|
viewModel.selectedDays.append(value)
|
||||||
}
|
}
|
||||||
filterEntries(startDate: entryStartDate, endDate: entryEndDate)
|
viewModel.filterEntries(startDate: viewModel.entryStartDate, endDate: viewModel.entryEndDate)
|
||||||
})
|
})
|
||||||
.frame(maxWidth: .infinity)
|
.frame(maxWidth: .infinity)
|
||||||
.foregroundColor(selectedDays.contains(value) || selectedDays.isEmpty ? .green : .red)
|
.foregroundColor(viewModel.selectedDays.contains(value) || viewModel.selectedDays.isEmpty ? .green : .red)
|
||||||
}
|
}
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
@@ -305,8 +192,8 @@ struct FilterView: View {
|
|||||||
|
|
||||||
VStack {
|
VStack {
|
||||||
ScrollView {
|
ScrollView {
|
||||||
ForEach(Array(self.dataHolder.data.keys.sorted(by: >)), id: \.self) { yearKey in
|
ForEach(Array(self.viewModel.data.keys.sorted(by: >)), id: \.self) { yearKey in
|
||||||
let yearData = self.dataHolder.data[yearKey]!
|
let yearData = self.viewModel.data[yearKey]!
|
||||||
Text(String(yearKey))
|
Text(String(yearKey))
|
||||||
.font(.title)
|
.font(.title)
|
||||||
yearGridView(yearData: yearData, columns: columns)
|
yearGridView(yearData: yearData, columns: columns)
|
||||||
|
|||||||
Reference in New Issue
Block a user