Created
January 2, 2019 06:13
-
-
Save vishalhalani/c0bd6e6b8fe9b467753659593c11185c to your computer and use it in GitHub Desktop.
controller class to make web call to login and store neccessary data to preferences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginController { | |
ApiClient apiService = null; | |
OnWebAPIResponseListener mRepsonseListener; | |
Context context; | |
public LoginController(OnWebAPIResponseListener mRepsonseListener, Context context) { | |
this.context = context; | |
this.mRepsonseListener = mRepsonseListener; | |
apiService = AppController.getAppInstance().getApiClient(); | |
} | |
/** | |
* This method handle login request | |
* | |
* @param requestCode to identify request | |
*/ | |
public void loginUser(final int requestCode, String email, String password) { | |
BaseModel<LoginModel> baseModel = new BaseModel<>(ApiConst.USER_LOGIN, new LoginModel(email, password, PrefManager.getAccessToken(context), PrefManager.getDeviceId(context), ApiConst.DEVICE_TYPE)); | |
Call<BaseResponse<UserModel>> call = apiService.loginUser(baseModel); | |
ApiCallManager.enqueue(ApiConst.USER_LOGIN, call, new CallbackAdapter<UserModel>() { | |
@Override | |
public void onApiResponse(ErrorResponse err, BaseResponse baseResponse) { | |
if (err != null) { | |
mRepsonseListener.onCallError(err, requestCode); | |
} | |
if (baseResponse != null && baseResponse.getData() != null) { | |
storeInPreference(baseResponse, requestCode, password); | |
} | |
} | |
}); | |
} | |
// store user data in shared preference | |
private void storeInPreference(BaseResponse baseResponse, int requestCode, String password) { | |
if (baseResponse.hasStatus()) { | |
if (baseResponse.getData() instanceof UserModel) { | |
UserModel userModel = (UserModel) baseResponse.getData(); | |
if (userModel != null) { | |
PrefManager.setUserVerified(context, true); | |
PrefManager.setUserLoggedIn(context, true); | |
PrefManager.setUserName(context, userModel.getFirst_name()); | |
PrefManager.setAuthToken(context, userModel.getApi_auth_token()); | |
PrefManager.setUserId(context, userModel.getUser_id()); | |
PrefManager.setUserEmail(context, userModel.getEmail()); | |
PrefManager.setUserImage(context, userModel.getProfile_pic_url()); | |
PrefManager.setIsFirstTime(context, false); | |
// set call back | |
mRepsonseListener.onCallComplete(baseResponse, requestCode); | |
} | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment