change a bunch of shit to get importing / adding new vote have the right weekday
This commit is contained in:
@@ -28,147 +28,14 @@ import SwiftUI
|
||||
|
||||
*/
|
||||
class ShowBasedOnVoteLogics {
|
||||
static func returnCurrentVoteStatus(onboardingData: OnboardingData) -> (Bool, DayOptions) {
|
||||
let passedTimeToVote = ShowBasedOnVoteLogics.ableToVoteBasedOnCurentTime(voteDate: onboardingData.date)
|
||||
static private func returnCurrentVoteStatus(onboardingData: OnboardingData) -> (Bool, DayOptions) {
|
||||
let passedTimeToVote = ShowBasedOnVoteLogics.passedTodaysVotingUnlock(voteDate: onboardingData.date)
|
||||
let inputDay: DayOptions = onboardingData.inputDay
|
||||
|
||||
return (passedTimeToVote, inputDay)
|
||||
}
|
||||
|
||||
static func getLastDateVoteShouldExistOnViews(onboardingData: OnboardingData) -> Date {
|
||||
var date: Date?
|
||||
|
||||
switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) {
|
||||
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: -2, to: Date())
|
||||
case (true, .Today):
|
||||
// if we're passed time to vote and the voting type is previous - last vote should be today
|
||||
date = Calendar.current.date(byAdding: .day, value: -1, to: 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: -3, to: Date())
|
||||
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: -2, to: Date())
|
||||
}
|
||||
|
||||
guard let date = date else {
|
||||
fatalError("missing getCurrentVotingDate")
|
||||
}
|
||||
|
||||
return date
|
||||
}
|
||||
|
||||
static func getCurrentVotingDate(onboardingData: OnboardingData) -> Date {
|
||||
var date: Date?
|
||||
|
||||
switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) {
|
||||
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())
|
||||
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 (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())
|
||||
}
|
||||
|
||||
guard let date = date else {
|
||||
fatalError("missing getCurrentVotingDate")
|
||||
}
|
||||
|
||||
return date
|
||||
}
|
||||
|
||||
static func isMissingCurrentVote(onboardingData: OnboardingData) -> Bool {
|
||||
let startDate = ShowBasedOnVoteLogics.getCurrentVotingDate(onboardingData: onboardingData).startOfDay
|
||||
let endDate = startDate.endOfDay
|
||||
|
||||
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(onboardingData: OnboardingData) -> String {
|
||||
switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) {
|
||||
case (false, .Today):
|
||||
return String(localized: "add_mood_header_view_title_yesterday")
|
||||
case (true, .Today):
|
||||
return String(localized: "add_mood_header_view_title_today")
|
||||
|
||||
case (false, .Previous):
|
||||
let date = Calendar.current.date(byAdding: .day, value: -2, to: Date())!
|
||||
return String(format: String(localized: "add_mood_header_view_title"), Random.weekdayName(fromDate: date))
|
||||
case (true, .Previous):
|
||||
return String(localized: "add_mood_header_view_title_yesterday")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------delete---------------------------------------*/
|
||||
|
||||
static func dateForHeaderVote(onboardingData: OnboardingData) -> Date? {
|
||||
var date: Date?
|
||||
|
||||
switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) {
|
||||
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())
|
||||
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 (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())
|
||||
}
|
||||
|
||||
if let date = date {
|
||||
return date
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
static func getLastDateVoteShouldExist(onboardingData: OnboardingData) -> Date {
|
||||
var date: Date?
|
||||
|
||||
switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) {
|
||||
case (false, .Today):
|
||||
// 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 (true, .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())!
|
||||
|
||||
case (false, .Previous):
|
||||
// if we're passed time to vote and the voting type is previous - last vote should be -3
|
||||
date = Calendar.current.date(byAdding: .day, value: -3, to: Date())!
|
||||
case (true, .Previous):
|
||||
// if we're passed time to vote and the voting type is previous - last vote should -2
|
||||
date = Calendar.current.date(byAdding: .day, value: -2, to: Date())!
|
||||
}
|
||||
|
||||
return date!
|
||||
}
|
||||
|
||||
static func ableToVoteBasedOnCurentTime(voteDate: Date) -> Bool {
|
||||
static public func passedTodaysVotingUnlock(voteDate: Date) -> Bool {
|
||||
let currentDateComp = Calendar.current.dateComponents([.hour, .minute], from: Date())
|
||||
let savedDateComp = Calendar.current.dateComponents([.hour, .minute], from: voteDate)
|
||||
|
||||
@@ -189,5 +56,60 @@ class ShowBasedOnVoteLogics {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
static public func isMissingCurrentVote(onboardingData: OnboardingData) -> Bool {
|
||||
let startDate = ShowBasedOnVoteLogics.getCurrentVotingDate(onboardingData: onboardingData).startOfDay
|
||||
let endDate = startDate.endOfDay
|
||||
|
||||
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 public func getCurrentVotingDate(onboardingData: OnboardingData) -> Date {
|
||||
var date: Date?
|
||||
// note to future self, this should account for midnight until next voting unlock
|
||||
// the vote at 12:03 am will not be passed time, and will be for yesterday
|
||||
switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) {
|
||||
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())
|
||||
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 (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())
|
||||
}
|
||||
|
||||
guard let date = date else {
|
||||
fatalError("missing getCurrentVotingDate")
|
||||
}
|
||||
|
||||
return date
|
||||
}
|
||||
|
||||
static public func getVotingTitle(onboardingData: OnboardingData) -> String {
|
||||
switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) {
|
||||
case (false, .Today):
|
||||
return String(localized: "add_mood_header_view_title_yesterday")
|
||||
case (true, .Today):
|
||||
return String(localized: "add_mood_header_view_title_today")
|
||||
|
||||
case (false, .Previous):
|
||||
let date = Calendar.current.date(byAdding: .day, value: -2, to: Date())!
|
||||
return String(format: String(localized: "add_mood_header_view_title"), Random.weekdayName(fromDate: date))
|
||||
case (true, .Previous):
|
||||
return String(localized: "add_mood_header_view_title_yesterday")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user