AutoCat/AutoCatCore/Utils/DateCache.swift

37 lines
1.0 KiB
Swift

//
// DateCache.swift
// AutoCatCore
//
// Created by Selim Mustafaev on 12.02.2023.
// Copyright © 2023 Selim Mustafaev. All rights reserved.
//
import Foundation
import SwiftDate
public class DateCache {
public static let shared = DateCache()
public var monthStart: DateInRegion
public var weekStart: DateInRegion
public init() {
let now = DateInRegion(Date(), region: Region.current)
self.monthStart = now.dateAtStartOf(.month)
self.weekStart = now.dateAtStartOf(.weekOfMonth)
NotificationCenter.default.addObserver(self,
selector: #selector(dayChanged),
name: .NSCalendarDayChanged,
object: nil)
}
@objc func dayChanged(_ notification: Notification) {
let now = DateInRegion(Date(), region: Region.current)
self.monthStart = now.dateAtStartOf(.month)
self.weekStart = now.dateAtStartOf(.weekOfMonth)
}
}