bunch of random work

This commit is contained in:
Trey t
2022-04-03 20:20:12 -05:00
parent 9d008236bc
commit dec8f2d84d
7 changed files with 100 additions and 32 deletions

View File

@@ -67,7 +67,6 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
date = Calendar.current.date(byAdding: .day, value: -1, to: Date())! date = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
} }
switch action { switch action {
case .horrible: case .horrible:
PersistenceController.shared.add(mood: .horrible, forDate: date, entryType: .notification) PersistenceController.shared.add(mood: .horrible, forDate: date, entryType: .notification)

View File

@@ -8,6 +8,20 @@
import CoreData import CoreData
extension PersistenceController { 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) { public func add(mood: Mood, forDate date: Date, entryType: EntryType) {
if let existingEntry = getEntry(byDate: date) { if let existingEntry = getEntry(byDate: date) {
viewContext.delete(existingEntry) viewContext.delete(existingEntry)
@@ -18,7 +32,10 @@ extension PersistenceController {
newItem.timestamp = Date() newItem.timestamp = Date()
newItem.moodValue = Int16(mood.rawValue) newItem.moodValue = Int16(mood.rawValue)
newItem.forDate = date 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.canEdit = true
newItem.canDelete = true newItem.canDelete = true
newItem.entryType = Int16(entryType.rawValue) newItem.entryType = Int16(entryType.rawValue)

View File

@@ -35,30 +35,64 @@ class ShowBasedOnVoteLogics {
return (passedTimeToVote, inputDay) return (passedTimeToVote, inputDay)
} }
static func isMissingCurrentVote(onboardingData: OnboardingData) -> Bool { static func getLastDateVoteShouldExistOnViews(onboardingData: OnboardingData) -> Date {
var startDate: Date? var date: Date?
switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) { switch ShowBasedOnVoteLogics.returnCurrentVoteStatus(onboardingData: onboardingData) {
case (false, .Today): case (false, .Today):
// if we're NOT passed time to vote and the voting type is previous - last vote should be -1 // 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())
case (true, .Today): case (true, .Today):
// if we're passed time to vote and the voting type is today - last vote should be current date // if we're passed time to vote and the voting type is previous - last vote should be today
startDate = Date() date = Calendar.current.date(byAdding: .day, value: -1, to: Date())
case (false, .Previous): case (false, .Previous):
// if we're NOT passed time to vote and the voting type is previous - last vote should be 2 days ago // if we're passed time to vote and the voting type is previous - last vote should be -2
startDate = Calendar.current.date(byAdding: .day, value: -2, to: Date())! date = Calendar.current.date(byAdding: .day, value: -3, to: Date())
case (true, .Previous): case (true, .Previous):
// if we're passed time to vote and the voting type is previous - last vote should be -1 // 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!) return date
let endDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate!)! }
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 fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
let fromPredicate = NSPredicate(format: "%@ <= %K", startDate! let fromPredicate = NSPredicate(format: "%@ <= %K", startDate
as NSDate, #keyPath(MoodEntry.forDate)) as NSDate, #keyPath(MoodEntry.forDate))
let toPredicate = NSPredicate(format: "%K < %@", #keyPath(MoodEntry.forDate), endDate as NSDate) let toPredicate = NSPredicate(format: "%K < %@", #keyPath(MoodEntry.forDate), endDate as NSDate)
let datePredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [fromPredicate, toPredicate]) let datePredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [fromPredicate, toPredicate])
@@ -83,6 +117,9 @@ class ShowBasedOnVoteLogics {
} }
} }
/*---------------------------delete---------------------------------------*/
static func dateForHeaderVote(onboardingData: OnboardingData) -> Date? { static func dateForHeaderVote(onboardingData: OnboardingData) -> Date? {
var date: Date? var date: Date?
@@ -108,7 +145,7 @@ class ShowBasedOnVoteLogics {
return nil return nil
} }
static func getLastDateVoteShouldExist(onboardingData: OnboardingData) -> Date { static func getLastDateVoteShouldExist(onboardingData: OnboardingData) -> Date {
var date: Date? var date: Date?
@@ -151,4 +188,6 @@ class ShowBasedOnVoteLogics {
return false return false
} }
} }

View File

@@ -12,13 +12,13 @@ struct DayFilterPickerView: View {
@AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor @AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor
@StateObject private var filteredDays = DaysFilterClass.shared @StateObject private var filteredDays = DaysFilterClass.shared
let weekdays = [("Sun", 1), let weekdays = [(Calendar.current.shortWeekdaySymbols[0], 1),
("mon", 2), (Calendar.current.shortWeekdaySymbols[1], 2),
("tue", 3), (Calendar.current.shortWeekdaySymbols[2], 3),
("wed", 4), (Calendar.current.shortWeekdaySymbols[3], 4),
("thur", 5), (Calendar.current.shortWeekdaySymbols[4], 5),
("fri", 6), (Calendar.current.shortWeekdaySymbols[5], 6),
("sat", 7)] (Calendar.current.shortWeekdaySymbols[6], 7)]
var body: some View { var body: some View {
ZStack { ZStack {

View File

@@ -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 { struct DayView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
DayView(viewModel: DayViewViewModel(addMonthStartWeekdayPadding: false)).environment(\.managedObjectContext, PersistenceController.shared.viewContext) DayView(viewModel: DayViewViewModel(addMonthStartWeekdayPadding: false)).environment(\.managedObjectContext, PersistenceController.shared.viewContext)

View File

@@ -204,6 +204,7 @@ extension MonthView {
LazyVGrid(columns: columns, spacing: 15) { LazyVGrid(columns: columns, spacing: 15) {
ForEach(entries, id: \.self) { entry in ForEach(entries, id: \.self) { entry in
if filteredDays.currentFilters.contains(Int(entry.weekDay)) { if filteredDays.currentFilters.contains(Int(entry.weekDay)) {
let _ = print(entry.weekDay, entry.forDate, filteredDays.currentFilters, entry.id)
shape.view(withText: Text(""), shape.view(withText: Text(""),
bgColor: entry.mood == .placeholder ? .clear : moodTint.color(forMood: entry.mood), bgColor: entry.mood == .placeholder ? .clear : moodTint.color(forMood: entry.mood),
textColor: .clear) textColor: .clear)

View File

@@ -50,7 +50,7 @@ struct SettingsView: View {
if useCloudKit { if useCloudKit {
cloudKitStatus cloudKitStatus
} }
fixWeekday
exportData exportData
importData importData
Divider() Divider()
@@ -96,6 +96,7 @@ struct SettingsView: View {
if selectedFile.startAccessingSecurityScopedResource() { if selectedFile.startAccessingSecurityScopedResource() {
let dateFormatter = DateFormatter() let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +0000" 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 } guard let input = String(data: try Data(contentsOf: selectedFile), encoding: .utf8) else { return }
defer { selectedFile.stopAccessingSecurityScopedResource() } defer { selectedFile.stopAccessingSecurityScopedResource() }
@@ -114,7 +115,10 @@ struct SettingsView: View {
moodEntry.forDate = dateFormatter.date(from: columns[3]) moodEntry.forDate = dateFormatter.date(from: columns[3])
moodEntry.moodValue = Int16(columns[4])! moodEntry.moodValue = Int16(columns[4])!
moodEntry.timestamp = dateFormatter.date(from: columns[5]) 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() try! PersistenceController.shared.viewContext.save()
} }
PersistenceController.shared.saveAndRunDataListerners() PersistenceController.shared.saveAndRunDataListerners()
@@ -208,6 +212,21 @@ struct SettingsView: View {
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight]) .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 { private var whyBackgroundMode: some View {
ZStack { ZStack {
theme.currentTheme.secondaryBGColor theme.currentTheme.secondaryBGColor