I am trying to re-write my app by adding Clean Architecture, and I have a problem. I get codes from my server in my every response. Codes can be 1,-1,32
, etc. Some codes are good to go, like they are success codes, and all negative codes are errors, and some codes like 32
indicate session has expired and I need to logout, while some codes indicate I need to navigate to specific screen.
I am not sure in which layer I should handle these codes.
I have following layers in my app.
ViewController -> ViewModel -> UseCase -> Repo -> NetworkManager
From NetworkManager, I get the decoded response model, that model contains code.
Should I handle it in Usecase or Repo? And how should I handle it?
I want to have a generic solution.
This is what I have tried so far.
class LoginUCImpl: LoginUC {
@Injected(\AppDIContainer.loginRepository) var loginRepo
@Injected(\AppDIContainer.responseCodeHandler) var responseCodeHandler
func getUser(username: String, password: String) async -> Result<Bool, Error> {
let res = await loginRepo.getUser(username: username, password: password)
switch res {
case .success(let data):
************** Should I handle it 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 -> Result<GetUser, Error> {
let request = LoginRequest.getUser(userName: username, password: password)
do {
let res: Result<GetUserDTO, Error> = try await requestManager.makeRequest(with: request)
switch res {
case .success(let data):
************** Should I handle it here??? **************
return .success(data.toDomain())
case .failure(let failure):
return .failure(failure)
}
} catch let error {
return .failure(error)
}
}
}
I have made a class that handles different response codes.
protocol ResponseCodeHandler {
func handle(responseCode: String)
}
class ResponseCodeHandlerImpl: ResponseCodeHandler {
func handle(responseCode: String) {
switch responseCode {
case "0", "1":
print("Normal Response")
case "32":
print("Session Expire")
case "112":
print("Forgot password")
default:
print("invalid response code")
}
}
}
But I don’t know how to implement it here in. I am also confused about how will I handle navigation here.
Any help or guidance is much appreciated. Thanks