From 41804cb90fea29024f3f2de3564ff4350d66ad67 Mon Sep 17 00:00:00 2001 From: Florina Muntenescu Date: Thu, 12 Mar 2020 13:46:08 +0000 Subject: [PATCH] Adding header and footer to the app and showing progress bar / retry button for new queries --- .../paging/data/GithubPagingSource.kt | 26 ++++---- .../paging/ui/ReposLoadStateAdapter.kt | 32 ++++++++++ .../paging/ui/ReposLoadStateViewHolder.kt | 61 +++++++++++++++++++ .../paging/ui/SearchRepositoriesActivity.kt | 31 ++++++---- .../layout/activity_search_repositories.xml | 31 ++++++---- .../repos_load_state_header_view_item.xml | 42 +++++++++++++ app/src/main/res/values/dimens.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 191 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateAdapter.kt create mode 100644 app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateViewHolder.kt create mode 100644 app/src/main/res/layout/repos_load_state_header_view_item.xml diff --git a/app/src/main/java/com/example/android/codelabs/paging/data/GithubPagingSource.kt b/app/src/main/java/com/example/android/codelabs/paging/data/GithubPagingSource.kt index 3d941400..a260fa9d 100644 --- a/app/src/main/java/com/example/android/codelabs/paging/data/GithubPagingSource.kt +++ b/app/src/main/java/com/example/android/codelabs/paging/data/GithubPagingSource.kt @@ -36,17 +36,21 @@ class GithubPagingSource( override suspend fun load(params: LoadParams): LoadResult { val position = params.key ?: GITHUB_STARTING_PAGE_INDEX val apiQuery = query + IN_QUALIFIER - val apiResponse = service.searchRepos(apiQuery, position, GithubRepository.NETWORK_PAGE_SIZE) - return if (apiResponse.isSuccessful) { - val repos = apiResponse.body()?.items ?: emptyList() - LoadResult.Page( - data = repos, - prevKey = if (position == GITHUB_STARTING_PAGE_INDEX) null else -1, - // if we don't get any results, we consider that we're at the last page - nextKey = if (repos.isEmpty()) null else position + 1 - ) - } else { - LoadResult.Error(IOException(apiResponse.message())) + try { + val apiResponse = service.searchRepos(apiQuery, position, GithubRepository.NETWORK_PAGE_SIZE) + return if (apiResponse.isSuccessful) { + val repos = apiResponse.body()?.items ?: emptyList() + LoadResult.Page( + data = repos, + prevKey = if (position == GITHUB_STARTING_PAGE_INDEX) null else -1, + // if we don't get any results, we consider that we're at the last page + nextKey = if (repos.isEmpty()) null else position + 1 + ) + } else { + LoadResult.Error(IOException(apiResponse.message())) + } + } catch (exception: Exception) { + return LoadResult.Error(exception) } } } diff --git a/app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateAdapter.kt b/app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateAdapter.kt new file mode 100644 index 00000000..d4c49a01 --- /dev/null +++ b/app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateAdapter.kt @@ -0,0 +1,32 @@ +package com.example.android.codelabs.paging.ui + +/* + * Copyright (C) 2018 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. + */ + +import android.view.ViewGroup +import androidx.paging.LoadState +import androidx.paging.LoadStateAdapter + +class ReposLoadStateAdapter(private val retry: () -> Unit) : LoadStateAdapter() { + override fun onBindViewHolder(holder: ReposLoadStateViewHolder, loadState: LoadState) { + holder.bind(loadState) + } + + override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): ReposLoadStateViewHolder { + return ReposLoadStateViewHolder.create(retry, parent) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateViewHolder.kt b/app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateViewHolder.kt new file mode 100644 index 00000000..59c13bfc --- /dev/null +++ b/app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateViewHolder.kt @@ -0,0 +1,61 @@ +package com.example.android.codelabs.paging.ui + +/* + * Copyright (C) 2018 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. + */ + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Button +import android.widget.ProgressBar +import android.widget.TextView +import androidx.paging.LoadState +import androidx.recyclerview.widget.RecyclerView +import com.example.android.codelabs.paging.R + +class ReposLoadStateViewHolder( + retry: () -> Unit, + view: View +) : RecyclerView.ViewHolder(view) { + private val progressBar: ProgressBar = itemView.findViewById(R.id.progress_bar) + private val errorMsg: TextView = itemView.findViewById(R.id.error_msg) + private val retry: Button = itemView.findViewById