diff --git a/FinBoxLending/LendingView.swift b/FinBoxLending/LendingView.swift index 852004d..e0580bc 100644 --- a/FinBoxLending/LendingView.swift +++ b/FinBoxLending/LendingView.swift @@ -14,6 +14,8 @@ public struct LendingView: View { // Result Function public let lendingResult : ((FinBoxJourneyResult) -> Void) + @Environment(\.dismiss) private var dismiss + public init(lendingResult: @escaping (FinBoxJourneyResult) -> Void) { self.lendingResult = lendingResult } @@ -45,7 +47,8 @@ public struct LendingView: View { func handleError(error: String) -> some View { lendingResult(FinBoxJourneyResult(code: "", screen: "", message: error)) - return Text("\(String(describing: error))") + dismiss() + return EmptyView() } } diff --git a/FinBoxLending/network/APIService.swift b/FinBoxLending/network/APIService.swift index 692bd0d..adaf241 100644 --- a/FinBoxLending/network/APIService.swift +++ b/FinBoxLending/network/APIService.swift @@ -10,6 +10,7 @@ import Foundation struct APIService { static let shared = APIService() + private static var callbackSent = CallbackSent() /// Constructs the URL for the session. /// - Returns: The URL for the session, formed by combining the `BASE_URL` and `END_POINT` @@ -49,7 +50,7 @@ struct APIService { let task = URLSession.shared.dataTask(with: requestParams) { data, response, error in if let error = error { - self.handleError(completion: completion, error: error) + self.handleError(completion: completion, error: error.localizedDescription) return } @@ -71,7 +72,7 @@ struct APIService { // Convert the response to object sessionResponse = try JSONDecoder().decode(SessionResponse.self, from: data) } catch { - self.handleClientError(completion: completion, error: error) + self.handleError(completion: completion, error: "JSON Error") } // Handle the HTTP response @@ -142,25 +143,36 @@ struct APIService { hidePoweredBy: userPref.hidePoweredBy, sdkType: "hybrid:ios:0.0.1") } + /// Creates SessionResult for Callback + /// - Parameters + /// - error (Optional) : String error + /// - sessionURL (Optional): String URL + /// - Returns + /// - SessionResult object with only error or sessionURL + func getResult(error: String?, sessionURL: String?) -> SessionResult { + if (error != nil && sessionURL == nil) { + return SessionResult(error: error, sessionURL: nil) + } else { + return SessionResult(error: nil, sessionURL: sessionURL) + } + } + /// Handles client errors func handleClientError(completion: @escaping (SessionResult) -> Void, error: Any) { debugPrint("Response Error Client: \(error as Any)") - let result = SessionResult(error: String(describing: error), sessionURL: nil) - sendCallback(completion: completion, result: result) + sendCallback(completion: completion, result: getResult(error: String(describing: error), sessionURL: nil)) } /// Handles server errors func handleServerError(completion: @escaping (SessionResult) -> Void, error: Any) { debugPrint("Response Error Server: \(String(describing: error))") - let result = SessionResult(error: String(describing: error), sessionURL: nil) - sendCallback(completion: completion, result: result) + sendCallback(completion: completion, result: getResult(error: String(describing: error), sessionURL: nil)) } /// Handles generic errors func handleError(completion: @escaping (SessionResult) -> Void, error: Any) { debugPrint("Response Error Generic: \(String(describing: error))") - let result = SessionResult(error: String(describing: error), sessionURL: nil) - sendCallback(completion: completion, result: result) + sendCallback(completion: completion, result: getResult(error: String(describing: error), sessionURL: nil)) } /// Sends an asynchronous callback after a (slight-possible) delay. @@ -168,6 +180,15 @@ struct APIService { /// - completion: A closure to be executed when the asynchronous operation completes. It takes a `String` parameter. /// - result: The result to be passed to the completion closure. func sendCallback(completion: @escaping (SessionResult) -> Void, result: SessionResult) { + // Check if the callback has already been sent + guard !APIService.callbackSent.status else { + return + } + + // Update the sent status + APIService.callbackSent.updateStatus() + + // Send callback DispatchQueue.global().asyncAfter(deadline: .now()) { completion(result) } diff --git a/FinBoxLending/network/CallbackSent.swift b/FinBoxLending/network/CallbackSent.swift new file mode 100644 index 0000000..81534e2 --- /dev/null +++ b/FinBoxLending/network/CallbackSent.swift @@ -0,0 +1,20 @@ +// +// CallbackSent.swift +// FinBoxLending +// +// Created by Ashutosh Jena on 13/02/24. +// + +import Foundation + +/// Struct to hold callback sent status +struct CallbackSent { + + /// Flag to hold status + var status = false + + /// Function which updates the sent status + mutating func updateStatus() { + status = true + } +}