-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FXIOS-10379 [Unified Search] Implement initial redux setup (#22767)
* Stub placeholder button for testing search engine toggle. * Stub in basic SearchEngineSelectionState. * Stub in SearchEngineSelectionAction and SearchEngineSelectionMiddleware. * Hook up SearchEngineSelectionViewController to use new redux setup for SearchEngineSelection. * Make OpenSearchEngine test helpers public. * Add SearchEngineSelectionStateTests. Add SearchEngineSelectionMiddlewareTests.
- Loading branch information
Showing
11 changed files
with
397 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
firefox-ios/Client/Frontend/Browser/SearchEngines/Redux/SearchEngineSelectionAction.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Common | ||
import Foundation | ||
import MenuKit | ||
import Redux | ||
|
||
final class SearchEngineSelectionAction: Action { | ||
let searchEngines: [OpenSearchEngine]? | ||
|
||
init(windowUUID: WindowUUID, actionType: ActionType, searchEngines: [OpenSearchEngine]? = nil) { | ||
self.searchEngines = searchEngines | ||
super.init(windowUUID: windowUUID, actionType: actionType) | ||
} | ||
} | ||
|
||
enum SearchEngineSelectionActionType: ActionType { | ||
case viewDidLoad | ||
case didLoadSearchEngines | ||
} | ||
|
||
enum SearchEngineSelectionMiddlewareActionType: ActionType {} |
50 changes: 50 additions & 0 deletions
50
...fox-ios/Client/Frontend/Browser/SearchEngines/Redux/SearchEngineSelectionMiddleware.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Common | ||
import Redux | ||
import ToolbarKit | ||
|
||
final class SearchEngineSelectionMiddleware { | ||
private let profile: Profile | ||
private let logger: Logger | ||
private let searchEnginesManager: SearchEnginesManager | ||
|
||
init(profile: Profile = AppContainer.shared.resolve(), | ||
searchEnginesManager: SearchEnginesManager? = nil, | ||
logger: Logger = DefaultLogger.shared) { | ||
self.profile = profile | ||
self.logger = logger | ||
self.searchEnginesManager = searchEnginesManager ?? SearchEnginesManager(prefs: profile.prefs, files: profile.files) | ||
} | ||
|
||
lazy var searchEngineSelectionProvider: Middleware<AppState> = { [self] state, action in | ||
guard let action = action as? SearchEngineSelectionAction else { return } | ||
|
||
switch action.actionType { | ||
case SearchEngineSelectionActionType.viewDidLoad: | ||
guard let searchEngines = searchEnginesManager.orderedEngines, !searchEngines.isEmpty else { | ||
// The SearchEngineManager should have loaded these by now, but if not, attempt to fetch the search engines | ||
self.searchEnginesManager.getOrderedEngines { [weak self] searchEngines in | ||
self?.notifyDidLoad(windowUUID: action.windowUUID, searchEngines: searchEngines) | ||
} | ||
return | ||
} | ||
|
||
notifyDidLoad(windowUUID: action.windowUUID, searchEngines: searchEngines) | ||
|
||
default: | ||
break | ||
} | ||
} | ||
|
||
private func notifyDidLoad(windowUUID: WindowUUID, searchEngines: [OpenSearchEngine]) { | ||
let action = SearchEngineSelectionAction( | ||
windowUUID: windowUUID, | ||
actionType: SearchEngineSelectionActionType.didLoadSearchEngines, | ||
searchEngines: searchEngines | ||
) | ||
store.dispatch(action) | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
firefox-ios/Client/Frontend/Browser/SearchEngines/Redux/SearchEngineSelectionState.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Common | ||
import Shared | ||
import Redux | ||
|
||
struct SearchEngineSelectionState: ScreenState, Equatable { | ||
var windowUUID: WindowUUID | ||
var shouldDismiss: Bool | ||
// Default search engine should appear in position 0 | ||
var searchEngines: [OpenSearchEngine] | ||
|
||
init(appState: AppState, uuid: WindowUUID) { | ||
guard let state = store.state.screenState( | ||
SearchEngineSelectionState.self, | ||
for: .searchEngineSelection, | ||
window: uuid | ||
) else { | ||
self.init(windowUUID: uuid) | ||
return | ||
} | ||
|
||
self.init( | ||
windowUUID: state.windowUUID, | ||
searchEngines: state.searchEngines, | ||
shouldDismiss: state.shouldDismiss | ||
) | ||
} | ||
|
||
init(windowUUID: WindowUUID) { | ||
self.init(windowUUID: windowUUID, searchEngines: []) | ||
} | ||
|
||
private init( | ||
windowUUID: WindowUUID, | ||
searchEngines: [OpenSearchEngine], | ||
shouldDismiss: Bool = false | ||
) { | ||
self.windowUUID = windowUUID | ||
self.searchEngines = searchEngines | ||
self.shouldDismiss = shouldDismiss | ||
} | ||
|
||
/// Returns a new `SearchEngineSelectionState` which clears any transient data. | ||
static func defaultState(fromPreviousState state: SearchEngineSelectionState) -> SearchEngineSelectionState { | ||
return SearchEngineSelectionState( | ||
windowUUID: state.windowUUID, | ||
searchEngines: state.searchEngines | ||
) | ||
} | ||
|
||
static let reducer: Reducer<Self> = { state, action in | ||
// Only process actions for the current window | ||
guard action.windowUUID == .unavailable || action.windowUUID == state.windowUUID else { | ||
return defaultState(fromPreviousState: state) | ||
} | ||
|
||
switch action.actionType { | ||
case SearchEngineSelectionActionType.didLoadSearchEngines: | ||
guard let action = action as? SearchEngineSelectionAction, | ||
let searchEngines = action.searchEngines | ||
else { return defaultState(fromPreviousState: state) } | ||
|
||
return SearchEngineSelectionState( | ||
windowUUID: state.windowUUID, | ||
searchEngines: searchEngines | ||
) | ||
|
||
default: | ||
return defaultState(fromPreviousState: state) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.