Skip to content

Commit

Permalink
Refactor FXIOS-10246 [Homepage Rebuild] Add status bar logic to new h…
Browse files Browse the repository at this point in the history
…omepage (#23696)

* Add status bar scroll logic to new homepage

* add tests and clean up other tests
  • Loading branch information
Cramsden authored Dec 12, 2024
1 parent c5a2ff9 commit e3410ca
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ class BrowserCoordinator: BaseCoordinator,
screenshotService.screenshotableView = nil
}

func showHomepage(overlayManager: OverlayModeManager, isZeroSearch: Bool) {
func showHomepage(
overlayManager: OverlayModeManager,
isZeroSearch: Bool,
statusBarScrollDelegate: StatusBarScrollDelegate
) {
let homepageCoordinator = childCoordinators[HomepageCoordinator.self] ?? HomepageCoordinator(
windowUUID: windowUUID,
profile: profile,
Expand All @@ -148,7 +152,8 @@ class BrowserCoordinator: BaseCoordinator,
let homepageController = self.homepageViewController ?? HomepageViewController(
windowUUID: windowUUID,
homepageDelegate: homepageCoordinator,
overlayManager: overlayManager
overlayManager: overlayManager,
statusBarScrollDelegate: statusBarScrollDelegate
)
guard browserViewController.embedContent(homepageController) else {
logger.log("Unable to embed new homepage", level: .debug, category: .coordinator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ protocol BrowserDelegate: AnyObject {
)

/// Show the new homepage to the user as part of the homepage rebuild project
func showHomepage(overlayManager: OverlayModeManager, isZeroSearch: Bool)
func showHomepage(
overlayManager: OverlayModeManager,
isZeroSearch: Bool,
statusBarScrollDelegate: StatusBarScrollDelegate
)

/// Show the private homepage to the user as part of felt privacy
func showPrivateHomepage(overlayManager: OverlayModeManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,11 @@ class BrowserViewController: UIViewController,

func frontEmbeddedContent(_ viewController: ContentContainable) {
contentContainer.update(content: viewController)
statusBarOverlay.resetState(isHomepage: contentContainer.hasLegacyHomepage)
if featureFlags.isFeatureEnabled(.homepageRebuild, checking: .buildOnly) {
statusBarOverlay.resetState(isHomepage: contentContainer.hasHomepage)
} else {
statusBarOverlay.resetState(isHomepage: contentContainer.hasLegacyHomepage)
}
}

/// Embed a ContentContainable inside the content container
Expand All @@ -1326,7 +1330,11 @@ class BrowserViewController: UIViewController,
viewController.willMove(toParent: self)
contentContainer.add(content: viewController)
viewController.didMove(toParent: self)
statusBarOverlay.resetState(isHomepage: contentContainer.hasLegacyHomepage)
if featureFlags.isFeatureEnabled(.homepageRebuild, checking: .buildOnly) {
statusBarOverlay.resetState(isHomepage: contentContainer.hasHomepage)
} else {
statusBarOverlay.resetState(isHomepage: contentContainer.hasLegacyHomepage)
}

UIAccessibility.post(notification: UIAccessibility.Notification.screenChanged, argument: nil)
return true
Expand All @@ -1352,7 +1360,11 @@ class BrowserViewController: UIViewController,
}

if featureFlags.isFeatureEnabled(.homepageRebuild, checking: .buildOnly) {
browserDelegate?.showHomepage(overlayManager: overlayManager, isZeroSearch: inline)
browserDelegate?.showHomepage(
overlayManager: overlayManager,
isZeroSearch: inline,
statusBarScrollDelegate: statusBarOverlay
)
} else {
browserDelegate?.showLegacyHomepage(
inline: inline,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ final class HomepageViewController: UIViewController,
let windowUUID: WindowUUID
var currentWindowUUID: UUID? { return windowUUID }

// MARK: - Layout variables
var statusBarFrame: CGRect? {
guard let keyWindow = UIWindow.keyWindow else { return nil }

return keyWindow.windowScene?.statusBarManager?.statusBarFrame
}

weak var statusBarScrollDelegate: StatusBarScrollDelegate?

// MARK: - Private variables
private typealias a11y = AccessibilityIdentifiers.FirefoxHomepage
private weak var homepageDelegate: HomepageDelegate?
Expand All @@ -47,6 +56,7 @@ final class HomepageViewController: UIViewController,
homepageDelegate: HomepageDelegate? = nil,
themeManager: ThemeManager = AppContainer.shared.resolve(),
overlayManager: OverlayModeManager,
statusBarScrollDelegate: StatusBarScrollDelegate? = nil,
notificationCenter: NotificationProtocol = NotificationCenter.default,
logger: Logger = DefaultLogger.shared
) {
Expand All @@ -55,6 +65,7 @@ final class HomepageViewController: UIViewController,
self.themeManager = themeManager
self.notificationCenter = notificationCenter
self.overlayManager = overlayManager
self.statusBarScrollDelegate = statusBarScrollDelegate
self.logger = logger
homepageState = HomepageState(windowUUID: windowUUID)
super.init(nibName: nil, bundle: nil)
Expand Down Expand Up @@ -112,6 +123,17 @@ final class HomepageViewController: UIViewController,
wallpaperView.updateImageForOrientationChange()
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if homepageState.wallpaperState.wallpaperConfiguration.hasImage {
let theme = themeManager.getCurrentTheme(for: windowUUID)
statusBarScrollDelegate?.scrollViewDidScroll(
scrollView,
statusBarFrame: statusBarFrame,
theme: theme
)
}
}

// MARK: - Redux
func subscribeToRedux() {
let action = ScreenAction(
Expand Down Expand Up @@ -154,11 +176,6 @@ final class HomepageViewController: UIViewController,
}

// MARK: - Layout
var statusBarFrame: CGRect? {
guard let keyWindow = UIWindow.keyWindow else { return nil }

return keyWindow.windowScene?.statusBarManager?.statusBarFrame
}

func configureWallpaperView() {
view.addSubview(wallpaperView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ struct HomepageState: ScreenState, Equatable {
var windowUUID: WindowUUID

// Homepage sections state in the order they appear on the collection view
var headerState: HeaderState
var topSitesState: TopSitesSectionState
var pocketState: PocketState
var wallpaperState: WallpaperState
let headerState: HeaderState
let topSitesState: TopSitesSectionState
let pocketState: PocketState
let wallpaperState: WallpaperState

init(appState: AppState, uuid: WindowUUID) {
guard let homepageState = store.state.screenState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,22 @@ struct WallpaperConfiguration: Equatable {
var textColor: UIColor?
var cardColor: UIColor?
var logoTextColor: UIColor?
var hasImage: Bool

init(
landscapeImage: UIImage? = nil,
portraitImage: UIImage? = nil,
textColor: UIColor? = nil,
cardColor: UIColor? = nil,
logoTextColor: UIColor? = nil
logoTextColor: UIColor? = nil,
hasImage: Bool = false
) {
self.landscapeImage = landscapeImage
self.portraitImage = portraitImage
self.textColor = textColor
self.cardColor = cardColor
self.logoTextColor = logoTextColor
self.hasImage = hasImage
}

init(wallpaper: Wallpaper) {
Expand All @@ -69,7 +72,8 @@ struct WallpaperConfiguration: Equatable {
portraitImage: wallpaper.portrait,
textColor: wallpaper.textColor,
cardColor: wallpaper.cardColor,
logoTextColor: wallpaper.logoTextColor
logoTextColor: wallpaper.logoTextColor,
hasImage: wallpaper.hasImage
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ final class BrowserCoordinatorTests: XCTestCase, FeatureFlaggable {

func testShowNewHomepage_setsProperViewController() {
let subject = createSubject()
subject.showHomepage(overlayManager: overlayModeManager, isZeroSearch: false)
subject.showHomepage(
overlayManager: overlayModeManager,
isZeroSearch: false,
statusBarScrollDelegate: scrollDelegate
)

XCTAssertNotNil(subject.homepageViewController)
XCTAssertNil(subject.webviewController)
Expand All @@ -150,11 +154,19 @@ final class BrowserCoordinatorTests: XCTestCase, FeatureFlaggable {

func testShowNewHomepage_hasSameInstance() {
let subject = createSubject()
subject.showHomepage(overlayManager: overlayModeManager, isZeroSearch: false)
subject.showHomepage(
overlayManager: overlayModeManager,
isZeroSearch: false,
statusBarScrollDelegate: scrollDelegate
)
let firstHomepage = subject.homepageViewController
XCTAssertNotNil(subject.homepageViewController)

subject.showHomepage(overlayManager: overlayModeManager, isZeroSearch: false)
subject.showHomepage(
overlayManager: overlayModeManager,
isZeroSearch: false,
statusBarScrollDelegate: scrollDelegate
)
let secondHomepage = subject.homepageViewController
XCTAssertEqual(firstHomepage, secondHomepage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ final class HomepageDiffableDataSourceTests: XCTestCase {
logoTextColor: .blue
)

let wallpaperState = WallpaperState(windowUUID: .XCTestDefaultUUID, wallpaperConfiguration: wallpaperConfig)
var state = HomepageState(windowUUID: .XCTestDefaultUUID)
state.wallpaperState = wallpaperState
let state = HomepageState.reducer(
HomepageState(windowUUID: .XCTestDefaultUUID),
WallpaperAction(
wallpaperConfiguration: wallpaperConfig,
windowUUID: .XCTestDefaultUUID,
actionType: WallpaperMiddlewareActionType.wallpaperDidInitialize
)
)

dataSource.updateSnapshot(state: state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,29 @@ final class HomepageViewControllerTests: XCTestCase {
XCTAssertEqual(mockNotificationCenter?.removeObserverCallCount, 1)
}

private func createSubject() -> HomepageViewController {
func test_scrollViewDidScroll_updatesStatusBarScrollDelegate() {
let mockStatusBarScrollDelegate = MockStatusBarScrollDelegate()
let homepageVC = createSubject(statusBarScrollDelegate: mockStatusBarScrollDelegate)
let wallpaperConfiguration = WallpaperConfiguration(hasImage: true)
let newState = HomepageState.reducer(
HomepageState(windowUUID: .XCTestDefaultUUID),
WallpaperAction(
wallpaperConfiguration: wallpaperConfiguration,
windowUUID: .XCTestDefaultUUID,
actionType: WallpaperMiddlewareActionType.wallpaperDidInitialize
)
)
homepageVC.newState(state: newState)
let scrollView = UIScrollView()

XCTAssertNil(mockStatusBarScrollDelegate.savedScrollView)

homepageVC.scrollViewDidScroll(scrollView)

XCTAssertEqual(mockStatusBarScrollDelegate.savedScrollView, scrollView)
}

private func createSubject(statusBarScrollDelegate: StatusBarScrollDelegate? = nil) -> HomepageViewController {
let notificationCenter = MockNotificationCenter()
let themeManager = MockThemeManager()
let mockOverlayManager = MockOverlayModeManager()
Expand All @@ -85,6 +107,7 @@ final class HomepageViewControllerTests: XCTestCase {
windowUUID: .XCTestDefaultUUID,
themeManager: themeManager,
overlayManager: mockOverlayManager,
statusBarScrollDelegate: statusBarScrollDelegate,
notificationCenter: notificationCenter
)
trackForMemoryLeaks(homepageViewController)
Expand Down

0 comments on commit e3410ca

Please sign in to comment.