From 3eab18bc8d280621b4461b1189a92dbcb42ebb64 Mon Sep 17 00:00:00 2001 From: Ethan Pippin Date: Fri, 14 Jan 2022 12:22:13 -0700 Subject: [PATCH 1/3] add redirects --- Shared/Generated/Strings.swift | 2 ++ .../ViewModels/ConnectToServerViewModel.swift | 25 +++++++++++++++++- Translations/en.lproj/Localizable.strings | Bin 11440 -> 11526 bytes 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Shared/Generated/Strings.swift b/Shared/Generated/Strings.swift index e62b4404..ccaf4c22 100644 --- a/Shared/Generated/Strings.swift +++ b/Shared/Generated/Strings.swift @@ -354,6 +354,8 @@ internal enum L10n { internal static let system = L10n.tr("Localizable", "system") /// Tags internal static let tags = L10n.tr("Localizable", "tags") + /// Too Many Redirects + internal static let tooManyRedirects = L10n.tr("Localizable", "tooManyRedirects") /// Try again internal static let tryAgain = L10n.tr("Localizable", "tryAgain") /// TV Shows diff --git a/Shared/ViewModels/ConnectToServerViewModel.swift b/Shared/ViewModels/ConnectToServerViewModel.swift index aa86ed37..12a5fbea 100644 --- a/Shared/ViewModels/ConnectToServerViewModel.swift +++ b/Shared/ViewModels/ConnectToServerViewModel.swift @@ -44,7 +44,8 @@ final class ConnectToServerViewModel: ViewModel { return message } - func connectToServer(uri: String) { + func connectToServer(uri: String, redirectCount: Int = 0) { + #if targetEnvironment(simulator) var uri = uri if uri == "localhost" { @@ -63,6 +64,28 @@ final class ConnectToServerViewModel: ViewModel { case .finished: () case let .failure(error): switch error { + case is ErrorResponse: + let errorResponse = error as! ErrorResponse + switch errorResponse { + case let .error(_, _, response, _): + // a url in the response is the result if a redirect + if let newURL = response?.url { + if redirectCount > 2 { + self.handleAPIRequestError(displayMessage: L10n.tooManyRedirects, + logLevel: .critical, + tag: "connectToServer", + completion: completion) + } else { + self + .connectToServer(uri: newURL.absoluteString + .removeRegexMatches(pattern: "/web/index.html", replaceWith: ""), + redirectCount: redirectCount + 1) + } + } else { + self.handleAPIRequestError(completion: completion) + } + } + print(errorResponse) case is SwiftfinStore.Errors: let swiftfinError = error as! SwiftfinStore.Errors switch swiftfinError { diff --git a/Translations/en.lproj/Localizable.strings b/Translations/en.lproj/Localizable.strings index cc09b41e36cdfbc25e4b2280cd27dce5b40fde0f..a1457c89db41a0995365734b460f4acc8c2524ba 100644 GIT binary patch delta 94 zcmdlG*%q~7gHD(dLkUAZ5c)DCGUPE-G6XTCGNdqMG86&nWT0FzgA#)RgDns%F@%6s PD!|n$U{!6+z{LOn?n)9) delta 7 OcmZpR+7P*6gAM==Jp* Date: Fri, 14 Jan 2022 12:24:34 -0700 Subject: [PATCH 2/3] cleanup --- Shared/ViewModels/ConnectToServerViewModel.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Shared/ViewModels/ConnectToServerViewModel.swift b/Shared/ViewModels/ConnectToServerViewModel.swift index 12a5fbea..426b5a37 100644 --- a/Shared/ViewModels/ConnectToServerViewModel.swift +++ b/Shared/ViewModels/ConnectToServerViewModel.swift @@ -78,14 +78,13 @@ final class ConnectToServerViewModel: ViewModel { } else { self .connectToServer(uri: newURL.absoluteString - .removeRegexMatches(pattern: "/web/index.html", replaceWith: ""), + .removeRegexMatches(pattern: "/web/index.html"), redirectCount: redirectCount + 1) } } else { self.handleAPIRequestError(completion: completion) } } - print(errorResponse) case is SwiftfinStore.Errors: let swiftfinError = error as! SwiftfinStore.Errors switch swiftfinError { From 815a91457ece2ca7d88a1f5f2d6d8a1eb3709900 Mon Sep 17 00:00:00 2001 From: Ethan Pippin Date: Sat, 15 Jan 2022 22:26:15 -0700 Subject: [PATCH 3/3] Update Strings.swift --- Shared/Generated/Strings.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shared/Generated/Strings.swift b/Shared/Generated/Strings.swift index bf8d6f49..b6b5fb87 100644 --- a/Shared/Generated/Strings.swift +++ b/Shared/Generated/Strings.swift @@ -355,7 +355,7 @@ internal enum L10n { /// Tags internal static var tags: String { return L10n.tr("Localizable", "tags") } /// Too Many Redirects - internal static let tooManyRedirects: String { return L10n.tr("Localizable", "tooManyRedirects") } + internal static var tooManyRedirects: String { return L10n.tr("Localizable", "tooManyRedirects") } /// Try again internal static var tryAgain: String { return L10n.tr("Localizable", "tryAgain") } /// TV Shows