// // 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 { static func isMissingCurrentVote(onboardingData: OnboardingData) -> Bool { let passedTimeToVote = onboardingData.ableToVoteBasedOnCurentTime() let inputDay = onboardingData.inputDay var startDate: Date? switch (passedTimeToVote, inputDay) { 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(onboardingData: OnboardingData) -> String { let passedTimeToVote = onboardingData.ableToVoteBasedOnCurentTime() let inputDay = onboardingData.inputDay switch (passedTimeToVote, inputDay) { 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(onboardingData: onboardingData) 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(onboardingData: OnboardingData) -> Date? { let passedTimeToVote = onboardingData.ableToVoteBasedOnCurentTime() let inputDay = onboardingData.inputDay var date: Date? switch (passedTimeToVote, inputDay) { 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()) } if let date = date { return date } return nil } static func getLastDateVoteShouldExist(onboardingData: OnboardingData) -> Date { let passedTimeToVote = onboardingData.ableToVoteBasedOnCurentTime() let inputDay = onboardingData.inputDay var endDate: Date? switch (passedTimeToVote, inputDay) { 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! } }