Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use result type in upgradalert.swift #5

Open
eonist opened this issue Sep 19, 2023 · 0 comments
Open

Use result type in upgradalert.swift #5

eonist opened this issue Sep 19, 2023 · 0 comments

Comments

@eonist
Copy link
Member

eonist commented Sep 19, 2023

The UpgradeAlert.swift file you've selected is well-structured and follows good Swift programming practices. However, there are a few areas where it could be improved:

  1. Documentation: There are several Fixme: ⚠️️ Add doc comments in the code, indicating that the documentation for some methods and parameters is incomplete. Completing this documentation would make the code easier to understand and maintain.

  2. Error Handling: The code uses a combination of NSError and a custom UAError type. It would be more idiomatic in Swift to use a single custom Error type for all errors that can occur in this module.

  3. Code Organization: The code could be better organized by separating different functionalities into different files. For example, network-related functions could be moved to a separate file.

  4. Use of Result Type: The code comments suggest using the Result type in some places. This is a good idea, as the Result type can make error handling code clearer and more concise.

  5. Code Cleanup: There are several comments about cleaning up the code. It would be beneficial to go through these and address them where possible.

Here's an example of how you might improve the getAppInfo function by using a Result type and a custom Error type:

enum UpgradeAlertError: Error {
    case invalidURL
    case dataUnavailable(String)
    case noAppInfo
    case invalidResponse(String)
}

private static func getAppInfo(completion: @escaping (Result<AppInfo, UpgradeAlertError>) -> Void) {
    guard let url: URL = requestURL else { 
        completion(.failure(.invalidURL))
        return 
    }
    let task = URLSession.shared.dataTask(with: url) { data, _, error in
        if let error = error {
            completion(.failure(.dataUnavailable(error.localizedDescription)))
            return
        }
        guard let data = data else {
            completion(.failure(.dataUnavailable("No data")))
            return
        }
        do {
            let result = try JSONDecoder().decode(LookupResult.self, from: data)
            guard let info: AppInfo = result.results.first else {
                completion(.failure(.noAppInfo))
                return
            }
            completion(.success(info))
        } catch {
            completion(.failure(.invalidResponse(error.localizedDescription)))
        }
    }
    task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant