split all the logic out of filter view into protocols and other shit

This commit is contained in:
Trey t
2022-01-17 15:46:38 -06:00
parent fd185ac80f
commit 9e10cfff1d
6 changed files with 209 additions and 147 deletions

View File

@@ -8,6 +8,9 @@
/* Begin PBXBuildFile section */
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 */; };
1C412083278F2B8800D9153A /* FilterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C412081278F2B8800D9153A /* FilterView.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 */; };
1CA2662D2793908700C0E12C /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD90AEF278C7DDF001C4FEA /* Persistence.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 */; };
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 */; };
@@ -100,11 +103,14 @@
/* End PBXCopyFilesBuildPhase 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>"; };
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>"; };
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>"; };
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>"; };
@@ -186,6 +192,15 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
1C26190127960CDA00FDC148 /* Protocol */ = {
isa = PBXGroup;
children = (
1C26190227960CE500FDC148 /* ChartDataBuildable.swift */,
1C26190627960DC900FDC148 /* ChartViewItemBuildable.swift */,
);
path = Protocol;
sourceTree = "<group>";
};
1CD90AE5278C7DDF001C4FEA = {
isa = PBXGroup;
children = (
@@ -207,6 +222,7 @@
1CD90AEA278C7DDF001C4FEA /* Shared */ = {
isa = PBXGroup;
children = (
1C26190127960CDA00FDC148 /* Protocol */,
1CD90AED278C7DDF001C4FEA /* FeelsApp.swift */,
1C744F2B278CE15600953A57 /* AppDelegate.swift */,
1CC469A9278F30A0003E0C6E /* BGTask.swift */,
@@ -269,7 +285,6 @@
1CD90B33278C7E38001C4FEA /* GraphView.swift */,
1CD90B36278C7E38001C4FEA /* HeaderStatsView.swift */,
1CD90B32278C7E38001C4FEA /* SettingsView.swift */,
1CC469AB27907D48003E0C6E /* CircleView.swift */,
);
path = views;
sourceTree = "<group>";
@@ -298,8 +313,10 @@
1CD90B60278C7EBA001C4FEA /* Models */ = {
isa = PBXGroup;
children = (
1CC469AB27907D48003E0C6E /* DayChartView.swift */,
1CD90B61278C7EBA001C4FEA /* Mood.swift */,
1CD90B62278C7EBA001C4FEA /* MoodEntryExtension.swift */,
1C2618FD27960A4F00FDC148 /* FilterViewModel.swift */,
);
path = Models;
sourceTree = "<group>";
@@ -504,15 +521,18 @@
1CD90B76278C8119001C4FEA /* LocalNotification.swift in Sources */,
1CD90B16278C7DE0001C4FEA /* Feels.xcdatamodeld in Sources */,
1CC469AA278F30A0003E0C6E /* BGTask.swift in Sources */,
1C26190727960DC900FDC148 /* ChartViewItemBuildable.swift in Sources */,
1CD90B5D278C7EAD001C4FEA /* Random.swift in Sources */,
1C2618FE27960A4F00FDC148 /* FilterViewModel.swift in Sources */,
1C744F2C278CE15600953A57 /* AppDelegate.swift in Sources */,
1CD90B63278C7EBA001C4FEA /* Mood.swift in Sources */,
1CD90B53278C7E7A001C4FEA /* FeelsWidget.intentdefinition in Sources */,
1CD90B3D278C7E38001C4FEA /* ContentView.swift in Sources */,
1CD90B3F278C7E38001C4FEA /* HeaderStatsView.swift in Sources */,
1CD90B3B278C7E38001C4FEA /* AddMoodHeaderView.swift in Sources */,
1CC469AC27907D48003E0C6E /* CircleView.swift in Sources */,
1CC469AC27907D48003E0C6E /* DayChartView.swift in Sources */,
1CD90B37278C7E38001C4FEA /* SettingsView.swift in Sources */,
1C26190327960CE500FDC148 /* ChartDataBuildable.swift in Sources */,
1CD90B66278C7EBA001C4FEA /* MoodEntryExtension.swift in Sources */,
1CD90B1C278C7DE0001C4FEA /* Persistence.swift in Sources */,
1C412082278F2B8800D9153A /* FilterView.swift in Sources */,

View File

@@ -8,18 +8,11 @@
import Foundation
import SwiftUI
struct DayChartView: View, Hashable {
struct DayChartView: ChartViewItemBuildable, View, Hashable {
var color: Color
var weekDay: Int
var viewType: ViewType
enum ViewType: Hashable {
case cicle
case square
case text(String)
}
let color: Color
let weekDay: Int
let viewType: ViewType
var body: some View {
switch viewType {
case .cicle:
@@ -36,6 +29,5 @@ struct DayChartView: View, Hashable {
.font(.footnote)
.frame(minWidth: 5, idealWidth: 50, maxWidth: 50, minHeight: 5, idealHeight: 20, maxHeight: 50, alignment: .center)
}
}
}

View 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
}
}

View 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
}

View 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)
}

View File

@@ -8,29 +8,12 @@
import SwiftUI
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 {
typealias Year = Int
typealias Month = Int
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")]
@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
@FetchRequest(
@@ -38,7 +21,7 @@ struct FilterView: View {
animation: .spring())
private var items: FetchedResults<MoodEntry>
@StateObject private var dataHolder = DataHolder()
@StateObject private var viewModel = FilterViewModel()
//[
// 2001: [0: [], 1: [], 2: []],
// 2002: [0: [], 1: [], 2: []]
@@ -58,97 +41,6 @@ struct FilterView: View {
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 {
VStack {
filterButon
@@ -159,7 +51,7 @@ struct FilterView: View {
.cornerRadius(25)
.padding([.leading, .trailing])
Text("Total: \(self.dataHolder.numberOfRatings)")
Text("Total: \(self.viewModel.numberOfRatings)")
.font(.title2)
if showFilter {
@@ -168,12 +60,7 @@ struct FilterView: View {
gridView
.onAppear(perform: {
let monthEntries = PersistenceController.shared.getData(startDate: Date(timeIntervalSince1970: 0), endDate: Date(), includedDays: selectedDays)
entryStartDate = monthEntries.first!.forDate!
entryEndDate = monthEntries.last!.forDate!
self.dataHolder.data = buildGridData(withData: monthEntries)
self.dataHolder.uncategorizedData = monthEntries
// filterEntries(startDate: entryStartDate, endDate: entryEndDate)
self.viewModel.filterEntries(startDate: Date(timeIntervalSince1970: 0), endDate: Date())
})
}
}
@@ -215,7 +102,7 @@ struct FilterView: View {
HStack {
Spacer()
ForEach(Mood.allValues, id: \.self) { mood in
StatsSubView(data: self.dataHolder.uncategorizedData, mood: mood)
StatsSubView(data: self.viewModel.uncategorizedData, mood: mood)
Spacer()
}
}
@@ -231,10 +118,10 @@ struct FilterView: View {
Color(UIColor.secondarySystemBackground)
DatePicker(
"Start Date",
selection: $entryStartDate,
selection: $viewModel.entryStartDate,
displayedComponents: [.date]
).onChange(of: entryStartDate, perform: { value in
filterEntries(startDate: self.entryStartDate, endDate: self.entryEndDate)
).onChange(of: viewModel.entryStartDate, perform: { value in
viewModel.filterEntries(startDate: viewModel.entryStartDate, endDate: viewModel.entryEndDate)
})
.padding()
}
@@ -246,10 +133,10 @@ struct FilterView: View {
Color(UIColor.secondarySystemBackground)
DatePicker(
"End Date",
selection: $entryEndDate,
selection: $viewModel.entryEndDate,
displayedComponents: [.date]
).onChange(of: entryStartDate, perform: { value in
filterEntries(startDate: self.entryStartDate, endDate: self.entryEndDate)
).onChange(of: viewModel.entryStartDate, perform: { value in
viewModel.filterEntries(startDate: viewModel.entryStartDate, endDate: viewModel.entryEndDate)
})
.padding()
}
@@ -266,15 +153,15 @@ struct FilterView: View {
let value = weekdays[dayIdx].1
Button(day.capitalized, action: {
if let index = selectedDays.firstIndex(of: value) {
selectedDays.remove(at: index)
if let index = viewModel.selectedDays.firstIndex(of: value) {
viewModel.selectedDays.remove(at: index)
} else {
selectedDays.append(value)
viewModel.selectedDays.append(value)
}
filterEntries(startDate: entryStartDate, endDate: entryEndDate)
viewModel.filterEntries(startDate: viewModel.entryStartDate, endDate: viewModel.entryEndDate)
})
.frame(maxWidth: .infinity)
.foregroundColor(selectedDays.contains(value) || selectedDays.isEmpty ? .green : .red)
.foregroundColor(viewModel.selectedDays.contains(value) || viewModel.selectedDays.isEmpty ? .green : .red)
}
Spacer()
}
@@ -305,8 +192,8 @@ struct FilterView: View {
VStack {
ScrollView {
ForEach(Array(self.dataHolder.data.keys.sorted(by: >)), id: \.self) { yearKey in
let yearData = self.dataHolder.data[yearKey]!
ForEach(Array(self.viewModel.data.keys.sorted(by: >)), id: \.self) { yearKey in
let yearData = self.viewModel.data[yearKey]!
Text(String(yearKey))
.font(.title)
yearGridView(yearData: yearData, columns: columns)