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

@@ -0,0 +1,123 @@
//
// ShowBasedOnVoteLogics.swift
// Feels (iOS)
//
// Created by Trey Tartt on 2/17/22.
//
import CoreData
import SwiftUI
//if this is being shown we're missing an entry
// voting time is noon
// vote for current day
// today at 11 am -> How as yesterday
// today at 1 pm -> How is today
// vote for previous day
// today at 11 am -> How as 2 days ago
// today at 1 pm -> How was yesterday
class ShowBasedOnVoteLogics {
private static var currentVoting: (passTimeToVote: Bool, dayOptions: DayOptions) {
let passedTimeToVote = UserDefaultsStore.getOnboarding().ableToVoteBasedOnCurentTime()
let inputDay = UserDefaultsStore.getOnboarding().inputDay
return (passedTimeToVote, inputDay)
}
static func isMissingCurrentVote() -> Bool {
var startDate: Date?
switch (currentVoting.passTimeToVote, currentVoting.dayOptions) {
case (true, .Previous):
// if we're passed time to vote and the voting type is previous - last vote should be -1
startDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
case (true, .Today):
// if we're passed time to vote and the voting type is today - last vote should be current date
startDate = Date()
case (false, .Previous):
// if we're NOT passed time to vote and the voting type is previous - last vote should be 2 days ago
startDate = Calendar.current.date(byAdding: .day, value: -2, to: Date())!
case (false, .Today):
// if we're NOT passed time to vote and the voting type is previous - last vote should be -1
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 < 1
}
static func getVotingTitle() -> String {
switch (currentVoting.passTimeToVote, currentVoting.dayOptions) {
case (true, .Previous):
// if we're passed time to vote and the voting type is previous - last vote should be -1
return "how was yesterday"
case (true, .Today):
// if we're passed time to vote and the voting type is previous - last vote should be today
return "how is today"
case (false, .Previous):
// if we're passed time to vote and the voting type is previous - last vote should be -2
let lastDayVoteShouldExist = ShowBasedOnVoteLogics.getLastDateVoteShouldExist()
return "how was \(Random.weekdayName(fromDate: lastDayVoteShouldExist))"
case (false, .Today):
// if we're passed time to vote and the voting type is previous - last vote should be -1
return "how as yesterday"
}
}
static func dateForHeaderVote() -> Date? {
var date: Date?
switch (currentVoting.passTimeToVote, currentVoting.dayOptions) {
case (true, .Previous):
// if we're passed time to vote and the voting type is previous - last vote should be -1
date = Calendar.current.date(byAdding: .day, value: -1, to: Date())
case (true, .Today):
// if we're passed time to vote and the voting type is previous - last vote should be today
date = Date()
case (false, .Previous):
// if we're passed time to vote and the voting type is previous - last vote should be -2
date = Calendar.current.date(byAdding: .day, value: -2, to: Date())
case (false, .Today):
// if we're passed time to vote and the voting type is previous - last vote should be -1
date = Calendar.current.date(byAdding: .day, value: -1, to: Date())
}
date = Calendar.current.date(byAdding: .day, value: -2, to: Date())
if let date = date {
return date
}
return nil
}
static func getLastDateVoteShouldExist() -> Date {
var endDate: Date?
switch (currentVoting.passTimeToVote, currentVoting.dayOptions) {
case (true, .Previous):
// if we're passed time to vote and the voting type is previous - last vote should -1
endDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
case (true, .Today):
// if we're passed time to vote and the voting type is previous - last vote should be today
endDate = Date()
case (false, .Previous):
// if we're passed time to vote and the voting type is previous - last vote should be -2
endDate = Calendar.current.date(byAdding: .day, value: -2, to: Date())!
case (false, .Today):
// if we're passed time to vote and the voting type is previous - last vote should be -1
endDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
}
return endDate!
}
}