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

@@ -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)
}