Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ abstract class ListEndpointPagingSource(private val listType: DfeListType, priva
AppLog.d("ListPagingSource load: [${params.key}] null")
return LoadResult.Page(emptyList(), null, null)
}
val nextPageUrl = params.key ?: ""
val response = execute(nextPageUrl = nextPageUrl).toListResponse(listType)
val requestedNextPageUrl = params.key ?: ""
val response = execute(nextPageUrl = requestedNextPageUrl).toListResponse(listType)

val items = response.items.map {
val installedInfo = installedApps.packageInfo(it.docId)
Expand All @@ -43,11 +43,15 @@ abstract class ListEndpointPagingSource(private val listType: DfeListType, priva
}

isFirst = false
AppLog.d("ListPagingSource load: [${params.key}] $nextPageUrl")
val nextPageUrl = resolveNextPageUrl(
requestedNextPageUrl = requestedNextPageUrl,
responseNextPageUrl = response.nextPageUrl
)
AppLog.d("ListPagingSource load: [${params.key}] $requestedNextPageUrl -> $nextPageUrl")
return LoadResult.Page(
data = items,
prevKey = null, // Only paging forward.
nextKey = response.nextPageUrl
nextKey = nextPageUrl
)
} catch (e: Exception) {
AppLog.e(e)
Expand All @@ -56,4 +60,11 @@ abstract class ListEndpointPagingSource(private val listType: DfeListType, priva
}

override fun getRefreshKey(state: PagingState<String, ListItem>): String? = null

companion object {
internal fun resolveNextPageUrl(requestedNextPageUrl: String, responseNextPageUrl: String?): String? {
val nextPageUrl = responseNextPageUrl?.takeIf { it.isNotBlank() }
return nextPageUrl?.takeUnless { it == requestedNextPageUrl }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.anod.appwatcher.search

import org.junit.Assert.assertEquals
import org.junit.Test

class ListEndpointPagingSourceTest {

@Test
fun `resolveNextPageUrl returns new continuation url`() {
assertEquals(
"purchaseHistory?o=158",
ListEndpointPagingSource.resolveNextPageUrl(
requestedNextPageUrl = "purchaseHistory?o=79",
responseNextPageUrl = "purchaseHistory?o=158"
)
)
}

@Test
fun `resolveNextPageUrl stops paging when continuation url repeats`() {
assertEquals(
null,
ListEndpointPagingSource.resolveNextPageUrl(
requestedNextPageUrl = "purchaseHistory?o=158",
responseNextPageUrl = "purchaseHistory?o=158"
)
)
}

@Test
fun `resolveNextPageUrl stops paging when continuation url is missing`() {
assertEquals(
null,
ListEndpointPagingSource.resolveNextPageUrl(
requestedNextPageUrl = "purchaseHistory?o=158",
responseNextPageUrl = null
)
)
}

@Test
fun `resolveNextPageUrl stops paging when continuation url is blank`() {
assertEquals(
null,
ListEndpointPagingSource.resolveNextPageUrl(
requestedNextPageUrl = "purchaseHistory?o=158",
responseNextPageUrl = ""
)
)
}
}
Loading