Skip to content

Commit

Permalink
Add and update lastViewedDate to Tab model
Browse files Browse the repository at this point in the history
  • Loading branch information
dus7 committed Nov 27, 2024
1 parent fe7c8fc commit 22fdf2e
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions DuckDuckGo/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -56,33 +64,40 @@ public class Tab: NSObject, NSCoding {

var viewed: Bool = false {
didSet {
if viewed {
lastViewedDate = Date()
}
notifyObservers()
}
}

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) {
let uid = decoder.decodeObject(forKey: NSCodingKeys.uid) as? String
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) {
coder.encode(uid, forKey: NSCodingKeys.uid)
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 {
Expand Down

0 comments on commit 22fdf2e

Please sign in to comment.