Permalink
Browse files

Add PagingSample (#153)

This CL adds a new sample for the new paging library.
  • Loading branch information...
ChrisCraik authored and yigit committed Sep 14, 2017
1 parent 10cdcd3 commit 456d05289294ec32592ee0b8278cb95c7acbc32a
Showing with 1,383 additions and 0 deletions.
  1. +9 −0 PagingSample/.gitignore
  2. +36 −0 PagingSample/README.md
  3. +1 −0 PagingSample/app/.gitignore
  4. +55 −0 PagingSample/app/build.gradle
  5. +21 −0 PagingSample/app/proguard-rules.pro
  6. +37 −0 PagingSample/app/src/main/AndroidManifest.xml
  7. +26 −0 PagingSample/app/src/main/java/paging/android/example/com/pagingsample/Cheese.kt
  8. +68 −0 PagingSample/app/src/main/java/paging/android/example/com/pagingsample/CheeseAdapter.kt
  9. +45 −0 PagingSample/app/src/main/java/paging/android/example/com/pagingsample/CheeseDao.kt
  10. +192 −0 PagingSample/app/src/main/java/paging/android/example/com/pagingsample/CheeseDb.kt
  11. +42 −0 PagingSample/app/src/main/java/paging/android/example/com/pagingsample/CheeseViewHolder.kt
  12. +65 −0 PagingSample/app/src/main/java/paging/android/example/com/pagingsample/CheeseViewModel.kt
  13. +28 −0 PagingSample/app/src/main/java/paging/android/example/com/pagingsample/Executors.kt
  14. +107 −0 PagingSample/app/src/main/java/paging/android/example/com/pagingsample/MainActivity.kt
  15. +68 −0 PagingSample/app/src/main/res/drawable/ic_launcher_background.xml
  16. +51 −0 PagingSample/app/src/main/res/layout/activity_main.xml
  17. +28 −0 PagingSample/app/src/main/res/layout/cheese_item.xml
  18. +21 −0 PagingSample/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
  19. +21 −0 PagingSample/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
  20. BIN PagingSample/app/src/main/res/mipmap-hdpi/ic_launcher.png
  21. BIN PagingSample/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png
  22. BIN PagingSample/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
  23. BIN PagingSample/app/src/main/res/mipmap-mdpi/ic_launcher.png
  24. BIN PagingSample/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png
  25. BIN PagingSample/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
  26. BIN PagingSample/app/src/main/res/mipmap-xhdpi/ic_launcher.png
  27. BIN PagingSample/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png
  28. BIN PagingSample/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
  29. BIN PagingSample/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
  30. BIN PagingSample/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png
  31. BIN PagingSample/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
  32. BIN PagingSample/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
  33. BIN PagingSample/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png
  34. BIN PagingSample/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
  35. +22 −0 PagingSample/app/src/main/res/values/colors.xml
  36. +20 −0 PagingSample/app/src/main/res/values/dimens.xml
  37. +21 −0 PagingSample/app/src/main/res/values/strings.xml
  38. +27 −0 PagingSample/app/src/main/res/values/styles.xml
  39. +50 −0 PagingSample/build.gradle
  40. +33 −0 PagingSample/gradle.properties
  41. BIN PagingSample/gradle/wrapper/gradle-wrapper.jar
  42. +22 −0 PagingSample/gradle/wrapper/gradle-wrapper.properties
  43. +160 −0 PagingSample/gradlew
  44. +90 −0 PagingSample/gradlew.bat
  45. +17 −0 PagingSample/settings.gradle
View
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
View
@@ -0,0 +1,36 @@
Android Architecture Components Paging Sample
===================================
This sample showcases the following Architecture Components:
* [Paging](https://developer.android.com/topic/libraries/architecture/paging.html)
* [Room](https://developer.android.com/topic/libraries/architecture/room.html)
* [ViewModels](https://developer.android.com/reference/android/arch/lifecycle/ViewModel.html)
* [LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData.html)
## Features
This sample contains a single screen with a list of text items. Items can be added to the list with the input at the top, and swiping items in the list removes them.
## License
-------
Copyright 2017 The Android Open Source Project, Inc.
Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
additional information regarding copyright ownership. The ASF licenses this
file to you 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.
@@ -0,0 +1 @@
/build
@@ -0,0 +1,55 @@
/*
* 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.
*/
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "paging.android.example.com.pagingsample"
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.android.support:appcompat-v7:${versions.support}"
implementation "com.android.support:recyclerview-v7:${versions.support}"
implementation "com.android.support:cardview-v7:${versions.support}"
implementation "android.arch.persistence.room:runtime:${versions.arch}"
implementation "android.arch.lifecycle:runtime:${versions.arch}"
implementation "android.arch.lifecycle:extensions:${versions.arch}"
implementation "android.arch.paging:runtime:${versions.paging}"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
kapt "android.arch.persistence.room:compiler:${versions.arch}"
}
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="paging.android.example.com.pagingsample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
@@ -0,0 +1,26 @@
/*
* 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 paging.android.example.com.pagingsample
import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey
/**
* Data class that represents our items.
*/
@Entity
data class Cheese(@PrimaryKey(autoGenerate = true) val id: Int, val name: String)
@@ -0,0 +1,68 @@
/*
* 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 paging.android.example.com.pagingsample
import android.arch.paging.PagedListAdapter
import android.support.v7.recyclerview.extensions.DiffCallback
import android.view.ViewGroup
/**
* A simple PagedListAdapter that binds Cheese items into CardViews.
* <p>
* PagedListAdapter is a RecyclerView.Adapter base class which can present the content of PagedLists
* in a RecyclerView. It requests new pages as the user scrolls, and handles new PagedLists by
* computing list differences on a background thread, and dispatching minimal, efficient updates to
* the RecyclerView to ensure minimal UI thread work.
* <p>
* If you want to use your own Adapter base class, try using a PagedListAdapterHelper inside your
* adapter instead.
*
* @see android.arch.paging.PagedListAdapter
* @see android.arch.paging.PagedListAdapterHelper
*/
class CheeseAdapter : PagedListAdapter<Cheese, CheeseViewHolder>(diffCallback) {
override fun onBindViewHolder(holder: CheeseViewHolder, position: Int) {
holder.bindTo(getItem(position))
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CheeseViewHolder =
CheeseViewHolder(parent)
companion object {
/**
* This diff callback informs the PagedListAdapter how to compute list differences when new
* PagedLists arrive.
* <p>
* When you add a Cheese with the 'Add' button, the PagedListAdapter uses diffCallback to
* detect there's only a single item difference from before, so it only needs to animate and
* rebind a single view.
*
* @see android.support.v7.util.DiffUtil
*/
private val diffCallback = object : DiffCallback<Cheese>() {
override fun areItemsTheSame(oldItem: Cheese, newItem: Cheese): Boolean =
oldItem.id == newItem.id
/**
* Note that in kotlin, == checking on data classes compares all contents, but in Java,
* typically you'll implement Object#equals, and use it to compare object contents.
*/
override fun areContentsTheSame(oldItem: Cheese, newItem: Cheese): Boolean =
oldItem == newItem
}
}
}
@@ -0,0 +1,45 @@
/*
* 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 paging.android.example.com.pagingsample
import android.arch.paging.LivePagedListProvider
import android.arch.persistence.room.Dao
import android.arch.persistence.room.Delete
import android.arch.persistence.room.Insert
import android.arch.persistence.room.Query
/**
* Database Access Object for the Cheese database.
*/
@Dao
interface CheeseDao {
/**
* Room knows how to return a LivePagedListProvider, from which we can get a LiveData and serve
* it back to UI via ViewModel.
*/
@Query("SELECT * FROM Cheese ORDER BY name COLLATE NOCASE ASC")
fun allCheesesByName(): LivePagedListProvider<Int, Cheese>
@Insert
fun insert(cheeses: List<Cheese>)
@Insert
fun insert(cheese: Cheese)
@Delete
fun delete(cheese: Cheese)
}
Oops, something went wrong.

0 comments on commit 456d052

Please sign in to comment.