Files
rider-pro-android-app/app/src/main/java/com/aiosman/riderpro/test/TestDatabase.kt

148 lines
6.8 KiB
Kotlin
Raw Normal View History

2024-07-29 00:01:09 +08:00
package com.aiosman.riderpro.test
import com.aiosman.riderpro.R
import com.aiosman.riderpro.data.AccountProfile
2024-07-29 16:50:07 +08:00
import com.aiosman.riderpro.data.Comment
2024-07-29 00:01:09 +08:00
import com.aiosman.riderpro.model.MomentItem
2024-07-29 16:50:07 +08:00
import com.google.gson.Gson
import com.google.gson.GsonBuilder
2024-07-29 00:01:09 +08:00
import io.github.serpro69.kfaker.faker
2024-07-29 16:50:07 +08:00
import java.io.File
2024-07-29 00:01:09 +08:00
object TestDatabase {
var momentData = emptyList<MomentItem>()
var accountData = emptyList<AccountProfile>()
2024-07-29 16:50:07 +08:00
var comment = emptyList<Comment>()
var commentIdCounter = 0
var selfId = 1
2024-07-29 00:01:09 +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",
"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>>()
2024-07-29 16:50:07 +08:00
var likeCommentList = emptyList<Pair<Int, Int>>()
2024-07-30 14:28:13 +08:00
var likeMomentList = emptyList<Pair<Int, Int>>()
2024-07-29 00:01:09 +08:00
init {
val faker = faker {
this.fakerConfig {
locale = "en"
}
}
2024-07-30 14:28:13 +08:00
accountData = (0..20).toList().mapIndexed { idx, _ ->
2024-07-29 00:01:09 +08:00
AccountProfile(
id = idx,
followerCount = 0,
followingCount = 0,
nickName = faker.name.name(),
avatar = imageList.random(),
bio = "I am a software engineer",
country = faker.address.country()
)
}
// make a random follow rel
2024-07-30 14:28:13 +08:00
for (i in 0..100) {
2024-07-29 00:01:09 +08:00
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
}
}
}
2024-07-30 14:28:13 +08:00
momentData = (0..60).toList().mapIndexed { idx, _ ->
2024-07-29 00:01:09 +08:00
val person = accountData.random()
2024-07-29 16:50:07 +08:00
// make fake comment
2024-07-30 14:28:13 +08:00
val commentCount = faker.random.nextInt(0, 50)
for (i in 0..commentCount) {
2024-07-29 16:50:07 +08:00
commentIdCounter += 1
val commentPerson = accountData.random()
2024-07-30 14:28:13 +08:00
var newComment = Comment(
2024-07-29 16:50:07 +08:00
name = commentPerson.nickName,
comment = "this is comment ${commentIdCounter}",
date = "2023-02-02 11:23",
likes = 0,
replies = emptyList(),
postId = idx,
avatar = commentPerson.avatar,
author = commentPerson.id,
id = commentIdCounter,
liked = false
)
// generate like comment list
for (likeIdx in 0..faker.random.nextInt(0, 5)) {
2024-07-30 14:28:13 +08:00
val likePerson = accountData.random()
2024-07-29 16:50:07 +08:00
likeCommentList += Pair(commentIdCounter, likePerson.id)
newComment = newComment.copy(likes = newComment.likes + 1)
}
comment += newComment
}
2024-07-30 14:28:13 +08:00
val likeCount = faker.random.nextInt(0, 5)
for (i in 0..likeCount) {
val likePerson = accountData.random()
likeMomentList += Pair(idx, likePerson.id)
}
2024-07-29 00:01:09 +08:00
MomentItem(
id = idx,
avatar = person.avatar,
nickname = person.nickName,
location = person.country,
time = "2023.02.02 11:23",
followStatus = false,
momentTextContent = "By strongarming Ducati into giving him the factory seat.Marquez effectively …",
momentPicture = R.drawable.default_moment_img,
2024-07-30 14:28:13 +08:00
likeCount = likeCount,
commentCount = commentCount + 1,
2024-07-29 00:01:09 +08:00
shareCount = faker.random.nextInt(0, 100),
favoriteCount = faker.random.nextInt(0, 100),
images = imageList.shuffled().take(3),
authorId = person.id
)
}
}
2024-07-29 16:50:07 +08:00
fun updateMomentById(id: Int, momentItem: MomentItem) {
momentData = momentData.map {
if (it.id == id) {
momentItem
} 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
}
2024-07-29 00:01:09 +08:00
}