整理代码
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
package com.aiosman.riderpro.data
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.paging.PagingState
|
||||
import com.aiosman.riderpro.data.api.ApiClient
|
||||
import com.aiosman.riderpro.data.api.ChangePasswordRequestBody
|
||||
import com.aiosman.riderpro.data.api.GoogleRegisterRequestBody
|
||||
import com.aiosman.riderpro.data.api.LoginUserRequestBody
|
||||
import com.aiosman.riderpro.data.api.RegisterRequestBody
|
||||
import com.aiosman.riderpro.data.api.UpdateNoticeRequestBody
|
||||
import com.aiosman.riderpro.test.TestDatabase
|
||||
import com.aiosman.riderpro.entity.AccountFavouriteEntity
|
||||
import com.aiosman.riderpro.entity.AccountLikeEntity
|
||||
import com.aiosman.riderpro.entity.AccountProfileEntity
|
||||
import com.aiosman.riderpro.entity.NoticePostEntity
|
||||
import com.aiosman.riderpro.entity.NoticeUserEntity
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.MultipartBody
|
||||
@@ -16,41 +18,29 @@ import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import java.io.File
|
||||
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(
|
||||
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 AccountProfile(
|
||||
// 用户ID
|
||||
val id: Int,
|
||||
// 用户名
|
||||
val username: String,
|
||||
// 昵称
|
||||
val nickname: String,
|
||||
// 头像
|
||||
val avatar: String,
|
||||
// 关注数
|
||||
val followingCount: Int,
|
||||
// 粉丝数
|
||||
val followerCount: Int,
|
||||
// 是否关注
|
||||
val isFollowing: Boolean
|
||||
) {
|
||||
/**
|
||||
* 转换为Entity
|
||||
*/
|
||||
fun toAccountProfileEntity(): AccountProfileEntity {
|
||||
return AccountProfileEntity(
|
||||
id = id,
|
||||
@@ -65,23 +55,27 @@ data class AccountProfile(
|
||||
}
|
||||
}
|
||||
|
||||
data class NoticePostEntity(
|
||||
val id: Int,
|
||||
val textContent: String,
|
||||
val images: List<Image>,
|
||||
val time: Date,
|
||||
)
|
||||
|
||||
/**
|
||||
* 消息关联资料
|
||||
*/
|
||||
data class NoticePost(
|
||||
// 动态ID
|
||||
@SerializedName("id")
|
||||
val id: Int,
|
||||
// 动态内容
|
||||
@SerializedName("textContent")
|
||||
// 动态图片
|
||||
val textContent: String,
|
||||
// 动态图片
|
||||
@SerializedName("images")
|
||||
val images: List<Image>,
|
||||
// 动态时间
|
||||
@SerializedName("time")
|
||||
val time: String,
|
||||
) {
|
||||
/**
|
||||
* 转换为Entity
|
||||
*/
|
||||
fun toNoticePostEntity(): NoticePostEntity {
|
||||
return NoticePostEntity(
|
||||
id = id,
|
||||
@@ -97,20 +91,23 @@ data class NoticePost(
|
||||
}
|
||||
}
|
||||
|
||||
data class NoticeUserEntity(
|
||||
val id: Int,
|
||||
val nickName: String,
|
||||
val avatar: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* 消息关联用户
|
||||
*/
|
||||
data class NoticeUser(
|
||||
// 用户ID
|
||||
@SerializedName("id")
|
||||
val id: Int,
|
||||
// 昵称
|
||||
@SerializedName("nickName")
|
||||
val nickName: String,
|
||||
// 头像
|
||||
@SerializedName("avatar")
|
||||
val avatar: String,
|
||||
) {
|
||||
/**
|
||||
* 转换为Entity
|
||||
*/
|
||||
fun toNoticeUserEntity(): NoticeUserEntity {
|
||||
return NoticeUserEntity(
|
||||
id = id,
|
||||
@@ -120,13 +117,20 @@ data class NoticeUser(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 点赞消息通知
|
||||
*/
|
||||
data class AccountLike(
|
||||
// 是否未读
|
||||
@SerializedName("isUnread")
|
||||
val isUnread: Boolean,
|
||||
// 动态
|
||||
@SerializedName("post")
|
||||
val post: NoticePost,
|
||||
// 点赞用户
|
||||
@SerializedName("user")
|
||||
val user: NoticeUser,
|
||||
// 点赞时间
|
||||
@SerializedName("likeTime")
|
||||
val likeTime: String,
|
||||
) {
|
||||
@@ -192,108 +196,93 @@ data class AccountNotice(
|
||||
)
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
interface AccountService {
|
||||
/**
|
||||
* 获取登录当前用户的资料
|
||||
*/
|
||||
suspend fun getMyAccountProfile(): AccountProfileEntity
|
||||
suspend fun getAccountProfileById(id: Int): AccountProfileEntity
|
||||
|
||||
/**
|
||||
* 获取登录的用户认证信息
|
||||
*/
|
||||
suspend fun getMyAccount(): UserAuth
|
||||
|
||||
/**
|
||||
* 使用用户名密码登录
|
||||
* @param loginName 用户名
|
||||
* @param password 密码
|
||||
*/
|
||||
suspend fun loginUserWithPassword(loginName: String, password: String): UserAuth
|
||||
|
||||
/**
|
||||
* 使用google登录
|
||||
* @param googleId googleId
|
||||
*/
|
||||
suspend fun loginUserWithGoogle(googleId: String): UserAuth
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
suspend fun logout()
|
||||
suspend fun updateAvatar(uri: String)
|
||||
|
||||
/**
|
||||
* 更新用户资料
|
||||
* @param avatar 头像
|
||||
* @param nickName 昵称
|
||||
* @param bio 简介
|
||||
*/
|
||||
suspend fun updateProfile(avatar: UploadImage?, nickName: String?, bio: String?)
|
||||
|
||||
/**
|
||||
* 注册用户
|
||||
* @param loginName 用户名
|
||||
* @param password 密码
|
||||
*/
|
||||
suspend fun registerUserWithPassword(loginName: String, password: String)
|
||||
|
||||
/**
|
||||
* 使用google账号注册
|
||||
* @param idToken googleIdToken
|
||||
*/
|
||||
suspend fun regiterUserWithGoogleAccount(idToken: String)
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
* @param oldPassword 旧密码
|
||||
* @param newPassword 新密码
|
||||
*/
|
||||
suspend fun changeAccountPassword(oldPassword: String, newPassword: String)
|
||||
|
||||
/**
|
||||
* 获取我的点赞通知
|
||||
* @param page 页码
|
||||
* @param pageSize 每页数量
|
||||
*/
|
||||
suspend fun getMyLikeNotice(page: Int, pageSize: Int): ListContainer<AccountLike>
|
||||
|
||||
/**
|
||||
* 获取我的关注通知
|
||||
* @param page 页码
|
||||
* @param pageSize 每页数量
|
||||
*/
|
||||
suspend fun getMyFollowNotice(page: Int, pageSize: Int): ListContainer<AccountFollow>
|
||||
|
||||
/**
|
||||
* 获取我的收藏通知
|
||||
* @param page 页码
|
||||
* @param pageSize 每页数量
|
||||
*/
|
||||
suspend fun getMyFavouriteNotice(page: Int, pageSize: Int): ListContainer<AccountFavourite>
|
||||
|
||||
/**
|
||||
* 获取我的通知信息
|
||||
*/
|
||||
suspend fun getMyNoticeInfo(): AccountNotice
|
||||
|
||||
/**
|
||||
* 更新通知信息,更新最后一次查看时间
|
||||
* @param payload 通知信息
|
||||
*/
|
||||
suspend fun updateNotice(payload: UpdateNoticeRequestBody)
|
||||
}
|
||||
|
||||
@@ -304,12 +293,6 @@ class AccountServiceImpl : AccountService {
|
||||
return body.data.toAccountProfileEntity()
|
||||
}
|
||||
|
||||
override suspend fun getAccountProfileById(id: Int): AccountProfileEntity {
|
||||
val resp = ApiClient.api.getAccountProfileById(id)
|
||||
val body = resp.body() ?: throw ServiceException("Failed to get account")
|
||||
return body.data.toAccountProfileEntity()
|
||||
}
|
||||
|
||||
override suspend fun getMyAccount(): UserAuth {
|
||||
val resp = ApiClient.api.checkToken()
|
||||
val body = resp.body() ?: throw ServiceException("Failed to get account")
|
||||
@@ -323,7 +306,7 @@ class AccountServiceImpl : AccountService {
|
||||
}
|
||||
|
||||
override suspend fun loginUserWithGoogle(googleId: String): UserAuth {
|
||||
val resp = ApiClient.api.login(LoginUserRequestBody(googleId=googleId))
|
||||
val resp = ApiClient.api.login(LoginUserRequestBody(googleId = googleId))
|
||||
val body = resp.body() ?: throw ServiceException("Failed to login")
|
||||
return UserAuth(0, body.token)
|
||||
}
|
||||
@@ -339,15 +322,6 @@ class AccountServiceImpl : AccountService {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
override suspend fun updateAvatar(uri: String) {
|
||||
TestDatabase.accountData = TestDatabase.accountData.map {
|
||||
if (it.id == 1) {
|
||||
it.copy(avatar = uri)
|
||||
} else {
|
||||
it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createMultipartBody(file: File, filename: String, name: String): MultipartBody.Part {
|
||||
val requestFile = file.asRequestBody("image/*".toMediaTypeOrNull())
|
||||
|
||||
Reference in New Issue
Block a user