Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit a875356

Browse files
authored
Merge pull request #1081 from claraf3/pagingWithNetworkSample-cleanUp
Clean up query order logic in PagingWithNetwork sample
2 parents d654ce4 + 7f08727 commit a875356

File tree

3 files changed

+9
-9
lines changed
  • PagingWithNetworkSample
    • app/src
      • main/java/com/android/example/paging/pagingwithnetwork/reddit/db
      • test-common/java/com/android/example/paging/pagingwithnetwork/repository
    • lib/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/vo

3 files changed

+9
-9
lines changed

PagingWithNetworkSample/app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/db/RedditPostDao.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ interface RedditPostDao {
2828
@Insert(onConflict = OnConflictStrategy.REPLACE)
2929
suspend fun insertAll(posts: List<RedditPost>)
3030

31-
@Query("SELECT * FROM posts WHERE subreddit = :subreddit ORDER BY indexInResponse ASC")
31+
// Order by _id which is an immutable key to help maintain consistent item ordering
32+
@Query("SELECT * FROM posts WHERE subreddit = :subreddit ORDER BY _id ASC")
3233
fun postsBySubreddit(subreddit: String): PagingSource<Int, RedditPost>
3334

3435
@Query("DELETE FROM posts WHERE subreddit = :subreddit")
3536
suspend fun deleteBySubreddit(subreddit: String)
36-
37-
@Query("SELECT MAX(indexInResponse) + 1 FROM posts WHERE subreddit = :subreddit")
38-
suspend fun getNextIndexInSubreddit(subreddit: String): Int
3937
}

PagingWithNetworkSample/app/src/test-common/java/com/android/example/paging/pagingwithnetwork/repository/PostFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class PostFactory {
3232
created = System.currentTimeMillis(),
3333
thumbnail = null,
3434
subreddit = subredditName,
35-
url = null
35+
url = null,
36+
_id = id
3637
)
37-
post.indexInResponse = -1
3838
return post
3939
}
4040
}

PagingWithNetworkSample/lib/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/vo/RedditPost.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import com.google.gson.annotations.SerializedName
2525
@Entity(tableName = "posts",
2626
indices = [Index(value = ["subreddit"], unique = false)])
2727
data class RedditPost(
28-
@PrimaryKey
28+
// A unique Int key starting at 0 which autoIncrements by 1 with every insert.
29+
// We need this immutable key to maintain consistent item ordering so that the UI
30+
// does not scroll out of order when new posts are appended and inserted into this db.
31+
@PrimaryKey(autoGenerate = true)
32+
val _id: Int?,
2933
@SerializedName("name")
3034
val name: String,
3135
@SerializedName("title")
@@ -43,6 +47,4 @@ data class RedditPost(
4347
val created: Long,
4448
val thumbnail: String?,
4549
val url: String?) {
46-
// to be consistent w/ changing backend order, we need to keep a data like this
47-
var indexInResponse: Int = -1
4850
}

0 commit comments

Comments
 (0)