Skip to content

Commit 5bd1758

Browse files
committedDec 7, 2017
Paging sample with different network configurations
This CL adds a new sample application that uses Reddit API to showcase 3 different paging implementations. 1) DB + Network: This sample keeps the data in the database and also uses a boundary callback to load more when necessary. 2) Network by Item Name: This sample does not persist results, instead uses an ItemKeyedDataSource to page results. 3) Network by Response Keys: This sample is also memory only and uses the before/after fields in the network response. Change-Id: Id36e23fef4c6abe134243a41e44a85d2d65835fd
1 parent 674b6be commit 5bd1758

File tree

68 files changed

+3696
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+3696
-0
lines changed
 

Diff for: ‎PagingWithNetworkSample/.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild

Diff for: ‎PagingWithNetworkSample/README.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Paging With Network Sample
2+
3+
This sample demonstrates how to use the Paging library with a backend API (in this
4+
case [Reddit API][8]).
5+
6+
There are 3 variations of the demo, which you can select in the `MainActivity` class.
7+
8+
After selecting an option, it starts the `RedditActivity` which is the activity that
9+
shows the list of posts in a given subreddit.
10+
11+
## Paging With Database And Network
12+
This sample, implemented in the [DbRedditPostRepository][1] class, demonstrates how to set up
13+
a Repository that will use the local database to page in data for the UI and also back-fill
14+
the database from the network as the user reaches to the end of the data in the database.
15+
16+
It uses `Room` to create the `DataSource.Factory` ([dao][3]) and the Paging Boundary Callback
17+
API to get notified when the Paging library consumes the available local data ([boundary callback
18+
implementation][4])
19+
20+
This usually provides the best user experience as the cached content is always available
21+
on the device and the user will still have a good experience even if the network is slow /
22+
unavailable.
23+
24+
## Paging Using Item Keys
25+
This sample, implemented in the [InMemoryByItemRepository][2] class, demonstrates how to
26+
set up a Repository that will directly page in from the network and will use the `key` from
27+
the previous item to find the request parameters for the next page.
28+
29+
[ItemKeyedSubredditDataSource][5]: The data source that uses the `key` in items
30+
(`name` in Reddit API) to find the next page. It extends from the `ItemKeyedDataSource` class
31+
in the Paging Library.
32+
33+
## Paging Using Next Tokens From The Previous Query
34+
This sample, implemented in the [InMemoryByPageKeyRepository][6] class, demonstrates how to
35+
utilize the `before` and `after` keys in the response to discover the next page. (This is
36+
the intended use of the Reddit API but this sample still provides
37+
[ItemKeyedSubredditDataSource][5] to serve as an example if the backend does not provide
38+
before/after links)
39+
40+
[PageKeyedSubredditDataSource][7]: The data source that uses the `after` and `before` fields
41+
in the API request response. It extends from the `PageKeyedDataSource` class
42+
in the Paging Library.
43+
44+
45+
### Libraries
46+
* [Android Support Library][support-lib]
47+
* [Android Architecture Components][arch]
48+
* [Retrofit][retrofit] for REST api communication
49+
* [Glide][glide] for image loading
50+
* [espresso][espresso] for UI tests
51+
* [mockito][mockito] for mocking in tests
52+
* [Retrofit Mock][retrofit-mock] for creating a fake API implementation for tests
53+
54+
[1]: app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/repository/inDb/DbRedditPostRepository.kt
55+
[2]: app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/repository/inMemory/byItem/InMemoryByItemRepository.kt
56+
[3]: app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/db/RedditPostDao.kt
57+
[4]: app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/repository/inDb/SubredditBoundaryCallback.kt
58+
[5]: app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/repository/inMemory/byItem/ItemKeyedSubredditDataSource.kt
59+
[6]: app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/repository/inMemory/byPage/InMemoryByPageKeyRepository.kt
60+
[7]: app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/repository/inMemory/byPage/PageKeyedSubredditDataSource.kt
61+
[8]: https://www.reddit.com/dev/api/#listings
62+
[mockwebserver]: https://github.com/square/okhttp/tree/master/mockwebserver
63+
[support-lib]: https://developer.android.com/topic/libraries/support-library/index.html
64+
[arch]: https://developer.android.com/arch
65+
[espresso]: https://google.github.io/android-testing-support-library/docs/espresso/
66+
[retrofit]: http://square.github.io/retrofit
67+
[glide]: https://github.com/bumptech/glide
68+
[mockito]: http://site.mockito.org
69+
[retrofit-mock]: https://github.com/square/retrofit/tree/master/retrofit-mock

0 commit comments

Comments
 (0)
Please sign in to comment.