diff --git a/DuckDuckGo/Tab.swift b/DuckDuckGo/Tab.swift index 04e568eec6..437356124c 100644 --- a/DuckDuckGo/Tab.swift +++ b/DuckDuckGo/Tab.swift @@ -36,12 +36,20 @@ public class Tab: NSObject, NSCoding { static let link = "link" static let viewed = "viewed" static let desktop = "desktop" + static let lastViewedDate = "lastViewedDate" } private var observersHolder = [WeaklyHeldTabObserver]() let uid: String - + + /// The date last time this tab was displayed. + /// + /// - Warning: This value **must not** be used for any other purpose than for inactive tabs buckets aggregation + /// into a daily pixel in `TabSwitcherOpenDailyPixel`. If you plan to do something else, + /// read through https://app.asana.com/0/69071770703008/1208795393823862/f and reopen if necessary. + private(set) var lastViewedDate: Date? + var isDesktop: Bool = false { didSet { notifyObservers() @@ -56,6 +64,9 @@ public class Tab: NSObject, NSCoding { var viewed: Bool = false { didSet { + if viewed { + lastViewedDate = Date() + } notifyObservers() } } @@ -63,11 +74,13 @@ public class Tab: NSObject, NSCoding { public init(uid: String? = nil, link: Link? = nil, viewed: Bool = false, - desktop: Bool = AppWidthObserver.shared.isLargeWidth) { + desktop: Bool = AppWidthObserver.shared.isLargeWidth, + lastViewedDate: Date? = nil) { self.uid = uid ?? UUID().uuidString self.link = link self.viewed = viewed self.isDesktop = desktop + self.lastViewedDate = lastViewedDate } public convenience required init?(coder decoder: NSCoder) { @@ -75,7 +88,8 @@ public class Tab: NSObject, NSCoding { let link = decoder.decodeObject(forKey: NSCodingKeys.link) as? Link let viewed = decoder.containsValue(forKey: NSCodingKeys.viewed) ? decoder.decodeBool(forKey: NSCodingKeys.viewed) : true let desktop = decoder.containsValue(forKey: NSCodingKeys.desktop) ? decoder.decodeBool(forKey: NSCodingKeys.desktop) : false - self.init(uid: uid, link: link, viewed: viewed, desktop: desktop) + let lastViewedDate = decoder.containsValue(forKey: NSCodingKeys.lastViewedDate) ? decoder.decodeObject(forKey: NSCodingKeys.lastViewedDate) as? Date : nil + self.init(uid: uid, link: link, viewed: viewed, desktop: desktop, lastViewedDate: lastViewedDate) } public func encode(with coder: NSCoder) { @@ -83,6 +97,7 @@ public class Tab: NSObject, NSCoding { coder.encode(link, forKey: NSCodingKeys.link) coder.encode(viewed, forKey: NSCodingKeys.viewed) coder.encode(isDesktop, forKey: NSCodingKeys.desktop) + coder.encode(lastViewedDate, forKey: NSCodingKeys.lastViewedDate) } public override func isEqual(_ other: Any?) -> Bool {