Skip to content

Commit

Permalink
📝 Define the Clipboard Collection and Clip Item concepts and modify t…
Browse files Browse the repository at this point in the history
…he code to match the naming
  • Loading branch information
guiyanakuang committed Jun 24, 2024
1 parent b619613 commit 0cbc886
Show file tree
Hide file tree
Showing 34 changed files with 192 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.clipevery.clip

import com.clipevery.dao.clip.ClipAppearItem
import com.clipevery.dao.clip.ClipItem
import io.realm.kotlin.MutableRealm

interface ClipPlugin {

fun pluginProcess(
clipAppearItems: List<ClipAppearItem>,
clipItems: List<ClipItem>,
realm: MutableRealm,
): List<ClipAppearItem>
): List<ClipItem>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ import kotlinx.serialization.Serializable
import java.io.IOException

@Serializable
@SerialName("content")
class ClipContent : RealmObject {
@SerialName("collection")
class ClipCollection : RealmObject {

@Serializable(with = RealmAnyRealmListSerializer::class)
var clipAppearItems: RealmList<RealmAny?> = realmListOf()
var clipItems: RealmList<RealmAny?> = realmListOf()

@Throws(IOException::class)
fun clear(
realm: MutableRealm,
clearResource: Boolean = true,
) {
val iterator = clipAppearItems.iterator()
val iterator = clipItems.iterator()
while (iterator.hasNext()) {
val clipAppearItem = iterator.next()
val clipItem = iterator.next()
iterator.remove()
getClipItem(clipAppearItem)?.clear(realm, clearResource)
getClipItem(clipItem)?.clear(realm, clearResource)
}
realm.delete(this)
}

companion object {
fun getClipItem(anyValue: RealmAny?): ClipAppearItem? {
fun getClipItem(anyValue: RealmAny?): ClipItem? {
return anyValue?.let {
return when (it.type) {
RealmAny.Type.OBJECT -> {
val asRealmObject = anyValue.asRealmObject<RealmObject>()
if (asRealmObject is ClipAppearItem) {
if (asRealmObject is ClipItem) {
asRealmObject
} else {
null
Expand Down
20 changes: 10 additions & 10 deletions composeApp/src/commonMain/kotlin/com/clipevery/dao/clip/ClipData.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.clipevery.dao.clip

import com.clipevery.clip.item.ClipFiles
import com.clipevery.dao.clip.ClipContent.Companion.getClipItem
import com.clipevery.dao.clip.ClipCollection.Companion.getClipItem
import com.clipevery.serializer.ClipDataSerializer
import com.clipevery.serializer.ClipLabelRealmSetSerializer
import io.realm.kotlin.MutableRealm
Expand Down Expand Up @@ -30,8 +30,8 @@ class ClipData : RealmObject {

@Index
var clipId: Long = 0
var clipAppearContent: RealmAny? = null
var clipContent: ClipContent? = null
var clipAppearItem: RealmAny? = null
var clipCollection: ClipCollection? = null

@Index
var clipType: Int = ClipType.INVALID
Expand Down Expand Up @@ -68,24 +68,24 @@ class ClipData : RealmObject {
realm: MutableRealm,
clearResource: Boolean = true,
) {
getClipItem(clipAppearContent)?.clear(realm, clearResource)
clipContent?.clear(realm, clearResource)
getClipItem(clipAppearItem)?.clear(realm, clearResource)
clipCollection?.clear(realm, clearResource)
realm.delete(this)
}

fun getClipDataSortObject(): ClipDataSortObject {
return ClipDataSortObject(createTime, clipId, appInstanceId)
}

fun getClipAppearItems(): List<ClipAppearItem> {
val appearItem: ClipAppearItem? = getClipItem(this.clipAppearContent)
fun getClipAppearItems(): List<ClipItem> {
val appearItem: ClipItem? = getClipItem(this.clipAppearItem)

val otherAppearItems: List<ClipAppearItem>? =
this.clipContent?.clipAppearItems?.mapNotNull {
val otherAppearItems: List<ClipItem>? =
this.clipCollection?.clipItems?.mapNotNull {
getClipItem(it)
}

val mutableList: MutableList<ClipAppearItem> = mutableListOf()
val mutableList: MutableList<ClipItem> = mutableListOf()

appearItem?.let {
mutableList.add(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.realm.kotlin.MutableRealm
import org.mongodb.kbson.ObjectId
import java.awt.datatransfer.DataFlavor

interface ClipAppearItem {
interface ClipItem {

var id: ObjectId

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.clipevery.serializer

import com.clipevery.clip.item.ClipInit
import com.clipevery.dao.clip.ClipContent
import com.clipevery.dao.clip.ClipCollection
import com.clipevery.dao.clip.ClipData
import com.clipevery.dao.clip.ClipLabel
import com.clipevery.dao.clip.ClipState
Expand All @@ -27,8 +27,8 @@ object ClipDataSerializer : KSerializer<ClipData> {
element<String>("id")
element<String>("appInstanceId")
element<Long>("clipId")
element<RealmAny?>("clipAppearContent")
element<ClipContent?>("clipContent")
element<RealmAny?>("clipAppearItem")
element<ClipCollection?>("clipCollection")
element<Int>("clipType")
element<String?>("source")
element<Long>("size")
Expand All @@ -44,7 +44,7 @@ object ClipDataSerializer : KSerializer<ClipData> {
var appInstanceId = ""
var clipId = 0L
var clipAppearContent: RealmAny? = null
var clipContent: ClipContent? = null
var clipCollection: ClipCollection? = null
var clipType = 0
var source: String? = null
var size = 0L
Expand All @@ -57,7 +57,7 @@ object ClipDataSerializer : KSerializer<ClipData> {
1 -> appInstanceId = dec.decodeStringElement(descriptor, index)
2 -> clipId = dec.decodeLongElement(descriptor, index)
3 -> clipAppearContent = dec.decodeSerializableElement(descriptor, index, RealmAnyKSerializer)
4 -> clipContent = dec.decodeSerializableElement(descriptor, index, ClipContent.serializer())
4 -> clipCollection = dec.decodeSerializableElement(descriptor, index, ClipCollection.serializer())
5 -> clipType = dec.decodeIntElement(descriptor, index)
6 -> source = dec.decodeNullableSerializableElement(descriptor, index, String.serializer())
7 -> size = dec.decodeLongElement(descriptor, index)
Expand All @@ -73,11 +73,11 @@ object ClipDataSerializer : KSerializer<ClipData> {
this.id = BsonObjectId(id)
this.appInstanceId = appInstanceId
this.clipId = clipId
this.clipAppearContent = clipAppearContent
this.clipContent = clipContent
this.clipAppearItem = clipAppearContent
this.clipCollection = clipCollection
this.clipType = clipType
this.source = source
this.clipSearchContent = ClipContent.getClipItem(clipAppearContent)?.getSearchContent()
this.clipSearchContent = ClipCollection.getClipItem(clipAppearContent)?.getSearchContent()
this.md5 = md5
this.size = size
this.createTime = RealmInstant.now()
Expand All @@ -104,11 +104,11 @@ object ClipDataSerializer : KSerializer<ClipData> {
compositeOutput.encodeStringElement(descriptor, 1, value.appInstanceId)

compositeOutput.encodeLongElement(descriptor, 2, value.clipId)
value.clipAppearContent?.let {
value.clipAppearItem?.let {
compositeOutput.encodeSerializableElement(descriptor, 3, RealmAnyKSerializer, it)
}
value.clipContent?.let {
compositeOutput.encodeSerializableElement(descriptor, 4, ClipContent.serializer(), it)
value.clipCollection?.let {
compositeOutput.encodeSerializableElement(descriptor, 4, ClipCollection.serializer(), it)
}
compositeOutput.encodeIntElement(descriptor, 5, value.clipType)
compositeOutput.encodeNullableSerializableElement(descriptor, 6, String.serializer(), value.source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import com.clipevery.LocalKoinApplication
import com.clipevery.clip.item.ClipFiles
import com.clipevery.dao.clip.ClipAppearItem
import com.clipevery.dao.clip.ClipData
import com.clipevery.dao.clip.ClipItem
import com.clipevery.i18n.GlobalCopywriter
import com.clipevery.ui.base.AsyncView
import com.clipevery.ui.base.ClipIconButton
Expand Down Expand Up @@ -62,7 +62,7 @@ fun ClipFilesDetailView(
val density = LocalDensity.current
val copywriter = current.koin.get<GlobalCopywriter>()

val clipAppearItem = clipFiles as ClipAppearItem
val clipItem = clipFiles as ClipItem

val dateUtils = getDateUtils()
val fileUtils = getFileUtils()
Expand Down Expand Up @@ -222,7 +222,7 @@ fun ClipFilesDetailView(
listOf(
ClipDetailInfoItem("File_Name", "${filePath.fileName}"),
ClipDetailInfoItem("Type", copywriter.getText("File")),
ClipDetailInfoItem("Size", fileUtils.formatBytes(clipAppearItem.size)),
ClipDetailInfoItem("Size", fileUtils.formatBytes(clipItem.size)),
ClipDetailInfoItem("Remote", copywriter.getText(if (clipData.remote) "Yes" else "No")),
ClipDetailInfoItem(
"Date",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.clipevery.LocalKoinApplication
import com.clipevery.clip.item.ClipText
import com.clipevery.dao.clip.ClipAppearItem
import com.clipevery.dao.clip.ClipData
import com.clipevery.dao.clip.ClipItem
import com.clipevery.i18n.GlobalCopywriter
import com.clipevery.utils.getDateUtils
import com.clipevery.utils.getFileUtils
Expand All @@ -33,7 +33,7 @@ fun ClipTextDetailView(
val dateUtils = getDateUtils()
val fileUtils = getFileUtils()
val text = clipText.text
val clipAppearItem = clipText as ClipAppearItem
val clipItem = clipText as ClipItem

ClipDetailView(
detailView = {
Expand All @@ -60,7 +60,7 @@ fun ClipTextDetailView(
items =
listOf(
ClipDetailInfoItem("Type", copywriter.getText("Text")),
ClipDetailInfoItem("Size", fileUtils.formatBytes(clipAppearItem.size)),
ClipDetailInfoItem("Size", fileUtils.formatBytes(clipItem.size)),
ClipDetailInfoItem("Remote", copywriter.getText(if (clipData.remote) "Yes" else "No")),
ClipDetailInfoItem(
"Date",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.clipevery.LocalKoinApplication
import com.clipevery.clip.item.ClipUrl
import com.clipevery.dao.clip.ClipAppearItem
import com.clipevery.dao.clip.ClipData
import com.clipevery.dao.clip.ClipItem
import com.clipevery.i18n.GlobalCopywriter
import com.clipevery.ui.base.UISupport
import com.clipevery.utils.getDateUtils
Expand All @@ -34,7 +34,7 @@ fun ClipUrlDetailView(
val dateUtils = getDateUtils()
val fileUtils = getFileUtils()
val url = clipUrl.url
val clipAppearItem = clipUrl as ClipAppearItem
val clipItem = clipUrl as ClipItem

ClipDetailView(
detailView = {
Expand Down Expand Up @@ -65,7 +65,7 @@ fun ClipUrlDetailView(
items =
listOf(
ClipDetailInfoItem("Type", copywriter.getText("Link")),
ClipDetailInfoItem("Size", fileUtils.formatBytes(clipAppearItem.size)),
ClipDetailInfoItem("Size", fileUtils.formatBytes(clipItem.size)),
ClipDetailInfoItem("Remote", copywriter.getText(if (clipData.remote) "Yes" else "No")),
ClipDetailInfoItem(
"Date",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.sp
import com.clipevery.LocalKoinApplication
import com.clipevery.clip.item.ClipHtml
import com.clipevery.dao.clip.ClipAppearItem
import com.clipevery.dao.clip.ClipData
import com.clipevery.dao.clip.ClipItem
import com.clipevery.i18n.GlobalCopywriter
import com.clipevery.ui.base.AsyncView
import com.clipevery.ui.base.LoadImageData
Expand All @@ -45,7 +45,7 @@ fun HtmlToImageDetailView(
val density = LocalDensity.current
val copywriter = current.koin.get<GlobalCopywriter>()
val uiSupport = current.koin.get<UISupport>()
val clipAppearItem = clipHtml as ClipAppearItem
val clipItem = clipHtml as ClipItem

val dateUtils = getDateUtils()
val fileUtils = getFileUtils()
Expand Down Expand Up @@ -115,7 +115,7 @@ fun HtmlToImageDetailView(
items =
listOf(
ClipDetailInfoItem("Type", copywriter.getText("Html")),
ClipDetailInfoItem("Size", fileUtils.formatBytes(clipAppearItem.size)),
ClipDetailInfoItem("Size", fileUtils.formatBytes(clipItem.size)),
ClipDetailInfoItem("Remote", copywriter.getText(if (clipData.remote) "Yes" else "No")),
ClipDetailInfoItem(
"Date",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.clipevery.dao.clip.ClipAppearItem
import com.clipevery.dao.clip.ClipContent
import com.clipevery.dao.clip.ClipCollection
import com.clipevery.dao.clip.ClipData
import com.clipevery.dao.clip.ClipItem
import com.clipevery.dao.clip.ClipState
import com.clipevery.dao.clip.ClipType
import com.clipevery.i18n.Copywriter
Expand All @@ -39,7 +39,7 @@ import kotlin.reflect.KClass
import kotlin.reflect.cast

fun <T : Any> ClipData.getClipItem(kclass: KClass<T>): T? {
return ClipContent.getClipItem(this.clipAppearContent)?.let {
return ClipCollection.getClipItem(this.clipAppearItem)?.let {
if (kclass.isInstance(it)) {
kclass.cast(it)
} else {
Expand All @@ -48,8 +48,8 @@ fun <T : Any> ClipData.getClipItem(kclass: KClass<T>): T? {
}
}

fun ClipData.getClipItem(): ClipAppearItem? {
return ClipContent.getClipItem(this.clipAppearContent)
fun ClipData.getClipItem(): ClipItem? {
return ClipCollection.getClipItem(this.clipAppearItem)
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.clipevery.clip

import com.clipevery.app.AppInfo
import com.clipevery.dao.clip.ClipAppearItem
import com.clipevery.dao.clip.ClipContent
import com.clipevery.dao.clip.ClipCollection
import com.clipevery.dao.clip.ClipDao
import com.clipevery.dao.clip.ClipData
import com.clipevery.dao.clip.ClipItem
import com.clipevery.dao.clip.ClipState
import com.clipevery.dao.clip.ClipType
import com.clipevery.utils.LoggerExtension.logSuspendExecutionTime
Expand All @@ -26,7 +26,7 @@ class ClipCollector(

private val logger = KotlinLogging.logger {}

private val preCollectors: Array<MutableMap<KClass<out ClipItemService>, ClipAppearItem>> = Array(itemCount) { mutableMapOf() }
private val preCollectors: Array<MutableMap<KClass<out ClipItemService>, ClipItem>> = Array(itemCount) { mutableMapOf() }

private val updateCollectors: Array<MutableSet<KClass<out ClipItemService>>> = Array(itemCount) { mutableSetOf() }

Expand All @@ -51,15 +51,15 @@ class ClipCollector(
fun preCollectItem(
itemIndex: Int,
kclass: KClass<out ClipItemService>,
clipItem: ClipAppearItem,
clipItem: ClipItem,
) {
preCollectors[itemIndex][kclass] = clipItem
}

fun updateCollectItem(
itemIndex: Int,
kclass: KClass<out ClipItemService>,
update: (ClipAppearItem, MutableRealm) -> Unit,
update: (ClipItem, MutableRealm) -> Unit,
) {
preCollectors[itemIndex][kclass]?.let {
updateCollectors[itemIndex].add(kclass)
Expand Down Expand Up @@ -90,17 +90,17 @@ class ClipCollector(
if (collector.isEmpty()) {
return@logSuspendExecutionTime null
}
val clipAppearItems: List<ClipAppearItem> = preCollectors.flatMap { it.values }
val clipItems: List<ClipItem> = preCollectors.flatMap { it.values }

val clipContent =
ClipContent().apply {
this.clipAppearItems = clipAppearItems.map { RealmAny.create(it as RealmObject) }.toRealmList()
val clipCollection =
ClipCollection().apply {
this.clipItems = clipItems.map { RealmAny.create(it as RealmObject) }.toRealmList()
}

val clipData =
ClipData().apply {
this.clipId = clipId
this.clipContent = clipContent
this.clipCollection = clipCollection
this.clipType = ClipType.INVALID
this.source = source
this.md5 = ""
Expand Down
Loading

0 comments on commit 0cbc886

Please sign in to comment.