diff --git a/Sources/History/HistoryCoordinator.swift b/Sources/History/HistoryCoordinator.swift index 191cfc558..e8f617b55 100644 --- a/Sources/History/HistoryCoordinator.swift +++ b/Sources/History/HistoryCoordinator.swift @@ -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 let historyStoringProvider: () -> HistoryStoring - public init(historyStoring: @autoclosure @escaping () -> HistoryStoring) { + public init(historyStoring: @autoclosure @escaping () -> HistoryStoring, eventMapper: EventMapping) { self.historyStoringProvider = historyStoring + self.eventMapper = eventMapper } public func loadHistory(onCleanFinished: @escaping () -> Void) { @@ -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 diff --git a/Tests/HistoryTests/HistoryCoordinatorTests.swift b/Tests/HistoryTests/HistoryCoordinatorTests.swift index 4d41286d8..aea81c105 100644 --- a/Tests/HistoryTests/HistoryCoordinatorTests.swift +++ b/Tests/HistoryTests/HistoryCoordinatorTests.swift @@ -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) @@ -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")! @@ -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) @@ -378,3 +378,11 @@ class MockHistoryStoreEventMapper: EventMapping fatalError("Use init()") } } + +final class MockHistoryCoordinatorEventMapper: EventMapping { + public init() { + super.init { _, _, _, _ in + + } + } +}