我正在使用分页库从API中检索数据并在列表中显示它们。
为此,在我的存储库中创建了以下方法:
fun getArticleList(query: String): Flow<PagingData<ArticleHeaderData>>
在我的视图模型中,我创建了搜索方法,大致如下:
override fun search(query: String) {
val lastResult = articleFlow
if (query == lastQuery && lastResult != null)
return
lastQuery = query
searchJob?.cancel()
searchJob = launch {
val newResult: Flow<PagingData<ArticleList>> = repo.getArticleList(query)
.map {
it.insertSeparators { //code to add separators }.cachedIn(this)
articleFlow = newResult
newResult.collectLatest {
articleList.postValue(it)
}
}
}
为了测试我的视图模型,我正在使用测试方法
PagingData.from
来创建一个流,从我的模拟存储库中返回,如下所示:
whenever(repo.getArticleList(query)).thenReturn(flowOf(PagingData.from(articles)))
然后,我从articleList LiveData中检索实际的分页数据,如下所示:
val data = vm.articleList.value!!
这将返回一个PagingData<ArticleList>
对象,我想验证它是否包含来自服务的数据(即由whenever返回的articles
)。我找到的唯一方法是创建以下扩展函数:
private val dcb = object : DifferCallback {
override fun onChanged(position: Int, count: Int) {}
override fun onInserted(position: Int, count: Int) {}
override fun onRemoved(position: Int, count: Int) {}
}
suspend fun <T : Any> PagingData<T>.collectData(): List<T> {
val items = mutableListOf<T>()
val dif = object : PagingDataDiffer<T>(dcb, TestDispatchers.Immediate) {
override suspend fun presentNewList(previousList: NullPaddedList<T>, newList: NullPaddedList<T>, newCombinedLoadStates: CombinedLoadStates, lastAccessedIndex: Int): Int? {
for (idx in 0 until newList.size)
items.add(newList.getFromStorage(idx))
return null
}
}
dif.collectFrom(this)
return items
}
这个方法看起来可以工作,但是它基于标记为@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
的PagingDataDiffer
类,因此将来可能无法使用。
有没有更好的方法来获取PagingData中的flow
(在库中标记为内部)或从中获取实际数据?