2024-07-29 00:01:09 +08:00
|
|
|
package com.aiosman.riderpro.data
|
|
|
|
|
|
2024-08-17 17:40:21 +08:00
|
|
|
import androidx.paging.PagingSource
|
|
|
|
|
import androidx.paging.PagingState
|
2024-08-11 17:15:17 +08:00
|
|
|
import com.aiosman.riderpro.data.api.ApiClient
|
2024-08-17 17:40:21 +08:00
|
|
|
import com.aiosman.riderpro.model.MomentEntity
|
2024-07-29 00:01:09 +08:00
|
|
|
import com.aiosman.riderpro.test.TestDatabase
|
2024-08-17 17:40:21 +08:00
|
|
|
import java.io.IOException
|
2024-07-29 00:01:09 +08:00
|
|
|
|
2024-07-30 16:57:25 +08:00
|
|
|
data class UserAuth(
|
|
|
|
|
val id: Int,
|
|
|
|
|
val token: String? = null
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-17 17:40:21 +08:00
|
|
|
class AccountPagingSource(
|
|
|
|
|
private val userService: UserService,
|
|
|
|
|
private val nickname: String? = null
|
|
|
|
|
) : PagingSource<Int, AccountProfileEntity>() {
|
|
|
|
|
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AccountProfileEntity> {
|
|
|
|
|
return try {
|
|
|
|
|
val currentPage = params.key ?: 1
|
|
|
|
|
val users = userService.getUsers(
|
|
|
|
|
page = currentPage,
|
|
|
|
|
nickname = nickname
|
|
|
|
|
)
|
|
|
|
|
LoadResult.Page(
|
|
|
|
|
data = users.list,
|
|
|
|
|
prevKey = if (currentPage == 1) null else currentPage - 1,
|
|
|
|
|
nextKey = if (users.list.isEmpty()) null else users.page + 1
|
|
|
|
|
)
|
|
|
|
|
} catch (exception: IOException) {
|
|
|
|
|
return LoadResult.Error(exception)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun getRefreshKey(state: PagingState<Int, AccountProfileEntity>): Int? {
|
|
|
|
|
return state.anchorPosition
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 00:01:09 +08:00
|
|
|
interface UserService {
|
2024-08-11 17:15:17 +08:00
|
|
|
suspend fun getUserProfile(id: String): AccountProfileEntity
|
|
|
|
|
suspend fun followUser(id: String)
|
|
|
|
|
suspend fun unFollowUser(id: String)
|
2024-08-17 17:40:21 +08:00
|
|
|
suspend fun getUsers(
|
|
|
|
|
pageSize: Int = 20,
|
|
|
|
|
page: Int = 1,
|
|
|
|
|
nickname: String? = null
|
|
|
|
|
): ListContainer<AccountProfileEntity>
|
2024-07-31 14:50:55 +08:00
|
|
|
|
2024-07-29 00:01:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TestUserServiceImpl : UserService {
|
2024-08-11 17:15:17 +08:00
|
|
|
override suspend fun getUserProfile(id: String): AccountProfileEntity {
|
|
|
|
|
val resp = ApiClient.api.getAccountProfileById(id.toInt())
|
|
|
|
|
val body = resp.body() ?: throw ServiceException("Failed to get account")
|
|
|
|
|
return body.data.toAccountProfileEntity()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun followUser(id: String) {
|
|
|
|
|
val resp = ApiClient.api.followUser(id.toInt())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun unFollowUser(id: String) {
|
|
|
|
|
val resp = ApiClient.api.unfollowUser(id.toInt())
|
|
|
|
|
return
|
2024-07-29 00:01:09 +08:00
|
|
|
}
|
2024-08-17 17:40:21 +08:00
|
|
|
|
|
|
|
|
override suspend fun getUsers(
|
|
|
|
|
pageSize: Int,
|
|
|
|
|
page: Int,
|
|
|
|
|
nickname: String?
|
|
|
|
|
): ListContainer<AccountProfileEntity> {
|
|
|
|
|
val resp = ApiClient.api.getUsers(page, pageSize, nickname)
|
|
|
|
|
val body = resp.body() ?: throw ServiceException("Failed to get account")
|
|
|
|
|
return ListContainer<AccountProfileEntity>(
|
|
|
|
|
list = body.list.map { it.toAccountProfileEntity() },
|
|
|
|
|
page = body.page,
|
|
|
|
|
total = body.total,
|
|
|
|
|
pageSize = body.pageSize
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-07-29 00:01:09 +08:00
|
|
|
}
|