Skip to content

Commit

Permalink
Refactor FXIOS-7817 [v122] Add BrowserViewControllerState (#17565)
Browse files Browse the repository at this point in the history
* Lots of learning

* Updates & cleanup

* Remove things

* Minor refactor lolololololol

* Language and big brain fixes

* PR comments

---------

Co-authored-by: roux g. buciu <github@fringe.foundation>
(cherry picked from commit 723d77f)

# Conflicts:
#	Client.xcodeproj/project.pbxproj
#	Client/Frontend/FeltPrivacy/FeltPrivacyAction.swift
#	Client/Frontend/Home/HomepageViewController.swift
#	Client/Redux/GlobalState/ActiveScreenState.swift
#	Client/Redux/GlobalState/AppState.swift
#	Client/TabManagement/TabManager.swift
  • Loading branch information
adudenamedruby authored and mergify[bot] committed Dec 5, 2023
1 parent c6265ac commit e540032
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 23 deletions.
35 changes: 35 additions & 0 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,14 @@
7BEB64441C7345600092C02E /* L10nSuite2SnapshotTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B3632D31C2983F000D12AF9 /* L10nSuite2SnapshotTests.swift */; };
7BEB64451C7345600092C02E /* SnapshotHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B60B0071BDE3AE10090C984 /* SnapshotHelper.swift */; };
7BEFC6801BFF68C30059C952 /* QuickActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BEFC67F1BFF68C30059C952 /* QuickActions.swift */; };
<<<<<<< HEAD
=======
810FF3542B178343009F062C /* FeltPrivacyMiddleware.swift in Sources */ = {isa = PBXBuildFile; fileRef = 810FF3532B178343009F062C /* FeltPrivacyMiddleware.swift */; };
810FF3562B1783B0009F062C /* FeltPrivacyManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 810FF3552B1783B0009F062C /* FeltPrivacyManager.swift */; };
810FF3582B1784E7009F062C /* FeltPrivacyAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 810FF3572B1784E7009F062C /* FeltPrivacyAction.swift */; };
810FF35A2B178556009F062C /* FeltPrivacyState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 810FF3592B178556009F062C /* FeltPrivacyState.swift */; };
81CAE4DB2B1A2C220040C78A /* BrowserViewControllerState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81CAE4DA2B1A2C220040C78A /* BrowserViewControllerState.swift */; };
>>>>>>> 723d77f29 (Refactor FXIOS-7817 [v122] Add BrowserViewControllerState (#17565))
884CA7492344A301002E4711 /* TextContentDetector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 884CA7482344A301002E4711 /* TextContentDetector.swift */; };
8A0017C128A3FF6100FEFC8B /* MessageCardDataAdaptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A0017C028A3FF6100FEFC8B /* MessageCardDataAdaptor.swift */; };
8A01891C275E9C2A00923EFE /* ClearHistorySheetProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A01891B275E9C2A00923EFE /* ClearHistorySheetProvider.swift */; };
Expand Down Expand Up @@ -5007,12 +5015,17 @@
80B54D93BC553D6E6CDFAA0A /* cy */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = cy; path = cy.lproj/AuthenticationManager.strings; sourceTree = "<group>"; };
80B8473B933671C9605DC837 /* gl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = gl; path = gl.lproj/InfoPlist.strings; sourceTree = "<group>"; };
80E24B20A5655593E1D9E85B /* zh-TW */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-TW"; path = "zh-TW.lproj/Search.strings"; sourceTree = "<group>"; };
810FF3532B178343009F062C /* FeltPrivacyMiddleware.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeltPrivacyMiddleware.swift; sourceTree = "<group>"; };
810FF3552B1783B0009F062C /* FeltPrivacyManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeltPrivacyManager.swift; sourceTree = "<group>"; };
810FF3572B1784E7009F062C /* FeltPrivacyAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeltPrivacyAction.swift; sourceTree = "<group>"; };
810FF3592B178556009F062C /* FeltPrivacyState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeltPrivacyState.swift; sourceTree = "<group>"; };
81504EA4876ED3D974FDA0F6 /* tr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = tr; path = tr.lproj/AuthenticationManager.strings; sourceTree = "<group>"; };
81584D62B22049E40FC78F93 /* sl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sl; path = sl.lproj/3DTouchActions.strings; sourceTree = "<group>"; };
816D458CBEAE2A6721134028 /* dsb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = dsb; path = dsb.lproj/ClearPrivateData.strings; sourceTree = "<group>"; };
81754147A06566E64C025F70 /* nn */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nn; path = "nn.lproj/Default Browser.strings"; sourceTree = "<group>"; };
81A244F4A7C6FC8976DC21F0 /* br */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = br; path = br.lproj/InfoPlist.strings; sourceTree = "<group>"; };
81C14765AA7C25DF1817AC04 /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/Today.strings; sourceTree = "<group>"; };
81CAE4DA2B1A2C220040C78A /* BrowserViewControllerState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserViewControllerState.swift; sourceTree = "<group>"; };
81DF4C79AE53A2FCA08EEE9C /* da */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = da; path = "da.lproj/Default Browser.strings"; sourceTree = "<group>"; };
81E446CF88B278374B416BF5 /* or */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = or; path = or.lproj/AuthenticationManager.strings; sourceTree = "<group>"; };
81F647D98E79EC6406CDFCB4 /* lt */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = lt; path = lt.lproj/ClearPrivateData.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -8462,6 +8475,18 @@
path = Menu;
sourceTree = "<group>";
};
810FF3522B1782FE009F062C /* FeltPrivacy */ = {
isa = PBXGroup;
children = (
81CAE4DA2B1A2C220040C78A /* BrowserViewControllerState.swift */,
810FF3532B178343009F062C /* FeltPrivacyMiddleware.swift */,
810FF3592B178556009F062C /* FeltPrivacyState.swift */,
810FF3572B1784E7009F062C /* FeltPrivacyAction.swift */,
810FF3552B1783B0009F062C /* FeltPrivacyManager.swift */,
);
path = FeltPrivacy;
sourceTree = "<group>";
};
8A0017BF28A3FED300FEFC8B /* MessageCard */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -10970,6 +10995,7 @@
F84B21F11A0910F600AAB793 /* Frontend */ = {
isa = PBXGroup;
children = (
810FF3522B1782FE009F062C /* FeltPrivacy */,
E1D3FFAF2A66A9BD002D31EF /* Fakespot */,
43D00490296FC44B00CB0F31 /* Autofill */,
C855728029AE7EF900AF32B0 /* SurveySurface */,
Expand Down Expand Up @@ -12758,6 +12784,7 @@
1DEBC55E2AC4ED70006E4801 /* RemoteTabsPanel.swift in Sources */,
435D660523D794B90046EFA2 /* UpdateViewModel.swift in Sources */,
9658143C29FAB610007339BD /* CreditCardInputFieldHelper.swift in Sources */,
810FF3542B178343009F062C /* FeltPrivacyMiddleware.swift in Sources */,
215B457F27D7FD4B00E5E800 /* LegacyTabGroupData.swift in Sources */,
E12BD0B028AC3A7E0029AAF0 /* UIEdgeInsets+Extension.swift in Sources */,
C2D80BE72AADE38100CDF7A9 /* CredentialAutofillCoordinator.swift in Sources */,
Expand Down Expand Up @@ -12847,6 +12874,7 @@
8A07910F278F62F2005529CB /* AdjustHelper.swift in Sources */,
E1442FC2294782C3003680B0 /* NSAttributedString+Extension.swift in Sources */,
967A028E28FA026F003C35E3 /* SceneDelegate.swift in Sources */,
810FF3562B1783B0009F062C /* FeltPrivacyManager.swift in Sources */,
C8B07A4128199500000AFCE7 /* NimbusFlaggableFeature.swift in Sources */,
0B62EFD21AD63CD100ACB9CD /* Clearables.swift in Sources */,
431C0CA925C890E500395CE4 /* DefaultBrowserOnboardingViewModel.swift in Sources */,
Expand Down Expand Up @@ -12899,6 +12927,7 @@
2178A6A0291454B5002EC290 /* ReaderModeThemeButton.swift in Sources */,
39F4C10A2045DB2E00746155 /* FocusHelper.swift in Sources */,
E4CD9F2D1A6DC91200318571 /* TabLocationView.swift in Sources */,
81CAE4DB2B1A2C220040C78A /* BrowserViewControllerState.swift in Sources */,
C8BD87602A0C248000CD803A /* OnboardingButtonsModel.swift in Sources */,
0BB5B2881AC0A2B90052877D /* SnackBar.swift in Sources */,
E1442FCF294782D9003680B0 /* UIView+Screenshot.swift in Sources */,
Expand Down Expand Up @@ -13244,6 +13273,7 @@
E1ADE23C2B0649F200FD17AA /* FakespotState.swift in Sources */,
2816F0001B33E05400522243 /* UIConstants.swift in Sources */,
21E78A7228F9A93100F8D687 /* UIDeviceInterface.swift in Sources */,
810FF35A2B178556009F062C /* FeltPrivacyState.swift in Sources */,
EBB89508219398E500EB91A0 /* ContentBlocker+Safelist.swift in Sources */,
437A9B6A2681257F00FB41C1 /* LegacyInactiveTabViewModel.swift in Sources */,
C838FD5E289981240068F60B /* WallpaperURLProvider.swift in Sources */,
Expand Down Expand Up @@ -13356,7 +13386,12 @@
DA4F826729AD221600189590 /* ZoomPageBar.swift in Sources */,
59A68E0B4ABBF55E14819668 /* BookmarksPanel.swift in Sources */,
BD4B2DE429BB4D9A005FAA50 /* TimerSnackBar.swift in Sources */,
<<<<<<< HEAD
21EA466A2B04130500AAAB2D /* TabsState.swift in Sources */,
=======
810FF3582B1784E7009F062C /* FeltPrivacyAction.swift in Sources */,
21EA466A2B04130500AAAB2D /* TabsPanelState.swift in Sources */,
>>>>>>> 723d77f29 (Refactor FXIOS-7817 [v122] Add BrowserViewControllerState (#17565))
D04D1B862097859B0074B35F /* DownloadToast.swift in Sources */,
C29B64EE2AD937D500F3244B /* QRCodeNavigationHandler.swift in Sources */,
8A19ACB42A3290D9001C2147 /* ContentBlockerSetting.swift in Sources */,
Expand Down
20 changes: 11 additions & 9 deletions Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class BrowserViewController: UIViewController,
static let ActionSheetTitleMaxLength = 120
}

typealias SubscriberStateType = BrowserViewControllerState

private let KVOs: [KVOConstants] = [
.estimatedProgress,
.loading,
Expand Down Expand Up @@ -80,7 +82,7 @@ class BrowserViewController: UIViewController,
let tabManager: TabManager
let ratingPromptManager: RatingPromptManager
lazy var isTabTrayRefactorEnabled: Bool = TabTrayFlagManager.isRefactorEnabled
private var fakespotState: FakespotState?
private var browserViewControllerState: BrowserViewControllerState?

// Header stack view can contain the top url bar, top reader mode, top ZoomPageBar
var header: BaseAlphaStackView = .build { _ in }
Expand Down Expand Up @@ -467,10 +469,10 @@ class BrowserViewController: UIViewController,

func subscribeToRedux() {
guard isReduxIntegrationEnabled else { return }
store.dispatch(ActiveScreensStateAction.showScreen(.fakespot))
store.dispatch(ActiveScreensStateAction.showScreen(.browserViewController))

store.subscribe(self, transform: {
$0.select(FakespotState.init)
$0.select(BrowserViewControllerState.init)
})
}

Expand All @@ -480,17 +482,17 @@ class BrowserViewController: UIViewController,
}
}

func newState(state: FakespotState) {
func newState(state: BrowserViewControllerState) {
ensureMainThread { [weak self] in
guard let self else { return }

fakespotState = state
browserViewControllerState = state

// opens or close sidebar/bottom sheet to match the saved state
if state.isOpenOnProductPage {
if state.fakespotState.isOpenOnProductPage {
guard let productURL = urlBar.currentURL else { return }
handleFakespotFlow(productURL: productURL)
} else {
} else if !state.fakespotState.isOpenOnProductPage {
_ = dismissFakespotIfNeeded()
}
}
Expand Down Expand Up @@ -761,7 +763,7 @@ class BrowserViewController: UIViewController,
var fakespotNeedsUpdate = false
if urlBar.currentURL != nil {
fakespotNeedsUpdate = contentStackView.isSidebarVisible != FakespotUtils().shouldDisplayInSidebar(viewSize: size)
if isReduxIntegrationEnabled, let fakespotState = fakespotState {
if isReduxIntegrationEnabled, let fakespotState = browserViewControllerState?.fakespotState {
fakespotNeedsUpdate = fakespotNeedsUpdate && fakespotState.isOpenOnProductPage
}

Expand Down Expand Up @@ -1707,7 +1709,7 @@ class BrowserViewController: UIViewController,
!tab.isPrivate,
FakespotUtils().shouldDisplayInSidebar(),
isReduxIntegrationEnabled,
let fakespotState = fakespotState,
let fakespotState = browserViewControllerState?.fakespotState,
fakespotState.isOpenOnProductPage {
handleFakespotFlow(productURL: url)
} else if contentStackView.isSidebarVisible {
Expand Down
9 changes: 2 additions & 7 deletions Client/Frontend/Fakespot/FakespotState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ import Redux
struct FakespotState: ScreenState, Equatable {
var isOpenOnProductPage: Bool

init(_ appState: AppState) {
guard let fakespotState = store.state.screenState(FakespotState.self, for: .fakespot) else {
self.init()
return
}

self.init(isOpenOnProductPage: fakespotState.isOpenOnProductPage)
init(_ appState: BrowserViewControllerState) {
self.init(isOpenOnProductPage: appState.fakespotState.isOpenOnProductPage)
}

init() {
Expand Down
53 changes: 53 additions & 0 deletions Client/Frontend/FeltPrivacy/BrowserViewControllerState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 Foundation
import Redux

struct BrowserViewControllerState: ScreenState, Equatable {
var feltPrivacyState: FeltPrivacyState
var fakespotState: FakespotState

init(_ appState: AppState) {
guard let bvcState = store.state.screenState(
BrowserViewControllerState.self,
for: .browserViewController)
else {
self.init()
return
}

self.init(feltPrivacyState: bvcState.feltPrivacyState,
fakespotState: bvcState.fakespotState)
}

init() {
self.init(
feltPrivacyState: FeltPrivacyState(),
fakespotState: FakespotState())
}

init(
feltPrivacyState: FeltPrivacyState,
fakespotState: FakespotState
) {
self.feltPrivacyState = feltPrivacyState
self.fakespotState = fakespotState
}

static let reducer: Reducer<Self> = { state, action in
switch action {
case FeltPrivacyAction.privateModeUpdated(let privacyState):
return BrowserViewControllerState(
feltPrivacyState: FeltPrivacyState.reducer(state.feltPrivacyState, action),
fakespotState: state.fakespotState)
case FakespotAction.toggleAppearance(let isEnabled):
return BrowserViewControllerState(
feltPrivacyState: state.feltPrivacyState,
fakespotState: FakespotState.reducer(state.fakespotState, action))
default:
return state
}
}
}
14 changes: 14 additions & 0 deletions Client/Frontend/FeltPrivacy/FeltPrivacyAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 Foundation
import Redux

enum FeltPrivacyAction: Action {
// middleware actions
case privateModeUpdated(Bool)

// user actions
case setPrivateModeTo(Bool)
}
26 changes: 26 additions & 0 deletions Client/Frontend/FeltPrivacy/FeltPrivacyManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 Foundation

protocol FeltPrivacyProtocol {
func getPrivateModeState() -> Bool
func setPrivateModeState(to state: Bool)
}

class FeltPrivacyManager {
private var isInPrivateMode: Bool

init(isInPrivateMode: Bool) {
self.isInPrivateMode = isInPrivateMode
}

func getPrivateModeState() -> Bool {
return isInPrivateMode
}

func setPrivateModeState(to newState: Bool) {
isInPrivateMode = newState
}
}
29 changes: 29 additions & 0 deletions Client/Frontend/FeltPrivacy/FeltPrivacyMiddleware.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 Foundation
import Redux

class FeltPrivacyMiddleware {
var privacyManager: FeltPrivacyManager

init(privacyManager: FeltPrivacyManager = FeltPrivacyManager(isInPrivateMode: false)) {
self.privacyManager = privacyManager
}

lazy var privacyManagerProvider: Middleware<AppState> = { state, action in
switch action {
case FeltPrivacyAction.setPrivateModeTo(let privateState):
self.updateManagerWith(newState: privateState)
store.dispatch(FeltPrivacyAction.privateModeUpdated(self.privacyManager.getPrivateModeState()))
default:
break
}
}

// MARK: - Helper Functions
private func updateManagerWith(newState: Bool) {
privacyManager.setPrivateModeState(to: newState)
}
}
31 changes: 31 additions & 0 deletions Client/Frontend/FeltPrivacy/FeltPrivacyState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 Foundation
import Redux

struct FeltPrivacyState: ScreenState, Equatable {
var shouldHideSearchSuggestionView: Bool

init(_ appState: BrowserViewControllerState) {
self.init(shouldHideSearchSuggestionView: appState.feltPrivacyState.shouldHideSearchSuggestionView)
}

init() {
self.init(shouldHideSearchSuggestionView: false)
}

init(shouldHideSearchSuggestionView: Bool) {
self.shouldHideSearchSuggestionView = shouldHideSearchSuggestionView
}

static let reducer: Reducer<Self> = { state, action in
switch action {
case FeltPrivacyAction.privateModeUpdated(let privacyState):
return FeltPrivacyState(shouldHideSearchSuggestionView: privacyState)
default:
return state
}
}
}
9 changes: 9 additions & 0 deletions Client/Frontend/Home/HomepageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ import MozillaAppServices
import Common
import ComponentLibrary

<<<<<<< HEAD
class HomepageViewController: UIViewController, FeatureFlaggable, Themeable, ContentContainable,
SearchBarLocationProvider {
=======
class HomepageViewController:
UIViewController,
FeatureFlaggable,
Themeable,
ContentContainable,
SearchBarLocationProvider {
>>>>>>> 723d77f29 (Refactor FXIOS-7817 [v122] Add BrowserViewControllerState (#17565))
// MARK: - Typealiases
private typealias a11y = AccessibilityIdentifiers.FirefoxHomepage
typealias SendToDeviceDelegate = InstructionsViewDelegate & DevicePickerViewControllerDelegate
Expand Down
2 changes: 1 addition & 1 deletion Client/Redux/GlobalState/ActiveScreenAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import Foundation
import Redux

enum AppScreen {
case browserViewController
case themeSettings
case tabsTray
case tabsPanel
case remoteTabsPanel
case fakespot
}

enum ActiveScreensStateAction: Action {
Expand Down
Loading

0 comments on commit e540032

Please sign in to comment.