Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit f3a9e71

Browse files
authored
Add History Debug Menu on macOS and display only 1 week of history when history view is enabled (#1217)
Task/Issue URL: https://app.asana.com/0/72649045549333/1209328755192085 Description: Add HistoryCoordinatingDebuggingSupport protocol that defines addVisit function taking 2 parameters and allowing to add a visit at an arbitrary time, rather than at current timestamp. This is required to be able to populate History with fake data in client apps. We already use a similar approach with SyncDependencies, where SyncDependenciesDebuggingSupport protocol is defined to support debug menus.
1 parent 7f6f5dd commit f3a9e71

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Sources/History/HistoryCoordinator.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,19 @@ import os.log
2323

2424
public typealias BrowsingHistory = [HistoryEntry]
2525

26-
public protocol HistoryCoordinating: AnyObject {
26+
/**
27+
* This protocol allows for debugging History.
28+
*/
29+
public protocol HistoryCoordinatingDebuggingSupport {
30+
/**
31+
* Adds visit at an arbitrary time, rather than current timestamp.
32+
*
33+
* > This function shouldn't be used in production code. Instead, `addVisit(of: URL)` should be used.
34+
*/
35+
@discardableResult func addVisit(of url: URL, at date: Date) -> Visit?
36+
}
37+
38+
public protocol HistoryCoordinating: AnyObject, HistoryCoordinatingDebuggingSupport {
2739

2840
func loadHistory(onCleanFinished: @escaping () -> Void)
2941

@@ -47,6 +59,12 @@ public protocol HistoryCoordinating: AnyObject {
4759
func removeUrlEntry(_ url: URL, completion: ((Error?) -> Void)?)
4860
}
4961

62+
extension HistoryCoordinating {
63+
public func addVisit(of url: URL) -> Visit? {
64+
addVisit(of: url, at: Date())
65+
}
66+
}
67+
5068
/// Coordinates access to History. Uses its own queue with high qos for all operations.
5169
final public class HistoryCoordinator: HistoryCoordinating {
5270

@@ -86,14 +104,14 @@ final public class HistoryCoordinator: HistoryCoordinating {
86104

87105
private var cancellables = Set<AnyCancellable>()
88106

89-
@discardableResult public func addVisit(of url: URL) -> Visit? {
107+
@discardableResult public func addVisit(of url: URL, at date: Date) -> Visit? {
90108
guard let historyDictionary = historyDictionary else {
91109
Logger.history.debug("Visit of \(url.absoluteString) ignored")
92110
return nil
93111
}
94112

95113
let entry = historyDictionary[url] ?? HistoryEntry(url: url)
96-
let visit = entry.addVisit()
114+
let visit = entry.addVisit(at: date)
97115
entry.failedToLoad = false
98116

99117
self.historyDictionary?[url] = entry

Sources/History/HistoryEntry.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ final public class HistoryEntry {
5656

5757
public var visits: Set<Visit>
5858

59-
func addVisit() -> Visit {
60-
let visit = Visit(date: Date(), historyEntry: self)
59+
func addVisit(at date: Date = Date()) -> Visit {
60+
let visit = Visit(date: date, historyEntry: self)
6161
visits.insert(visit)
6262

63+
lastVisit = numberOfTotalVisits == 0 ? date : max(lastVisit, date)
6364
numberOfTotalVisits += 1
64-
lastVisit = Date()
6565

6666
return visit
6767
}

0 commit comments

Comments
 (0)