Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion FinBoxLending/LendingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}

}
37 changes: 29 additions & 8 deletions FinBoxLending/network/APIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -142,32 +143,52 @@ 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.
/// - Parameters:
/// - 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)
}
Expand Down
20 changes: 20 additions & 0 deletions FinBoxLending/network/CallbackSent.swift
Original file line number Diff line number Diff line change
@@ -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
}
}