So, I have a pager setup using kotlin flows in android. I have a string that I get from the user and whenever that changes I use the flatmaplatest operator to get the latest item from the Pager object. The pager object is as follows.
fun searchAedItem(search: String) {
viewModelScope.launch {
mutableSearchData.emit(search)
}
}
val searchFlow = mutableSearchData.flatMapLatest {
IOLog.d(TAG, "search Flow happening..$it")
val jsonObject = JSONObject()
jsonObject.put("keyword", it)
val gson = getGsonObject(jsonObject)
Pager(PagingConfig(pageSize = 1)) { AedSearchSource(aedApi, gson) }.flow
}
the above is inside my viewmodel and I'm observing these inside my activity as follows:
lifecycleScope.launchWhenStarted {
viewmodel.searchFlow.collect {
IOLog.d("mutableSearchData", it.toString())
adapter?.submitData(it)
}
}
My adapter seems to be running fine since there was no problem when I displayed it with a different list
AedSearchSource class looks like this.
class AedSearchSource(val aedApi: AedApi, val searchObje: JsonObject) :
PagingSource<Int, AedDevicesListItem>() {
override fun getRefreshKey(state: PagingState<Int, AedDevicesListItem>): Int? {
TODO("Not yet implemented")
}
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AedDevicesListItem> {
return try {
val nextPage = params.key ?: 1
val response = aedApi.serachAed(searchObje, nextPage).await()
val list = response.body()!!
return if (list.isEmpty()) {
LoadResult.Error(Throwable("End of list reached."))
} else
LoadResult.Page(
data = list,
prevKey = if (nextPage == 1) null else nextPage - 1,
nextKey = nextPage.inc()
)
} catch (e: Exception) {
LoadResult.Error(e)
}
}
}
so, the problem is that it only works the first time api gets called and executes fine. and then subsequent times when I have a different text in searchAedItem() Pager(PagingConfig(pageSize = 1)) { AedSearchSource(aedApi, gson) }.flow does not get executed. but the flatMapLatest executes just fine.but pager object doesnot execute that api that I want to search. Any ideas of what I am doing wrong?