From 37975dc7217fb9722bece70623943cfa9b51e8ce Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Sun, 18 Feb 2024 15:54:44 +0800 Subject: [PATCH] :bug: Fix realm RealmInstant type filtering --- .../kotlin/com/clipevery/utils/DateUtils.kt | 8 ++- .../com/clipevery/dao/clip/ClipRealm.kt | 58 +++++++++---------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt b/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt index 880b0a7d8..a75e267b3 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/utils/DateUtils.kt @@ -1,12 +1,16 @@ package com.clipevery.utils +import io.realm.kotlin.types.RealmInstant import java.util.Calendar object DateUtils { - fun getPrevDay(): Long { + fun getPrevDay(): RealmInstant { val calendar = Calendar.getInstance() calendar.add(Calendar.DAY_OF_MONTH, -1) - return calendar.timeInMillis + // Convert milliseconds to seconds for the epochSeconds parameter + val epochSeconds = calendar.timeInMillis / 1000 + // The nanosecondAdjustment parameter is 0 as we're not adjusting nanoseconds here + return RealmInstant.from(epochSeconds, 0) } } \ No newline at end of file diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/dao/clip/ClipRealm.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/dao/clip/ClipRealm.kt index d31d607e6..2152f7152 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/dao/clip/ClipRealm.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/dao/clip/ClipRealm.kt @@ -2,6 +2,7 @@ package com.clipevery.dao.clip import com.clipevery.utils.DateUtils import io.github.oshai.kotlinlogging.KotlinLogging +import io.realm.kotlin.MutableRealm import io.realm.kotlin.Realm import io.realm.kotlin.query.Sort import io.realm.kotlin.types.RealmObject @@ -19,46 +20,41 @@ class ClipRealm(private val realm: Realm): ClipDao { realm.writeBlocking { copyToRealm(clipData) } - - val clipDatas = realm.query(ClipData::class, "md5 == $0", clipData.md5).find() - clipDatas.query("createTime > $0", DateUtils.getPrevDay()).find() - realm.writeBlocking { - clipDatas.forEach { - doDeleteClipData(it) - } + doDeleteClipData { + query(ClipData::class, "md5 == $0", clipData.md5) + .query("createTime > $0", DateUtils.getPrevDay()).find().toList() } } override fun deleteClipData(id: ObjectId) { - realm.query(ClipData::class, "id == $0", id).first().find()?.let { - realm.writeBlocking { - doDeleteClipData(it) - } + doDeleteClipData { + query(ClipData::class, "id == $0", id).first().find()?.let { listOf(it) } ?: emptyList() } } - private fun doDeleteClipData(clipData: ClipData) { - try { - clipData.clear() - } catch (e: Exception) { - logger.error(e) { "clear id ${clipData.id} fail" } - } - - val clipAppearContent = clipData.clipAppearContent - val clipAppearItem = ClipContent.getClipItem(clipAppearContent) - val clipAppearItems = clipData.clipContent?.clipAppearItems?.mapNotNull{ anyValue -> - ClipContent.getClipItem(anyValue) - } + private fun doDeleteClipData(queryToDelete: MutableRealm.() -> List) { realm.writeBlocking { - delete(clipData) - clipAppearItem?.let { - (clipAppearItem as? RealmObject)?.let { - delete(it) + for (clipData in queryToDelete.invoke(this)) { + try { + clipData.clear() + } catch (e: Exception) { + logger.error(e) { "clear id ${clipData.id} fail" } } - } - clipAppearItems?.forEach { it -> - (it as? RealmObject)?.let { - delete(it) + val clipAppearContent = clipData.clipAppearContent + val clipAppearItem = ClipContent.getClipItem(clipAppearContent) + val clipAppearItems = clipData.clipContent?.clipAppearItems?.mapNotNull{ anyValue -> + ClipContent.getClipItem(anyValue) + } + delete(clipData) + clipAppearItem?.let { + (clipAppearItem as? RealmObject)?.let { + delete(it) + } + } + clipAppearItems?.forEach { it -> + (it as? RealmObject)?.let { + delete(it) + } } } }