整理代码
This commit is contained in:
176
app/src/main/java/com/aiosman/riderpro/entity/Account.kt
Normal file
176
app/src/main/java/com/aiosman/riderpro/entity/Account.kt
Normal file
@@ -0,0 +1,176 @@
|
||||
package com.aiosman.riderpro.entity
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.paging.PagingState
|
||||
import com.aiosman.riderpro.data.AccountFollow
|
||||
import com.aiosman.riderpro.data.AccountService
|
||||
import com.aiosman.riderpro.data.Image
|
||||
import com.aiosman.riderpro.data.api.ApiClient
|
||||
import java.io.IOException
|
||||
import java.util.Date
|
||||
|
||||
/**
|
||||
* 用户点赞
|
||||
*/
|
||||
data class AccountLikeEntity(
|
||||
// 动态
|
||||
val post: NoticePostEntity,
|
||||
// 点赞用户
|
||||
val user: NoticeUserEntity,
|
||||
// 点赞时间
|
||||
val likeTime: Date,
|
||||
)
|
||||
|
||||
/**
|
||||
* 用户收藏
|
||||
*/
|
||||
data class AccountFavouriteEntity(
|
||||
// 动态
|
||||
val post: NoticePostEntity,
|
||||
// 收藏用户
|
||||
val user: NoticeUserEntity,
|
||||
// 收藏时间
|
||||
val favoriteTime: Date,
|
||||
)
|
||||
|
||||
/**
|
||||
* 用户信息
|
||||
*/
|
||||
data class AccountProfileEntity(
|
||||
// 用户ID
|
||||
val id: Int,
|
||||
// 粉丝数
|
||||
val followerCount: Int,
|
||||
// 关注数
|
||||
val followingCount: Int,
|
||||
// 昵称
|
||||
val nickName: String,
|
||||
// 头像
|
||||
val avatar: String,
|
||||
// 个人简介
|
||||
val bio: String,
|
||||
// 国家
|
||||
val country: String,
|
||||
// 是否关注,针对当前登录用户
|
||||
val isFollowing: Boolean
|
||||
)
|
||||
|
||||
/**
|
||||
* 消息关联的动态
|
||||
*/
|
||||
data class NoticePostEntity(
|
||||
// 动态ID
|
||||
val id: Int,
|
||||
// 动态内容
|
||||
val textContent: String,
|
||||
// 动态图片
|
||||
val images: List<Image>,
|
||||
// 时间
|
||||
val time: Date,
|
||||
)
|
||||
|
||||
/**
|
||||
* 消息关联的用户
|
||||
*/
|
||||
data class NoticeUserEntity(
|
||||
// 用户ID
|
||||
val id: Int,
|
||||
// 昵称
|
||||
val nickName: String,
|
||||
// 头像
|
||||
val avatar: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* 用户点赞消息分页数据加载器
|
||||
*/
|
||||
class LikeItemPagingSource(
|
||||
private val accountService: AccountService,
|
||||
) : PagingSource<Int, AccountLikeEntity>() {
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AccountLikeEntity> {
|
||||
return try {
|
||||
val currentPage = params.key ?: 1
|
||||
val likes = accountService.getMyLikeNotice(
|
||||
page = currentPage,
|
||||
pageSize = 20,
|
||||
)
|
||||
|
||||
LoadResult.Page(
|
||||
data = likes.list.map {
|
||||
it.toAccountLikeEntity()
|
||||
},
|
||||
prevKey = if (currentPage == 1) null else currentPage - 1,
|
||||
nextKey = if (likes.list.isEmpty()) null else likes.page + 1
|
||||
)
|
||||
} catch (exception: IOException) {
|
||||
return LoadResult.Error(exception)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getRefreshKey(state: PagingState<Int, AccountLikeEntity>): Int? {
|
||||
return state.anchorPosition
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户收藏消息分页数据加载器
|
||||
*/
|
||||
class FavoriteItemPagingSource(
|
||||
private val accountService: AccountService,
|
||||
) : PagingSource<Int, AccountFavouriteEntity>() {
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AccountFavouriteEntity> {
|
||||
return try {
|
||||
val currentPage = params.key ?: 1
|
||||
val favouriteListContainer = accountService.getMyFavouriteNotice(
|
||||
page = currentPage,
|
||||
pageSize = 20,
|
||||
)
|
||||
LoadResult.Page(
|
||||
data = favouriteListContainer.list.map {
|
||||
it.toAccountFavouriteEntity()
|
||||
},
|
||||
prevKey = if (currentPage == 1) null else currentPage - 1,
|
||||
nextKey = if (favouriteListContainer.list.isEmpty()) null else favouriteListContainer.page + 1
|
||||
)
|
||||
} catch (exception: IOException) {
|
||||
return LoadResult.Error(exception)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getRefreshKey(state: PagingState<Int, AccountFavouriteEntity>): Int? {
|
||||
return state.anchorPosition
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户关注消息分页数据加载器
|
||||
*/
|
||||
class FollowItemPagingSource(
|
||||
private val accountService: AccountService,
|
||||
) : PagingSource<Int, AccountFollow>() {
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, AccountFollow> {
|
||||
return try {
|
||||
val currentPage = params.key ?: 1
|
||||
val followListContainer = accountService.getMyFollowNotice(
|
||||
page = currentPage,
|
||||
pageSize = 20,
|
||||
)
|
||||
|
||||
LoadResult.Page(
|
||||
data = followListContainer.list.map {
|
||||
it.copy(
|
||||
avatar = "${ApiClient.BASE_SERVER}${it.avatar}",
|
||||
)
|
||||
},
|
||||
prevKey = if (currentPage == 1) null else currentPage - 1,
|
||||
nextKey = if (followListContainer.list.isEmpty()) null else followListContainer.page + 1
|
||||
)
|
||||
} catch (exception: IOException) {
|
||||
return LoadResult.Error(exception)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getRefreshKey(state: PagingState<Int, AccountFollow>): Int? {
|
||||
return state.anchorPosition
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user