Created
November 29, 2018 07:45
-
-
Save vishalhalani/dbfa1ec6c41ee47183120a6bb6feaa7e to your computer and use it in GitHub Desktop.
Controller Class handle all web request and responses
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
import android.content.Context; | |
import java.util.List; | |
import retrofit2.Call; | |
public class DemoController { | |
ApiClient apiService = null; | |
OnWebAPIResponseListener mRepsonseListener; | |
Context context; | |
public DemoController(OnWebAPIResponseListener mRepsonseListener, Context context) { | |
this.context = context; | |
this.mRepsonseListener = mRepsonseListener; | |
apiService = AppController.getAppInstance().getApiClient(); | |
} | |
/** | |
* This method call add data | |
* | |
* @param requestCode to identify request | |
*/ | |
public void addData(final int requestCode, String title, int typeId, String datetime, String desc) { | |
int userId = PrefManager.getUserId(context); | |
String authToken = PrefManager.getAuthToken(context); | |
AddDataModel model = new AddDataModel(authToken, userId, title, typeId, datetime, | |
desc, | |
PrefManager.getAccessToken(context), PrefManager.getDeviceId(context), ApiConst.DEVICE_TYPE); | |
BaseModel<AddDataModel> baseModel = new BaseModel<AddDataModel>(ApiConst.ADD_DATA, model); | |
Call<BaseResponse<Void>> call = apiService.addData(baseModel); | |
ApiCallManager.enqueue(ApiConst.ADD_DATA, call, new CallbackAdapter<Void>() { | |
@Override | |
public void onApiResponse(ErrorResponse err, BaseResponse baseResponse) { | |
if (err != null) { | |
mRepsonseListener.onCallError(err, requestCode); | |
} | |
if (baseResponse != null) { | |
if (baseResponse.getStatusAsBoolean()) { | |
mRepsonseListener.onCallComplete(baseResponse, requestCode); | |
} | |
} | |
} | |
}); | |
} | |
/** | |
* This method call update data | |
* | |
* @param requestCode to identify request | |
*/ | |
public void updateData(final int requestCode, int id, String title, int typeId, String datetime, String desc) { | |
int userId = PrefManager.getUserId(context); | |
String authToken = PrefManager.getAuthToken(context); | |
UpdateDataModel model = new UpdateDataModel(authToken, userId, id,title, typeId, datetime, | |
desc, | |
PrefManager.getAccessToken(context), PrefManager.getDeviceId(context), ApiConst.DEVICE_TYPE); | |
BaseModel<UpdateDataModel> baseModel = new BaseModel<UpdateDataModel>(ApiConst.UPDATE_DATA, model); | |
Call<BaseResponse<Void>> call = apiService.updateData(baseModel); | |
ApiCallManager.enqueue(ApiConst.UPDATE_DATA, call, new CallbackAdapter<Void>() { | |
@Override | |
public void onApiResponse(ErrorResponse err, BaseResponse baseResponse) { | |
if (err != null) { | |
mRepsonseListener.onCallError(err, requestCode); | |
} | |
if (baseResponse != null) { | |
if (baseResponse.getStatusAsBoolean()) { | |
mRepsonseListener.onCallComplete(baseResponse, requestCode); | |
} | |
} | |
} | |
}); | |
} | |
/** | |
* This method call get data | |
* | |
* @param requestCode to identify request | |
* @param tab flag to identify tab | |
*/ | |
public void getData(final int requestCode, int currentPage) { | |
int userId = PrefManager.getUserId(context); | |
String authToken = PrefManager.getAuthToken(context); | |
GetDataModel model = new GetDataModel(authToken, userId, currentPage, | |
PrefManager.getAccessToken(context), PrefManager.getDeviceId(context), ApiConst.DEVICE_TYPE); | |
BaseModel<GetDataModel> baseModel = new BaseModel<GetDataModel>(ApiConst.GET_DATA, model); | |
Call<BaseResponse<List<DataModel>>> call = apiService.getData(baseModel); | |
ApiCallManager.enqueue(ApiConst.GET_DATA, call, new CallbackAdapter<List<DataModel>>() { | |
@Override | |
public void onApiResponse(ErrorResponse err, BaseResponse baseResponse) { | |
if (err != null) { | |
mRepsonseListener.onCallError(err, requestCode); | |
} | |
if (baseResponse != null) { | |
if (baseResponse.getData() != null) { | |
mRepsonseListener.onCallComplete(baseResponse, requestCode); | |
} | |
} | |
} | |
}); | |
} | |
/** | |
* This method call delete data | |
* | |
* @param requestCode to identify request | |
*/ | |
public void deleteData(final int requestCode, int Id) { | |
int userId = PrefManager.getUserId(context); | |
String authToken = PrefManager.getAuthToken(context); | |
DeleteDataModel model = new DeleteDataModel(authToken, userId,Id, | |
PrefManager.getAccessToken(context), PrefManager.getDeviceId(context), ApiConst.DEVICE_TYPE); | |
BaseModel<DeleteDataModel> baseModel = new BaseModel<DeleteDataModel>(ApiConst.DELETE_DATA, model); | |
Call<BaseResponse<Void>> call = apiService.deleteData(baseModel); | |
ApiCallManager.enqueue(ApiConst.DELETE_DATA, call, new CallbackAdapter<Void>() { | |
@Override | |
public void onApiResponse(ErrorResponse err, BaseResponse baseResponse) { | |
if (err != null) { | |
mRepsonseListener.onCallError(err, requestCode); | |
} | |
if (baseResponse != null) { | |
if (baseResponse.getStatusAsBoolean()) { | |
mRepsonseListener.onCallComplete(baseResponse, requestCode); | |
} | |
} | |
} | |
}); | |
} | |
public void cancelCall() { | |
ApiCallManager.cancelCall(ApiConst.GET_DATA); | |
ApiCallManager.cancelCall(ApiConst.ADD_DATA); | |
ApiCallManager.cancelCall(ApiConst.UPDATE_DATA); | |
ApiCallManager.cancelCall(ApiConst.DELETE_DATA); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment