Skip to content

Commit

Permalink
Refactor FXIOS-7853 [v122] Redux pattern protocol improvements (backp…
Browse files Browse the repository at this point in the history
…ort #17542) (#17626)

* Refactor FXIOS-7853 [v122] Redux pattern protocol improvements (#17542)

* Add helper functions

* updates

* Update further places

* Update tests

---------

Co-authored-by: roux g. buciu <[email protected]>
(cherry picked from commit b7db64a)

# Conflicts:
#	Client/Frontend/Browser/Tabs/TabDisplayViewController.swift
#	Client/Frontend/Browser/Tabs/TabTrayViewController.swift
#	Client/Frontend/Home/HomepageViewController.swift

* Fix merge issues

---------

Co-authored-by: roux g. buciu <[email protected]>
Co-authored-by: Winnie Teichmann <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent ba50475 commit c6265ac
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 51 deletions.
2 changes: 2 additions & 0 deletions BrowserKit/Sources/Redux/StoreSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import Foundation

public protocol AnyStoreSubscriber: AnyObject {
func subscribeToRedux()
func unsubscribeFromRedux()
func newState(state: Any)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,32 @@ class FakeReduxViewController: UIViewController, StoreSubscriber {

override func viewDidLoad() {
super.viewDidLoad()
subscribeToRedux()
view.addSubview(label)
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
unsubscribeFromRedux()
}

// MARK: - Redux

func subscribeToRedux() {
store.subscribe(self)
store.dispatch(FakeReduxAction.requestInitialValue)
view.addSubview(label)
}

func unsubscribeFromRedux() {
store.unsubscribe(self)
}

func newState(state: FakeReduxState) {
label.text = "\(state.counter)"
}

// MARK: - Helper functions

func increaseCounter() {
store.dispatch(FakeReduxAction.increaseCounter)
}
Expand Down
14 changes: 9 additions & 5 deletions Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ class BrowserViewController: UIViewController,
}

deinit {
if isReduxIntegrationEnabled {
store.unsubscribe(self)
}
unsubscribeFromRedux()
}

override var prefersStatusBarHidden: Bool {
Expand Down Expand Up @@ -467,7 +465,7 @@ class BrowserViewController: UIViewController,

// MARK: - Redux

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

Expand All @@ -476,6 +474,12 @@ class BrowserViewController: UIViewController,
})
}

func unsubscribeFromRedux() {
if isReduxIntegrationEnabled {
store.unsubscribe(self)
}
}

func newState(state: FakespotState) {
ensureMainThread { [weak self] in
guard let self else { return }
Expand Down Expand Up @@ -550,7 +554,7 @@ class BrowserViewController: UIViewController,
// Send settings telemetry for Fakespot
FakespotUtils().addSettingTelemetry()

subscribeRedux()
subscribeToRedux()
}

private func setupAccessibleActions() {
Expand Down
16 changes: 10 additions & 6 deletions Client/Frontend/Browser/Tabs/RemoteTabs/RemoteTabsPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ class RemoteTabsPanel: UIViewController,
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

deinit {
if isReduxIntegrationEnabled {
store.dispatch(ActiveScreensStateAction.closeScreen(.remoteTabsPanel))
store.unsubscribe(self)
}
unsubscribeFromRedux()
}

// MARK: - Actions
Expand Down Expand Up @@ -96,7 +93,7 @@ class RemoteTabsPanel: UIViewController,

listenForThemeChange(view)
setupLayout()
subscribeRedux()
subscribeToRedux()
applyTheme()
}

Expand Down Expand Up @@ -128,7 +125,7 @@ class RemoteTabsPanel: UIViewController,

// MARK: - Redux

private func subscribeRedux() {
func subscribeToRedux() {
guard isReduxIntegrationEnabled else { return }
store.dispatch(ActiveScreensStateAction.showScreen(.remoteTabsPanel))
store.dispatch(RemoteTabsPanelAction.panelDidAppear)
Expand All @@ -137,6 +134,13 @@ class RemoteTabsPanel: UIViewController,
})
}

func unsubscribeFromRedux() {
if isReduxIntegrationEnabled {
store.dispatch(ActiveScreensStateAction.closeScreen(.remoteTabsPanel))
store.unsubscribe(self)
}
}

func newState(state: RemoteTabsPanelState) {
ensureMainThread { [weak self] in
guard let self else { return }
Expand Down
41 changes: 23 additions & 18 deletions Client/Frontend/Browser/Tabs/TabDisplayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class TabDisplayViewController: UIViewController,
private var backgroundPrivacyOverlay: UIView = .build()
private lazy var emptyPrivateTabsView: EmptyPrivateTabsView = .build()

// MARK: Redux state
var tabsState: TabsState

init(isPrivateMode: Bool,
Expand All @@ -43,30 +42,15 @@ class TabDisplayViewController: UIViewController,
}

deinit {
store.dispatch(ActiveScreensStateAction.closeScreen(.tabsPanel))
store.unsubscribe(self)
unsubscribeFromRedux()
}

override func viewDidLoad() {
super.viewDidLoad()
setupView()
listenForThemeChange(view)
applyTheme()
subscribeRedux()
}

private func subscribeRedux() {
store.dispatch(ActiveScreensStateAction.showScreen(.tabsPanel))
store.dispatch(TabPanelAction.tabPanelDidLoad(tabsState.isPrivateMode))
store.subscribe(self, transform: {
return $0.select(TabsState.init)
})
}

func newState(state: TabsState) {
tabsState = state
tabDisplayView.newState(state: tabsState)
shouldShowEmptyView(tabsState.isPrivateTabsEmpty)
subscribeToRedux()
}

func setupView() {
Expand Down Expand Up @@ -116,6 +100,27 @@ class TabDisplayViewController: UIViewController,
tabDisplayView.applyTheme(theme: themeManager.currentTheme)
}

// MARK: - Redux

func subscribeToRedux() {
store.dispatch(ActiveScreensStateAction.showScreen(.tabsPanel))
store.dispatch(TabPanelAction.tabPanelDidLoad(tabsState.isPrivateMode))
store.subscribe(self, transform: {
return $0.select(TabsState.init)
})
}

func unsubscribeFromRedux() {
store.dispatch(ActiveScreensStateAction.closeScreen(.tabsPanel))
store.unsubscribe(self)
}

func newState(state: TabsState) {
tabsState = state
tabDisplayView.newState(state: tabsState)
shouldShowEmptyView(tabsState.isPrivateTabsEmpty)
}

// MARK: EmptyPrivateTabsViewDelegate
func didTapLearnMore(urlRequest: URLRequest) {}
}
35 changes: 20 additions & 15 deletions Client/Frontend/Browser/Tabs/TabTrayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class TabTrayViewController: UIViewController,
override func viewDidLoad() {
super.viewDidLoad()
setupView()
subscribeRedux()
subscribeToRedux()
listenForThemeChange(view)
updateToolbarItems()
}
Expand Down Expand Up @@ -229,20 +229,7 @@ class TabTrayViewController: UIViewController,
super.viewDidDisappear(animated)
delegate?.didFinish()

store.dispatch(ActiveScreensStateAction.closeScreen(.tabsTray))
store.unsubscribe(self)
}

private func subscribeRedux() {
store.dispatch(ActiveScreensStateAction.showScreen(.tabsTray))
store.dispatch(TabTrayAction.tabTrayDidLoad(tabTrayState.selectedPanel))
store.subscribe(self, transform: {
return $0.select(TabTrayState.init)
})
}

func newState(state: TabTrayState) {
tabTrayState = state
unsubscribeFromRedux()
}

private func updateLayout() {
Expand All @@ -260,6 +247,24 @@ class TabTrayViewController: UIViewController,
updateToolbarItems()
}

// MARK: - Redux

func subscribeToRedux() {
store.dispatch(ActiveScreensStateAction.showScreen(.tabsTray))
store.dispatch(TabTrayAction.tabTrayDidLoad(tabTrayState.selectedPanel))
store.subscribe(self, transform: {
return $0.select(TabTrayState.init)
})
}

func unsubscribeFromRedux() {
store.dispatch(ActiveScreensStateAction.closeScreen(.tabsTray))
store.unsubscribe(self)
}

func newState(state: TabTrayState) {
}

// MARK: Themeable
func applyTheme() {
view.backgroundColor = themeManager.currentTheme.colors.layer1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ class ThemeSettingsController: ThemedTableViewController, StoreSubscriber {

override func viewDidLoad() {
super.viewDidLoad()
if isReduxIntegrationEnabled {
store.dispatch(ThemeSettingsAction.themeSettingsDidAppear)
store.subscribe(self, transform: {
$0.select(ThemeSettingsState.init)
})
}
subscribeToRedux()

title = .SettingsDisplayThemeTitle
tableView.accessibilityIdentifier = "DisplayTheme.Setting.Options"
Expand All @@ -76,6 +71,21 @@ class ThemeSettingsController: ThemedTableViewController, StoreSubscriber {

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
unsubscribeFromRedux()
}

// MARK: - Redux

func subscribeToRedux() {
if isReduxIntegrationEnabled {
store.dispatch(ThemeSettingsAction.themeSettingsDidAppear)
store.subscribe(self, transform: {
$0.select(ThemeSettingsState.init)
})
}
}

func unsubscribeFromRedux() {
if isReduxIntegrationEnabled {
store.dispatch(ActiveScreensStateAction.closeScreen(.themeSettings))
store.unsubscribe(self)
Expand Down

0 comments on commit c6265ac

Please sign in to comment.