hopefully fix issue where voting is filled in with missing when its time to vote
This commit is contained in:
@@ -16,6 +16,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
|
||||
|
||||
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
|
||||
// PersistenceController.shared.clearDB()
|
||||
// PersistenceController.shared.deleteLast(numberOfEntries: 5)
|
||||
PersistenceController.shared.removeNoForDates()
|
||||
PersistenceController.shared.fillInMissingDates()
|
||||
UNUserNotificationCenter.current().delegate = self
|
||||
|
||||
@@ -56,15 +56,25 @@ extension Date: RawRepresentable {
|
||||
Calendar.current.dateComponents([.calendar, .year,.month], from: self).date!
|
||||
}
|
||||
|
||||
static func dates(from fromDate: Date, to toDate: Date) -> [Date] {
|
||||
static public func dates(from fromDate: Date, toDate: Date, includingToDate: Bool = false) -> [Date] {
|
||||
var dates: [Date] = []
|
||||
var date = fromDate
|
||||
var date = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: fromDate)!
|
||||
let toDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: toDate)!
|
||||
|
||||
while date <= toDate {
|
||||
dates.append(date)
|
||||
guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: date) else { break }
|
||||
date = newDate
|
||||
if includingToDate {
|
||||
while date <= toDate {
|
||||
dates.append(date)
|
||||
guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: date) else { break }
|
||||
date = newDate
|
||||
}
|
||||
} else {
|
||||
while date < toDate {
|
||||
dates.append(date)
|
||||
guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: date) else { break }
|
||||
date = newDate
|
||||
}
|
||||
}
|
||||
|
||||
return dates
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class MoodEntryFunctions {
|
||||
var startOfMonth = date.startOfMonth
|
||||
startOfMonth = Calendar.current.date(byAdding: .hour, value: 9, to: startOfMonth)!
|
||||
let lastMissingDate = mutableEntries.first?.forDate ?? date.endOfMonth
|
||||
var missingDates = Date.dates(from: startOfMonth, to: lastMissingDate)
|
||||
var missingDates = Date.dates(from: startOfMonth, toDate: lastMissingDate, includingToDate: true)
|
||||
missingDates = missingDates.dropLast()
|
||||
|
||||
for date in missingDates {
|
||||
|
||||
@@ -31,28 +31,7 @@ final class OnboardingData: NSObject, ObservableObject, Codable {
|
||||
date = try container.decode(Date.self, forKey: .date)
|
||||
inputDay = try container.decode(DayOptions.self, forKey: .inputDay)
|
||||
}
|
||||
|
||||
func ableToVoteBasedOnCurentTime() -> Bool {
|
||||
let currentDateComp = Calendar.current.dateComponents([.hour, .minute], from: Date())
|
||||
let savedDateComp = Calendar.current.dateComponents([.hour, .minute], from: self.date)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
override init() { }
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ extension PersistenceController {
|
||||
let entries = try! viewContext.fetch(fetchRequest)
|
||||
|
||||
if let firstEntry = entries.last?.forDate {
|
||||
let allDates: [Date] = Date.dates(from: firstEntry, to: endDate).map({
|
||||
let allDates: [Date] = Date.dates(from: firstEntry, toDate: endDate, includingToDate: true).map({
|
||||
let zeroDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: $0)!
|
||||
return zeroDate
|
||||
})
|
||||
|
||||
@@ -19,4 +19,14 @@ extension PersistenceController {
|
||||
fatalError("Unresolved error \(error), \(error.userInfo)")
|
||||
}
|
||||
}
|
||||
|
||||
func deleteLast(numberOfEntries: Int) {
|
||||
let entries = PersistenceController.shared.getData(startDate: Calendar.current.date(byAdding: .day, value: -numberOfEntries, to: Date())!,
|
||||
endDate: Date(),
|
||||
includedDays: [])
|
||||
for entry in entries {
|
||||
viewContext.delete(entry)
|
||||
}
|
||||
try! viewContext.save()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,3 +189,21 @@ extension UIColor {
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
extension Bundle {
|
||||
var appName: String {
|
||||
return infoDictionary?["CFBundleName"] as! String
|
||||
}
|
||||
|
||||
var bundleId: String {
|
||||
return bundleIdentifier!
|
||||
}
|
||||
|
||||
var versionNumber: String {
|
||||
return infoDictionary?["CFBundleShortVersionString"] as! String
|
||||
}
|
||||
|
||||
var buildNumber: String {
|
||||
return infoDictionary?["CFBundleVersion"] as! String
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ struct SettingsView: View {
|
||||
showOnboardingButton
|
||||
whyBackgroundMode
|
||||
specialThanksCell
|
||||
|
||||
}
|
||||
|
||||
Group {
|
||||
@@ -44,6 +45,9 @@ struct SettingsView: View {
|
||||
}
|
||||
}
|
||||
Spacer()
|
||||
|
||||
Text("\(Bundle.main.appName) v \(Bundle.main.versionNumber) (Build \(Bundle.main.buildNumber))")
|
||||
.font(.body)
|
||||
}
|
||||
.padding()
|
||||
}.sheet(isPresented: $showOnboarding) {
|
||||
|
||||
Reference in New Issue
Block a user