bohregard
09/16/2021, 11:54 PMbohregard
09/16/2021, 11:55 PMprivate val postsPagingSource by lazy { PostsPagingSource(redditApi) }
private val posts = Pager(
config = PagingConfig(
enablePlaceholders = false,
initialLoadSize = 5,
pageSize = 5,
prefetchDistance = 20
),
initialKey = null,
pagingSourceFactory = { postsPagingSource }
).flow.cachedIn(this)
bohregard
09/16/2021, 11:55 PMpackage com.bohregard.pagertest.web.paging
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bohregard.pagertest.extension.debug
import <http://com.bohregard.pagertest.model.Post|com.bohregard.pagertest.model.Post>
import com.bohregard.pagertest.web.RedditApi
import java.lang.Exception
class PostsPagingSource(
val redditApi: RedditApi
): PagingSource<String, Post>() {
companion object {
private var _lastFetchedPosts = listOf<Post>()
}
override suspend fun load(params: LoadParams<String>): LoadResult<String, Post> {
debug("Loading...${params.key}")
try {
val response = redditApi.getAllPosts(
"r/all",
"hot",
params.key,
null
)
val loadResult = LoadResult.Page(
data = response.posts,
prevKey = params.key,
nextKey = response.after,
)
_lastFetchedPosts = response.posts
return loadResult
} catch (e: Exception) {
return LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState<String, Post>): String? {
debug("Refresh Key")
return null
}
}
bohregard
09/16/2021, 11:55 PMPagerTestTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val posts = posts.collectAsLazyPagingItems()
debug("Testing...")
LazyColumn {
items(posts) { post ->
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.padding(10.dp)
) {
Text("Post: ${post!!.title}")
}
}
}
}
}
Ian Lake
09/16/2021, 11:57 PM_lastFetchedPosts = response.posts
? You'd want to append the newly loaded data, not replace the entire set, right?Ian Lake
09/16/2021, 11:59 PMcompanion object
seems very suspicious in generalbohregard
09/16/2021, 11:59 PMbohregard
09/16/2021, 11:59 PMbohregard
09/16/2021, 11:59 PMbohregard
09/17/2021, 12:00 AMIan Lake
09/17/2021, 12:07 AMthis
passed to cachedIn
?)Ian Lake
09/17/2021, 12:08 AMbohregard
09/17/2021, 12:09 AMbohregard
09/17/2021, 12:09 AMbohregard
09/17/2021, 12:09 AMbohregard
09/17/2021, 12:30 AMDustin Lam
09/17/2021, 1:04 AMDustin Lam
09/17/2021, 1:07 AMbohregard
09/17/2021, 1:26 AMbohregard
09/17/2021, 1:29 AM2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: He was funny and kind
2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: instant karma from a squirrel
2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Jerry Springer is a tough SOB
2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Hmmm, im seeing a pattern
2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: "Dont get me what i asked for"
2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: He was funny and kind
2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: instant karma from a squirrel
2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Jerry Springer is a tough SOB
2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Hmmm, im seeing a pattern
2021-09-16 21:28:26.556 23852-23852/com.bohregard.pagertest D/DEBUG: Post: "Dont get me what i asked for"
Which prevents me from using the key mapping since the keys will end up being duplicatedbohregard
09/17/2021, 1:29 AM2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: To call out a racist
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Pickles likes tuna more than the raccoons apparently.
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Pretty sure this counts.
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Rate my setup.
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: When you drove into a strange area and a fight started 🙈😳😅
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: To call out a racist
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Pickles likes tuna more than the raccoons apparently.
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Pretty sure this counts.
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Rate my setup.
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: When you drove into a strange area and a fight started 🙈😳😅
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: To call out a racist
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Pickles likes tuna more than the raccoons apparently.
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Pretty sure this counts.
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: Rate my setup.
2021-09-16 21:28:26.789 23852-23852/com.bohregard.pagertest D/DEBUG: Post: When you drove into a strange area and a fight started 🙈😳😅
bohregard
09/17/2021, 1:30 AMjava.lang.IllegalArgumentException: Key ppkbl0 was already used. If you are using LazyColumn/Row please make sure you provide a unique key for each item.
Dustin Lam
09/17/2021, 2:39 AMDustin Lam
09/17/2021, 2:39 AMdustin
09/17/2021, 6:36 PMdustin
09/17/2021, 6:37 PMdustin
09/17/2021, 6:37 PMdustin
09/17/2021, 6:37 PMreturn PostResponse(after, posts.toList())
making a deep copy like this solves your issuedustin
09/17/2021, 6:38 PMdustin
09/17/2021, 6:39 PMdustin
09/17/2021, 6:42 PMbohregard
09/17/2021, 6:58 PMbohregard
09/17/2021, 7:00 PM