Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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