android10 / Android-CleanArchitecture

This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle multiple scenario from API call

chrizzz35 opened this issue · comments

Hi, I'm currently following this architecture for my apps, and wondering how to handle multiple scenario code response from API call.

Let's say I have something like this:
architecture

UserRepository contain the logic between API fetch or loading from SQLite cache, I can assure that UserLocalDataStore will return the Success Response. The problem is, my UserRemoteData has many scenario, ex:

  • Success Response
  • Expired Session
  • Invalid Token

Currently the UseCase, Repository, LocalDataStore & RemoteDataStore return Observable<UserResponse>, and I put my validation in my presenter, but feels so wrong because I need to validate it everytime I use GetUserListUseCase.

if(response.getCode().equals(API_SUCCESS)) {
    view.showUserList(response.getUserList());
}
else if(response.getCode().equals(EXPIRED_SESSION)) {
    view.moveToLoginScreen(response.getMessage());
}
else if(response.getCode().equals(TOKEN_INVALID)) {
    view.moveToLoginScreen(response.getMessage());
}
else {
    view.showFailedFetchUserList("Unexpected Error Occured");
}

*Note: all of my API fetch can return this response scenario
My Question is:

  1. Which layer is reponsible for validate this scenario?
  2. Do I really need to validate this scenario in other API Call for different Entity (Let say I have Product, Purchase entity, instead only User entity. Since all of my API have this scenario)?

Personally, I would encapsulate the logic of expired session and invalid token in the remote data store and every data store would emit users. If error happened then i would pass specific exception (let's call it SessionException) in observable's onError method.

Then I would have an observer inside the UserListPresenter which would have an implementation like that:

private final class UserObserver extends DisposableObserver {

    @Override
    public void onNext(User module) {
        //...
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof SessionException) {
            view.moveToLoginScreen(e.getMessage());
        } else {
            view.showFailedFetchUserList("Unexpected Error Occured");
        }
    }

    @Override
    public void onComplete() {
        //...
    }

}