fix issue with two votes on the same date

fix issue with header not showing correct vote date
split logic for Persistence into different files
create class that deals with voting time, existing votes, and what should be shown based on that
This commit is contained in:
Trey t
2022-02-17 14:46:11 -06:00
parent f0ed56fe94
commit 675e44bca9
12 changed files with 547 additions and 427 deletions

View File

@@ -47,51 +47,12 @@ class ContentModeViewModel: ObservableObject {
grouped = PersistenceController.shared.splitIntoYearMonth()
numberOfItems = numberOfEntries
}
public func shouldShowVotingHeader() -> Bool {
if isMissingCurrentVote() {
return true
}
return savedOnboardingData.ableToVoteBasedOnCurentTime() ? true : false
}
public func updateOnboardingData(onboardingData: OnboardingData) {
self.savedOnboardingData = UserDefaultsStore.saveOnboarding(onboardingData: onboardingData)
LocalNotification.scheduleReminder(atTime: onboardingData.date, withTitle: onboardingData.title)
}
private func isMissingCurrentVote() -> Bool {
let latestVoteUnLocked = UserDefaultsStore.getOnboarding().ableToVoteBasedOnCurentTime()
let inputDay = UserDefaultsStore.getOnboarding().inputDay
var startDate: Date?
switch (latestVoteUnLocked, inputDay) {
case (true, .Previous):
startDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
case (true, .Today):
startDate = Date()
case (false, .Previous):
startDate = Calendar.current.date(byAdding: .day, value: -2, to: Date())!
case (false, .Today):
startDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
}
startDate = Calendar.current.startOfDay(for: startDate!)
let endDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate!)!
let fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
let fromPredicate = NSPredicate(format: "%@ <= %K", startDate!
as NSDate, #keyPath(MoodEntry.forDate))
let toPredicate = NSPredicate(format: "%K < %@", #keyPath(MoodEntry.forDate), endDate as NSDate)
let datePredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [fromPredicate, toPredicate])
fetchRequest.predicate = datePredicate
let entries = try! PersistenceController.shared.viewContext.count(for: fetchRequest)
return entries == 0
}
public func updateData() {
getGroupedData()
}