更改目录结构
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.aiosman.riderpro.test
|
||||
|
||||
import kotlin.math.min
|
||||
|
||||
class MockDataContainer<T>(
|
||||
val success: Boolean,
|
||||
val data: T?
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
class MockListContainer<T>(
|
||||
val total: Int,
|
||||
val page: Int,
|
||||
val pageSize: Int,
|
||||
val list: List<T>
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
abstract class MockDataSource<T> {
|
||||
var list = mutableListOf<T>()
|
||||
suspend fun fetchData(page: Int, pageSize: Int): MockDataContainer<MockListContainer<T>> {
|
||||
// over page return empty
|
||||
if (page * pageSize > list.size) {
|
||||
return MockDataContainer(false, MockListContainer(0, page, pageSize, emptyList()))
|
||||
}
|
||||
val backData = list.subList((page - 1) * pageSize, min(page * pageSize, list.size))
|
||||
return MockDataContainer(true, MockListContainer(list.size, page, pageSize, backData))
|
||||
}
|
||||
}
|
||||
51
app/src/main/java/com/aiosman/riderpro/test/SimpleUtils.kt
Normal file
51
app/src/main/java/com/aiosman/riderpro/test/SimpleUtils.kt
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.aiosman.riderpro.test
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.paging.PagingState
|
||||
import com.aiosman.riderpro.model.MomentItem
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlin.math.ceil
|
||||
|
||||
class TestBackend(
|
||||
private val backendDataList: List<MomentItem>,
|
||||
private val loadDelay: Long = 500,
|
||||
) {
|
||||
val DataBatchSize = 5
|
||||
class DesiredLoadResultPageResponse(val data: List<MomentItem>)
|
||||
/** Returns [DataBatchSize] items for a key */
|
||||
fun searchItemsByKey(key: Int): DesiredLoadResultPageResponse {
|
||||
val maxKey = ceil(backendDataList.size.toFloat() / DataBatchSize).toInt()
|
||||
if (key >= maxKey) {
|
||||
return DesiredLoadResultPageResponse(emptyList())
|
||||
}
|
||||
val from = key * DataBatchSize
|
||||
val to = minOf((key + 1) * DataBatchSize, backendDataList.size)
|
||||
val currentSublist = backendDataList.subList(from, to)
|
||||
return DesiredLoadResultPageResponse(currentSublist)
|
||||
}
|
||||
fun getAllData() = TestPagingSource(this, loadDelay)
|
||||
}
|
||||
class TestPagingSource(
|
||||
private val backend: TestBackend,
|
||||
private val loadDelay: Long,
|
||||
) : PagingSource<Int, MomentItem>() {
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MomentItem> {
|
||||
// Simulate latency
|
||||
delay(loadDelay)
|
||||
val pageNumber = params.key ?: 0
|
||||
val response = backend.searchItemsByKey(pageNumber)
|
||||
// Since 0 is the lowest page number, return null to signify no more pages should
|
||||
// be loaded before it.
|
||||
val prevKey = if (pageNumber > 0) pageNumber - 1 else null
|
||||
// This API defines that it's out of data when a page returns empty. When out of
|
||||
// data, we return `null` to signify no more pages should be loaded
|
||||
val nextKey = if (response.data.isNotEmpty()) pageNumber + 1 else null
|
||||
return LoadResult.Page(data = response.data, prevKey = prevKey, nextKey = nextKey)
|
||||
}
|
||||
override fun getRefreshKey(state: PagingState<Int, MomentItem>): Int? {
|
||||
return state.anchorPosition?.let {
|
||||
state.closestPageToPosition(it)?.prevKey?.plus(1)
|
||||
?: state.closestPageToPosition(it)?.nextKey?.minus(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
app/src/main/java/com/aiosman/riderpro/test/TestStreetMap.kt
Normal file
28
app/src/main/java/com/aiosman/riderpro/test/TestStreetMap.kt
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.aiosman.riderpro.test
|
||||
|
||||
data class StreetPosition(
|
||||
val name:String,
|
||||
val lat:Double,
|
||||
val lng:Double
|
||||
)
|
||||
val countries = listOf(
|
||||
StreetPosition("哈龙湾, 越南",16.5000, 107.1000),
|
||||
StreetPosition("芽庄, 越南",12.2500, 109.0833),
|
||||
StreetPosition("岘港, 越南",16.0667, 108.2167),
|
||||
StreetPosition("美奈, 越南",11.9333, 108.9833),
|
||||
StreetPosition("富国岛, 越南",10.0000, 104.0000),
|
||||
StreetPosition("金三角, 泰国, 缅甸, 老挝",20.2500, 99.7500),
|
||||
StreetPosition("普吉岛, 泰国",7.9444, 98.3000),
|
||||
StreetPosition("苏梅岛, 泰国",9.5333, 99.9333),
|
||||
StreetPosition("曼谷, 泰国",13.7500, 100.5000),
|
||||
StreetPosition("马六甲, 马来西亚",2.2000, 102.2500),
|
||||
StreetPosition("兰卡威群岛, 马来西亚",6.3000, 99.9000),
|
||||
StreetPosition("沙巴, 马来西亚",6.0833, 116.0833),
|
||||
StreetPosition("巴厘岛, 印度尼西亚",8.3333, 115.1000),
|
||||
StreetPosition("龙目岛, 印度尼西亚",8.3333, 116.4000),
|
||||
StreetPosition("婆罗洲, 印度尼西亚",3.0000, 114.0000),
|
||||
StreetPosition("宿务, 菲律宾",10.3167, 123.8833),
|
||||
StreetPosition("长滩岛, 菲律宾",11.5833, 121.9167),
|
||||
StreetPosition("保和岛, 菲律宾",10.3000, 123.3333),
|
||||
StreetPosition("科隆岛, 菲律宾",5.1167, 119.3333)
|
||||
)
|
||||
Reference in New Issue
Block a user