Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/commonMain/kotlin/com/crosspaste/db/paste/PasteDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ class PasteDao(
)
}

fun searchBySource(source: String): List<PasteData> {
return pasteDatabaseQueries.searchBySource(source, mapper = PasteData::mapper)
.executeAsList()
}

fun searchByAllMatch(
pasteType: Long,
pasteSearchContent: String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.crosspaste.db.task

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("switchLanguage")
class SwitchLanguageInfo(
val language: String,
) : PasteTaskExtraInfo {

@SerialName("executionHistories")
override val executionHistories: MutableList<ExecutionHistory> = mutableListOf()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ object TaskType {
const val PULL_ICON_TASK = 5
const val RTF_TO_IMAGE_TASK = 6
const val CLEAN_TASK_TASK = 7
const val SWITCH_LANGUAGE_TASK = 8
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum class StandardErrorCode(code: Int, errorType: ErrorType) : ErrorCodeSupplie
NOT_FOUND_FILES_INDEX(1010, ErrorType.EXTERNAL_ERROR),
NOT_MATCH_APP_INSTANCE_ID(1011, ErrorType.EXTERNAL_ERROR),
CLEAN_TASK_FAIL(1012, ErrorType.EXTERNAL_ERROR),
SWITCH_LANGUAGE_TASK_FAIL(1013, ErrorType.EXTERNAL_ERROR),

SIGN_INVALID(2000, ErrorType.EXTERNAL_ERROR),
EXCHANGE_FAIL(2001, ErrorType.EXTERNAL_ERROR),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.crosspaste.paste

import com.crosspaste.app.AppInfo
import com.crosspaste.app.AppLaunchState
import com.crosspaste.db.paste.PasteCollection
import com.crosspaste.db.paste.PasteDao
import com.crosspaste.db.paste.PasteData
import com.crosspaste.db.paste.PasteState
import com.crosspaste.db.paste.PasteType
import com.crosspaste.i18n.GlobalCopywriter
import com.crosspaste.paste.item.PasteItem
import com.crosspaste.paste.item.TextPasteItem
import com.crosspaste.paste.item.UrlPasteItem
import com.crosspaste.utils.getCodecsUtils
import kotlinx.serialization.json.Json.Default.parseToJsonElement
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.put

abstract class GuidePasteDataService(
private val appInfo: AppInfo,
private val appLaunchState: AppLaunchState,
private val copywriter: GlobalCopywriter,
private val pasteDao: PasteDao,
) {

companion object {
const val CROSSPASTE_GUIDE = "CrossPaste Guide"
}

private val codecsUtils = getCodecsUtils()

abstract val guideKey: String

fun isFirstLaunch(): Boolean {
return appLaunchState.firstLaunch
}

fun saveData() {
for (i in 5 downTo 0) {
val pasteData = getGuidePasteData(i)
pasteDao.createPasteData(pasteData)
}
}

private fun buildJson(index: Int): String {
return buildJsonObject {
put("guideIndex", index)
}.toString()
}

private fun getGuideIndexFromJson(json: String): Int? {
val jsonObject = parseToJsonElement(json).jsonObject
return jsonObject["guideIndex"]?.jsonPrimitive?.int
}

fun updateData() {
val pasteDataList = pasteDao.searchBySource(CROSSPASTE_GUIDE)
if (pasteDataList.isNotEmpty()) {
pasteDataList.forEach { pasteData ->
pasteData.pasteAppearItem?.let { pasteItem ->
pasteItem.extraInfo?.let { extraInfo ->
getGuideIndexFromJson(extraInfo)?.let { index ->
pasteDao.updatePasteAppearItem(
id = pasteData.id,
pasteItem = getGuidePasteItem(index),
)
}
}
}
}
}
}

private fun getGuidePasteItem(index: Int): PasteItem {
val extraInfo = buildJson(index)

return if (index == 5) {
val githubUrl = "https://github.com/CrossPaste/crosspaste-desktop"
val githubUrlBytes = githubUrl.encodeToByteArray()
val githubUrlHash = codecsUtils.hash(githubUrlBytes)
val githubUrlSize = githubUrlBytes.size.toLong()
UrlPasteItem(
identifiers = listOf(),
url = githubUrl,
size = githubUrlSize,
hash = githubUrlHash,
extraInfo = extraInfo,
)
} else {
val text = copywriter.getText("$guideKey$index").replace("\\n", "\n")
val byteArray = text.encodeToByteArray()
val size = byteArray.size.toLong()
val hash = codecsUtils.hash(byteArray)
TextPasteItem(
identifiers = listOf(),
text = text,
size = size,
hash = hash,
extraInfo = extraInfo,
)
}
}

private fun getGuidePasteData(index: Int): PasteData {
return if (index == 5) {
val githubUrl = "https://github.com/CrossPaste/crosspaste-desktop"
val githubUrlBytes = githubUrl.encodeToByteArray()
val githubUrlHash = codecsUtils.hash(githubUrlBytes)
val githubUrlSize = githubUrlBytes.size.toLong()
PasteData(
appInstanceId = appInfo.appInstanceId,
favorite = false,
pasteAppearItem = getGuidePasteItem(index),
pasteCollection = PasteCollection(listOf()),
pasteType = PasteType.URL_TYPE.type,
source = CROSSPASTE_GUIDE,
size = githubUrlSize,
hash = githubUrlHash,
pasteState = PasteState.LOADED,
)
} else {
val text = copywriter.getText("$guideKey$index").replace("\\n", "\n")
val byteArray = text.encodeToByteArray()
val size = byteArray.size.toLong()
val hash = codecsUtils.hash(byteArray)

PasteData(
appInstanceId = appInfo.appInstanceId,
favorite = false,
pasteAppearItem = getGuidePasteItem(index),
pasteCollection = PasteCollection(listOf()),
pasteType = PasteType.TEXT_TYPE.type,
source = CROSSPASTE_GUIDE,
size = size,
hash = hash,
pasteState = PasteState.LOADED,
)
}
}

fun initData() {
if (isFirstLaunch()) {
if (pasteDao.getSize(allOrFavorite = true) == 0L) {
saveData()
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ class ColorPasteItem(
override val hash: String,
override val size: Long,
override val color: Long,
override val extraInfo: String? = null,
) : PasteItem, PasteColor {

constructor(jsonObject: JsonObject) : this(
identifiers = jsonObject["identifiers"]!!.jsonPrimitive.content.split(","),
hash = jsonObject["hash"]!!.jsonPrimitive.content,
size = jsonObject["size"]!!.jsonPrimitive.long,
color = jsonObject["color"]!!.jsonPrimitive.content.toLong(),
extraInfo = jsonObject["extraInfo"]?.jsonPrimitive?.content,
)

override fun getPasteType(): PasteType {
Expand Down Expand Up @@ -62,6 +64,7 @@ class ColorPasteItem(
put("hash", hash)
put("size", size)
put("color", color)
extraInfo?.let { put("extraInfo", it) }
}.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ data class FilesPasteItem(
override val basePath: String? = null,
override val fileInfoTreeMap: Map<String, FileInfoTree>,
override val relativePathList: List<String>,
override val extraInfo: String? = null,
) : PasteItem, PasteFiles {

companion object {
Expand Down Expand Up @@ -61,6 +62,7 @@ data class FilesPasteItem(
it.jsonPrimitive.content
},
fileInfoTreeMap = fileInfoTreeMap,
extraInfo = jsonObject["extraInfo"]?.jsonPrimitive?.content,
)

override fun getAppFileType(): AppFileType {
Expand Down Expand Up @@ -109,6 +111,7 @@ data class FilesPasteItem(
basePath = basePath,
relativePathList = newRelativePathList,
fileInfoTreeMap = fileInfoTreeMap,
extraInfo = extraInfo,
)
}

Expand Down Expand Up @@ -153,6 +156,7 @@ data class FilesPasteItem(
basePath?.let { put("basePath", it) }
put("relativePathList", jsonUtils.JSON.encodeToJsonElement(relativePathList))
put("fileInfoTreeMap", jsonUtils.JSON.encodeToJsonElement(fileInfoTreeMap))
extraInfo?.let { put("extraInfo", it) }
}.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class HtmlPasteItem(
override val basePath: String? = null,
override val relativePath: String,
override val html: String,
override val extraInfo: String? = null,
) : PasteItem, PasteHtml {

companion object {
Expand All @@ -46,6 +47,7 @@ class HtmlPasteItem(
basePath = jsonObject["basePath"]?.jsonPrimitive?.content,
relativePath = jsonObject["relativePath"]!!.jsonPrimitive.content,
html = jsonObject["html"]!!.jsonPrimitive.content,
extraInfo = jsonObject["extraInfo"]?.jsonPrimitive?.content,
)

override fun getHtmlImagePath(userDataPathProvider: UserDataPathProvider): Path {
Expand All @@ -65,6 +67,7 @@ class HtmlPasteItem(
fileName = "html2Image.png",
),
html = html,
extraInfo = extraInfo,
)
}

Expand Down Expand Up @@ -132,6 +135,7 @@ class HtmlPasteItem(
basePath?.let { put("basePath", it) }
put("relativePath", relativePath)
put("html", html)
extraInfo?.let { put("extraInfo", it) }
}.toString()
}
}
Loading