Skip to content

Use result type in upgradalert.swift #5

Description

@eonist

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()
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions