This commit is contained in:
2024-08-17 17:40:21 +08:00
parent e8140579e0
commit 6137e1c3b5
18 changed files with 494 additions and 63 deletions

View File

@@ -1,10 +1,13 @@
package com.aiosman.riderpro.data
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.aiosman.riderpro.AppStore
import com.aiosman.riderpro.data.api.ApiClient
import com.aiosman.riderpro.data.api.ChangePasswordRequestBody
import com.aiosman.riderpro.data.api.LoginUserRequestBody
import com.aiosman.riderpro.data.api.RegisterRequestBody
import com.aiosman.riderpro.model.MomentEntity
import com.aiosman.riderpro.test.TestDatabase
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
@@ -12,6 +15,7 @@ import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File
import java.io.IOException
data class AccountProfileEntity(
val id: Int,

View File

@@ -6,6 +6,7 @@ import com.aiosman.riderpro.AppStore
import com.aiosman.riderpro.R
import com.aiosman.riderpro.data.api.ApiClient
import com.aiosman.riderpro.model.MomentEntity
import com.aiosman.riderpro.model.MomentImageEntity
import com.aiosman.riderpro.test.TestDatabase
import com.google.gson.annotations.SerializedName
@@ -57,7 +58,13 @@ data class Moment(
commentCount = commentCount.toInt(),
shareCount = 0,
favoriteCount = favoriteCount.toInt(),
images = images.map { ApiClient.BASE_SERVER + it.url + "?token=${AppStore.token}" },
images = images.map {
MomentImageEntity(
url = ApiClient.BASE_SERVER + it.url + "?token=${AppStore.token}",
thumbnail = ApiClient.BASE_SERVER + it.thumbnail + "?token=${AppStore.token}",
id = it.id
)
},
authorId = user.id.toInt(),
liked = isLiked,
isFavorite = isFavorite
@@ -82,12 +89,14 @@ data class User(
@SerializedName("avatar")
val avatar: String
)
data class UploadImage(
val file: File,
val filename: String,
val url: String,
val ext: String
)
interface MomentService {
suspend fun getMomentById(id: Int): MomentEntity
suspend fun likeMoment(id: Int)
@@ -95,7 +104,8 @@ interface MomentService {
suspend fun getMoments(
pageNumber: Int,
author: Int? = null,
timelineId: Int? = null
timelineId: Int? = null,
contentSearch: String? = null
): ListContainer<MomentEntity>
suspend fun createMoment(
@@ -104,6 +114,7 @@ interface MomentService {
images: List<UploadImage>,
relPostId: Int? = null
): MomentEntity
suspend fun favoriteMoment(id: Int)
suspend fun unfavoriteMoment(id: Int)
}
@@ -112,7 +123,8 @@ interface MomentService {
class MomentPagingSource(
private val remoteDataSource: MomentRemoteDataSource,
private val author: Int? = null,
private val timelineId: Int? = null
private val timelineId: Int? = null,
private val contentSearch: String? = null
) : PagingSource<Int, MomentEntity>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MomentEntity> {
return try {
@@ -120,7 +132,8 @@ class MomentPagingSource(
val moments = remoteDataSource.getMoments(
pageNumber = currentPage,
author = author,
timelineId = timelineId
timelineId = timelineId,
contentSearch = contentSearch
)
LoadResult.Page(
@@ -145,9 +158,10 @@ class MomentRemoteDataSource(
suspend fun getMoments(
pageNumber: Int,
author: Int?,
timelineId: Int?
timelineId: Int?,
contentSearch: String?
): ListContainer<MomentEntity> {
return momentService.getMoments(pageNumber, author, timelineId)
return momentService.getMoments(pageNumber, author, timelineId, contentSearch)
}
}
@@ -158,9 +172,10 @@ class TestMomentServiceImpl() : MomentService {
override suspend fun getMoments(
pageNumber: Int,
author: Int?,
timelineId: Int?
timelineId: Int?,
contentSearch: String?
): ListContainer<MomentEntity> {
return testMomentBackend.fetchMomentItems(pageNumber, author, timelineId)
return testMomentBackend.fetchMomentItems(pageNumber, author, timelineId, contentSearch)
}
override suspend fun getMomentById(id: Int): MomentEntity {
@@ -202,13 +217,15 @@ class TestMomentBackend(
suspend fun fetchMomentItems(
pageNumber: Int,
author: Int? = null,
timelineId: Int?
timelineId: Int?,
contentSearch: String?
): ListContainer<MomentEntity> {
val resp = ApiClient.api.getPosts(
pageSize = DataBatchSize,
page = pageNumber,
timelineId = timelineId,
authorId = author
authorId = author,
contentSearch = contentSearch
)
val body = resp.body() ?: throw ServiceException("Failed to get moments")
return ListContainer(
@@ -258,6 +275,7 @@ class TestMomentBackend(
suspend fun favoriteMoment(id: Int) {
ApiClient.api.favoritePost(id)
}
suspend fun unfavoriteMoment(id: Int) {
ApiClient.api.unfavoritePost(id)
}

View File

@@ -1,17 +1,53 @@
package com.aiosman.riderpro.data
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.aiosman.riderpro.data.api.ApiClient
import com.aiosman.riderpro.model.MomentEntity
import com.aiosman.riderpro.test.TestDatabase
import java.io.IOException
data class UserAuth(
val id: Int,
val token: String? = null
)
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
}
}
interface UserService {
suspend fun getUserProfile(id: String): AccountProfileEntity
suspend fun followUser(id: String)
suspend fun unFollowUser(id: String)
suspend fun getUsers(
pageSize: Int = 20,
page: Int = 1,
nickname: String? = null
): ListContainer<AccountProfileEntity>
}
@@ -31,4 +67,19 @@ class TestUserServiceImpl : UserService {
val resp = ApiClient.api.unfollowUser(id.toInt())
return
}
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
)
}
}

View File

@@ -75,6 +75,7 @@ interface RiderProAPI {
@Query("pageSize") pageSize: Int = 20,
@Query("timelineId") timelineId: Int? = null,
@Query("authorId") authorId: Int? = null,
@Query("contentSearch") contentSearch: String? = null,
): Response<ListContainer<Moment>>
@Multipart
@@ -163,4 +164,11 @@ interface RiderProAPI {
@Path("id") id: Int
): Response<Unit>
@GET("users")
suspend fun getUsers(
@Query("page") page: Int = 1,
@Query("pageSize") pageSize: Int = 20,
@Query("nickname") search: String? = null,
): Response<ListContainer<AccountProfile>>
}