更新
This commit is contained in:
@@ -2,16 +2,16 @@ package com.aiosman.riderpro.test
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.paging.PagingState
|
||||
import com.aiosman.riderpro.model.MomentItem
|
||||
import com.aiosman.riderpro.model.MomentEntity
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlin.math.ceil
|
||||
|
||||
class TestBackend(
|
||||
private val backendDataList: List<MomentItem>,
|
||||
private val backendDataList: List<MomentEntity>,
|
||||
private val loadDelay: Long = 500,
|
||||
) {
|
||||
val DataBatchSize = 5
|
||||
class DesiredLoadResultPageResponse(val data: List<MomentItem>)
|
||||
class DesiredLoadResultPageResponse(val data: List<MomentEntity>)
|
||||
/** Returns [DataBatchSize] items for a key */
|
||||
fun searchItemsByKey(key: Int): DesiredLoadResultPageResponse {
|
||||
val maxKey = ceil(backendDataList.size.toFloat() / DataBatchSize).toInt()
|
||||
@@ -28,8 +28,8 @@ class TestBackend(
|
||||
class TestPagingSource(
|
||||
private val backend: TestBackend,
|
||||
private val loadDelay: Long,
|
||||
) : PagingSource<Int, MomentItem>() {
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MomentItem> {
|
||||
) : PagingSource<Int, MomentEntity>() {
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MomentEntity> {
|
||||
// Simulate latency
|
||||
delay(loadDelay)
|
||||
val pageNumber = params.key ?: 0
|
||||
@@ -42,7 +42,7 @@ class TestPagingSource(
|
||||
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? {
|
||||
override fun getRefreshKey(state: PagingState<Int, MomentEntity>): Int? {
|
||||
return state.anchorPosition?.let {
|
||||
state.closestPageToPosition(it)?.prevKey?.plus(1)
|
||||
?: state.closestPageToPosition(it)?.nextKey?.minus(1)
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
package com.aiosman.riderpro.test
|
||||
|
||||
import com.aiosman.riderpro.R
|
||||
import com.aiosman.riderpro.data.AccountProfile
|
||||
import com.aiosman.riderpro.data.Comment
|
||||
import com.aiosman.riderpro.model.MomentItem
|
||||
import com.aiosman.riderpro.data.AccountProfileEntity
|
||||
import com.aiosman.riderpro.data.CommentEntity
|
||||
import com.aiosman.riderpro.model.MomentEntity
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import io.github.serpro69.kfaker.faker
|
||||
import java.io.File
|
||||
|
||||
object TestDatabase {
|
||||
var momentData = emptyList<MomentItem>()
|
||||
var accountData = emptyList<AccountProfile>()
|
||||
var comment = emptyList<Comment>()
|
||||
var momentData = emptyList<MomentEntity>()
|
||||
var accountData = emptyList<AccountProfileEntity>()
|
||||
var commentEntity = emptyList<CommentEntity>()
|
||||
var commentIdCounter = 0
|
||||
var momentIdCounter = 0
|
||||
var selfId = 1
|
||||
@@ -41,6 +41,7 @@ object TestDatabase {
|
||||
var followList = emptyList<Pair<Int, Int>>()
|
||||
var likeCommentList = emptyList<Pair<Int, Int>>()
|
||||
var likeMomentList = emptyList<Pair<Int, Int>>()
|
||||
|
||||
init {
|
||||
val faker = faker {
|
||||
this.fakerConfig {
|
||||
@@ -48,14 +49,15 @@ object TestDatabase {
|
||||
}
|
||||
}
|
||||
accountData = (0..20).toList().mapIndexed { idx, _ ->
|
||||
AccountProfile(
|
||||
AccountProfileEntity(
|
||||
id = idx,
|
||||
followerCount = 0,
|
||||
followingCount = 0,
|
||||
nickName = faker.name.name(),
|
||||
avatar = imageList.random(),
|
||||
bio = "I am a software engineer",
|
||||
country = faker.address.country()
|
||||
country = faker.address.country(),
|
||||
isFollowing = false
|
||||
|
||||
)
|
||||
}
|
||||
@@ -84,7 +86,7 @@ object TestDatabase {
|
||||
for (i in 0..commentCount) {
|
||||
commentIdCounter += 1
|
||||
val commentPerson = accountData.random()
|
||||
var newComment = Comment(
|
||||
var newCommentEntity = CommentEntity(
|
||||
name = commentPerson.nickName,
|
||||
comment = "this is comment ${commentIdCounter}",
|
||||
date = "2023-02-02 11:23",
|
||||
@@ -92,7 +94,7 @@ object TestDatabase {
|
||||
replies = emptyList(),
|
||||
postId = momentIdCounter,
|
||||
avatar = commentPerson.avatar,
|
||||
author = commentPerson.id,
|
||||
author = commentPerson.id.toLong(),
|
||||
id = commentIdCounter,
|
||||
liked = false
|
||||
)
|
||||
@@ -100,16 +102,16 @@ object TestDatabase {
|
||||
for (likeIdx in 0..faker.random.nextInt(0, 5)) {
|
||||
val likePerson = accountData.random()
|
||||
likeCommentList += Pair(commentIdCounter, likePerson.id)
|
||||
newComment = newComment.copy(likes = newComment.likes + 1)
|
||||
newCommentEntity = newCommentEntity.copy(likes = newCommentEntity.likes + 1)
|
||||
}
|
||||
comment += newComment
|
||||
commentEntity += newCommentEntity
|
||||
}
|
||||
val likeCount = faker.random.nextInt(0, 5)
|
||||
for (i in 0..likeCount) {
|
||||
val likePerson = accountData.random()
|
||||
likeMomentList += Pair(momentIdCounter, likePerson.id)
|
||||
}
|
||||
MomentItem(
|
||||
MomentEntity(
|
||||
id = momentIdCounter,
|
||||
avatar = person.avatar,
|
||||
nickname = person.nickName,
|
||||
@@ -129,10 +131,10 @@ object TestDatabase {
|
||||
}
|
||||
|
||||
|
||||
fun updateMomentById(id: Int, momentItem: MomentItem) {
|
||||
fun updateMomentById(id: Int, momentEntity: MomentEntity) {
|
||||
momentData = momentData.map {
|
||||
if (it.id == id) {
|
||||
momentItem
|
||||
momentEntity
|
||||
} else {
|
||||
it
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user