diff --git a/Riot/Assets/de.lproj/Bwi.strings b/Riot/Assets/de.lproj/Bwi.strings index f3d270b7f..0d591a9b8 100644 --- a/Riot/Assets/de.lproj/Bwi.strings +++ b/Riot/Assets/de.lproj/Bwi.strings @@ -59,6 +59,9 @@ "bwi_settings_developer_well_known" = "Well-Known"; "bwi_settings_developer_capabilities" = "Capabilities"; "bwi_settings_developer_maintenance" = "Maintenance"; +"bwi_settings_developer_maintenance_reload" = "Aktualisieren"; +"bwi_settings_developer_maintenance_server_data" = "Daten vom Server"; +"bwi_settings_developer_maintenance_local_data" = "UserDefaults / lokale Daten"; "bwi_settings_developer_user_account_data" = "Account Data"; "bwi_settings_developer_user_account_data_type" = "Type"; "bwi_settings_developer_key_backup" = "Key Backup"; diff --git a/Riot/Assets/en.lproj/Bwi.strings b/Riot/Assets/en.lproj/Bwi.strings index 6d7d7318e..610bd56f7 100644 --- a/Riot/Assets/en.lproj/Bwi.strings +++ b/Riot/Assets/en.lproj/Bwi.strings @@ -60,7 +60,10 @@ "bwi_settings_developer_well_known" = "Well-Known"; "bwi_settings_developer_capabilities" = "Capabilities"; "bwi_settings_developer_maintenance" = "Maintenance"; +"bwi_settings_developer_maintenance_reload" = "Reload"; "bwi_settings_developer_user_account_data" = "Account Data"; +"bwi_settings_developer_maintenance_server_data" = "Data from Server"; +"bwi_settings_developer_maintenance_local_data" = "UserDefaults / local data"; "bwi_settings_developer_user_account_data_type" = "Type"; "bwi_settings_developer_key_backup" = "Key Backup"; "bwi_settings_developer_key_backup_algorithm" = "Algorithm"; diff --git a/Riot/Generated/BWIStrings.swift b/Riot/Generated/BWIStrings.swift index dc8bac845..a9edfa35f 100644 --- a/Riot/Generated/BWIStrings.swift +++ b/Riot/Generated/BWIStrings.swift @@ -599,6 +599,18 @@ public class BWIL10n: NSObject { public static var bwiSettingsDeveloperMaintenance: String { return BWIL10n.tr("Bwi", "bwi_settings_developer_maintenance") } + /// UserDefaults / lokale Daten + public static var bwiSettingsDeveloperMaintenanceLocalData: String { + return BWIL10n.tr("Bwi", "bwi_settings_developer_maintenance_local_data") + } + /// Aktualisieren + public static var bwiSettingsDeveloperMaintenanceReload: String { + return BWIL10n.tr("Bwi", "bwi_settings_developer_maintenance_reload") + } + /// Daten vom Server + public static var bwiSettingsDeveloperMaintenanceServerData: String { + return BWIL10n.tr("Bwi", "bwi_settings_developer_maintenance_server_data") + } /// Der alte Notizen-Raum Raum wurde durch einen neuen ersetzt. public static var bwiSettingsDeveloperNewPersonalNotesRoomCreated: String { return BWIL10n.tr("Bwi", "bwi_settings_developer_new_personal_notes_room_created") diff --git a/Riot/Modules/Application/LegacyAppDelegate.m b/Riot/Modules/Application/LegacyAppDelegate.m index 2b620b52d..04822f851 100644 --- a/Riot/Modules/Application/LegacyAppDelegate.m +++ b/Riot/Modules/Application/LegacyAppDelegate.m @@ -2281,8 +2281,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni // bwi reset outdated Alert for next login if (BWIBuildSettings.shared.bwiCheckAppVersion) { - ValidAppVersionsDefaultService *service = [[ValidAppVersionsDefaultService alloc] init]; - [service setOutdatedAlertShown:false]; + [ValidAppVersionsDefaultService.shared setOutdatedAlertShown:false]; } } diff --git a/Riot/Modules/Common/Recents/RecentsViewController.m b/Riot/Modules/Common/Recents/RecentsViewController.m index bbce46cd7..fedbd2a97 100644 --- a/Riot/Modules/Common/Recents/RecentsViewController.m +++ b/Riot/Modules/Common/Recents/RecentsViewController.m @@ -2475,8 +2475,7 @@ NSString *const RecentsViewControllerDataReadyNotification = @"RecentsViewContro #pragma mark - Bwi Check App version - (void)checkAppVersionDeprecated { - ValidAppVersionsDefaultService *appVersionsService = [[ValidAppVersionsDefaultService alloc] init]; - if( BWIBuildSettings.shared.bwiCheckAppVersion && appVersionsService.isCurrentAppVersionDeprecated ) { + if( BWIBuildSettings.shared.bwiCheckAppVersion && ValidAppVersionsDefaultService.shared.isCurrentAppVersionDeprecated ) { MXWeakify(self); UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:[BWIL10n bwiOutdatedVersionWarningTitle] @@ -2498,8 +2497,7 @@ NSString *const RecentsViewControllerDataReadyNotification = @"RecentsViewContro } - (void)checkAppVersionOutdated { - ValidAppVersionsDefaultService *appVersionsService = [[ValidAppVersionsDefaultService alloc] init]; - if (BWIBuildSettings.shared.bwiCheckAppVersion && appVersionsService.isCurrentAppVersionOutdated && !appVersionsService.wasOutdatedAlertShown) { + if (BWIBuildSettings.shared.bwiCheckAppVersion && ValidAppVersionsDefaultService.shared.isCurrentAppVersionOutdated && !ValidAppVersionsDefaultService.shared.wasOutdatedAlertShown) { MXWeakify(self); UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:[BWIL10n bwiOutdatedVersionWarningTitle] message:[BWIL10n bwiOutdatedVersionWarningMessage:AppInfo.current.displayName] @@ -2510,7 +2508,7 @@ NSString *const RecentsViewControllerDataReadyNotification = @"RecentsViewContro handler:^(UIAlertAction * action) { [self gotoAppStore]; - [appVersionsService setOutdatedAlertShown:true]; + [ValidAppVersionsDefaultService.shared setOutdatedAlertShown:true]; MXStrongifyAndReturnIfNil(self); self->currentAlert = nil; @@ -2519,15 +2517,15 @@ NSString *const RecentsViewControllerDataReadyNotification = @"RecentsViewContro [errorAlert addAction:[UIAlertAction actionWithTitle:[BWIL10n bwiOutdatedVersionLogoutButton] style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { - [appVersionsService setOutdatedAlertShown:true]; + [ValidAppVersionsDefaultService.shared setOutdatedAlertShown:true]; }]]; [self presentViewController:errorAlert animated:YES completion:nil]; } // bwi #5276: if outdated was shown but our app version is valid that means we have updated and have to reset outdated shown - if (BWIBuildSettings.shared.bwiCheckAppVersion && !appVersionsService.isCurrentAppVersionOutdated && appVersionsService.wasOutdatedAlertShown) { - [appVersionsService setOutdatedAlertShown:false]; + if (BWIBuildSettings.shared.bwiCheckAppVersion && !ValidAppVersionsDefaultService.shared.isCurrentAppVersionOutdated && ValidAppVersionsDefaultService.shared.wasOutdatedAlertShown) { + [ValidAppVersionsDefaultService.shared setOutdatedAlertShown:false]; } } diff --git a/Riot/Modules/Home/AllChats/AllChatsCoordinator.swift b/Riot/Modules/Home/AllChats/AllChatsCoordinator.swift index 2ff21d755..abb9e2a9f 100644 --- a/Riot/Modules/Home/AllChats/AllChatsCoordinator.swift +++ b/Riot/Modules/Home/AllChats/AllChatsCoordinator.swift @@ -395,10 +395,9 @@ class AllChatsCoordinator: NSObject, SplitViewMasterCoordinatorProtocol { button.autoresizingMask = [.flexibleHeight, .flexibleWidth] button.accessibilityLabel = VectorL10n.allChatsUserMenuAccessibilityLabel - let serverDownTime = ServerDowntimeDefaultService() - if serverDownTime.isDowntimePresentable() { - button = ServerDowntimeBadge().applyBadgeToButton(button: button, color: serverDownTime.downtimeColor()) - } else if BWIBuildSettings.shared.bwiCheckAppVersion && ValidAppVersionsDefaultService().isCurrentAppVersionOutdated() { + if ServerDowntimeDefaultService.shared.isDowntimePresentable() { + button = ServerDowntimeBadge().applyBadgeToButton(button: button, color: ServerDowntimeDefaultService.shared.downtimeColor()) + } else if BWIBuildSettings.shared.bwiCheckAppVersion && ValidAppVersionsDefaultService.shared.isCurrentAppVersionOutdated() { button = ServerDowntimeBadge().applyBadgeToButton(button: button, color: .yellow) } @@ -722,13 +721,12 @@ class AllChatsCoordinator: NSObject, SplitViewMasterCoordinatorProtocol { // bwi: check if there are changes in maintenance status @objc private func checkMaintenanceStatus() { - let serverDownTime = ServerDowntimeDefaultService() // bwi: bwi specific if BWIBuildSettings.shared.useTestDataForDowntime { - serverDownTime.fetchDowntimes(completion: { + ServerDowntimeDefaultService.shared.fetchDowntimes(completion: { self.createLeftButtonItem(for: self.allChatsViewController) - if serverDownTime.isBlocking() && serverDownTime.isDowntimeNow() && !serverDownTime.isManuallyIgnored() { + if ServerDowntimeDefaultService.shared.isBlocking() && ServerDowntimeDefaultService.shared.isDowntimeNow() && !ServerDowntimeDefaultService.shared.isManuallyIgnored() { UserDefaults.standard.set(true, forKey: "ServerDownTimeBlockingKey") } else { UserDefaults.standard.set(false, forKey: "ServerDownTimeBlockingKey") @@ -739,10 +737,10 @@ class AllChatsCoordinator: NSObject, SplitViewMasterCoordinatorProtocol { } }) } else { - serverDownTime.fetchDowntimesWithDirectRequest { success in + ServerDowntimeDefaultService.shared.fetchDowntimesWithDirectRequest(completion: { success, _, _, _ in self.createLeftButtonItem(for: self.allChatsViewController) - - if serverDownTime.isBlocking() && serverDownTime.isDowntimeNow() && !serverDownTime.isManuallyIgnored() { + + if ServerDowntimeDefaultService.shared.isBlocking() && ServerDowntimeDefaultService.shared.isDowntimeNow() && !ServerDowntimeDefaultService.shared.isManuallyIgnored() { UserDefaults.standard.set(true, forKey: "ServerDownTimeBlockingKey") } else { UserDefaults.standard.set(false, forKey: "ServerDownTimeBlockingKey") @@ -752,7 +750,7 @@ class AllChatsCoordinator: NSObject, SplitViewMasterCoordinatorProtocol { self.allChatsViewController.checkAppVersionDeprecated() self.allChatsViewController.checkAppVersionOutdated() } - } + }) } } diff --git a/Riot/Modules/Settings/SettingsViewController.m b/Riot/Modules/Settings/SettingsViewController.m index d6ea336d3..52261199b 100644 --- a/Riot/Modules/Settings/SettingsViewController.m +++ b/Riot/Modules/Settings/SettingsViewController.m @@ -437,7 +437,7 @@ SSOAuthenticationPresenterDelegate> [tmpSections addObject:sectionDowntime]; } - if (BWIBuildSettings.shared.bwiCheckAppVersion && [[[ValidAppVersionsDefaultService alloc] init] isCurrentAppVersionOutdated]) { + if (BWIBuildSettings.shared.bwiCheckAppVersion && [ValidAppVersionsDefaultService.shared isCurrentAppVersionOutdated]) { Section *sectionOutdated = [Section sectionWithTag:SECTION_TAG_OUTDATED_WARNING]; [sectionOutdated addRowWithTag:OUTDATED_WARNING]; sectionOutdated.headerTitle = [BWIL10n bwiOutdatedSettingsHeader]; diff --git a/Riot/Modules/TabBar/TabBarCoordinator.swift b/Riot/Modules/TabBar/TabBarCoordinator.swift index 8d0c36ba8..325f6812a 100644 --- a/Riot/Modules/TabBar/TabBarCoordinator.swift +++ b/Riot/Modules/TabBar/TabBarCoordinator.swift @@ -199,15 +199,14 @@ final class TabBarCoordinator: NSObject, SplitViewMasterCoordinatorProtocol { } func pinUnlocked() { - let serverDownTime = ServerDowntimeDefaultService() if BWIBuildSettings.shared.useTestDataForDowntime { - serverDownTime.fetchDowntimes { + ServerDowntimeDefaultService.shared.fetchDowntimes { self.createSettingsButton(self.masterTabBarController) } } else { if let session = self.currentMatrixSession { - serverDownTime.fetchDowntimes(session: session, completion: { + ServerDowntimeDefaultService.shared.fetchDowntimes(session: session, completion: { self.createSettingsButton(self.masterTabBarController) }) } @@ -779,10 +778,9 @@ final class TabBarCoordinator: NSObject, SplitViewMasterCoordinatorProtocol { private func createSettingsButton(_ controller: MasterTabBarController) { var settingsBarButtonItem: UIBarButtonItem - let serverDownTime = ServerDowntimeDefaultService() - if serverDownTime.isDowntimePresentable() { - settingsBarButtonItem = ServerDowntimeBadge().createBadgeBarButton(color: serverDownTime.downtimeColor(), target: self, selector: #selector(showSettings)) - } else if BWIBuildSettings.shared.bwiCheckAppVersion && ValidAppVersionsDefaultService().isCurrentAppVersionOutdated() { + if ServerDowntimeDefaultService.shared.isDowntimePresentable() { + settingsBarButtonItem = ServerDowntimeBadge().createBadgeBarButton(color: ServerDowntimeDefaultService.shared.downtimeColor(), target: self, selector: #selector(showSettings)) + } else if BWIBuildSettings.shared.bwiCheckAppVersion && ValidAppVersionsDefaultService.shared.isCurrentAppVersionOutdated() { settingsBarButtonItem = ServerDowntimeBadge().createBadgeBarButton(color: .yellow, target: self, selector: #selector(showSettings)) } else { settingsBarButtonItem = MXKBarButtonItem(image: Asset.Images.settingsIcon.image, style: .plain) { [weak self] in diff --git a/RiotSwiftUI/Modules/Authentication/Login/Coordinator/AuthenticationLoginCoordinator.swift b/RiotSwiftUI/Modules/Authentication/Login/Coordinator/AuthenticationLoginCoordinator.swift index 764a42c21..acfa75344 100644 --- a/RiotSwiftUI/Modules/Authentication/Login/Coordinator/AuthenticationLoginCoordinator.swift +++ b/RiotSwiftUI/Modules/Authentication/Login/Coordinator/AuthenticationLoginCoordinator.swift @@ -130,7 +130,7 @@ final class AuthenticationLoginCoordinator: Coordinator, Presentable { self.showForgotPasswordScreen() } case .login(let username, let password): - if BWIBuildSettings.shared.bwiCheckAppVersion && ValidAppVersionsDefaultService().isCurrentAppVersionDeprecated() { + if BWIBuildSettings.shared.bwiCheckAppVersion && ValidAppVersionsDefaultService.shared.isCurrentAppVersionDeprecated() { self.authenticationLoginViewModel.displayError(.appVersion) } else { self.login(username: username, password: password) diff --git a/RiotSwiftUI/Modules/Authentication/ServerSelection/View/AuthenticationServerSelectionScreen.swift b/RiotSwiftUI/Modules/Authentication/ServerSelection/View/AuthenticationServerSelectionScreen.swift index 250377052..e1cc77e7d 100644 --- a/RiotSwiftUI/Modules/Authentication/ServerSelection/View/AuthenticationServerSelectionScreen.swift +++ b/RiotSwiftUI/Modules/Authentication/ServerSelection/View/AuthenticationServerSelectionScreen.swift @@ -24,7 +24,6 @@ struct AuthenticationServerSelectionScreen: View { @Environment(\.theme) private var theme - private let service = ServerDowntimeDefaultService() @State private var showAlertForMissingCameraAuthorization = false @State private var showAlertForInvalidServer = false @@ -98,7 +97,7 @@ struct AuthenticationServerSelectionScreen: View { if isInvalidServerAlert { return self.invalidServerAlert() } else { - return service.alert(alertType: activeAlert) { + return ServerDowntimeDefaultService.shared.alert(alertType: activeAlert) { self.submit() } } @@ -281,12 +280,12 @@ struct AuthenticationServerSelectionScreen: View { isFetchingDowntime = true // show progresview if BWIBuildSettings.shared.useTestDataForDowntime { - service.fetchDowntimes { + ServerDowntimeDefaultService.shared.fetchDowntimes { self.isFetchingDowntime = false // hide progressview self.showAlertIfNeeded() } } else { - service.fetchDowntimesWithDirectRequest(localUrlString:viewModel.homeserverAddress) { success in + ServerDowntimeDefaultService.shared.fetchDowntimesWithDirectRequest(localUrlString:viewModel.homeserverAddress) { success, _, _, _ in DispatchQueue.main.async { self.isFetchingDowntime = false // hide progressview if success { @@ -304,8 +303,8 @@ struct AuthenticationServerSelectionScreen: View { } private func showAlertIfNeeded() { - if service.showAlert() { - activeAlert = service.alertType() + if ServerDowntimeDefaultService.shared.showAlert() { + activeAlert = ServerDowntimeDefaultService.shared.alertType() showAlert = true } else { self.submit() diff --git a/bwi/DeveloperSettings/MaintenanceView.swift b/bwi/DeveloperSettings/MaintenanceView.swift index 236110943..09bf6d2fd 100644 --- a/bwi/DeveloperSettings/MaintenanceView.swift +++ b/bwi/DeveloperSettings/MaintenanceView.swift @@ -18,52 +18,148 @@ import SwiftUI struct MaintenanceView: View { - @State var jsonString = "" + @State var jsonStringFromServer = "" + @State var jsonStringFromUserDefaults = "" + @State var isFetching: Bool = false var session: MXSession? var body: some View { ScrollView { VStack(alignment: .leading) { - HStack { - Text(jsonString) - .font(.caption) - .padding() - Spacer() - } + ServerDataView + + Divider() + + UserDefaultDataView } } .navigationTitle(BWIL10n.bwiSettingsDeveloperMaintenance) .navigationBarTitleDisplayMode(.inline) .onAppear { Task { - await fetchData() + await fetchDataFromServer() + } + fetchDataFromUserDefaults() + } + } + + + private var ServerDataView: some View { + return VStack(alignment: .leading) { + Text(BWIL10n.bwiSettingsDeveloperMaintenanceServerData) + .font(.title3) + .padding() + + if isFetching { + HStack { + Spacer() + ProgressView() + .progressViewStyle(CircularProgressViewStyle()) + Spacer() + } + } + + HStack { + Text(jsonStringFromServer) + .font(.caption) + .padding() + Spacer() + } + + HStack { + Spacer() + Button { + Task { + await fetchDataFromServer() + } + } label: { + Text(BWIL10n.bwiSettingsDeveloperMaintenanceReload) + } + Spacer() } } } - private func fetchData() async { - guard let restClient = session?.matrixRestClient else { - return - } - - let responseDict = await withCheckedContinuation { continuation in - restClient.getDowntime { jsonResponse, error in - continuation.resume(returning: jsonResponse) + private var UserDefaultDataView: some View { + return VStack(alignment: .leading) { + Text(BWIL10n.bwiSettingsDeveloperMaintenanceLocalData) + .font(.title3) + .padding() + HStack { + Text(jsonStringFromUserDefaults) + .font(.caption) + .padding() + Spacer() + } + HStack { + Spacer() + Button { + fetchDataFromUserDefaults() + } label: { + Text(BWIL10n.bwiSettingsDeveloperMaintenanceReload) + } + Spacer() } } - await MainActor.run { - do { - if let responseDict = responseDict { - let data = try JSONSerialization.data(withJSONObject: responseDict as Any, options: [.prettyPrinted]) - jsonString = String(data: data, encoding: .utf8) ?? "" + } + + + // MARK: sync functions + private func fetchDataFromServer() async { + isFetching = true + + ServerDowntimeDefaultService.shared.fetchDowntimesWithDirectRequest(localUrlString: nil) { _, response, data, error in + isFetching = false + guard let response = response, response.statusCode == 200 else { + if let error = error { + jsonStringFromServer = error.localizedDescription } else { - jsonString = "" + jsonStringFromServer = "" + } + return + } + + do { + if let data = data, let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] { + let jsonData = try JSONSerialization.data(withJSONObject: jsonObject as Any, options: [.prettyPrinted]) + jsonStringFromServer = String(data: jsonData, encoding: .utf8) ?? "" + } else { + jsonStringFromServer = "" } } catch { - jsonString = "" + jsonStringFromServer = "" } } } + + private func fetchDataFromUserDefaults() { + jsonStringFromUserDefaults = "" + let encodedDowntimes = UserDefaults.standard.object(forKey: "BWI_ServerDowntimes") as? [Data] ?? [Data]() + let encodedValidAppVersions = UserDefaults.standard.data(forKey: "BWI_ValidAppVersions") ?? Data() + + var jsonString = "" + + let serverDowntimes = encodedDowntimes + .compactMap { try? JSONDecoder().decode(ServerDowntime.self, from: $0) } + + for serverDowntime in serverDowntimes { + do { + let data = try JSONEncoder().encode(serverDowntime) + let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] + let newJsonData = try JSONSerialization.data(withJSONObject: jsonObject as Any, options: [.prettyPrinted]) + jsonString.append(contentsOf: String(data: newJsonData, encoding: .utf8) ?? "") + jsonString.append(contentsOf: !jsonString.isEmpty ? ",\n" : "") + } catch {} + } + do { + let jsonObjectAppVersion = try JSONSerialization.jsonObject(with: encodedValidAppVersions, options: []) as? [String: Any] + let jsonDataAppVersion = try JSONSerialization.data(withJSONObject: jsonObjectAppVersion as Any, options: [.prettyPrinted]) + jsonString.append(contentsOf: String(data: jsonDataAppVersion, encoding: .utf8) ?? "") + } catch {} + + jsonStringFromUserDefaults = jsonString + + } } struct MaintenanceView_Previews: PreviewProvider { diff --git a/bwi/ServerMaintenance/ServerDowntimeDefaultService.swift b/bwi/ServerMaintenance/ServerDowntimeDefaultService.swift index 1874ec8f4..2f96012f6 100644 --- a/bwi/ServerMaintenance/ServerDowntimeDefaultService.swift +++ b/bwi/ServerMaintenance/ServerDowntimeDefaultService.swift @@ -28,6 +28,7 @@ fileprivate let exampleFile = "exampleDowntime" fileprivate let maintenanceURL = "/_matrix/cmaintenance" @objcMembers class ServerDowntimeDefaultService : NSObject { + static let shared = ServerDowntimeDefaultService() var isCurrentDowntime: ServerDowntimeStatus = .none static var manuallyIgnored: Bool = false @@ -186,16 +187,16 @@ extension ServerDowntimeDefaultService : ServerDowntimeService { func alert(alertType: ServerMaintenanceAlertType, completion: @escaping () -> Void) -> Alert { switch alertType { case .showInvalidAppVersionAlert: - return Alert(title: Text(BWIL10n.bwiOutdatedVersionWarningTitle), - message: Text(BWIL10n.bwiOutdatedVersionWarningMessage(AppInfo.current.displayName)), - dismissButton: .destructive(Text(BWIL10n.bwiOutdatedVersionAppstoreButton), action: { + return Alert( title: Text(BWIL10n.bwiOutdatedVersionWarningTitle), + message: Text(BWIL10n.bwiOutdatedVersionWarningMessage(AppInfo.current.displayName)), + dismissButton: .destructive(Text(BWIL10n.bwiOutdatedVersionAppstoreButton), action: { let iTunesLink = BWIBuildSettings.shared.itunesAppLink UIApplication.shared.vc_open(URL(string: iTunesLink)!, completionHandler: nil) })) case .showDowntimeTimeAlert: if BWIBuildSettings.shared.ignoreBlockingMaintenance && isBlocking() { return Alert( title: Text(""), - message: Text(ServerDowntimeDefaultService().downtimeText()), + message: Text(downtimeText()), primaryButton: .cancel(Text(BWIL10n.blockingDowntimeAlertIgnoreButton)) { UserDefaults.standard.set(false, forKey: "ServerDownTimeBlockingKey") self.setManuallyIgnored() @@ -205,8 +206,8 @@ extension ServerDowntimeDefaultService : ServerDowntimeService { ) } else { - return Alert(title: Text(BWIL10n.downtimeTitle), - message: Text(ServerDowntimeDefaultService().downtimeText() != "" ? BWIL10n.downtimeDefaultMessage + "\n\n" + ServerDowntimeDefaultService().downtimeText() : BWIL10n.downtimeDefaultMessage), + return Alert( title: Text(BWIL10n.downtimeTitle), + message: Text(downtimeText() != "" ? BWIL10n.downtimeDefaultMessage + "\n\n" + downtimeText() : BWIL10n.downtimeDefaultMessage), dismissButton: .destructive(Text(isBlocking() ? BWIL10n.blockingDowntimeAlertDismissButton : BWIL10n.downtimeAlertDismissButton)) { if self.isBlocking() { return @@ -218,7 +219,7 @@ extension ServerDowntimeDefaultService : ServerDowntimeService { case .showServerMaintenanceInfoMessageAlert: if BWIBuildSettings.shared.ignoreBlockingMaintenance && isBlocking() { return Alert( title: Text(""), - message: Text(ServerDowntimeDefaultService().downtimeText()), + message: Text(downtimeText()), primaryButton: .cancel(Text(BWIL10n.blockingDowntimeAlertIgnoreButton)) { completion() }, @@ -226,9 +227,9 @@ extension ServerDowntimeDefaultService : ServerDowntimeService { ) } else { - return Alert(title: Text(""), - message: Text(ServerDowntimeDefaultService().downtimeText()), - dismissButton: .destructive(Text(isBlocking() ? BWIL10n.blockingDowntimeAlertDismissButton : BWIL10n.downtimeAlertDismissButton)) { + return Alert( title: Text(""), + message: Text(downtimeText()), + dismissButton: .destructive(Text(isBlocking() ? BWIL10n.blockingDowntimeAlertDismissButton : BWIL10n.downtimeAlertDismissButton)) { if self.isBlocking() { return } else { @@ -237,9 +238,9 @@ extension ServerDowntimeDefaultService : ServerDowntimeService { }) } case .showServerMaintenanceDefaultAlert: - return Alert(title: Text(BWIL10n.downtimeTitle), - message: Text(BWIL10n.downtimeDefaultMessage), - dismissButton: .destructive(Text(BWIL10n.downtimeAlertDismissButton)) { + return Alert( title: Text(BWIL10n.downtimeTitle), + message: Text(BWIL10n.downtimeDefaultMessage), + dismissButton: .destructive(Text(BWIL10n.downtimeAlertDismissButton)) { completion() }) } @@ -274,7 +275,7 @@ extension ServerDowntimeDefaultService : ServerDowntimeService { try copyServerDowntimesToUserDefaults(downtimes: downtimes) } - func fetchDowntimesWithDirectRequest(localUrlString: String? = nil, completion: @escaping (Bool) -> Void) { + func fetchDowntimesWithDirectRequest(localUrlString: String? = nil, completion: @escaping (Bool,HTTPURLResponse?,Data?,Error?) -> Void) { var urlString = AppConfigService.shared.serverUrl() if let localUrlString = localUrlString { @@ -282,18 +283,18 @@ extension ServerDowntimeDefaultService : ServerDowntimeService { } guard let url = URL(string: urlString + maintenanceURL) else { - completion(false) + completion(false, nil, nil, nil) return } let config = URLSessionConfiguration.default config.httpAdditionalHeaders = ["User-Agent": UserAgentService().bwiUserAgent] + config.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData let session : URLSession = URLSession(configuration: config) - session.configuration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData let task = session.dataTask(with: url) { data, response, error in guard let response = response as? HTTPURLResponse, response.statusCode == 200 else { - completion(false) + completion(false, nil, data, error) return } @@ -305,7 +306,7 @@ extension ServerDowntimeDefaultService : ServerDowntimeService { print(error.localizedDescription) } - completion(true) + completion(true, response, data, error) } task.resume() } diff --git a/bwi/ServerMaintenance/ValidAppVersionsDefaultService.swift b/bwi/ServerMaintenance/ValidAppVersionsDefaultService.swift index 2449759de..72787e898 100644 --- a/bwi/ServerMaintenance/ValidAppVersionsDefaultService.swift +++ b/bwi/ServerMaintenance/ValidAppVersionsDefaultService.swift @@ -18,7 +18,8 @@ import Foundation @objcMembers class ValidAppVersionsDefaultService : NSObject { - + static let shared = ValidAppVersionsDefaultService() + var currentAppVersion: String? { return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String } diff --git a/bwi/SplashScreen/View/OnboardingBwiSplashScreen.swift b/bwi/SplashScreen/View/OnboardingBwiSplashScreen.swift index d9aa25ce4..767baa674 100644 --- a/bwi/SplashScreen/View/OnboardingBwiSplashScreen.swift +++ b/bwi/SplashScreen/View/OnboardingBwiSplashScreen.swift @@ -26,7 +26,6 @@ struct OnboardingBwiSplashScreen: View { @Environment(\.theme) private var theme @Environment(\.layoutDirection) private var layoutDirection - private let service = ServerDowntimeDefaultService() /// The dimensions of the stack with the action buttons and page indicator. @State private var overlayFrame: CGRect = .zero @@ -75,7 +74,7 @@ struct OnboardingBwiSplashScreen: View { .navigationTitle("") .navigationBarHidden(true) .alert(isPresented: $showAlert, content: { - service.alert(alertType: activeAlert) { + ServerDowntimeDefaultService.shared.alert(alertType: activeAlert) { viewModel.send(viewAction: .login) } }) @@ -86,12 +85,12 @@ struct OnboardingBwiSplashScreen: View { isFetchingDowntime = true // show progresview if BWIBuildSettings.shared.useTestDataForDowntime { - service.fetchDowntimes { + ServerDowntimeDefaultService.shared.fetchDowntimes { self.isFetchingDowntime = false // hide progressview self.showAlertIfNeeded() } } else { - service.fetchDowntimesWithDirectRequest { success in + ServerDowntimeDefaultService.shared.fetchDowntimesWithDirectRequest { success, _, _, _ in DispatchQueue.main.async { self.isFetchingDowntime = false // hide progressview if success { @@ -110,8 +109,8 @@ struct OnboardingBwiSplashScreen: View { private func showAlertIfNeeded() { - if service.showAlert() { - activeAlert = service.alertType() + if ServerDowntimeDefaultService.shared.showAlert() { + activeAlert = ServerDowntimeDefaultService.shared.alertType() showAlert = true } else { viewModel.send(viewAction: .login) diff --git a/bwi/WelcomeExperience/WelcomeExperienceView.swift b/bwi/WelcomeExperience/WelcomeExperienceView.swift index bdc7b088f..abfdcb189 100644 --- a/bwi/WelcomeExperience/WelcomeExperienceView.swift +++ b/bwi/WelcomeExperience/WelcomeExperienceView.swift @@ -36,7 +36,6 @@ fileprivate struct Page: View { @State var buttonCallback: (() -> Void)? - private let service = ServerDowntimeDefaultService() @State private var isFetchingDowntime = false @State private var showAlert = false @State private var activeAlert: ServerMaintenanceAlertType = .showInvalidAppVersionAlert @@ -71,7 +70,7 @@ fileprivate struct Page: View { } .padding(.bottom, 100) .alert(isPresented: $showAlert, content: { - service.alert(alertType: activeAlert) { + ServerDowntimeDefaultService.shared.alert(alertType: activeAlert) { buttonCallback() } }) @@ -92,12 +91,12 @@ fileprivate struct Page: View { isFetchingDowntime = true // show progresview if BWIBuildSettings.shared.useTestDataForDowntime { - service.fetchDowntimes { + ServerDowntimeDefaultService.shared.fetchDowntimes { self.isFetchingDowntime = false // hide progressview self.showAlertIfNeeded() } } else { - service.fetchDowntimesWithDirectRequest { success in + ServerDowntimeDefaultService.shared.fetchDowntimesWithDirectRequest { success, _, _, _ in DispatchQueue.main.async { self.isFetchingDowntime = false // hide progressview if success { @@ -118,8 +117,8 @@ fileprivate struct Page: View { return } - if service.showAlert() { - activeAlert = service.alertType() + if ServerDowntimeDefaultService.shared.showAlert() { + activeAlert = ServerDowntimeDefaultService.shared.alertType() showAlert = true } else { buttonCallback()