closed #38
This commit is contained in:
@@ -10,7 +10,8 @@ import CoreData
|
||||
|
||||
class ContentModeViewModel: ObservableObject {
|
||||
@Published var grouped = [Int: [Int: [MoodEntry]]]()
|
||||
|
||||
@Published public private(set) var savedOnboardingData = UserDefaultsStore.getOnboarding()
|
||||
|
||||
init() {
|
||||
updateData()
|
||||
}
|
||||
@@ -19,18 +20,30 @@ class ContentModeViewModel: ObservableObject {
|
||||
grouped = PersistenceController.shared.splitIntoYearMonth()
|
||||
}
|
||||
|
||||
public func shouldShowTodayInput() -> Bool {
|
||||
public func shouldShowVotingHeader() -> Bool {
|
||||
isMissingCurrentVote() && savedOnboardingData.ableToVoteBasedOnCurentTime() ? true : false
|
||||
}
|
||||
|
||||
public func updateOnboardingData(onboardingData: OnboardingData) {
|
||||
self.savedOnboardingData = UserDefaultsStore.saveOnboarding(onboardingData: onboardingData)
|
||||
}
|
||||
|
||||
private func isMissingCurrentVote() -> Bool {
|
||||
let fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
|
||||
|
||||
var calendar = Calendar.current
|
||||
calendar.timeZone = NSTimeZone.local
|
||||
|
||||
// Get today's beginning & end
|
||||
let dateFrom = calendar.startOfDay(for: Date()) // eg. 2016-10-10 00:00:00
|
||||
var dateFrom: Date
|
||||
switch savedOnboardingData.inputDay {
|
||||
case .Today:
|
||||
dateFrom = calendar.startOfDay(for: Date())
|
||||
case .Previous:
|
||||
dateFrom = calendar.startOfDay(for: Date())
|
||||
dateFrom = calendar.date(byAdding: .day, value: -1, to: dateFrom)!
|
||||
}
|
||||
let dateTo = calendar.date(byAdding: .day, value: 1, to: dateFrom)!
|
||||
// Note: Times are printed in UTC. Depending on where you live it won't print 00:00:00 but it will work with UTC times which can be converted to local time
|
||||
|
||||
// Set predicate as date being today's date
|
||||
let fromPredicate = NSPredicate(format: "%@ <= %K", dateFrom as NSDate, #keyPath(MoodEntry.forDate))
|
||||
let toPredicate = NSPredicate(format: "%K < %@", #keyPath(MoodEntry.forDate), dateTo as NSDate)
|
||||
let datePredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [fromPredicate, toPredicate])
|
||||
@@ -45,7 +58,7 @@ class ContentModeViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
public func add(mood: Mood, forDate date: Date) {
|
||||
PersistenceController.shared.add(mood: mood, forDate: Date())
|
||||
PersistenceController.shared.add(mood: mood, forDate: date)
|
||||
getGroupedData()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user