Reputation: 135
I have a modular app that has 3 modules feature and local and remote.
I used pagination in the feature module that used remoteMediator.
My remoteMediator use PagingSource<Int, UserModel> but when I want to get data from the Dao, I have a problem:
remoteMediator neede PagingSource<Int, UserMode> but the local database return PagingSource<Int, UserEntity>.
How can i convert PagingSource<Int, UserEntity> to PagingSource<Int, UserModel>?
My Dao:
@Dao
interface UsersDao {
@Query("SELECT * FROM ${Constants.USERS_TABLE}")
fun getAllUsers(): PagingSource<Int, UserEntity>
}
My Repository:
override fun getUsers(): Flow<PagingData<UserModel>> {
val pagingSourceFactory = {
githubDatabase.usersDao().getAllUsers()
}
return Pager(
config = PagingConfig(pageSize = ITEMS_PER_PAGE),
remoteMediator = AllUsersPagingSource(
githubApi = githubApi,
githubDatabase = githubDatabase,
allUsersMapper = allUsersMapper,
allUsersRemoteLocalMapper = allUsersRemoteLocalMapper
), pagingSourceFactory = pagingSourceFactory
).flow
}
Upvotes: 2
Views: 830
Reputation: 188
I'm having the same problem. Currently, we can not apply any mapping to PagingSource, my solution is to apply the transformation to the flow from Pager:
return Pager(
config = PagingConfig(pageSize = ITEMS_PER_PAGE),
remoteMediator = RemoteMediator<Int, UserEntity>(), // your mediator's Value type is UserEntity, not the UserModel
pagingSourceFactory = pagingSourceFactory
).flow.map { pagingData ->
pagingData.map { userEntity ->
userEntity.toUserModel() // convert UserEntity to UserModel
}
}
Notice that the type of remote mediator is RemoteMediator<Int, UserEntity >
(UserEntity, not UserModel).
Upvotes: 4
Reputation: 14
Did you try to apply mapper on the return data?
Use a mapper in a repository to map data from UserEntity to UserModel
Try something like this:
githubDatabase.usersDao().getAllUsers().map{ mapper.mapToModel(it) }
I hope this will be helpful for you.
Upvotes: -1