Skip to content

Instantly share code, notes, and snippets.

@vishalhalani
Created January 2, 2019 06:13
Show Gist options
  • Save vishalhalani/c0bd6e6b8fe9b467753659593c11185c to your computer and use it in GitHub Desktop.
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
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