This commit is contained in:
Trey t
2022-02-18 18:58:38 -06:00
parent 065b7bcac1
commit 1cf38cb854
16 changed files with 501 additions and 176 deletions

View File

@@ -10,9 +10,10 @@ import CoreData
class ContentModeViewModel: ObservableObject {
@Published var grouped = [Int: [Int: [MoodEntry]]]()
@Published public private(set) var savedOnboardingData = UserDefaultsStore.getOnboarding()
@Published var numberOfItems = 0
let addMonthStartWeekdayPadding: Bool
var hasNoData: Bool {
grouped.isEmpty
}
@@ -35,60 +36,66 @@ class ContentModeViewModel: ObservableObject {
// }.reduce(0, +)
}
init() {
init(addMonthStartWeekdayPadding: Bool) {
self.addMonthStartWeekdayPadding = addMonthStartWeekdayPadding
PersistenceController.shared.switchContainerListeners.append {
self.getGroupedData()
self.getGroupedData(addMonthStartWeekdayPadding: self.addMonthStartWeekdayPadding)
}
updateData()
}
private func getGroupedData() {
private func getGroupedData(addMonthStartWeekdayPadding: Bool) {
grouped = PersistenceController.shared.splitIntoYearMonth()
if addMonthStartWeekdayPadding {
var newGrouped = [Int: [Int: [MoodEntry]]]()
let allYears = grouped.keys.sorted(by: > )
for year in allYears {
var newMonth = [Int: [MoodEntry]]()
let oldMonths = grouped[year]!
let monthKeys = oldMonths.keys.sorted(by: > )
for key in monthKeys {
if let entries = oldMonths[key] {
let sortedEntries = entries.sorted(by: { $0.forDate! < $1.forDate! })
var mutableEntries = sortedEntries
if let firstDate = sortedEntries.first {
let date = firstDate.forDate!
let weekday = Int16(Calendar.current.component(.weekday, from: date))
for _ in 1..<weekday {
mutableEntries.insert(PersistenceController.shared.generateObjectNotInArray(), at: 0)
}
}
newMonth[key] = mutableEntries
}
newGrouped[year] = newMonth
}
}
grouped = newGrouped
}
numberOfItems = numberOfEntries
}
public func updateOnboardingData(onboardingData: OnboardingData) {
self.savedOnboardingData = UserDefaultsStore.saveOnboarding(onboardingData: onboardingData)
LocalNotification.scheduleReminder(atTime: onboardingData.date, withTitle: onboardingData.title)
}
public func updateData() {
getGroupedData()
getGroupedData(addMonthStartWeekdayPadding: self.addMonthStartWeekdayPadding)
}
public func add(mood: Mood, forDate date: Date, entryType: EntryType) {
PersistenceController.shared.add(mood: mood, forDate: date, entryType: entryType)
getGroupedData()
updateData()
}
public func update(entry: MoodEntry, toMood mood: Mood) {
let forDate = entry.forDate!
if let entry = PersistenceController.shared.getEntry(byDate: entry.forDate!) {
PersistenceController.shared.viewContext.delete(entry)
}
do {
try PersistenceController.shared.viewContext.save()
getGroupedData()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
PersistenceController.shared.add(mood: mood, forDate: forDate, entryType: .listView)
do {
try PersistenceController.shared.viewContext.save()
getGroupedData()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
if PersistenceController.shared.update(entryDate: entry.forDate!, withModd: mood) {
updateData()
}
}
@@ -112,7 +119,7 @@ class ContentModeViewModel: ObservableObject {
do {
try PersistenceController.shared.viewContext.save()
getGroupedData()
updateData()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
@@ -120,4 +127,27 @@ class ContentModeViewModel: ObservableObject {
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
static func updateTitleHeader(forEntry entry: MoodEntry?) -> String {
guard let entry = entry else {
return ""
}
guard let forDate = entry.forDate else {
return ""
}
let components = Calendar.current.dateComponents([.day, .month, .year], from: forDate)
// let day = components.day!
let month = components.month!
let year = components.year!
let monthName = Random.monthName(fromMonthInt: month)
let weekday = Random.weekdayName(fromDate:entry.forDate!)
let dayz = Random.dayFormat(fromDate:entry.forDate!)
let string = weekday + " " + monthName + " " + dayz + " " + String(year)
return String(format: String(localized: "content_view_fill_in_missing_entry"), string)
}
}

View File

@@ -15,6 +15,7 @@ enum Mood: Int {
case good
case great
case missing
case placeholder
var strValue: String {
switch self {
@@ -30,6 +31,8 @@ enum Mood: Int {
return String(localized: "mood_value_great")
case .missing:
return String(localized: "mood_value_missing")
case .placeholder:
return String("placeholder")
}
}
@@ -47,6 +50,8 @@ enum Mood: Int {
return .green
case .missing:
return Color(uiColor: UIColor.systemGray2)
case .placeholder:
return .clear
}
}
@@ -69,6 +74,8 @@ enum Mood: Int {
return Image("great", bundle: .main)
case .missing:
return Image("missing", bundle: .main)
case .placeholder:
return Image("missing", bundle: .main)
}
}
@@ -87,6 +94,8 @@ enum Mood: Int {
return Image("GreatGraphic", bundle: .main)
case .missing:
return Image("MissingGraphic", bundle: .main)
case .placeholder:
return Image("MissingGraphic", bundle: .main)
}
}
}

View File

@@ -0,0 +1,19 @@
//
// OnboardingDataDataManager.swift
// Feels (iOS)
//
// Created by Trey Tartt on 2/18/22.
//
import Foundation
final class OnboardingDataDataManager {
static let shared = OnboardingDataDataManager()
@Published public private(set) var savedOnboardingData = UserDefaultsStore.getOnboarding()
public func updateOnboardingData(onboardingData: OnboardingData) {
let onboardingData = UserDefaultsStore.saveOnboarding(onboardingData: onboardingData)
LocalNotification.scheduleReminder(atTime: onboardingData.date, withTitle: onboardingData.title)
}
}