Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
FetchNextSearchPageTask.java
/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.example.github.repository; import com.android.example.github.api.ApiResponse; import com.android.example.github.api.GithubService; import com.android.example.github.api.RepoSearchResponse; import com.android.example.github.db.GithubDb; import com.android.example.github.vo.RepoSearchResult; import com.android.example.github.vo.Resource; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.MutableLiveData; import java.io.IOException; import java.util.ArrayList; import java.util.List; import retrofit2.Response; /** * A task that reads the search result in the database and fetches the next page, if it has one. */ public class FetchNextSearchPageTask implements Runnable { private final MutableLiveData
> liveData = new MutableLiveData<>(); private final String query; private final GithubService githubService; private final GithubDb db; FetchNextSearchPageTask(String query, GithubService githubService, GithubDb db) { this.query = query; this.githubService = githubService; this.db = db; } @Override public void run() { RepoSearchResult current = db.repoDao().findSearchResult(query); if(current == null) { liveData.postValue(null); return; } final Integer nextPage = current.next; if (nextPage == null) { liveData.postValue(Resource.success(false)); return; } try { Response
response = githubService .searchRepos(query, nextPage).execute(); ApiResponse
apiResponse = new ApiResponse<>(response); if (apiResponse.isSuccessful()) { // we merge all repo ids into 1 list so that it is easier to fetch the result list. List
ids = new ArrayList<>(); ids.addAll(current.repoIds); //noinspection ConstantConditions ids.addAll(apiResponse.body.getRepoIds()); RepoSearchResult merged = new RepoSearchResult(query, ids, apiResponse.body.getTotal(), apiResponse.getNextPage()); try { db.beginTransaction(); db.repoDao().insert(merged); db.repoDao().insertRepos(apiResponse.body.getItems()); db.setTransactionSuccessful(); } finally { db.endTransaction(); } liveData.postValue(Resource.success(apiResponse.getNextPage() != null)); } else { liveData.postValue(Resource.error(apiResponse.errorMessage, true)); } } catch (IOException e) { liveData.postValue(Resource.error(e.getMessage(), true)); } } LiveData
> getLiveData() { return liveData; } }