2024-07-29 00:01:09 +08:00
|
|
|
package com.aiosman.riderpro.data
|
2024-07-23 15:25:00 +08:00
|
|
|
|
|
|
|
|
import androidx.paging.PagingSource
|
|
|
|
|
import androidx.paging.PagingState
|
|
|
|
|
import com.aiosman.riderpro.R
|
|
|
|
|
import com.aiosman.riderpro.model.MomentItem
|
2024-07-29 00:01:09 +08:00
|
|
|
import com.aiosman.riderpro.test.TestDatabase
|
2024-07-23 15:25:00 +08:00
|
|
|
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>
|
2024-07-28 15:07:08 +08:00
|
|
|
suspend fun getMomentById(id: Int): MomentItem
|
2024-07-29 00:01:09 +08:00
|
|
|
suspend fun likeMoment(id: Int)
|
2024-07-23 15:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TestMomentServiceImpl() : MomentService {
|
2024-07-28 15:07:08 +08:00
|
|
|
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"
|
|
|
|
|
)
|
2024-07-29 00:01:09 +08:00
|
|
|
var mockData = TestDatabase.momentData
|
2024-07-23 15:25:00 +08:00
|
|
|
val testMomentBackend = TestMomentBackend(mockData)
|
|
|
|
|
override suspend fun getMoments(pageNumber: Int): ListContainer<MomentItem> {
|
|
|
|
|
return testMomentBackend.fetchMomentItems(pageNumber)
|
|
|
|
|
}
|
2024-07-28 15:07:08 +08:00
|
|
|
|
|
|
|
|
override suspend fun getMomentById(id: Int): MomentItem {
|
|
|
|
|
return mockData[id]
|
|
|
|
|
}
|
2024-07-29 00:01:09 +08:00
|
|
|
|
|
|
|
|
override suspend fun likeMoment(id: Int) {
|
|
|
|
|
// mockData = mockData.map {
|
|
|
|
|
// if (it.id == id) {
|
|
|
|
|
// it.copy(likeCount = it.likeCount + 1)
|
|
|
|
|
// } else {
|
|
|
|
|
// it
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// mockData
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 15:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|