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

Commit

Permalink
Add History Debug Menu on macOS and display only 1 week of history wh…
Browse files Browse the repository at this point in the history
…en 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.
  • Loading branch information
ayoy authored Feb 5, 2025
1 parent 7f6f5dd commit f3a9e71
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
24 changes: 21 additions & 3 deletions Sources/History/HistoryCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ import os.log

public typealias BrowsingHistory = [HistoryEntry]

public protocol HistoryCoordinating: AnyObject {
/**
* This protocol allows for debugging History.
*/
public protocol HistoryCoordinatingDebuggingSupport {
/**
* Adds visit at an arbitrary time, rather than current timestamp.
*
* > This function shouldn't be used in production code. Instead, `addVisit(of: URL)` should be used.
*/
@discardableResult func addVisit(of url: URL, at date: Date) -> Visit?
}

public protocol HistoryCoordinating: AnyObject, HistoryCoordinatingDebuggingSupport {

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

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

extension HistoryCoordinating {
public func addVisit(of url: URL) -> Visit? {
addVisit(of: url, at: Date())
}
}

/// Coordinates access to History. Uses its own queue with high qos for all operations.
final public class HistoryCoordinator: HistoryCoordinating {

Expand Down Expand Up @@ -86,14 +104,14 @@ final public class HistoryCoordinator: HistoryCoordinating {

private var cancellables = Set<AnyCancellable>()

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

let entry = historyDictionary[url] ?? HistoryEntry(url: url)
let visit = entry.addVisit()
let visit = entry.addVisit(at: date)
entry.failedToLoad = false

self.historyDictionary?[url] = entry
Expand Down
6 changes: 3 additions & 3 deletions Sources/History/HistoryEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ final public class HistoryEntry {

public var visits: Set<Visit>

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

lastVisit = numberOfTotalVisits == 0 ? date : max(lastVisit, date)
numberOfTotalVisits += 1
lastVisit = Date()

return visit
}
Expand Down

0 comments on commit f3a9e71

Please sign in to comment.