Search in sources :

Example 1 with LiveData

use of android.arch.lifecycle.LiveData in project bugzy by cpunq.

the class CasesRepository method searchCases.

public LiveData<SearchResultsResource<List<Case>>> searchCases(final String query) {
    this.recordResearch(query);
    return Transformations.map(new NetworkBoundResource<List<Case>, Response<ListCasesData>>(mAppExecutors) {

        List<Case> mCases = null;

        @Override
        protected void saveCallResult(@NonNull Response<ListCasesData> item) {
            mCases = item.getData().getCases();
        }

        @Override
        protected boolean shouldFetch(@Nullable List<Case> data) {
            // Fetch always
            return true;
        }

        @NonNull
        @Override
        protected LiveData<List<Case>> loadFromDb() {
            return new LiveData<List<Case>>() {

                @Override
                protected void onActive() {
                    super.onActive();
                    setValue(mCases);
                }
            };
        }

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListCasesData>>> createCall() {
            String[] cols = new String[] { "sTitle", "ixPriority", "sStatus", "sProject", "sFixFor", "sArea", "sPersonAssignedTo", "sPersonOpenedBy" };
            return mApiService.searchCases(new SearchCasesRequest(cols, query));
        }
    }.asLiveData(), v -> {
        return new SearchResultsResource<>(query, v);
    });
}
Also used : AbsentLiveData(com.bluestacks.bugzy.ui.search.AbsentLiveData) LiveData(android.arch.lifecycle.LiveData) MutableLiveData(android.arch.lifecycle.MutableLiveData) SearchCasesRequest(com.bluestacks.bugzy.data.remote.model.SearchCasesRequest) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) Case(com.bluestacks.bugzy.data.model.Case) Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) ListCasesData(com.bluestacks.bugzy.data.remote.model.ListCasesData) NonNull(android.support.annotation.NonNull) SearchResultsResource(com.bluestacks.bugzy.data.model.SearchResultsResource) NetworkBoundResource(com.bluestacks.bugzy.data.remote.NetworkBoundResource) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(android.support.annotation.Nullable)

Aggregations

LiveData (android.arch.lifecycle.LiveData)1 MutableLiveData (android.arch.lifecycle.MutableLiveData)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 Case (com.bluestacks.bugzy.data.model.Case)1 SearchResultsResource (com.bluestacks.bugzy.data.model.SearchResultsResource)1 ApiResponse (com.bluestacks.bugzy.data.remote.ApiResponse)1 NetworkBoundResource (com.bluestacks.bugzy.data.remote.NetworkBoundResource)1 ListCasesData (com.bluestacks.bugzy.data.remote.model.ListCasesData)1 Response (com.bluestacks.bugzy.data.remote.model.Response)1 SearchCasesRequest (com.bluestacks.bugzy.data.remote.model.SearchCasesRequest)1 AbsentLiveData (com.bluestacks.bugzy.ui.search.AbsentLiveData)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1