Using coroutines for the first time. Need help.
Here is my flow:
Presenter wants to login so calls Repository Interface. Repository implements RepositoryInterface. So Repository calls APIInterface. APIInterface is implemented by APIInterfaceImpl. The APIInterfaceImpl finally calls the MyRetrofitInterface.
Here is the flow diagrammatically:
Presenter -> Repository -> APIInterfaceImpl -> MyRetrofitInterface
Once I get login response:
APIInterfaceImpl -> Repository -> Stores the data in cache -> Gives http status code to Presenter
Here is my code:
RepositoryInterface.kt
fun onUserLogin(loginRequest: LoginRequest): LoginResponse
Repository.kt
class Repository : RepositoryInterface {
private var apiInterface: APIInterface? = null
override fun onUserLogin(loginRequest: LoginRequest): LoginResponse {
return apiInterface?.makeLoginCall(loginRequest)
}
}
APIInterface.kt
suspend fun makeLoginCall(loginRequest): LoginResponse?
APIInterfaceImpl.kt
override suspend fun makeLoginCall(loginRequest: LoginRequest): LoginResponse? {
if (isInternetPresent(context)) {
try {
val response = MyRetrofitInterface?.loginRequest(loginRequest)?.await()
return response
} catch (e: Exception) {
//How do i return a status code here
}
} else {
//How do i return no internet here
return Exception(Constants.NO_INTERNET)
}
}
MyRetrofitInterface.kt
@POST("login/....")
fun loginRequest(@Body loginRequest: LoginRequest): Deferred<LoginResponse>?
My questions are:
- Is my approach architecturally right?
- How do I pass http error codes or no internet connection in my code
- Any more nicer approach to my solution?