更改目录结构
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package com.aiosman.riderpro.data
|
||||
|
||||
|
||||
data class ListContainer<T>(
|
||||
val total: Int,
|
||||
val page: Int,
|
||||
val pageSize: Int,
|
||||
val list: List<T>
|
||||
)
|
||||
@@ -0,0 +1,92 @@
|
||||
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
|
||||
|
||||
class MomentPagingSource(
|
||||
private val remoteDataSource: MomentRemoteDataSource,
|
||||
) : PagingSource<Int, MomentItem>() {
|
||||
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MomentItem> {
|
||||
return try {
|
||||
val currentPage = params.key ?: 1
|
||||
val moments = remoteDataSource.getMoments(
|
||||
pageNumber = currentPage
|
||||
)
|
||||
|
||||
LoadResult.Page(
|
||||
data = moments.list,
|
||||
prevKey = if (currentPage == 1) null else currentPage - 1,
|
||||
nextKey = if (moments.list.isEmpty()) null else moments.page + 1
|
||||
)
|
||||
} catch (exception: IOException) {
|
||||
return LoadResult.Error(exception)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getRefreshKey(state: PagingState<Int, MomentItem>): Int? {
|
||||
return state.anchorPosition
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MomentRemoteDataSource(
|
||||
private val momentService: MomentService,
|
||||
) {
|
||||
suspend fun getMoments(pageNumber: Int): ListContainer<MomentItem> {
|
||||
return momentService.getMoments(pageNumber)
|
||||
}
|
||||
}
|
||||
|
||||
interface MomentService {
|
||||
suspend fun getMoments(pageNumber: Int): ListContainer<MomentItem>
|
||||
}
|
||||
|
||||
class TestMomentServiceImpl() : MomentService {
|
||||
val mockData = (0..300).toList().mapIndexed { idx, _ ->
|
||||
MomentItem(
|
||||
id = idx,
|
||||
avatar = R.drawable.default_avatar,
|
||||
nickname = "Onyama Limba",
|
||||
location = "Japan",
|
||||
time = "2023.02.02 11:23",
|
||||
followStatus = false,
|
||||
momentTextContent = "By strongarming Ducati into giving him the factory seat.Marquez effectively …",
|
||||
momentPicture = R.drawable.default_moment_img,
|
||||
likeCount = 21,
|
||||
commentCount = 43,
|
||||
shareCount = 33,
|
||||
favoriteCount = 211
|
||||
)
|
||||
}
|
||||
val testMomentBackend = TestMomentBackend(mockData)
|
||||
override suspend fun getMoments(pageNumber: Int): ListContainer<MomentItem> {
|
||||
return testMomentBackend.fetchMomentItems(pageNumber)
|
||||
}
|
||||
}
|
||||
|
||||
class TestMomentBackend(
|
||||
private val mockData: List<MomentItem>,
|
||||
private val loadDelay: Long = 500,
|
||||
) {
|
||||
val DataBatchSize = 5
|
||||
suspend fun fetchMomentItems(pageNumber: Int): ListContainer<MomentItem> {
|
||||
val from = pageNumber * DataBatchSize
|
||||
val to = (pageNumber + 1) * DataBatchSize
|
||||
val currentSublist = mockData.subList(from, to)
|
||||
// delay
|
||||
kotlinx.coroutines.delay(loadDelay)
|
||||
return ListContainer(
|
||||
total = mockData.size,
|
||||
page = pageNumber,
|
||||
pageSize = DataBatchSize,
|
||||
list = currentSublist
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user