feat: 实现探索动态功能

在动态列表中新增探索动态功能,允许用户浏览和发现新的动态内容。

具体修改包括:

- 在API接口中添加`explore`参数,用于请求探索动态数据。
- 修改MomentService,添加`explore`参数,用于获取探索动态数据。
- 更新MomentViewModel,使用`explore`参数请求探索动态数据。
- 修改MomentPagingSource,使用`explore`参数请求探索动态数据。
- 修改MomentCard,使用`explore`参数显示探索动态数据。
This commit is contained in:
2024-10-26 17:17:29 +08:00
parent 5c1cca6a69
commit fe2bd2f382
5 changed files with 16 additions and 5 deletions

View File

@@ -121,6 +121,7 @@ interface MomentService {
* @param timelineId 用户时间线ID,指定用户 ID 的时间线 * @param timelineId 用户时间线ID,指定用户 ID 的时间线
* @param contentSearch 内容搜索,过滤条件 * @param contentSearch 内容搜索,过滤条件
* @param trend 是否趋势动态 * @param trend 是否趋势动态
* @param explore 是否探索动态
* @return 动态列表 * @return 动态列表
*/ */
suspend fun getMoments( suspend fun getMoments(
@@ -129,6 +130,7 @@ interface MomentService {
timelineId: Int? = null, timelineId: Int? = null,
contentSearch: String? = null, contentSearch: String? = null,
trend: Boolean? = false, trend: Boolean? = false,
explore: Boolean? = false,
favoriteUserId: Int? = null favoriteUserId: Int? = null
): ListContainer<MomentEntity> ): ListContainer<MomentEntity>

View File

@@ -211,6 +211,7 @@ interface RiderProAPI {
@Query("postUser") postUser: Int? = null, @Query("postUser") postUser: Int? = null,
@Query("trend") trend: String? = null, @Query("trend") trend: String? = null,
@Query("favouriteUserId") favouriteUserId: Int? = null, @Query("favouriteUserId") favouriteUserId: Int? = null,
@Query("explore") explore: String? = null,
): Response<ListContainer<Moment>> ): Response<ListContainer<Moment>>
@Multipart @Multipart

View File

@@ -27,6 +27,7 @@ class MomentPagingSource(
private val timelineId: Int? = null, private val timelineId: Int? = null,
private val contentSearch: String? = null, private val contentSearch: String? = null,
private val trend: Boolean? = false, private val trend: Boolean? = false,
private val explore: Boolean? = false,
private val favoriteUserId: Int? = null private val favoriteUserId: Int? = null
) : PagingSource<Int, MomentEntity>() { ) : PagingSource<Int, MomentEntity>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MomentEntity> { override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MomentEntity> {
@@ -38,6 +39,7 @@ class MomentPagingSource(
timelineId = timelineId, timelineId = timelineId,
contentSearch = contentSearch, contentSearch = contentSearch,
trend = trend, trend = trend,
explore = explore,
favoriteUserId = favoriteUserId favoriteUserId = favoriteUserId
) )
@@ -66,6 +68,7 @@ class MomentRemoteDataSource(
timelineId: Int?, timelineId: Int?,
contentSearch: String?, contentSearch: String?,
trend: Boolean?, trend: Boolean?,
explore: Boolean?,
favoriteUserId: Int? favoriteUserId: Int?
): ListContainer<MomentEntity> { ): ListContainer<MomentEntity> {
return momentService.getMoments( return momentService.getMoments(
@@ -74,6 +77,7 @@ class MomentRemoteDataSource(
timelineId = timelineId, timelineId = timelineId,
contentSearch = contentSearch, contentSearch = contentSearch,
trend = trend, trend = trend,
explore = explore,
favoriteUserId = favoriteUserId favoriteUserId = favoriteUserId
) )
} }
@@ -88,7 +92,8 @@ class MomentServiceImpl() : MomentService {
timelineId: Int?, timelineId: Int?,
contentSearch: String?, contentSearch: String?,
trend: Boolean?, trend: Boolean?,
favoriteUserId: Int? explore: Boolean?,
favoriteUserId: Int?,
): ListContainer<MomentEntity> { ): ListContainer<MomentEntity> {
return momentBackend.fetchMomentItems( return momentBackend.fetchMomentItems(
pageNumber = pageNumber, pageNumber = pageNumber,
@@ -96,7 +101,8 @@ class MomentServiceImpl() : MomentService {
timelineId = timelineId, timelineId = timelineId,
contentSearch = contentSearch, contentSearch = contentSearch,
trend = trend, trend = trend,
favoriteUserId = favoriteUserId favoriteUserId = favoriteUserId,
explore = explore
) )
} }
@@ -144,6 +150,7 @@ class MomentBackend {
timelineId: Int?, timelineId: Int?,
contentSearch: String?, contentSearch: String?,
trend: Boolean?, trend: Boolean?,
explore: Boolean?,
favoriteUserId: Int? = null favoriteUserId: Int? = null
): ListContainer<MomentEntity> { ): ListContainer<MomentEntity> {
val resp = ApiClient.api.getPosts( val resp = ApiClient.api.getPosts(
@@ -153,7 +160,8 @@ class MomentBackend {
authorId = author, authorId = author,
contentSearch = contentSearch, contentSearch = contentSearch,
trend = if (trend == true) "true" else "", trend = if (trend == true) "true" else "",
favouriteUserId = favoriteUserId favouriteUserId = favoriteUserId,
explore = if (explore == true) "true" else ""
) )
val body = resp.body() ?: throw ServiceException("Failed to get moments") val body = resp.body() ?: throw ServiceException("Failed to get moments")
return ListContainer( return ListContainer(

View File

@@ -46,7 +46,7 @@ fun ExploreMomentsList() {
) { ) {
items( items(
moments.itemCount, moments.itemCount,
key = { idx -> moments[idx]?.id ?: idx } key = { idx -> idx }
) { idx -> ) { idx ->
val momentItem = moments[idx] ?: return@items val momentItem = moments[idx] ?: return@items
MomentCard(momentEntity = momentItem, MomentCard(momentEntity = momentItem,

View File

@@ -53,7 +53,7 @@ object MomentExploreViewModel : ViewModel() {
MomentPagingSource( MomentPagingSource(
MomentRemoteDataSource(momentService), MomentRemoteDataSource(momentService),
// 如果没有动态,则显示热门动态 // 如果没有动态,则显示热门动态
trend = true explore = true
) )
} }
).flow.cachedIn(viewModelScope).collectLatest { ).flow.cachedIn(viewModelScope).collectLatest {