hopefully fix issue where voting is filled in with missing when its time to vote

This commit is contained in:
Trey t
2022-03-05 11:28:29 -06:00
parent 012fcc43d5
commit d56e74053e
13 changed files with 168 additions and 105 deletions

View File

@@ -9,33 +9,48 @@ 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
/*
current day 3/5/22
day option = .today
-------
voting for 3/4 | voting for 3/5
------------------------*-------------------------
db should contain 3/3 | db should contain 3/4
----------------------------------------------------------------------------
day option = .yesterday
--------
voting for 3/3 | voting for 3/4
------------------------*-------------------------
db should contain 3/2 | db should contain 3/3
*/
class ShowBasedOnVoteLogics {
static func isMissingCurrentVote(onboardingData: OnboardingData) -> Bool {
let passedTimeToVote = onboardingData.ableToVoteBasedOnCurentTime()
let inputDay = onboardingData.inputDay
static func returnCurrentVoteStatus(onboardingData: OnboardingData) -> (Bool, DayOptions) {
let passedTimeToVote = ShowBasedOnVoteLogics.ableToVoteBasedOnCurentTime(voteDate: onboardingData.date)
let inputDay: DayOptions = onboardingData.inputDay
return (passedTimeToVote, inputDay)
}
static func isMissingCurrentVote(onboardingData: OnboardingData) -> Bool {
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
switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) {
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())!
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
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())!
}
@@ -54,43 +69,35 @@ class ShowBasedOnVoteLogics {
}
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))"
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
return "how as yesterday"
case (true, .Today):
return "how is today"
case (false, .Previous):
let date = Calendar.current.date(byAdding: .day, value: -2, to: Date())!
return "how was \(Random.weekdayName(fromDate: date))"
case (true, .Previous):
return "how was yesterday"
}
}
static func dateForHeaderVote(onboardingData: OnboardingData) -> Date? {
let passedTimeToVote = onboardingData.ableToVoteBasedOnCurentTime()
let inputDay = onboardingData.inputDay
var date: Date?
switch (passedTimeToVote, inputDay) {
case (true, .Previous):
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 (false, .Today):
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())
}
@@ -103,26 +110,45 @@ class ShowBasedOnVoteLogics {
}
static func getLastDateVoteShouldExist(onboardingData: OnboardingData) -> Date {
let passedTimeToVote = onboardingData.ableToVoteBasedOnCurentTime()
let inputDay = onboardingData.inputDay
var date: Date?
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())!
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
endDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
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 endDate!
return date!
}
static func ableToVoteBasedOnCurentTime(voteDate: Date) -> Bool {
let currentDateComp = Calendar.current.dateComponents([.hour, .minute], from: Date())
let savedDateComp = Calendar.current.dateComponents([.hour, .minute], from: voteDate)
if let currentHour = currentDateComp.hour,
let currentMin = currentDateComp.minute,
let savedHour = savedDateComp.hour,
let savedMin = savedDateComp.minute {
if currentHour > savedHour {
return true
}
if currentHour == savedHour {
return currentMin >= savedMin
}
}
return false
}
}