整理代码
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.aiosman.riderpro.test
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.paging.PagingState
|
||||
import com.aiosman.riderpro.model.MomentEntity
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlin.math.ceil
|
||||
|
||||
class TestBackend(
|
||||
private val backendDataList: List<MomentEntity>,
|
||||
private val loadDelay: Long = 500,
|
||||
) {
|
||||
val DataBatchSize = 5
|
||||
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()
|
||||
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, MomentEntity>() {
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MomentEntity> {
|
||||
// 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, MomentEntity>): Int? {
|
||||
return state.anchorPosition?.let {
|
||||
state.closestPageToPosition(it)?.prevKey?.plus(1)
|
||||
?: state.closestPageToPosition(it)?.nextKey?.minus(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
package com.aiosman.riderpro.test
|
||||
|
||||
import com.aiosman.riderpro.R
|
||||
import com.aiosman.riderpro.data.AccountProfileEntity
|
||||
import com.aiosman.riderpro.data.CommentEntity
|
||||
import com.aiosman.riderpro.model.MomentEntity
|
||||
import com.aiosman.riderpro.model.MomentImageEntity
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import io.github.serpro69.kfaker.faker
|
||||
import java.io.File
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
|
||||
object TestDatabase {
|
||||
var momentData = emptyList<MomentEntity>()
|
||||
var accountData = emptyList<AccountProfileEntity>()
|
||||
var commentEntity = emptyList<CommentEntity>()
|
||||
var commentIdCounter = 0
|
||||
var momentIdCounter = 0
|
||||
var selfId = 1
|
||||
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",
|
||||
"https://t4.ftcdn.net/jpg/02/27/00/89/240_F_227008949_5O7yXuEqTwUgs3BGqdcvrNutM5MSxs1t.jpg",
|
||||
"https://t4.ftcdn.net/jpg/01/86/86/49/240_F_186864971_NixcoDg1zBjjN7soUNhpEVraI4vdzOFD.jpg",
|
||||
"https://t3.ftcdn.net/jpg/00/84/01/30/240_F_84013057_fsOdzBgskSFUyWyD6YKjIAdtKdBPiKRD.jpg",
|
||||
"https://t4.ftcdn.net/jpg/00/93/89/23/240_F_93892312_SNyGGruVaWKpJQiVG314gIQmS4EAghdy.jpg",
|
||||
"https://t3.ftcdn.net/jpg/02/94/56/58/240_F_294565895_IOqZC2OpcHGEibWF04MPEP09KZaewEl5.jpg",
|
||||
"https://t3.ftcdn.net/jpg/01/01/66/84/240_F_101668484_FopHBSMBq4t6BlvwI9awPMzUdi501sJ7.jpg",
|
||||
"https://t3.ftcdn.net/jpg/05/65/11/60/240_F_565116019_oHbZ6Hc8VYCMcZWpexXF7Z5lOWeNNYtD.jpg",
|
||||
"https://t3.ftcdn.net/jpg/03/52/21/48/240_F_352214843_dQ3JtTJrKyqrh2yd1emYCDPSrzrwqaNK.jpg",
|
||||
"https://t3.ftcdn.net/jpg/07/22/47/16/240_F_722471661_T25r329RFRxgK88S6oBJ9dUksOC2arLl.jpg",
|
||||
"https://t3.ftcdn.net/jpg/02/18/11/26/240_F_218112603_jBChzLJGuz8smPZsdFsy17wB0O0QF3Xo.jpg",
|
||||
"https://t4.ftcdn.net/jpg/04/11/49/07/240_F_411490703_KRvV0aRyxHWYVUO8bGXxuQGo2mHblYnv.jpg",
|
||||
"https://img.freepik.com/premium-photo/man-wearing-orange-helmet-white-background_466494-5539.jpg?ga=GA1.1.1334458544.1722150011&semt=sph",
|
||||
"https://img.freepik.com/premium-photo/motorcycle-vehicle-3d-modelling_274824-502.jpg?ga=GA1.1.1334458544.1722150011&semt=sph",
|
||||
"https://t3.ftcdn.net/jpg/01/68/26/06/240_F_168260687_UfaDjjs6TxcIB6BdsquSeCmYWEFmN1Sh.jpg",
|
||||
"https://t3.ftcdn.net/jpg/03/48/50/34/240_F_348503435_On7Tt5Eqn7IP9QWYTQL0H1smubU8gvLv.jpg",
|
||||
"https://t3.ftcdn.net/jpg/02/76/70/70/240_F_276707060_WpP9bwHWv0Wdqqn0pEgtSuIgXUvgkbs7.jpg",
|
||||
"https://t3.ftcdn.net/jpg/02/65/43/04/240_F_265430460_DIHqnrziar7WL2rmW0qbDO07TbxjlPQo.jpg"
|
||||
)
|
||||
var followList = emptyList<Pair<Int, Int>>()
|
||||
var likeCommentList = emptyList<Pair<Int, Int>>()
|
||||
var likeMomentList = emptyList<Pair<Int, Int>>()
|
||||
|
||||
init {
|
||||
val faker = faker {
|
||||
this.fakerConfig {
|
||||
locale = "en"
|
||||
}
|
||||
}
|
||||
accountData = (0..20).toList().mapIndexed { idx, _ ->
|
||||
AccountProfileEntity(
|
||||
id = idx,
|
||||
followerCount = 0,
|
||||
followingCount = 0,
|
||||
nickName = faker.name.name(),
|
||||
avatar = imageList.random(),
|
||||
bio = "I am a software engineer",
|
||||
country = faker.address.country(),
|
||||
isFollowing = false
|
||||
|
||||
)
|
||||
}
|
||||
// make a random follow rel
|
||||
for (i in 0..100) {
|
||||
var person1 = accountData.random()
|
||||
var persion2 = accountData.random()
|
||||
followList += Pair(person1.id, persion2.id)
|
||||
// update followerCount and followingCount
|
||||
accountData = accountData.map {
|
||||
if (it.id == person1.id) {
|
||||
it.copy(followingCount = it.followingCount + 1)
|
||||
} else if (it.id == persion2.id) {
|
||||
it.copy(followerCount = it.followerCount + 1)
|
||||
} else {
|
||||
it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
momentData = (0..60).toList().mapIndexed { idx, _ ->
|
||||
momentIdCounter += 1
|
||||
val person = accountData.random()
|
||||
// make fake comment
|
||||
val commentCount = faker.random.nextInt(0, 50)
|
||||
for (i in 0..commentCount) {
|
||||
commentIdCounter += 1
|
||||
val commentPerson = accountData.random()
|
||||
var newCommentEntity = CommentEntity(
|
||||
name = commentPerson.nickName,
|
||||
comment = "this is comment ${commentIdCounter}",
|
||||
date = Calendar.getInstance().time,
|
||||
likes = 0,
|
||||
replies = emptyList(),
|
||||
postId = momentIdCounter,
|
||||
avatar = commentPerson.avatar,
|
||||
author = commentPerson.id.toLong(),
|
||||
id = commentIdCounter,
|
||||
liked = false,
|
||||
unread = false,
|
||||
post = null
|
||||
)
|
||||
// generate like comment list
|
||||
for (likeIdx in 0..faker.random.nextInt(0, 5)) {
|
||||
val likePerson = accountData.random()
|
||||
likeCommentList += Pair(commentIdCounter, likePerson.id)
|
||||
newCommentEntity = newCommentEntity.copy(likes = newCommentEntity.likes + 1)
|
||||
}
|
||||
commentEntity += newCommentEntity
|
||||
}
|
||||
val likeCount = faker.random.nextInt(0, 5)
|
||||
for (i in 0..likeCount) {
|
||||
val likePerson = accountData.random()
|
||||
likeMomentList += Pair(momentIdCounter, likePerson.id)
|
||||
}
|
||||
MomentEntity(
|
||||
id = momentIdCounter,
|
||||
avatar = person.avatar,
|
||||
nickname = person.nickName,
|
||||
location = person.country,
|
||||
time = Date(),
|
||||
followStatus = false,
|
||||
momentTextContent = "By strongarming Ducati into giving him the factory seat.Marquez effectively …",
|
||||
momentPicture = R.drawable.default_moment_img,
|
||||
likeCount = likeCount,
|
||||
commentCount = commentCount + 1,
|
||||
shareCount = faker.random.nextInt(0, 100),
|
||||
favoriteCount = faker.random.nextInt(0, 100),
|
||||
images = imageList.shuffled().take(3).map {
|
||||
MomentImageEntity(
|
||||
id = faker.random.nextLong(),
|
||||
url = it,
|
||||
thumbnail = it
|
||||
)
|
||||
},
|
||||
authorId = person.id
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun updateMomentById(id: Int, momentEntity: MomentEntity) {
|
||||
momentData = momentData.map {
|
||||
if (it.id == id) {
|
||||
momentEntity
|
||||
} else {
|
||||
it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun saveResultToJsonFile() {
|
||||
val gson: Gson = GsonBuilder().setPrettyPrinting().create()
|
||||
|
||||
// save accountData to json file
|
||||
File("accountData.json").writeText(accountData.toString())
|
||||
// save momentData to json file
|
||||
// save comment to json file
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user