From 793ec853cb088eb89a92d17cc903071fe2997ef0 Mon Sep 17 00:00:00 2001 From: cyndichin Date: Mon, 10 Nov 2025 09:02:47 -0500 Subject: [PATCH] Add FXIOS-13782 [Trending Searches] use new most recent method --- .../Places/Places.swift | 7 ++++++ .../SearchEngines/RecentSearchProvider.swift | 24 +++++++++---------- firefox-ios/Storage/Rust/RustPlaces.swift | 11 +++++---- .../Mocks/MockHistoryHandler.swift | 8 +++---- .../DefaultRecentSearchProviderTests.swift | 2 ++ 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/MozillaRustComponents/Sources/MozillaRustComponentsWrapper/Places/Places.swift b/MozillaRustComponents/Sources/MozillaRustComponentsWrapper/Places/Places.swift index 3cae5f92b358d..e97efdf792116 100644 --- a/MozillaRustComponents/Sources/MozillaRustComponentsWrapper/Places/Places.swift +++ b/MozillaRustComponents/Sources/MozillaRustComponentsWrapper/Places/Places.swift @@ -358,6 +358,13 @@ public class PlacesReadConnection { } } + open func getMostRecentHistoryMetadata(limit: Int32) throws -> [HistoryMetadata] { + return try queue.sync { + try self.checkApi() + return try self.conn.getMostRecentHistoryMetadata(limit: limit) + } + } + open func queryHistoryMetadata(query: String, limit: Int32) throws -> [HistoryMetadata] { return try queue.sync { try self.checkApi() diff --git a/firefox-ios/Client/Frontend/Browser/SearchEngines/RecentSearchProvider.swift b/firefox-ios/Client/Frontend/Browser/SearchEngines/RecentSearchProvider.swift index 09b4025e2e30f..0b116b525112c 100644 --- a/firefox-ios/Client/Frontend/Browser/SearchEngines/RecentSearchProvider.swift +++ b/firefox-ios/Client/Frontend/Browser/SearchEngines/RecentSearchProvider.swift @@ -18,8 +18,8 @@ final class DefaultRecentSearchProvider: RecentSearchProvider { private let logger: Logger private let nimbus: FxNimbus - private var maxNumberOfSuggestions: Int { - return nimbus.features.recentSearchesFeature.value().maxSuggestions + private var maxNumberOfSuggestions: Int32 { + return Int32(nimbus.features.recentSearchesFeature.value().maxSuggestions) } init( @@ -57,16 +57,14 @@ final class DefaultRecentSearchProvider: RecentSearchProvider { /// Only care about returning the `maxNumberOfSuggestions`. /// We don't have an interface to fetch only a certain amount, so we follow what Android does for now. func loadRecentSearches(completion: @escaping ([String]) -> Void) { - // TODO: FXIOS-13782 Use get_most_recent method to fetch history - historyStorage.getHistoryMetadataSince(since: Int64.min) { [weak self] result in - if case .success(let historyMetadata) = result { - let uniqueSearchTermResult = historyMetadata.compactMap { $0.searchTerm } - .uniqued() - .prefix(self?.maxNumberOfSuggestions ?? 5) - completion(Array(uniqueSearchTermResult)) - } else { - completion([]) - } - } + historyStorage.getMostRecentHistoryMetadata(limit: maxNumberOfSuggestions) { result in + if case .success(let historyMetadata) = result { + let uniqueSearchTermResult = historyMetadata.compactMap { $0.searchTerm } + .uniqued() + completion(uniqueSearchTermResult) + } else { + completion([]) + } + } } } diff --git a/firefox-ios/Storage/Rust/RustPlaces.swift b/firefox-ios/Storage/Rust/RustPlaces.swift index eb079dcd7b710..9d217b39c16f4 100644 --- a/firefox-ios/Storage/Rust/RustPlaces.swift +++ b/firefox-ios/Storage/Rust/RustPlaces.swift @@ -61,8 +61,9 @@ public protocol BookmarksHandler { public protocol HistoryHandler { func applyObservation(visitObservation: VisitObservation, completion: @escaping (Result) -> Void) - func getHistoryMetadataSince( - since startDate: Int64, + + func getMostRecentHistoryMetadata( + limit: Int32, completion: @Sendable @escaping (Result<[HistoryMetadata], any Error>) -> Void ) @@ -550,12 +551,12 @@ public class RustPlaces: @unchecked Sendable, BookmarksHandler, HistoryHandler { // MARK: History metadata /// Currently only used to get the recent searches from the user's history storage. - public func getHistoryMetadataSince( - since startDate: Int64, + public func getMostRecentHistoryMetadata( + limit: Int32, completion: @Sendable @escaping (Result<[HistoryMetadata], any Error>) -> Void ) { withReader({ connection in - return try connection.getHistoryMetadataSince(since: startDate) + return try connection.getMostRecentHistoryMetadata(limit: limit) }, completion: completion) } diff --git a/firefox-ios/firefox-ios-tests/Tests/ClientTests/Mocks/MockHistoryHandler.swift b/firefox-ios/firefox-ios-tests/Tests/ClientTests/Mocks/MockHistoryHandler.swift index bf5429df2e5e8..59c59e94893ea 100644 --- a/firefox-ios/firefox-ios-tests/Tests/ClientTests/Mocks/MockHistoryHandler.swift +++ b/firefox-ios/firefox-ios-tests/Tests/ClientTests/Mocks/MockHistoryHandler.swift @@ -16,7 +16,7 @@ final class MockHistoryHandler: HistoryHandler { var onApply: (() -> Void)? // MARK: History Metadata - var getHistoryMetadataSinceCallCount = 0 + var getMostRecentHistoryMetadataCallCount = 0 var noteHistoryMetadataCallCount = 0 var result: Result<[MozillaAppServices.HistoryMetadata], Error> = .success( [ @@ -53,11 +53,11 @@ final class MockHistoryHandler: HistoryHandler { onApply?() } - func getHistoryMetadataSince( - since startDate: Int64, + func getMostRecentHistoryMetadata( + limit: Int32, completion: @escaping @Sendable (Result<[MozillaAppServices.HistoryMetadata], any Error>) -> Void ) { - getHistoryMetadataSinceCallCount += 1 + getMostRecentHistoryMetadataCallCount += 1 completion(result) } diff --git a/firefox-ios/firefox-ios-tests/Tests/ClientTests/Search/DefaultRecentSearchProviderTests.swift b/firefox-ios/firefox-ios-tests/Tests/ClientTests/Search/DefaultRecentSearchProviderTests.swift index f0560ffc9df99..926a56be4c34e 100644 --- a/firefox-ios/firefox-ios-tests/Tests/ClientTests/Search/DefaultRecentSearchProviderTests.swift +++ b/firefox-ios/firefox-ios-tests/Tests/ClientTests/Search/DefaultRecentSearchProviderTests.swift @@ -61,6 +61,7 @@ final class DefaultRecentSearchProviderTests: XCTestCase { expectation.fulfill() } wait(for: [expectation], timeout: 1) + XCTAssertEqual(mockHistoryStorage.getMostRecentHistoryMetadataCallCount, 1) } func test_loadRecentSearches_withError_returnsEmptyList() { @@ -75,6 +76,7 @@ final class DefaultRecentSearchProviderTests: XCTestCase { expectation.fulfill() } wait(for: [expectation], timeout: 1) + XCTAssertEqual(mockHistoryStorage.getMostRecentHistoryMetadataCallCount, 1) } func createSubject(for searchEngineID: String) -> RecentSearchProvider {