-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationQuery.swift
More file actions
80 lines (71 loc) · 2 KB
/
Copy pathPushNotificationQuery.swift
File metadata and controls
80 lines (71 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// PushNotificationQuery.swift
// DevLogCore
//
// Created by opfic on 2/18/26.
//
import Foundation
public struct PushNotificationQuery: Equatable, Sendable {
public enum SortOrder: Equatable, Sendable {
case latest
case oldest
}
public enum TimeFilter: Equatable, Hashable, Sendable {
case none
case hours(Int)
case days(Int)
}
public var sortOrder: SortOrder
public var timeFilter: TimeFilter
public var unreadOnly: Bool
public var pageSize: Int
public init(
sortOrder: SortOrder,
timeFilter: TimeFilter,
unreadOnly: Bool,
pageSize: Int
) {
self.sortOrder = sortOrder
self.timeFilter = timeFilter
self.unreadOnly = unreadOnly
self.pageSize = pageSize
}
public static let `default` = PushNotificationQuery(
sortOrder: .latest,
timeFilter: .none,
unreadOnly: false,
pageSize: 20
)
}
public extension PushNotificationQuery.TimeFilter {
var id: String {
switch self {
case .none: return "none"
case .hours(let value): return "hours-\(value)"
case .days(let value): return "days-\(value)"
}
}
init(id: String) {
if id == "none" {
self = .none
} else if id.hasPrefix("hours-") {
let value = Int(id.replacingOccurrences(of: "hours-", with: "")) ?? 0
self = 0 < value ? .hours(value) : .none
} else if id.hasPrefix("days-") {
let value = Int(id.replacingOccurrences(of: "days-", with: "")) ?? 0
self = 0 < value ? .days(value) : .none
} else {
self = .none
}
}
var thresholdDate: Date? {
switch self {
case .none:
return nil
case .hours(let value):
return Date().addingTimeInterval(-Double(value) * 3600.0)
case .days(let value):
return Date().addingTimeInterval(-Double(value) * 86400.0)
}
}
}