mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-09 06:43:25 +01:00
- Show Toast regarding server switch due to fallback if UI context is available
This commit is contained in:
@@ -45,8 +45,10 @@ final class FetchAnisetteDataOperation: ResultOperation<ALTAnisetteData>, WebSoc
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Pass in proper view context to show the Toast messages
|
||||||
|
let viewContext = context.presentingViewController
|
||||||
|
|
||||||
getAnisetteServerUrl{ url, error in
|
getAnisetteServerUrl(viewContext){ url, error in
|
||||||
guard let urlString = url else {
|
guard let urlString = url else {
|
||||||
self.finish(.failure(error!))
|
self.finish(.failure(error!))
|
||||||
return
|
return
|
||||||
@@ -68,7 +70,7 @@ final class FetchAnisetteDataOperation: ResultOperation<ALTAnisetteData>, WebSoc
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func getAnisetteServerUrl(completion: @escaping (String?, Error?) -> Void) {
|
func getAnisetteServerUrl(_ viewContext: UIViewController?, completion: @escaping (String?, Error?) -> Void) {
|
||||||
var serverUrls = UserDefaults.standard.menuAnisetteServersList
|
var serverUrls = UserDefaults.standard.menuAnisetteServersList
|
||||||
let currentServer = UserDefaults.standard.menuAnisetteURL
|
let currentServer = UserDefaults.standard.menuAnisetteURL
|
||||||
|
|
||||||
@@ -78,10 +80,22 @@ final class FetchAnisetteDataOperation: ResultOperation<ALTAnisetteData>, WebSoc
|
|||||||
serverUrls.insert(currentServer, at: 0)
|
serverUrls.insert(currentServer, at: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
tryNextServer(from: serverUrls, currentIndex: 0, completion: completion)
|
tryNextServer(from: serverUrls, viewContext, currentIndex: 0, completion: completion)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func tryNextServer(from serverUrls: [String], currentIndex: Int, completion: @escaping (String?, Error?) -> Void) {
|
private func showToast(viewContext: UIViewController?, message: String){
|
||||||
|
if let viewContext = viewContext{
|
||||||
|
let error = OperationError.anisetteV1Error(message: message)
|
||||||
|
let toastView = ToastView(error: error)
|
||||||
|
// toastView.textLabel.textColor = .altPrimary
|
||||||
|
// toastView.detailTextLabel.textColor = .altPrimary
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
toastView.show(in: viewContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func tryNextServer(from serverUrls: [String], _ viewContext: UIViewController?,currentIndex: Int, completion: @escaping (String?, Error?) -> Void) {
|
||||||
// Check if all URLs have been exhausted
|
// Check if all URLs have been exhausted
|
||||||
guard currentIndex < serverUrls.count else {
|
guard currentIndex < serverUrls.count else {
|
||||||
let error = NSError(domain: "AnisetteError", code: 0, userInfo: [NSLocalizedDescriptionKey: "No valid server found."])
|
let error = NSError(domain: "AnisetteError", code: 0, userInfo: [NSLocalizedDescriptionKey: "No valid server found."])
|
||||||
@@ -92,8 +106,10 @@ final class FetchAnisetteDataOperation: ResultOperation<ALTAnisetteData>, WebSoc
|
|||||||
let currentServerUrlString = serverUrls[currentIndex]
|
let currentServerUrlString = serverUrls[currentIndex]
|
||||||
guard let url = URL(string: currentServerUrlString) else {
|
guard let url = URL(string: currentServerUrlString) else {
|
||||||
// Invalid URL, skip to next
|
// Invalid URL, skip to next
|
||||||
print("Skipping invalid URL: \(currentServerUrlString)")
|
let errmsg = "Skipping invalid URL: \(currentServerUrlString)"
|
||||||
tryNextServer(from: serverUrls, currentIndex: currentIndex + 1, completion: completion)
|
print(errmsg)
|
||||||
|
showToast(viewContext: viewContext, message: errmsg)
|
||||||
|
tryNextServer(from: serverUrls, viewContext, currentIndex: currentIndex + 1, completion: completion)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,12 +117,19 @@ final class FetchAnisetteDataOperation: ResultOperation<ALTAnisetteData>, WebSoc
|
|||||||
pingServer(url) { success, error in
|
pingServer(url) { success, error in
|
||||||
if success {
|
if success {
|
||||||
// If the server is reachable, return the URL
|
// If the server is reachable, return the URL
|
||||||
print("Found working server: \(url.absoluteString)")
|
let okmsg = "Found working server: \(url.absoluteString)"
|
||||||
|
print(okmsg)
|
||||||
|
if(currentIndex > 0){
|
||||||
|
// notify user if available server is different the user-specified one
|
||||||
|
self.showToast(viewContext: viewContext, message: okmsg)
|
||||||
|
}
|
||||||
completion(url.absoluteString, nil)
|
completion(url.absoluteString, nil)
|
||||||
} else {
|
} else {
|
||||||
// If not, try the next URL
|
// If not, try the next URL
|
||||||
print("Failed to reach server: \(url.absoluteString), trying next server.")
|
let errmsg = "Failed to reach server: \(url.absoluteString), trying next server."
|
||||||
self.tryNextServer(from: serverUrls, currentIndex: currentIndex + 1, completion: completion)
|
print(errmsg)
|
||||||
|
self.showToast(viewContext: viewContext, message: errmsg)
|
||||||
|
self.tryNextServer(from: serverUrls, viewContext, currentIndex: currentIndex + 1, completion: completion)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user