I have implemented Pager 3 for my RecyclerView. Now I have to add a header to my recycler view if the API response has a flag set.
What I am doing now is when i get the response, I set this flag to a static variable then in the ViewModel check for this variable before adding header like this:
//PagingDataSource
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, UIModel> {
val position = params.key ?: STARTING_PAGE_INDEX
val response = getUseCase(
orgId = orgId,
isPhysical = null,
limit = params.loadSize,
page = position
)
// SETTING FLAG HERE
AppVariable.SessionData.myFlag = response.data.flag ?:false
return LoadResult.Page(
data = response.data.data,
prevKey = if (position == STARTING_PAGE_INDEX) null else position - 1,
nextKey = if (data.isEmpty()) null else position + 1
)
}
In viewModel i am checking like this:
fun fetchData(): Flow<PagingData<UIModel>> {
val flow = Pager(
...
pagingSourceFactory = {
PagingDataSource(
...
)
}
).flow
.map { pagingData ->
//ISSUE HERE
if (AppVariables.SessionData.myFlag) {
return@map pagingData.insertHeaderItem(
TerminalSeparatorType.SOURCE_COMPLETE,
UIModel.ActivateCardHeader
)
}
pagingData
}
return flow
}
Now the issue is that the if condition is getting invoked before I get the response from the server, and after I get the response from the server this if condition is not getting invoked(I know this because I tried setting debug points and it's not hitting after the API response.) If call recyclerViewAdapter.refresh() my header becomes visible.
How can I fix this issue?