// // 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(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! } }