Download this file
| 1 | import Foundation |
| 2 | |
| 3 | public struct ScreenshotPolicy: Sendable { |
| 4 | public private(set) var explained: Bool |
| 5 | public private(set) var shouldExplain = false |
| 6 | private var dismissed: [String: Date] = [:] |
| 7 | public init(explained: Bool = false) { self.explained = explained } |
| 8 | public mutating func plusTapped() { if !explained { shouldExplain = true } } |
| 9 | public mutating func decline() { explained = true; shouldExplain = false } |
| 10 | public mutating func prune(now: Date) { dismissed = dismissed.filter { now.timeIntervalSince($0.value) <= 300 } } |
| 11 | public mutating func dismiss(_ id: String, created: Date) { prune(now: created); dismissed[id] = created } |
| 12 | public func eligible(id: String, created: Date, isScreenshot: Bool, hidden: Bool, permitted: Bool, now: Date) -> Bool { |
| 13 | let age = now.timeIntervalSince(created) |
| 14 | return permitted && isScreenshot && !hidden && age >= 0 && age <= 300 && dismissed[id] == nil |
| 15 | } |
| 16 | } |
| 17 | |