iOS Development

ios – Easy methods to deal with totally different codes in response Mannequin in Clear Structure Swift?

Spread the love


I’m making an attempt to re-write my app by including Clear Structure, and I’ve an issue. I get codes from my server in my each response. Codes might be 1,-1,32, and so on. Some codes are good to go, like they’re success codes, and all unfavorable codes are errors, and a few codes like 32 point out session has expired and I have to logout, whereas some codes point out I have to navigate to particular display screen.
I’m not positive by which layer I ought to deal with these codes.
I’ve following layers in my app.

ViewController -> ViewModel -> UseCase -> Repo -> NetworkManager

From NetworkManager, I get the decoded response mannequin, that mannequin comprises code.
Ought to I deal with it in Usecase or Repo? And the way ought to I deal with it?
I need to have a generic resolution.
That is what I’ve tried to this point.

class LoginUCImpl: LoginUC {

@Injected(AppDIContainer.loginRepository) var loginRepo
@Injected(AppDIContainer.responseCodeHandler) var responseCodeHandler

func getUser(username: String, password: String) async -> Outcome<Bool, Error> {
    let res = await loginRepo.getUser(username: username, password: password)
    change res {
    case .success(let knowledge):
         ************** Ought to I deal with it right here??? **************
        return .success(true)
    case .failure(let error):
        return .failure(error)
    }
  }
}

class LoginRepositoryImpl: LoginRepository {

@Injected(AppDIContainer.requestManager) var requestManager

func getUser(username: String, password: String) async -> Outcome<GetUser, Error> {
    let request = LoginRequest.getUser(userName: username, password: password)
    do {
        let res: Outcome<GetUserDTO, Error> = attempt await requestManager.makeRequest(with: request)
        change res {
        case .success(let knowledge):
         ************** Ought to I deal with it right here??? **************
            return .success(knowledge.toDomain())
        case .failure(let failure):
            return .failure(failure)
        }
    } catch let error {
        return .failure(error)
    }
  }
}

I’ve made a category that handles totally different response codes.

protocol ResponseCodeHandler {
  func deal with(responseCode: String)
}

class ResponseCodeHandlerImpl: ResponseCodeHandler {
func deal with(responseCode: String) {
    change responseCode {
    case "0", "1":
        print("Regular Response")
    case "32":
        print("Session Expire")
    case "112":
        print("Forgot password")
    default:
        print("invalid response code")
     }
   }
 }

However I do not know easy methods to implement it right here in. I’m additionally confused about how will I deal with navigation right here.
Any assist or steerage is way appreciated. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *