diff --git a/Shared/AppDelegate.swift b/Shared/AppDelegate.swift index 48a42d6..a60eb9e 100644 --- a/Shared/AppDelegate.swift +++ b/Shared/AppDelegate.swift @@ -67,7 +67,6 @@ extension AppDelegate: UNUserNotificationCenterDelegate { date = Calendar.current.date(byAdding: .day, value: -1, to: Date())! } - switch action { case .horrible: PersistenceController.shared.add(mood: .horrible, forDate: date, entryType: .notification) diff --git a/Shared/Persisence/PersistenceADD.swift b/Shared/Persisence/PersistenceADD.swift index 16c0cbb..78f3e26 100644 --- a/Shared/Persisence/PersistenceADD.swift +++ b/Shared/Persisence/PersistenceADD.swift @@ -8,6 +8,20 @@ import CoreData extension PersistenceController { + public func fixWrongWeekdays() { + let data = PersistenceController.shared.getData(startDate: Date(timeIntervalSince1970: 0), + endDate: Date(), + includedDays: []).sorted(by: { + $0.forDate! < $1.forDate! + }) + + data.forEach({ + $0.weekDay = Int16(Calendar.current.component(.weekday, from: $0.forDate!.startOfDay)) + $0.forDate = $0.forDate?.startOfDay + }) + try? viewContext.save() + } + public func add(mood: Mood, forDate date: Date, entryType: EntryType) { if let existingEntry = getEntry(byDate: date) { viewContext.delete(existingEntry) @@ -18,7 +32,10 @@ extension PersistenceController { newItem.timestamp = Date() newItem.moodValue = Int16(mood.rawValue) newItem.forDate = date - newItem.weekDay = Int16(Calendar.current.component(.weekday, from: date)) + + let localTime = date.toLocalTime() + newItem.weekDay = Int16(Calendar.current.component(.weekday, from: localTime)) + newItem.canEdit = true newItem.canDelete = true newItem.entryType = Int16(entryType.rawValue) diff --git a/Shared/ShowBasedOnVoteLogics.swift b/Shared/ShowBasedOnVoteLogics.swift index 48b7637..8e8ec46 100644 --- a/Shared/ShowBasedOnVoteLogics.swift +++ b/Shared/ShowBasedOnVoteLogics.swift @@ -35,30 +35,64 @@ class ShowBasedOnVoteLogics { return (passedTimeToVote, inputDay) } - static func isMissingCurrentVote(onboardingData: OnboardingData) -> Bool { - var startDate: Date? - + static func getLastDateVoteShouldExistOnViews(onboardingData: OnboardingData) -> Date { + var date: Date? + 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())! + // 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 today - last vote should be current date - startDate = Date() + // 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 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())! + // 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 - startDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())! + date = Calendar.current.date(byAdding: .day, value: -2, to: Date()) + } + + guard let date = date else { + fatalError("missing getCurrentVotingDate") } - startDate = Calendar.current.startOfDay(for: startDate!) - let endDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate!)! + 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(entityName: "MoodEntry") - let fromPredicate = NSPredicate(format: "%@ <= %K", startDate! + 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]) @@ -83,6 +117,9 @@ class ShowBasedOnVoteLogics { } } + + /*---------------------------delete---------------------------------------*/ + static func dateForHeaderVote(onboardingData: OnboardingData) -> Date? { var date: Date? @@ -108,7 +145,7 @@ class ShowBasedOnVoteLogics { return nil } - + static func getLastDateVoteShouldExist(onboardingData: OnboardingData) -> Date { var date: Date? @@ -151,4 +188,6 @@ class ShowBasedOnVoteLogics { return false } + + } diff --git a/Shared/views/CustomizeView/SubViews/DayFilterPickerView.swift b/Shared/views/CustomizeView/SubViews/DayFilterPickerView.swift index eff7d29..ae10229 100644 --- a/Shared/views/CustomizeView/SubViews/DayFilterPickerView.swift +++ b/Shared/views/CustomizeView/SubViews/DayFilterPickerView.swift @@ -12,13 +12,13 @@ struct DayFilterPickerView: View { @AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor @StateObject private var filteredDays = DaysFilterClass.shared - let weekdays = [("Sun", 1), - ("mon", 2), - ("tue", 3), - ("wed", 4), - ("thur", 5), - ("fri", 6), - ("sat", 7)] + let weekdays = [(Calendar.current.shortWeekdaySymbols[0], 1), + (Calendar.current.shortWeekdaySymbols[1], 2), + (Calendar.current.shortWeekdaySymbols[2], 3), + (Calendar.current.shortWeekdaySymbols[3], 4), + (Calendar.current.shortWeekdaySymbols[4], 5), + (Calendar.current.shortWeekdaySymbols[5], 6), + (Calendar.current.shortWeekdaySymbols[6], 7)] var body: some View { ZStack { diff --git a/Shared/views/DayView/DayView.swift b/Shared/views/DayView/DayView.swift index 1444339..d185131 100644 --- a/Shared/views/DayView/DayView.swift +++ b/Shared/views/DayView/DayView.swift @@ -320,13 +320,6 @@ struct ViewOffsetKey: PreferenceKey { } } -private let itemFormatter: DateFormatter = { - let formatter = DateFormatter() - formatter.dateStyle = .short - formatter.timeStyle = .medium - return formatter -}() - struct DayView_Previews: PreviewProvider { static var previews: some View { DayView(viewModel: DayViewViewModel(addMonthStartWeekdayPadding: false)).environment(\.managedObjectContext, PersistenceController.shared.viewContext) diff --git a/Shared/views/MonthView/MonthView.swift b/Shared/views/MonthView/MonthView.swift index 8f38362..2a8d82e 100644 --- a/Shared/views/MonthView/MonthView.swift +++ b/Shared/views/MonthView/MonthView.swift @@ -204,6 +204,7 @@ extension MonthView { LazyVGrid(columns: columns, spacing: 15) { ForEach(entries, id: \.self) { entry in if filteredDays.currentFilters.contains(Int(entry.weekDay)) { + let _ = print(entry.weekDay, entry.forDate, filteredDays.currentFilters, entry.id) shape.view(withText: Text(""), bgColor: entry.mood == .placeholder ? .clear : moodTint.color(forMood: entry.mood), textColor: .clear) diff --git a/Shared/views/SettingsView/SettingsView.swift b/Shared/views/SettingsView/SettingsView.swift index 1c27742..15add4c 100644 --- a/Shared/views/SettingsView/SettingsView.swift +++ b/Shared/views/SettingsView/SettingsView.swift @@ -50,7 +50,7 @@ struct SettingsView: View { if useCloudKit { cloudKitStatus } - + fixWeekday exportData importData Divider() @@ -96,6 +96,7 @@ struct SettingsView: View { if selectedFile.startAccessingSecurityScopedResource() { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +0000" + dateFormatter.timeZone = TimeZone(abbreviation: "UTC") guard let input = String(data: try Data(contentsOf: selectedFile), encoding: .utf8) else { return } defer { selectedFile.stopAccessingSecurityScopedResource() } @@ -114,7 +115,10 @@ struct SettingsView: View { moodEntry.forDate = dateFormatter.date(from: columns[3]) moodEntry.moodValue = Int16(columns[4])! moodEntry.timestamp = dateFormatter.date(from: columns[5]) - moodEntry.weekDay = Int16(columns[6])! + + let localTime = dateFormatter.date(from: columns[3])!.toLocalTime() + moodEntry.weekDay = Int16(Calendar.current.component(.weekday, from: localTime)) + try! PersistenceController.shared.viewContext.save() } PersistenceController.shared.saveAndRunDataListerners() @@ -208,6 +212,21 @@ struct SettingsView: View { .cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight]) } + private var fixWeekday: some View { + ZStack { + theme.currentTheme.secondaryBGColor + Button(action: { + PersistenceController.shared.fixWrongWeekdays() + }, label: { + Text("Fix Weekday") + .foregroundColor(textColor) + }) + .padding() + } + .fixedSize(horizontal: false, vertical: true) + .cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight]) + } + private var whyBackgroundMode: some View { ZStack { theme.currentTheme.secondaryBGColor