添加业务逻辑

This commit is contained in:
2024-07-28 15:07:08 +08:00
parent dfbc151e4e
commit f35bde65a2
8 changed files with 355 additions and 260 deletions

View File

@@ -0,0 +1,98 @@
package com.aiosman.riderpro.data.comment
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.aiosman.riderpro.data.ListContainer
import java.io.IOException
import kotlin.random.Random
data class Comment(
val name: String,
val comment: String,
val date: String,
val likes: Int,
val replies: List<Comment>
)
class CommentPagingSource(
private val remoteDataSource: CommentRemoteDataSource,
) : PagingSource<Int, Comment>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Comment> {
return try {
val currentPage = params.key ?: 1
val comments = remoteDataSource.getComments(
pageNumber = currentPage
)
LoadResult.Page(
data = comments.list,
prevKey = if (currentPage == 1) null else currentPage - 1,
nextKey = if (comments.list.isEmpty()) null else comments.page + 1
)
} catch (exception: IOException) {
return LoadResult.Error(exception)
}
}
override fun getRefreshKey(state: PagingState<Int, Comment>): Int? {
return state.anchorPosition
}
}
class CommentRemoteDataSource(
private val commentService: CommentService,
) {
suspend fun getComments(pageNumber: Int): ListContainer<Comment> {
return commentService.getComments(pageNumber)
}
}
interface CommentService {
suspend fun getComments(pageNumber: Int): ListContainer<Comment>
}
class TestCommentServiceImpl : CommentService {
private val mockData = generateMockComments(100)
override suspend fun getComments(pageNumber: Int): ListContainer<Comment> {
val from = pageNumber * DataBatchSize
val to = (pageNumber + 1) * DataBatchSize
val currentSublist = mockData.subList(from, to)
return ListContainer(
total = mockData.size,
page = pageNumber,
pageSize = DataBatchSize,
list = currentSublist
)
}
private fun generateMockComments(count: Int): List<Comment> {
return (0 until count).map {
Comment(
name = "User $it",
comment = "This is comment $it",
date = "2023-02-02 11:23",
likes = Random.nextInt(0, 100),
replies = generateMockReplies()
)
}
}
private fun generateMockReplies(): List<Comment> {
val replyCount = Random.nextInt(0, 6)
return (0 until replyCount).map {
Comment(
name = "Reply User $it",
comment = "This is reply $it",
date = "2023-02-02 11:23",
likes = Random.nextInt(0, 100),
replies = emptyList()
)
}
}
companion object {
const val DataBatchSize = 5
}
}

View File

@@ -1,12 +1,12 @@
package com.aiosman.riderpro.data.moment
import android.net.http.HttpException
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.aiosman.riderpro.R
import com.aiosman.riderpro.data.ListContainer
import com.aiosman.riderpro.model.MomentItem
import java.io.IOException
import kotlin.random.Random
class MomentPagingSource(
private val remoteDataSource: MomentRemoteDataSource,
@@ -45,9 +45,15 @@ class MomentRemoteDataSource(
interface MomentService {
suspend fun getMoments(pageNumber: Int): ListContainer<MomentItem>
suspend fun getMomentById(id: Int): MomentItem
}
class TestMomentServiceImpl() : MomentService {
var imageList = listOf(
"https://img.freepik.com/free-photo/white-billboard-template_23-2147726635.jpg?t=st=1722150015~exp=1722153615~hmac=5540620196d7898215d822be26353c87a63d51bbfb2b814e032626e1948a1583&w=740",
"https://img.freepik.com/free-photo/minimal-clothing-label-fashion-brands_53876-111053.jpg?w=1060&t=st=1722150122~exp=1722150722~hmac=67f8a2b6abfe3d08714cf0cc0085485c3221e1ba00dda14378b03753dce39153",
"https://img.freepik.com/free-photo/marketing-strategy-planning-strategy-concept_53876-42950.jpg"
)
val mockData = (0..300).toList().mapIndexed { idx, _ ->
MomentItem(
id = idx,
@@ -61,13 +67,18 @@ class TestMomentServiceImpl() : MomentService {
likeCount = 21,
commentCount = 43,
shareCount = 33,
favoriteCount = 211
favoriteCount = 211,
images = imageList.shuffled().take(3),
)
}
val testMomentBackend = TestMomentBackend(mockData)
override suspend fun getMoments(pageNumber: Int): ListContainer<MomentItem> {
return testMomentBackend.fetchMomentItems(pageNumber)
}
override suspend fun getMomentById(id: Int): MomentItem {
return mockData[id]
}
}
class TestMomentBackend(