yattee/Model/Cache/CacheModel.swift

42 lines
993 B
Swift
Raw Permalink Normal View History

2022-12-10 02:01:59 +00:00
import Cache
2022-11-10 17:11:28 +00:00
import Foundation
2022-12-10 02:01:59 +00:00
import SwiftyJSON
2022-11-10 17:11:28 +00:00
2022-12-12 09:21:46 +00:00
protocol CacheModel {
var storage: Storage<String, JSON>? { get }
2022-11-10 17:11:28 +00:00
2022-12-12 09:21:46 +00:00
func clear()
}
2022-12-10 02:01:59 +00:00
2022-12-12 09:21:46 +00:00
extension CacheModel {
2022-12-10 02:01:59 +00:00
func clear() {
2022-12-12 09:21:46 +00:00
try? storage?.removeAll()
2022-12-10 02:01:59 +00:00
}
2022-12-12 09:21:46 +00:00
func getFormattedDate(_ date: Date?) -> String {
guard let date else { return "unknown" }
2022-12-10 02:01:59 +00:00
2022-12-12 09:21:46 +00:00
let isSameDay = Calendar(identifier: .iso8601).isDate(date, inSameDayAs: Date())
let formatter = isSameDay ? dateFormatterForTimeOnly : dateFormatter
return formatter.string(from: date)
2022-12-10 02:01:59 +00:00
}
2022-12-11 17:04:39 +00:00
var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
return formatter
}
var dateFormatterForTimeOnly: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .medium
return formatter
2022-12-10 02:01:59 +00:00
}
2022-12-11 17:04:39 +00:00
var iso8601DateFormatter: ISO8601DateFormatter { .init() }
2022-11-10 17:11:28 +00:00
}