Skip to content

Commit

Permalink
Refactor FXIOS-10908 [Homepage Rebuild] Move logic for cancelling edi…
Browse files Browse the repository at this point in the history
…t from homepage to toolbar state (#23976)

* Move logic for cancelling edit to toolbar state

* Add pass through action to toolbar state

* PR Feedback
  • Loading branch information
Cramsden authored Jan 2, 2025
1 parent 4eabc8c commit 8aa4fa6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ struct AddressBarState: StateType, Equatable {
case ToolbarActionType.didStartEditingUrl:
return handleDidStartEditingUrlAction(state: state, action: action)

case ToolbarActionType.cancelEditOnHomepage:
return handleCancelEditOnHomepageAction(state: state, action: action)

case ToolbarActionType.cancelEdit:
return handleCancelEditAction(state: state, action: action)

Expand Down Expand Up @@ -523,6 +526,14 @@ struct AddressBarState: StateType, Equatable {
)
}

private static func handleCancelEditOnHomepageAction(state: Self, action: Action) -> Self {
if state.url == nil {
return handleCancelEditAction(state: state, action: action)
} else {
return handleHideKeyboardAction(state: state)
}
}

private static func handleCancelEditAction(state: Self, action: Action) -> Self {
guard let toolbarAction = action as? ToolbarAction else { return defaultState(from: state) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ enum ToolbarActionType: ActionType {
case showMenuWarningBadge
case didPasteSearchTerm
case didStartEditingUrl
case cancelEditOnHomepage
case cancelEdit
case hideKeyboard
case readerModeStateChanged
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ struct ToolbarState: ScreenState, Equatable {
case ToolbarActionType.borderPositionChanged, ToolbarActionType.urlDidChange,
ToolbarActionType.didSetTextInLocationView, ToolbarActionType.didPasteSearchTerm,
ToolbarActionType.didStartEditingUrl, ToolbarActionType.cancelEdit,
ToolbarActionType.cancelEditOnHomepage,
ToolbarActionType.hideKeyboard, ToolbarActionType.websiteLoadingStateDidChange,
ToolbarActionType.searchEngineDidChange, ToolbarActionType.clearSearch,
ToolbarActionType.didDeleteSearchTerm, ToolbarActionType.didEnterSearchTerm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,10 @@ final class HomepageViewController: UIViewController,
}

private func handleToolbarStateOnScroll() {
// TODO: FXIOS-10877 This logic will be handled by toolbar state, the homepage will just dispatch the action
let toolbarState = store.state.screenState(ToolbarState.self, for: .toolbar, window: windowUUID)

// Only dispatch action when user is in edit mode to avoid having the toolbar re-displayed
if featureFlags.isFeatureEnabled(.toolbarRefactor, checking: .buildOnly),
let toolbarState,
toolbarState.addressToolbar.isEditing {
// When the user scrolls the homepage (not overlaid on a webpage when searching) we cancel edit mode
// On a website we just dismiss the keyboard
if toolbarState.addressToolbar.url == nil {
let action = ToolbarAction(windowUUID: windowUUID, actionType: ToolbarActionType.cancelEdit)
store.dispatch(action)
} else {
let action = ToolbarAction(windowUUID: windowUUID, actionType: ToolbarActionType.hideKeyboard)
store.dispatch(action)
}
}
guard featureFlags.isFeatureEnabled(.toolbarRefactor, checking: .buildOnly) else { return }
// When the user scrolls the homepage (not overlaid on a webpage when searching) we cancel edit mode
let action = ToolbarAction(windowUUID: windowUUID, actionType: ToolbarActionType.cancelEditOnHomepage)
store.dispatch(action)
}

// MARK: - Redux
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class HomepageViewControllerTests: XCTestCase, StoreTestUtility {

override func setUp() {
super.setUp()
LegacyFeatureFlagsManager.shared.initializeDeveloperFeatures(with: MockProfile())
DependencyHelperMock().bootstrapDependencies()
setupStore()
}
Expand Down Expand Up @@ -144,6 +145,23 @@ final class HomepageViewControllerTests: XCTestCase, StoreTestUtility {
XCTAssertEqual(actionType, GeneralBrowserMiddlewareActionType.websiteDidScroll)
}

func test_scrollViewWillBeginDragging_triggersHomepageAction() throws {
let homepageVC = createSubject()
let scrollView = UIScrollView()
scrollView.contentOffset.y = 10
setupNimbusToolbarRefactorTesting(isEnabled: true)

homepageVC.scrollViewWillBeginDragging(scrollView)

let actionCalled = try XCTUnwrap(
mockStore.dispatchedActions.first(where: {
$0 is ToolbarAction
}) as? ToolbarAction
)
let actionType = try XCTUnwrap(actionCalled.actionType as? ToolbarActionType)
XCTAssertEqual(actionType, ToolbarActionType.cancelEditOnHomepage)
}

private func createSubject(statusBarScrollDelegate: StatusBarScrollDelegate? = nil) -> HomepageViewController {
let notificationCenter = MockNotificationCenter()
let themeManager = MockThemeManager()
Expand Down Expand Up @@ -174,4 +192,12 @@ final class HomepageViewControllerTests: XCTestCase, StoreTestUtility {
func resetStore() {
StoreTestUtilityHelper.resetStore()
}

private func setupNimbusToolbarRefactorTesting(isEnabled: Bool) {
FxNimbus.shared.features.toolbarRefactorFeature.with { _, _ in
return ToolbarRefactorFeature(
enabled: isEnabled
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -428,35 +428,46 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
XCTAssertFalse(newState.showQRPageAction)
}

func test_cancelEditAction_onHomepage_returnsExpectedState() {
func test_cancelEditOnHomepageAction_withURL_returnsExpectedState() {
setupStore()
let initialState = createSubject()
let reducer = addressBarReducer()
let didChangeURLAction = ToolbarAction(url: URL(string: "https://mozilla.com")!,
windowUUID: windowUUID,
actionType: ToolbarActionType.urlDidChange
)

let stateWithURL = reducer(initialState, didChangeURLAction)

let newState = reducer(
initialState,
stateWithURL,
ToolbarAction(
windowUUID: windowUUID,
actionType: ToolbarActionType.cancelEdit
actionType: ToolbarActionType.cancelEditOnHomepage
)
)

XCTAssertEqual(newState.windowUUID, windowUUID)
XCTAssertEqual(newState.navigationActions.count, 0)
XCTAssertFalse(newState.shouldShowKeyboard)
XCTAssertEqual(newState.isEditing, initialState.isEditing)
}

XCTAssertEqual(newState.pageActions.count, 1)
XCTAssertEqual(newState.pageActions[0].actionType, .qrCode)
func test_cancelEditOnHomepageAction_withNoURL_returnsExpectedState() {
setupStore()
let initialState = createSubject()
let reducer = addressBarReducer()

XCTAssertEqual(newState.browserActions.count, 2)
XCTAssertEqual(newState.browserActions[0].actionType, .tabs)
XCTAssertEqual(newState.browserActions[1].actionType, .menu)
let newState = reducer(
initialState,
ToolbarAction(
windowUUID: windowUUID,
actionType: ToolbarActionType.cancelEditOnHomepage
)
)

XCTAssertEqual(newState.searchTerm, nil)
XCTAssertEqual(newState.windowUUID, windowUUID)
XCTAssertFalse(newState.isEditing)
XCTAssertTrue(newState.shouldShowKeyboard)
XCTAssertFalse(newState.shouldSelectSearchTerm)
XCTAssertFalse(newState.didStartTyping)
XCTAssertTrue(newState.showQRPageAction)
}

func test_cancelEditAction_withWebsite_returnsExpectedState() {
Expand Down

0 comments on commit 8aa4fa6

Please sign in to comment.