添加更新数据
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.aiosman.riderpro.data
|
||||
|
||||
import com.aiosman.riderpro.test.TestDatabase
|
||||
|
||||
data class AccountProfile(
|
||||
val id: Int,
|
||||
val followerCount: Int,
|
||||
@@ -11,19 +13,16 @@ data class AccountProfile(
|
||||
)
|
||||
|
||||
interface AccountService {
|
||||
suspend fun getAccountProfile(): AccountProfile
|
||||
suspend fun getMyAccountProfile(): AccountProfile
|
||||
suspend fun getAccountProfileById(id: Int): AccountProfile
|
||||
}
|
||||
|
||||
class TestAccountServiceImpl : AccountService {
|
||||
override suspend fun getAccountProfile(): AccountProfile {
|
||||
return AccountProfile(
|
||||
id = 1,
|
||||
followerCount = 100,
|
||||
followingCount = 200,
|
||||
nickName = "Aiosman",
|
||||
avatar = "https://img.freepik.com/free-photo/white-billboard-template_23-2147726635.jpg?t=st=1722150015~exp=1722153615~hmac=5540620196d7898215d822be26353c87a63d51bbfb2b814e032626e1948a1583&w=740",
|
||||
bio = "I am a software engineer",
|
||||
country = "Nigeria"
|
||||
)
|
||||
override suspend fun getMyAccountProfile(): AccountProfile {
|
||||
return TestDatabase.accountData.first { it.id == 0 }
|
||||
}
|
||||
|
||||
override suspend fun getAccountProfileById(id: Int): AccountProfile {
|
||||
return TestDatabase.accountData.first { it.id == id }
|
||||
}
|
||||
}
|
||||
@@ -2,28 +2,43 @@ package com.aiosman.riderpro.data
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.paging.PagingState
|
||||
import com.aiosman.riderpro.test.TestDatabase
|
||||
import java.io.IOException
|
||||
import java.util.Calendar
|
||||
import kotlin.math.min
|
||||
import kotlin.random.Random
|
||||
|
||||
interface CommentService {
|
||||
suspend fun getComments(pageNumber: Int, postId: Int? = null): ListContainer<Comment>
|
||||
suspend fun createComment(postId: Int, content: String, authorId: Int): Comment
|
||||
suspend fun likeComment(commentId: Int)
|
||||
}
|
||||
|
||||
|
||||
data class Comment(
|
||||
val id: Int,
|
||||
val name: String,
|
||||
val comment: String,
|
||||
val date: String,
|
||||
val likes: Int,
|
||||
val replies: List<Comment>
|
||||
val replies: List<Comment>,
|
||||
val postId: Int = 0,
|
||||
val avatar: String,
|
||||
val author: Int,
|
||||
val liked: Boolean,
|
||||
)
|
||||
|
||||
class CommentPagingSource(
|
||||
private val remoteDataSource: CommentRemoteDataSource,
|
||||
private val postId: Int? = null
|
||||
) : PagingSource<Int, Comment>() {
|
||||
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Comment> {
|
||||
return try {
|
||||
val currentPage = params.key ?: 1
|
||||
val comments = remoteDataSource.getComments(
|
||||
pageNumber = currentPage
|
||||
pageNumber = currentPage,
|
||||
postId = postId
|
||||
)
|
||||
|
||||
LoadResult.Page(
|
||||
data = comments.list,
|
||||
prevKey = if (currentPage == 1) null else currentPage - 1,
|
||||
@@ -43,52 +58,70 @@ class CommentPagingSource(
|
||||
class CommentRemoteDataSource(
|
||||
private val commentService: CommentService,
|
||||
) {
|
||||
suspend fun getComments(pageNumber: Int): ListContainer<Comment> {
|
||||
return commentService.getComments(pageNumber)
|
||||
suspend fun getComments(pageNumber: Int, postId: Int?): ListContainer<Comment> {
|
||||
return commentService.getComments(pageNumber, postId)
|
||||
}
|
||||
}
|
||||
|
||||
interface CommentService {
|
||||
suspend fun getComments(pageNumber: Int): ListContainer<Comment>
|
||||
}
|
||||
|
||||
class TestCommentServiceImpl : CommentService {
|
||||
private val mockData = generateMockComments(100)
|
||||
override suspend fun getComments(pageNumber: Int): ListContainer<Comment> {
|
||||
val from = pageNumber * DataBatchSize
|
||||
val to = (pageNumber + 1) * DataBatchSize
|
||||
val currentSublist = mockData.subList(from, to)
|
||||
override suspend fun getComments(pageNumber: Int, postId: Int?): ListContainer<Comment> {
|
||||
var rawList = TestDatabase.comment
|
||||
if (postId != null) {
|
||||
rawList = rawList.filter { it.postId == postId }
|
||||
}
|
||||
val from = (pageNumber - 1) * DataBatchSize
|
||||
val to = (pageNumber) * DataBatchSize
|
||||
rawList = rawList.sortedBy { -it.id }
|
||||
if (from >= rawList.size) {
|
||||
return ListContainer(
|
||||
total = rawList.size,
|
||||
page = pageNumber,
|
||||
pageSize = DataBatchSize,
|
||||
list = emptyList()
|
||||
)
|
||||
}
|
||||
rawList = rawList.sortedBy { -it.id }
|
||||
val currentSublist = rawList.subList(from, min(to, rawList.size))
|
||||
return ListContainer(
|
||||
total = mockData.size,
|
||||
total = rawList.size,
|
||||
page = pageNumber,
|
||||
pageSize = DataBatchSize,
|
||||
list = currentSublist
|
||||
)
|
||||
}
|
||||
|
||||
private fun generateMockComments(count: Int): List<Comment> {
|
||||
return (0 until count).map {
|
||||
Comment(
|
||||
name = "User $it",
|
||||
comment = "This is comment $it",
|
||||
date = "2023-02-02 11:23",
|
||||
likes = Random.nextInt(0, 100),
|
||||
replies = generateMockReplies()
|
||||
)
|
||||
override suspend fun createComment(postId: Int, content: String, authorId: Int): Comment {
|
||||
var author = TestDatabase.accountData.find { it.id == authorId }
|
||||
if (author == null) {
|
||||
author = TestDatabase.accountData.random()
|
||||
}
|
||||
TestDatabase.commentIdCounter += 1
|
||||
val newComment = Comment(
|
||||
name = author.nickName,
|
||||
comment = content,
|
||||
date = Calendar.getInstance().time.toString(),
|
||||
likes = 0,
|
||||
replies = emptyList(),
|
||||
postId = postId,
|
||||
avatar = author.avatar,
|
||||
author = author.id,
|
||||
id = TestDatabase.commentIdCounter,
|
||||
liked = false
|
||||
)
|
||||
TestDatabase.comment += newComment
|
||||
return newComment
|
||||
}
|
||||
|
||||
private fun generateMockReplies(): List<Comment> {
|
||||
val replyCount = Random.nextInt(0, 6)
|
||||
return (0 until replyCount).map {
|
||||
Comment(
|
||||
name = "Reply User $it",
|
||||
comment = "This is reply $it",
|
||||
date = "2023-02-02 11:23",
|
||||
likes = Random.nextInt(0, 100),
|
||||
replies = emptyList()
|
||||
)
|
||||
override suspend fun likeComment(commentId: Int) {
|
||||
TestDatabase.comment = TestDatabase.comment.map {
|
||||
if (it.id == commentId) {
|
||||
it.copy(likes = it.likes + 1)
|
||||
} else {
|
||||
it
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -2,19 +2,34 @@ package com.aiosman.riderpro.data
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.paging.PagingState
|
||||
import com.aiosman.riderpro.R
|
||||
import com.aiosman.riderpro.model.MomentItem
|
||||
import com.aiosman.riderpro.test.TestDatabase
|
||||
import java.io.IOException
|
||||
import kotlin.math.min
|
||||
|
||||
interface MomentService {
|
||||
suspend fun getMomentById(id: Int): MomentItem
|
||||
suspend fun likeMoment(id: Int)
|
||||
suspend fun getMoments(
|
||||
pageNumber: Int,
|
||||
author: Int? = null,
|
||||
timelineId: Int? = null
|
||||
): ListContainer<MomentItem>
|
||||
}
|
||||
|
||||
|
||||
class MomentPagingSource(
|
||||
private val remoteDataSource: MomentRemoteDataSource,
|
||||
private val author: Int? = null,
|
||||
private val timelineId: Int? = null
|
||||
) : 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
|
||||
pageNumber = currentPage,
|
||||
author = author,
|
||||
timelineId = timelineId
|
||||
)
|
||||
|
||||
LoadResult.Page(
|
||||
@@ -36,63 +51,88 @@ class MomentPagingSource(
|
||||
class MomentRemoteDataSource(
|
||||
private val momentService: MomentService,
|
||||
) {
|
||||
suspend fun getMoments(pageNumber: Int): ListContainer<MomentItem> {
|
||||
return momentService.getMoments(pageNumber)
|
||||
suspend fun getMoments(
|
||||
pageNumber: Int,
|
||||
author: Int?,
|
||||
timelineId: Int?
|
||||
): ListContainer<MomentItem> {
|
||||
return momentService.getMoments(pageNumber, author, timelineId)
|
||||
}
|
||||
}
|
||||
|
||||
interface MomentService {
|
||||
suspend fun getMoments(pageNumber: Int): ListContainer<MomentItem>
|
||||
suspend fun getMomentById(id: Int): MomentItem
|
||||
suspend fun likeMoment(id: Int)
|
||||
}
|
||||
|
||||
class TestMomentServiceImpl() : MomentService {
|
||||
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"
|
||||
)
|
||||
var mockData = TestDatabase.momentData
|
||||
val testMomentBackend = TestMomentBackend(mockData)
|
||||
override suspend fun getMoments(pageNumber: Int): ListContainer<MomentItem> {
|
||||
return testMomentBackend.fetchMomentItems(pageNumber)
|
||||
val testMomentBackend = TestMomentBackend()
|
||||
|
||||
override suspend fun getMoments(
|
||||
pageNumber: Int,
|
||||
author: Int?,
|
||||
timelineId: Int?
|
||||
): ListContainer<MomentItem> {
|
||||
return testMomentBackend.fetchMomentItems(pageNumber, author, timelineId)
|
||||
}
|
||||
|
||||
override suspend fun getMomentById(id: Int): MomentItem {
|
||||
return mockData[id]
|
||||
return testMomentBackend.getMomentById(id)
|
||||
}
|
||||
|
||||
|
||||
override suspend fun likeMoment(id: Int) {
|
||||
// mockData = mockData.map {
|
||||
// if (it.id == id) {
|
||||
// it.copy(likeCount = it.likeCount + 1)
|
||||
// } else {
|
||||
// it
|
||||
// }
|
||||
// }
|
||||
// mockData
|
||||
testMomentBackend.likeMoment(id)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
suspend fun fetchMomentItems(
|
||||
pageNumber: Int,
|
||||
author: Int? = null,
|
||||
timelineId: Int?
|
||||
): ListContainer<MomentItem> {
|
||||
var rawList = TestDatabase.momentData
|
||||
if (author != null) {
|
||||
rawList = rawList.filter { it.authorId == author }
|
||||
}
|
||||
if (timelineId != null) {
|
||||
val followIdList = TestDatabase.followList.filter {
|
||||
it.first == timelineId
|
||||
}.map { it.second }
|
||||
rawList = rawList.filter { it.authorId in followIdList }
|
||||
}
|
||||
val from = (pageNumber - 1) * DataBatchSize
|
||||
val to = (pageNumber) * DataBatchSize
|
||||
if (from >= rawList.size) {
|
||||
return ListContainer(
|
||||
total = rawList.size,
|
||||
page = pageNumber,
|
||||
pageSize = DataBatchSize,
|
||||
list = emptyList()
|
||||
)
|
||||
}
|
||||
val currentSublist = rawList.subList(from, min(to, rawList.size))
|
||||
// delay
|
||||
kotlinx.coroutines.delay(loadDelay)
|
||||
return ListContainer(
|
||||
total = mockData.size,
|
||||
total = rawList.size,
|
||||
page = pageNumber,
|
||||
pageSize = DataBatchSize,
|
||||
list = currentSublist
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun getMomentById(id: Int): MomentItem {
|
||||
return TestDatabase.momentData[id]
|
||||
}
|
||||
|
||||
suspend fun likeMoment(id: Int) {
|
||||
val oldMoment = TestDatabase.momentData.first {
|
||||
it.id == id
|
||||
}
|
||||
val newMoment = oldMoment.copy(likeCount = oldMoment.likeCount + 1)
|
||||
TestDatabase.updateMomentById(id, newMoment)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user