Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Sources/History/HistoryCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,20 @@ public protocol HistoryCoordinating: AnyObject {

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

public enum Event {

case dataCleaningFailed(_ error: Error)
case dataCleaningFinished

}

let eventMapper: EventMapping<Event>
let historyStoringProvider: () -> HistoryStoring

public init(historyStoring: @autoclosure @escaping () -> HistoryStoring) {
public init(historyStoring: @autoclosure @escaping () -> HistoryStoring, eventMapper: EventMapping<Event>) {
self.historyStoringProvider = historyStoring
self.eventMapper = eventMapper
}

public func loadHistory(onCleanFinished: @escaping () -> Void) {
Expand Down Expand Up @@ -203,13 +212,15 @@ final public class HistoryCoordinator: HistoryCoordinating {
private func clean(until date: Date, onCleanFinished: (() -> Void)? = nil) {
historyStoring.cleanOld(until: date)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { completion in
.sink(receiveCompletion: { [weak self] completion in
switch completion {
case .finished:
self?.eventMapper.fire(.dataCleaningFinished)
Logger.history.debug("History cleaned successfully")
case .failure(let error):
// This should really be a pixel
Logger.history.error("Cleaning of history failed: \(error.localizedDescription)")
self?.eventMapper.fire(.dataCleaningFailed(error))
}
onCleanFinished?()
}, receiveValue: { [weak self] history in
Expand Down
14 changes: 11 additions & 3 deletions Tests/HistoryTests/HistoryCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HistoryCoordinatorTests: XCTestCase {
func testWhenAddVisitIsCalledBeforeHistoryIsLoadedFromStorage_ThenVisitIsIgnored() {
let historyStoringMock = HistoryStoringMock()
historyStoringMock.cleanOldResult = nil
let historyCoordinator = HistoryCoordinator(historyStoring: historyStoringMock)
let historyCoordinator = HistoryCoordinator(historyStoring: historyStoringMock, eventMapper: MockHistoryCoordinatorEventMapper())

let url = URL.duckDuckGo
historyCoordinator.addVisit(of: url)
Expand Down Expand Up @@ -195,7 +195,7 @@ class HistoryCoordinatorTests: XCTestCase {
}

let historyStore = HistoryStore(context: context, eventMapper: MockHistoryStoreEventMapper())
let historyCoordinator = HistoryCoordinator(historyStoring: historyStore)
let historyCoordinator = HistoryCoordinator(historyStoring: historyStore, eventMapper: MockHistoryCoordinatorEventMapper())
historyCoordinator.loadHistory { }

let url1 = URL(string: "https://duckduckgo.com")!
Expand Down Expand Up @@ -287,7 +287,7 @@ fileprivate extension HistoryCoordinator {
let historyStoringMock = HistoryStoringMock()
historyStoringMock.cleanOldResult = .success(BrowsingHistory())
historyStoringMock.removeEntriesResult = .success(())
let historyCoordinator = HistoryCoordinator(historyStoring: historyStoringMock)
let historyCoordinator = HistoryCoordinator(historyStoring: historyStoringMock, eventMapper: MockHistoryCoordinatorEventMapper())
historyCoordinator.loadHistory { }

return (historyStoringMock, historyCoordinator)
Expand Down Expand Up @@ -378,3 +378,11 @@ class MockHistoryStoreEventMapper: EventMapping<HistoryStore.HistoryStoreEvents>
fatalError("Use init()")
}
}

final class MockHistoryCoordinatorEventMapper: EventMapping<HistoryCoordinator.Event> {
public init() {
super.init { _, _, _, _ in

}
}
}