From b7532915cd92197284fe58511f1338505387dbf7 Mon Sep 17 00:00:00 2001 From: Hwee-Boon Yar Date: Tue, 7 Dec 2021 01:51:53 +0800 Subject: [PATCH] Add diagnostic action in Settings > Advanced > Ping Infura --- AlphaWallet.xcodeproj/project.pbxproj | 4 + .../Analytics/Models/AnalyticsTypes.swift | 1 + .../Localization/en.lproj/Localizable.strings | 3 + .../Localization/es.lproj/Localizable.strings | 3 + .../Localization/ja.lproj/Localizable.strings | 3 + .../Localization/ko.lproj/Localizable.strings | 3 + .../zh-Hans.lproj/Localizable.strings | 3 + .../Coordinators/PingInfuraCoordinator.swift | 78 +++++++++++++++++++ .../Coordinators/SettingsCoordinator.swift | 19 ++++- .../AdvancedSettingsViewController.swift | 6 +- .../AdvancedSettingsViewModel.swift | 10 ++- 11 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 AlphaWallet/Settings/Coordinators/PingInfuraCoordinator.swift diff --git a/AlphaWallet.xcodeproj/project.pbxproj b/AlphaWallet.xcodeproj/project.pbxproj index 21045824d..e16acef08 100644 --- a/AlphaWallet.xcodeproj/project.pbxproj +++ b/AlphaWallet.xcodeproj/project.pbxproj @@ -220,6 +220,7 @@ 5E7C70397E7E3A9C88E995B1 /* WKWebViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7BEDC786FB048A1DD9A8 /* WKWebViewExtension.swift */; }; 5E7C7041A8A78E0EF84A94A2 /* ActivityViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C71DB4EDD10EA665101C2 /* ActivityViewCell.swift */; }; 5E7C705166218753CAA19A6D /* TokenIdOrigin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7587F30FF1896039B8C8 /* TokenIdOrigin.swift */; }; + 5E7C7062A44AF416B110008A /* PingInfuraCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C757AEC6586F51EC75646 /* PingInfuraCoordinator.swift */; }; 5E7C706322B52C0D0CED8018 /* aETH.tsml in Resources */ = {isa = PBXBuildFile; fileRef = 5E7C7B211FF0FE5BE98BB7D0 /* aETH.tsml */; }; 5E7C7066794663954D9C18E5 /* withRetry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C71AFE747055742986023 /* withRetry.swift */; }; 5E7C7067E8FEA5055BF83553 /* HDWallet+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7C6759CA1C223DABA462 /* HDWallet+Extension.swift */; }; @@ -1377,6 +1378,7 @@ 5E7C7553BB089397B7E74BE0 /* WalletSecurityLevelIndicator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WalletSecurityLevelIndicator.swift; sourceTree = ""; }; 5E7C7558286761EF1ADD2988 /* ABIError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ABIError.swift; sourceTree = ""; }; 5E7C7570889AD33EA942D9A6 /* AlphaWalletAddressTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlphaWalletAddressTests.swift; sourceTree = ""; }; + 5E7C757AEC6586F51EC75646 /* PingInfuraCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PingInfuraCoordinator.swift; sourceTree = ""; }; 5E7C7587F30FF1896039B8C8 /* TokenIdOrigin.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TokenIdOrigin.swift; sourceTree = ""; }; 5E7C758EB5C7B77A0EE6BC9E /* MixpanelCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MixpanelCoordinator.swift; sourceTree = ""; }; 5E7C758EEBD945A3451C96C8 /* OpenSeaNonFungibleTokenHandling.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenSeaNonFungibleTokenHandling.swift; sourceTree = ""; }; @@ -2483,6 +2485,7 @@ 5E7C7C51CEC4AAFDFBD75482 /* ConsoleCoordinator.swift */, 5E7C72571AB0FECB26FEB1B1 /* ClearDappBrowserCacheCoordinator.swift */, 02220CFB273A9A02006A09BF /* SaveCustomRpcCoordinator.swift */, + 5E7C757AEC6586F51EC75646 /* PingInfuraCoordinator.swift */, ); path = Coordinators; sourceTree = ""; @@ -6204,6 +6207,7 @@ 5E7C7FF312AB61DBEBD86C94 /* WhatsNewViewModels.swift in Sources */, 5E7C7CC1E06FFC3A5BEBF1EC /* WhatsNewViews.swift in Sources */, 5E7C71967C34DD3F207F8126 /* WhatsNewExperimentCoordinator.swift in Sources */, + 5E7C7062A44AF416B110008A /* PingInfuraCoordinator.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/AlphaWallet/Analytics/Models/AnalyticsTypes.swift b/AlphaWallet/Analytics/Models/AnalyticsTypes.swift index b62323f01..6b0b51984 100644 --- a/AlphaWallet/Analytics/Models/AnalyticsTypes.swift +++ b/AlphaWallet/Analytics/Models/AnalyticsTypes.swift @@ -58,6 +58,7 @@ enum Analytics { case walletConnectConnectionTimeout = "WalletConnect Connection Timeout" case walletConnectConnectionFailed = "WalletConnect Connection Failed" case clearBrowserCache = "Clear Browser Cache" + case pingInfura = "Ping Infura" case rectifySendTransactionErrorInActionSheet = "Rectify Send Txn Error" case nameWallet = "Name Wallet" case firstWalletAction = "First Wallet Action" diff --git a/AlphaWallet/Localization/en.lproj/Localizable.strings b/AlphaWallet/Localization/en.lproj/Localizable.strings index 31d8dd838..106a20a65 100644 --- a/AlphaWallet/Localization/en.lproj/Localizable.strings +++ b/AlphaWallet/Localization/en.lproj/Localizable.strings @@ -102,6 +102,9 @@ "settings.contactUs.title" = "Contact Us"; "settings.changeCurrency.title" = "Change Currency"; "settings.analitycs.title" = "Analytics"; +"settings.pingInfura.title" = "Ping Infura"; +"settings.pingInfura.successful" = "Infura looks OK"; +"settings.pingInfura.fail" = "Infura Error"; "settings.support.title" = "Support"; "settings.notifications.title" = "Notifications"; "settings.passcode.title" = "Passcode / Touch ID"; diff --git a/AlphaWallet/Localization/es.lproj/Localizable.strings b/AlphaWallet/Localization/es.lproj/Localizable.strings index 931acf6db..cf6ad06d7 100644 --- a/AlphaWallet/Localization/es.lproj/Localizable.strings +++ b/AlphaWallet/Localization/es.lproj/Localizable.strings @@ -102,6 +102,9 @@ "settings.contactUs.title" = "Contacta con nosotros"; "settings.changeCurrency.title" = "Cambiar moneda"; "settings.analitycs.title" = "Análisis"; +"settings.pingInfura.title" = "Ping Infura"; +"settings.pingInfura.successful" = "Infura looks OK"; +"settings.pingInfura.fail" = "Infura Error"; "settings.support.title" = "Asistencia"; "settings.notifications.title" = "Notificaciones"; "settings.passcode.title" = "Código / Touch ID"; diff --git a/AlphaWallet/Localization/ja.lproj/Localizable.strings b/AlphaWallet/Localization/ja.lproj/Localizable.strings index 151dafcd5..773c43ce1 100644 --- a/AlphaWallet/Localization/ja.lproj/Localizable.strings +++ b/AlphaWallet/Localization/ja.lproj/Localizable.strings @@ -101,6 +101,9 @@ "settings.contactUs.title" = "Contact Us"; "settings.changeCurrency.title" = "Change Currency"; "settings.analitycs.title" = "Analytics"; +"settings.pingInfura.title" = "Ping Infura"; +"settings.pingInfura.successful" = "Infura looks OK"; +"settings.pingInfura.fail" = "Infura Error"; "settings.support.title" = "Support"; "settings.notifications.title" = "Notifications"; "settings.passcode.title" = "Passcode / Touch ID"; diff --git a/AlphaWallet/Localization/ko.lproj/Localizable.strings b/AlphaWallet/Localization/ko.lproj/Localizable.strings index 4edfe371c..f828833d4 100644 --- a/AlphaWallet/Localization/ko.lproj/Localizable.strings +++ b/AlphaWallet/Localization/ko.lproj/Localizable.strings @@ -101,6 +101,9 @@ "settings.contactUs.title" = "Contact Us"; "settings.changeCurrency.title" = "Change Currency"; "settings.analitycs.title" = "Analytics"; +"settings.pingInfura.title" = "Ping Infura"; +"settings.pingInfura.successful" = "Infura looks OK"; +"settings.pingInfura.fail" = "Infura Error"; "settings.support.title" = "Support"; "settings.notifications.title" = "Notifications"; "settings.passcode.title" = "Passcode / Touch ID"; diff --git a/AlphaWallet/Localization/zh-Hans.lproj/Localizable.strings b/AlphaWallet/Localization/zh-Hans.lproj/Localizable.strings index 044abd6a2..e6a2c11b6 100644 --- a/AlphaWallet/Localization/zh-Hans.lproj/Localizable.strings +++ b/AlphaWallet/Localization/zh-Hans.lproj/Localizable.strings @@ -102,6 +102,9 @@ "settings.contactUs.title" = "联系我们"; "settings.changeCurrency.title" = "更改货币"; "settings.analitycs.title" = "分析"; +"settings.pingInfura.title" = "Ping Infura"; +"settings.pingInfura.successful" = "Infura looks OK"; +"settings.pingInfura.fail" = "Infura Error"; "settings.support.title" = "支持"; "settings.notifications.title" = "通知设置"; "settings.passcode.title" = "密码 / Touch ID"; diff --git a/AlphaWallet/Settings/Coordinators/PingInfuraCoordinator.swift b/AlphaWallet/Settings/Coordinators/PingInfuraCoordinator.swift new file mode 100644 index 000000000..243ba77d1 --- /dev/null +++ b/AlphaWallet/Settings/Coordinators/PingInfuraCoordinator.swift @@ -0,0 +1,78 @@ +// Copyright © 2021 Stormbird PTE. LTD. + +import UIKit +import APIKit +import JSONRPCKit +import PromiseKit + +protocol PingInfuraCoordinatorDelegate: AnyObject { + func didPing(in coordinator: PingInfuraCoordinator) + func didCancel(in coordinator: PingInfuraCoordinator) +} + +class PingInfuraCoordinator: Coordinator { + private let viewController: UIViewController + private let analyticsCoordinator: AnalyticsCoordinator + + var coordinators: [Coordinator] = [] + weak var delegate: PingInfuraCoordinatorDelegate? + + init(inViewController viewController: UIViewController, analyticsCoordinator: AnalyticsCoordinator) { + self.viewController = viewController + self.analyticsCoordinator = analyticsCoordinator + } + + func start() { + UIAlertController.alert(title: "\(R.string.localizable.settingsPingInfuraTitle())?", + message: nil, + alertButtonTitles: [R.string.localizable.oK(), R.string.localizable.cancel()], + alertButtonStyles: [.default, .cancel], + viewController: viewController, + completion: { choice in + guard choice == 0 else { + self.delegate?.didCancel(in: self) + return + } + self.pingInfura() + self.logUse() + self.delegate?.didPing(in: self) + }) + } + + private func pingInfura() { + let request = EtherServiceRequest(server: .main, batch: BatchFactory().create(BlockNumberRequest())) + firstly { + Session.send(request) + }.done { (x: Int) in + UIAlertController.alert( + title: R.string.localizable.settingsPingInfuraSuccessful(), + message: nil, + alertButtonTitles: [ + R.string.localizable.oK() + ], + alertButtonStyles: [ + .cancel + ], + viewController: self.viewController, + style: .alert) + }.catch { error in + UIAlertController.alert(title: R.string.localizable.settingsPingInfuraFail(), + message: "\(error)", + alertButtonTitles: [ + R.string.localizable.oK(), + ], + alertButtonStyles: [ + .cancel + ], + viewController: self.viewController, + style: .alert) + } + } +} + +// MARK: Analytics +extension PingInfuraCoordinator { + private func logUse() { + analyticsCoordinator.log(action: Analytics.Action.pingInfura) + } +} \ No newline at end of file diff --git a/AlphaWallet/Settings/Coordinators/SettingsCoordinator.swift b/AlphaWallet/Settings/Coordinators/SettingsCoordinator.swift index 172dcafde..91261db68 100644 --- a/AlphaWallet/Settings/Coordinators/SettingsCoordinator.swift +++ b/AlphaWallet/Settings/Coordinators/SettingsCoordinator.swift @@ -310,10 +310,27 @@ extension SettingsCoordinator: AdvancedSettingsViewControllerDelegate { controller.delegate = self navigationController.pushViewController(controller, animated: true) } + + func advancedSettingsViewControllerPingInfuraSelected(in controller: AdvancedSettingsViewController) { + let coordinator = PingInfuraCoordinator(inViewController: rootViewController, analyticsCoordinator: analyticsCoordinator) + coordinator.delegate = self + coordinator.start() + addCoordinator(coordinator) + } } extension SettingsCoordinator: ChooseSendPrivateTransactionsProviderViewControllerDelegate { func privateTransactionProviderSelected(provider: SendPrivateTransactionsProvider?, inController viewController: ChooseSendPrivateTransactionsProviderViewController) { advancedSettingsViewController?.configure() } -} \ No newline at end of file +} + +extension SettingsCoordinator: PingInfuraCoordinatorDelegate { + func didPing(in coordinator: PingInfuraCoordinator) { + removeCoordinator(self) + } + + func didCancel(in coordinator: PingInfuraCoordinator) { + removeCoordinator(self) + } +} diff --git a/AlphaWallet/Settings/ViewControllers/AdvancedSettingsViewController.swift b/AlphaWallet/Settings/ViewControllers/AdvancedSettingsViewController.swift index 39e24f4f8..249a17f2a 100644 --- a/AlphaWallet/Settings/ViewControllers/AdvancedSettingsViewController.swift +++ b/AlphaWallet/Settings/ViewControllers/AdvancedSettingsViewController.swift @@ -15,6 +15,7 @@ protocol AdvancedSettingsViewControllerDelegate: AnyObject { func advancedSettingsViewControllerChangeCurrencySelected(in controller: AdvancedSettingsViewController) func advancedSettingsViewControllerAnalyticsSelected(in controller: AdvancedSettingsViewController) func advancedSettingsViewControllerUsePrivateNetworkSelected(in controller: AdvancedSettingsViewController) + func advancedSettingsViewControllerPingInfuraSelected(in controller: AdvancedSettingsViewController) } class AdvancedSettingsViewController: UIViewController { @@ -78,10 +79,9 @@ extension AdvancedSettingsViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let row = viewModel.rows[indexPath.row] switch row { - case .analytics, .changeCurrency, .changeLanguage, .clearBrowserCache, .console, .tokenScript: + case .analytics, .changeCurrency, .changeLanguage, .clearBrowserCache, .console, .tokenScript, .pingInfura: let cell: SettingTableViewCell = tableView.dequeueReusableCell(for: indexPath) cell.configure(viewModel: .init(titleText: row.title, subTitleText: nil, icon: row.icon)) - return cell case .usePrivateNetwork: let cell: SettingTableViewCell = tableView.dequeueReusableCell(for: indexPath) @@ -132,6 +132,8 @@ extension AdvancedSettingsViewController: UITableViewDelegate { delegate?.advancedSettingsViewControllerAnalyticsSelected(in: self) case .usePrivateNetwork: delegate?.advancedSettingsViewControllerUsePrivateNetworkSelected(in: self) + case .pingInfura: + delegate?.advancedSettingsViewControllerPingInfuraSelected(in: self) } } } \ No newline at end of file diff --git a/AlphaWallet/Settings/ViewModels/AdvancedSettingsViewModel.swift b/AlphaWallet/Settings/ViewModels/AdvancedSettingsViewModel.swift index 1dbc3dd6a..f715f7d6b 100644 --- a/AlphaWallet/Settings/ViewModels/AdvancedSettingsViewModel.swift +++ b/AlphaWallet/Settings/ViewModels/AdvancedSettingsViewModel.swift @@ -12,9 +12,9 @@ struct AdvancedSettingsViewModel { var rows: [AdvancedSettingsRow] = { let privateNerworkRow: [AdvancedSettingsRow] = Features.isUsingPrivateNetwork ? [.usePrivateNetwork] : [] if Features.isLanguageSwitcherDisabled { - return [.console, .clearBrowserCache, .tokenScript] + privateNerworkRow + return [.console, .clearBrowserCache, .tokenScript, .pingInfura] + privateNerworkRow } else { - return [.console, .clearBrowserCache, .tokenScript, .changeLanguage] + privateNerworkRow + return [.console, .clearBrowserCache, .tokenScript, .changeLanguage, .pingInfura] + privateNerworkRow } }() @@ -31,6 +31,7 @@ enum AdvancedSettingsRow: CaseIterable { case changeCurrency case analytics case usePrivateNetwork + case pingInfura var title: String { switch self { @@ -48,6 +49,8 @@ enum AdvancedSettingsRow: CaseIterable { return R.string.localizable.settingsAnalitycsTitle() case .usePrivateNetwork: return R.string.localizable.settingsChooseSendPrivateTransactionsProviderButtonTitle() + case .pingInfura: + return R.string.localizable.settingsPingInfuraTitle() } } @@ -67,6 +70,9 @@ enum AdvancedSettingsRow: CaseIterable { return R.image.settings_analytics()! case .usePrivateNetwork: return R.image.iconsSettingsEthermine()! + case .pingInfura: + //TODO need a more appropriate icon, maybe represent diagnostic or (to a lesser degree Infura) + return R.image.settings_analytics()! } } }