From 65becf385bd557de5939d20d84ced5766f15af1a Mon Sep 17 00:00:00 2001 From: Vladyslav shepitko Date: Sun, 14 Nov 2021 11:34:21 +0200 Subject: [PATCH] When selecting/deselecting networks on the chain list screen, enable/disable changes are not saved if the new chain is saved #3420 --- AlphaWallet/InCoordinator.swift | 14 ++++----- AlphaWallet/Models/RestartTaskQueue.swift | 1 + .../EnabledServersCoordinator.swift | 30 ++++++------------ .../Coordinators/SettingsCoordinator.swift | 31 +++---------------- .../EnabledServersViewController.swift | 25 ++++++++++++--- .../SettingsCoordinatorTests.swift | 3 +- 6 files changed, 43 insertions(+), 61 deletions(-) diff --git a/AlphaWallet/InCoordinator.swift b/AlphaWallet/InCoordinator.swift index 3f4831ab0..a4ab4866f 100644 --- a/AlphaWallet/InCoordinator.swift +++ b/AlphaWallet/InCoordinator.swift @@ -702,7 +702,7 @@ class InCoordinator: NSObject, Coordinator { case .addServer(let server): restartQueue.remove(each) RPCServer.customRpcs.append(server) - case .editServer(original: let original, edited: let edited): + case .editServer(let original, let edited): restartQueue.remove(each) replaceServer(original: original, edited: edited) case .removeServer(let server): @@ -721,6 +721,10 @@ class InCoordinator: NSObject, Coordinator { Config.setChainId(server.chainID) case .loadUrlInDappBrowser: break + case .reloadServers(let servers): + restartQueue.remove(each) + var c = config + c.enabledServers = servers } } } @@ -757,7 +761,7 @@ class InCoordinator: NSObject, Coordinator { private func processRestartQueueAfterRestart(config: Config, coordinator: InCoordinator, restartQueue: RestartTaskQueue) { for each in restartQueue.queue { switch each { - case .addServer, .editServer, .removeServer, .enableServer, .switchDappServer: + case .addServer, .reloadServers, .editServer, .removeServer, .enableServer, .switchDappServer: break case .loadUrlInDappBrowser(let url): restartQueue.remove(each) @@ -862,11 +866,7 @@ extension InCoordinator: SettingsCoordinatorDelegate { TransactionsStorage.deleteAllTransactions(realm: Wallet.functional.realm(forAccount: account)) } - func restartToAddEnableAndSwitchBrowserToServer(in coordinator: SettingsCoordinator) { - processRestartQueueAndRestartUI() - } - - func restartToRemoveServer(in coordinator: SettingsCoordinator) { + func restartToReloadServersQueued(in coordinator: SettingsCoordinator) { processRestartQueueAndRestartUI() } } diff --git a/AlphaWallet/Models/RestartTaskQueue.swift b/AlphaWallet/Models/RestartTaskQueue.swift index ce6573ad7..b134b3893 100644 --- a/AlphaWallet/Models/RestartTaskQueue.swift +++ b/AlphaWallet/Models/RestartTaskQueue.swift @@ -13,6 +13,7 @@ class RestartTaskQueue { case enableServer(RPCServer) case switchDappServer(server: RPCServer) case loadUrlInDappBrowser(URL) + case reloadServers([RPCServer]) } init() { diff --git a/AlphaWallet/Settings/Coordinators/EnabledServersCoordinator.swift b/AlphaWallet/Settings/Coordinators/EnabledServersCoordinator.swift index 09c9cbef8..3ed129275 100644 --- a/AlphaWallet/Settings/Coordinators/EnabledServersCoordinator.swift +++ b/AlphaWallet/Settings/Coordinators/EnabledServersCoordinator.swift @@ -3,10 +3,7 @@ import UIKit protocol EnabledServersCoordinatorDelegate: AnyObject { - func didSelectServers(servers: [RPCServer], in coordinator: EnabledServersCoordinator) - func didSelectDismiss(in coordinator: EnabledServersCoordinator) - func restartToAddEnableAndSwitchBrowserToServer(in coordinator: EnabledServersCoordinator) - func restartToRemoveServer(in coordinator: EnabledServersCoordinator) + func restartToReloadServersQueued(in coordinator: EnabledServersCoordinator) } class EnabledServersCoordinator: Coordinator { @@ -46,10 +43,6 @@ class EnabledServersCoordinator: Coordinator { navigationController.pushViewController(enabledServersViewController, animated: true) } - func stop() { - navigationController.popViewController(animated: true) - } - @objc private func addRPCSelected() { let coordinator = AddRPCServerCoordinator(navigationController: navigationController, config: Config(), restartQueue: restartQueue, analyticsCoordinator: analyticsCoordinator) coordinator.delegate = self @@ -68,20 +61,13 @@ class EnabledServersCoordinator: Coordinator { } extension EnabledServersCoordinator: EnabledServersViewControllerDelegate { - func didEditSelectedServer(customRpc: CustomRPC, in viewController: EnabledServersViewController) { - self.edit(customRpc: customRpc, in: viewController) - } - func didSelectServers(servers: [RPCServer], in viewController: EnabledServersViewController) { - delegate?.didSelectServers(servers: servers, in: self) + func notifyReloadServersQueued(in viewController: EnabledServersViewController) { + delegate?.restartToReloadServersQueued(in: self) } - func didDismiss(viewController: EnabledServersViewController) { - delegate?.didSelectDismiss(in: self) - } - - func notifyRemoveCustomChainQueued(in viewController: EnabledServersViewController) { - delegate?.restartToRemoveServer(in: self) + func didEditSelectedServer(customRpc: CustomRPC, in viewController: EnabledServersViewController) { + self.edit(customRpc: customRpc, in: viewController) } } @@ -91,7 +77,8 @@ extension EnabledServersCoordinator: AddRPCServerCoordinatorDelegate { } func restartToAddEnableAndSwitchBrowserToServer(in coordinator: AddRPCServerCoordinator) { - delegate?.restartToAddEnableAndSwitchBrowserToServer(in: self) + enabledServersViewController.pushReloadServersIfNeeded() + delegate?.restartToReloadServersQueued(in: self) } } @@ -101,6 +88,7 @@ extension EnabledServersCoordinator: EditCustomRpcCoordinatorDelegate { } func restartToEdit(in coordinator: EditCustomRpcCoordinator) { - delegate?.restartToAddEnableAndSwitchBrowserToServer(in: self) + enabledServersViewController.pushReloadServersIfNeeded() + delegate?.restartToReloadServersQueued(in: self) } } diff --git a/AlphaWallet/Settings/Coordinators/SettingsCoordinator.swift b/AlphaWallet/Settings/Coordinators/SettingsCoordinator.swift index 007d697b1..5306780b1 100644 --- a/AlphaWallet/Settings/Coordinators/SettingsCoordinator.swift +++ b/AlphaWallet/Settings/Coordinators/SettingsCoordinator.swift @@ -18,8 +18,7 @@ protocol SettingsCoordinatorDelegate: class, CanOpenURL { func assetDefinitionsOverrideViewController(for: SettingsCoordinator) -> UIViewController? func showConsole(in coordinator: SettingsCoordinator) func delete(account: Wallet, in coordinator: SettingsCoordinator) - func restartToAddEnableAndSwitchBrowserToServer(in coordinator: SettingsCoordinator) - func restartToRemoveServer(in coordinator: SettingsCoordinator) + func restartToReloadServersQueued(in coordinator: SettingsCoordinator) } class SettingsCoordinator: Coordinator { @@ -239,34 +238,12 @@ extension SettingsCoordinator: LocalesCoordinatorDelegate { } extension SettingsCoordinator: EnabledServersCoordinatorDelegate { - func didSelectServers(servers: [RPCServer], in coordinator: EnabledServersCoordinator) { - //Defensive. Shouldn't allow no server to be selected - guard !servers.isEmpty else { return } - - let isUnchanged = config.enabledServers.sorted(by: { $0.chainID < $1.chainID }) == servers.sorted(by: { $0.chainID < $1.chainID }) - if isUnchanged { - coordinator.stop() - removeCoordinator(coordinator) - } else { - config.enabledServers = servers - restart(for: account, reason: .serverChange) - } - } - - func didSelectDismiss(in coordinator: EnabledServersCoordinator) { - coordinator.stop() - removeCoordinator(coordinator) - } - - func restartToAddEnableAndSwitchBrowserToServer(in coordinator: EnabledServersCoordinator) { - delegate?.restartToAddEnableAndSwitchBrowserToServer(in: self) + + func restartToReloadServersQueued(in coordinator: EnabledServersCoordinator) { + delegate?.restartToReloadServersQueued(in: self) removeCoordinator(coordinator) } - func restartToRemoveServer(in coordinator: EnabledServersCoordinator) { - delegate?.restartToRemoveServer(in: self) - removeCoordinator(coordinator) - } } extension SettingsCoordinator: PromptBackupCoordinatorSubtlePromptDelegate { diff --git a/AlphaWallet/Settings/ViewControllers/EnabledServersViewController.swift b/AlphaWallet/Settings/ViewControllers/EnabledServersViewController.swift index b6b820b98..c9a128ad7 100644 --- a/AlphaWallet/Settings/ViewControllers/EnabledServersViewController.swift +++ b/AlphaWallet/Settings/ViewControllers/EnabledServersViewController.swift @@ -3,9 +3,8 @@ import UIKit protocol EnabledServersViewControllerDelegate: AnyObject { - func didSelectServers(servers: [RPCServer], in viewController: EnabledServersViewController) - func notifyRemoveCustomChainQueued(in viewController: EnabledServersViewController) func didEditSelectedServer(customRpc: CustomRPC, in viewController: EnabledServersViewController) + func notifyReloadServersQueued(in viewController: EnabledServersViewController) } class EnabledServersViewController: UIViewController { @@ -35,6 +34,7 @@ class EnabledServersViewController: UIViewController { private var sectionIndices: IndexSet { IndexSet(integersIn: Range(uncheckedBounds: (lower: 0, sections.count))) } + private let config: Config = Config() var viewModel: EnabledServersViewModel weak var delegate: EnabledServersViewControllerDelegate? @@ -82,7 +82,21 @@ class EnabledServersViewController: UIViewController { } @objc private func done() { - delegate?.didSelectServers(servers: viewModel.selectedServers, in: self) + pushReloadServersIfNeeded() + delegate?.notifyReloadServersQueued(in: self) + } + + func pushReloadServersIfNeeded() { + let servers = viewModel.selectedServers + //Defensive. Shouldn't allow no server to be selected + guard !servers.isEmpty else { return } + + let isUnchanged = config.enabledServers.sorted(by: { $0.chainID < $1.chainID }) == servers.sorted(by: { $0.chainID < $1.chainID }) + if isUnchanged { + //no-op + } else { + restartQueue.add(.reloadServers(servers)) + } } private func confirmDelete(server: RPCServer) { @@ -103,12 +117,15 @@ class EnabledServersViewController: UIViewController { private func markForDeletion(server: RPCServer) { guard let customRpc = server.customRpc else { return } + pushReloadServersIfNeeded() restartQueue.add(.removeServer(customRpc)) - delegate?.notifyRemoveCustomChainQueued(in: self) + + delegate?.notifyReloadServersQueued(in: self) } } extension EnabledServersViewController: UITableViewDelegate, UITableViewDataSource { + func numberOfSections(in tableView: UITableView) -> Int { sections.count } diff --git a/AlphaWalletTests/Settings/Coordinators/SettingsCoordinatorTests.swift b/AlphaWalletTests/Settings/Coordinators/SettingsCoordinatorTests.swift index b20645313..997511aee 100644 --- a/AlphaWalletTests/Settings/Coordinators/SettingsCoordinatorTests.swift +++ b/AlphaWalletTests/Settings/Coordinators/SettingsCoordinatorTests.swift @@ -28,8 +28,7 @@ class SettingsCoordinatorTests: XCTestCase { func didPressViewContractWebPage(forContract contract: AlphaWallet.Address, server: RPCServer, in viewController: UIViewController) {} func didPressViewContractWebPage(_ url: URL, in viewController: UIViewController) {} func didPressOpenWebPage(_ url: URL, in viewController: UIViewController) {} - func restartToAddEnableAndSwitchBrowserToServer(in coordinator: SettingsCoordinator) {} - func restartToRemoveServer(in coordinator: SettingsCoordinator) {} + func restartToReloadServersQueued(in coordinator: SettingsCoordinator) {} } let storage = FakeTransactionsStorage(server: .main)