use of in.bugzy.data.remote.model.Response in project bugzy by cpunq.
the class Repository method getCategories.
public LiveData<Resource<List<Category>>> getCategories(boolean mustFetch) {
if (mFetchCategoriesLiveData != null) {
if (mFetchCategoriesLiveData.getValue().status == Status.LOADING && !mustFetch) {
// If the content is in loading state and the request doesn't require us to fetch again
return mCategoriesPublicLiveData;
}
mCategoriesPublicLiveData.removeSource(mFetchCategoriesLiveData);
mFetchCategoriesLiveData = null;
}
mFetchCategoriesLiveData = new NetworkBoundResource<List<Category>, Response<ListCategoriesData>>(mAppExecutors) {
@Override
protected void saveCallResult(@NonNull Response<ListCategoriesData> item) {
db.beginTransaction();
try {
mMiscDao.insertCategories(item.getData().getCategories());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
protected boolean shouldFetch(@Nullable List<Category> data) {
if (data == null || data.size() == 0) {
return true;
}
if (mustFetch) {
return true;
}
return false;
}
@NonNull
@Override
protected LiveData<List<Category>> loadFromDb() {
return mMiscDao.loadCategories();
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListCategoriesData>>> createCall() {
return mApiService.getCategories(new Request("listCategories"));
}
}.asLiveData();
mCategoriesPublicLiveData.addSource(mFetchCategoriesLiveData, value -> {
mCategoriesPublicLiveData.setValue(value);
});
return mCategoriesPublicLiveData;
}
use of in.bugzy.data.remote.model.Response in project bugzy by cpunq.
the class Repository method getStatuses.
public LiveData<Resource<List<CaseStatus>>> getStatuses(boolean mustFetch) {
if (mFetchStatusesLiveData != null) {
if (mFetchStatusesLiveData.getValue().status == Status.LOADING && !mustFetch) {
// If the content is in loading state and the request doesn't require us to fetch again
return mStatusesPublicLiveData;
}
mStatusesPublicLiveData.removeSource(mFetchStatusesLiveData);
mFetchStatusesLiveData = null;
}
mFetchStatusesLiveData = new NetworkBoundResource<List<CaseStatus>, Response<ListStatusesData>>(mAppExecutors) {
@Override
protected void saveCallResult(@NonNull Response<ListStatusesData> item) {
db.beginTransaction();
try {
mMiscDao.insertStatuses(item.getData().getStatuses());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
protected boolean shouldFetch(@Nullable List<CaseStatus> data) {
if (data == null || data.size() == 0) {
return true;
}
if (mustFetch) {
return true;
}
return false;
}
@NonNull
@Override
protected LiveData<List<CaseStatus>> loadFromDb() {
return mMiscDao.loadStatuses();
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListStatusesData>>> createCall() {
return mApiService.getStatuses(new Request("listStatuses"));
}
}.asLiveData();
mStatusesPublicLiveData.addSource(mFetchStatusesLiveData, value -> {
mStatusesPublicLiveData.setValue(value);
});
return mStatusesPublicLiveData;
}
use of in.bugzy.data.remote.model.Response in project bugzy by cpunq.
the class CasesRepository method cases.
public LiveData<Resource<FilterCasesResult>> cases(final String filter, boolean sortChanged) {
return sorted(new NetworkBoundResource<FilterCasesResult, Response<ListCasesData>>(mAppExecutors) {
@Override
protected void saveCallResult(@NonNull Response<ListCasesData> item) {
db.beginTransaction();
try {
mCaseDao.upsertCases(item.getData().getCases());
mCaseDao.upsertFilterCaseIds(new FilterCasesResult(filter, item.getData().getCaseIds(), null));
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
protected boolean shouldFetch(@Nullable FilterCasesResult data) {
if (sortChanged) {
// network thing this time.
return false;
}
return true;
}
@NonNull
@Override
protected LiveData<FilterCasesResult> loadFromDb() {
return Transformations.switchMap(mCaseDao.loadCasesForFilter(filter), filterCasesData -> {
if (filterCasesData == null) {
return AbsentLiveData.create();
}
return Transformations.map(mCaseDao.loadCasesById(filterCasesData.getCaseIds()), kases -> {
Collections.sort(kases, new Comparator<Case>() {
@Override
public int compare(Case aCase, Case t1) {
return Long.compare(t1.getLastUpdatedAt().getTime(), aCase.getLastUpdatedAt().getTime());
}
});
filterCasesData.setCases(kases);
return filterCasesData;
});
});
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListCasesData>>> createCall() {
ListCasesRequest request = new ListCasesRequest(mColsForCaseList, filter);
return mApiService.listCases(request);
}
}.asLiveData());
}
use of in.bugzy.data.remote.model.Response in project bugzy by cpunq.
the class CasesRepository method editCase.
public LiveData<Resource<Response<EditCaseData>>> editCase(final CaseEditRequest request, int mode, List<Attachment> attachments) {
NetworkBoundTask<Response<EditCaseData>> task = new NetworkBoundTask<Response<EditCaseData>>(mAppExecutors, mGson) {
@Override
public void saveCallResult(@NonNull Response<EditCaseData> result) {
// Save?? ;)
if (result.getData() == null) {
return;
}
}
@NonNull
@Override
protected Call<Response<EditCaseData>> createCall() {
List<MultipartBody.Part> fileParts = new ArrayList<>();
int i = 0;
for (Attachment attachment : attachments) {
fileParts.add(prepareFilePart("File" + i, attachment.getUri()));
i++;
}
request.setFileCount(fileParts.size());
request.setToken(mPrefs.getString(PrefsHelper.Key.ACCESS_TOKEN));
switch(mode) {
case CaseEditActivity.MODE_EDIT:
case CaseEditActivity.MODE_ASSIGN:
return mApiService.editCase(request, fileParts);
case CaseEditActivity.MODE_NEW:
return mApiService.newCase(request, fileParts);
case CaseEditActivity.MODE_CLOSE:
return mApiService.closeCase(request, fileParts);
case CaseEditActivity.MODE_RESOLVE:
return mApiService.resolveCase(request, fileParts);
case CaseEditActivity.MODE_REACTIVATE:
return mApiService.reactivateCase(request, fileParts);
case CaseEditActivity.MODE_REOPEN:
return mApiService.reopenCase(request, fileParts);
default:
return mApiService.editCase(request, fileParts);
}
}
};
mAppExecutors.networkIO().execute(task);
return task.asLiveData();
}
use of in.bugzy.data.remote.model.Response in project bugzy by cpunq.
the class Repository method login.
public LiveData<Resource<Response<LoginData>>> login(String email, String password, String organisation) {
mHostSelectionInterceptor.setHost(organisation.toLowerCase() + ".manuscript.com");
NetworkBoundTask<Response<LoginData>> task = new NetworkBoundTask<Response<LoginData>>(mAppExecutors, mGson) {
@Override
public void saveCallResult(@NonNull Response<LoginData> result) {
String token = result.getData().getToken();
mPrefs.setString(PrefsHelper.Key.ORGANISATION, organisation.toLowerCase());
mPrefs.setString(PrefsHelper.Key.ACCESS_TOKEN, token);
mPrefs.setBoolean(PrefsHelper.Key.USER_LOGGED_IN, true);
mSsRespository.insertDefaultSearchSuggestions();
mBugzyUrlGenerator.setToken(token);
mBugzyUrlGenerator.setOrganisationName(organisation);
mAppExecutors.mainThread().execute(new Runnable() {
@Override
public void run() {
mToken.setValue(token);
}
});
}
@NonNull
@Override
protected Call<Response<LoginData>> createCall() {
return mApiService.login(new LoginRequest(email, password));
}
};
mAppExecutors.networkIO().execute(task);
return task.asLiveData();
}
Aggregations