Skip to content

Commit

Permalink
[feature]支持通过文件导入导出数据
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Oct 4, 2023
1 parent f9ca2ef commit 37edb96
Show file tree
Hide file tree
Showing 28 changed files with 1,139 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ android {
applicationId "com.skyd.rays"
minSdk 24
targetSdk 34
versionCode 33
versionName "1.6-beta03"
versionCode 34
versionName "1.6-beta04"
flavorDimensions = ["versionName"]

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/skyd/rays/config/StickerConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ val STICKER_DIR: String = File(appContext.filesDir.path, "Sticker").path

val TEMP_STICKER_DIR: File = File(appContext.cacheDir, "TempSticker")

val IMPORT_FILES_DIR: File = File(appContext.cacheDir, "ImportFiles")
val EXPORT_FILES_DIR: File = File(appContext.cacheDir, "ExportFiles")

val refreshStickerData: MutableSharedFlow<Unit> =
MutableSharedFlow(replay = 1, extraBufferCapacity = 1)
2 changes: 1 addition & 1 deletion app/src/main/java/com/skyd/rays/di/DatabaseModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import com.skyd.rays.model.db.AppDatabase
import com.skyd.rays.model.db.dao.ApiGrantPackageDao
import com.skyd.rays.model.db.dao.SearchDomainDao
import com.skyd.rays.model.db.dao.StickerDao
import com.skyd.rays.model.db.dao.sticker.StickerDao
import com.skyd.rays.model.db.dao.TagDao
import com.skyd.rays.model.db.dao.UriStringSharePackageDao
import dagger.Module
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/skyd/rays/model/bean/ImportExportInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.skyd.rays.model.bean

import android.net.Uri
import android.os.Parcelable
import androidx.annotation.Keep
import kotlinx.parcelize.Parcelize

@Keep
sealed interface ImportExportInfo : BaseBean

@Parcelize
data class ImportExportResultInfo(
var time: Long,
var count: Int,
var backupFile: Uri,
) : ImportExportInfo, Parcelable

@Parcelize
data class ImportExportWaitingInfo(
var current: Int?,
var total: Int?,
var msg: String,
) : ImportExportInfo, Parcelable
7 changes: 7 additions & 0 deletions app/src/main/java/com/skyd/rays/model/bean/StickerWithTags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.room.Embedded
import androidx.room.Relation
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable
import java.io.File

@Parcelize
@Serializable
Expand All @@ -16,3 +17,9 @@ data class StickerWithTags(
)
val tags: List<TagBean>
) : Parcelable

@Parcelize
data class StickerWithTagsAndFile(
val stickerWithTags: StickerWithTags,
val stickerFile: File
) : Parcelable
2 changes: 1 addition & 1 deletion app/src/main/java/com/skyd/rays/model/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.skyd.rays.model.bean.TagBean
import com.skyd.rays.model.bean.UriStringSharePackageBean
import com.skyd.rays.model.db.dao.ApiGrantPackageDao
import com.skyd.rays.model.db.dao.SearchDomainDao
import com.skyd.rays.model.db.dao.StickerDao
import com.skyd.rays.model.db.dao.sticker.StickerDao
import com.skyd.rays.model.db.dao.TagDao
import com.skyd.rays.model.db.dao.UriStringSharePackageDao
import com.skyd.rays.model.db.migration.Migration1To2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.skyd.rays.model.db.dao.sticker

import android.os.Parcelable
import com.skyd.rays.R
import com.skyd.rays.appContext
import com.skyd.rays.config.STICKER_DIR
import com.skyd.rays.model.bean.StickerWithTags
import com.skyd.rays.model.db.dao.TagDao
import kotlinx.parcelize.Parcelize
import java.io.File
import java.util.UUID


@Parcelize
sealed interface HandleImportedStickerProxy : Parcelable {
fun handle(
stickerDao: StickerDao,
tagDao: TagDao,
importedStickerWithTags: StickerWithTags,
stickerFile: File,
): Boolean

fun checkStickerWithTagsFormat(stickerWithTags: StickerWithTags) {
check(stickerWithTags.sticker.stickerMd5.isNotBlank()) { "sticker's md5 is blank!" }
UUID.fromString(stickerWithTags.sticker.uuid)
}

fun moveFile(stickerFile: File) {
val destStickerFile = File(File(STICKER_DIR), stickerFile.name)
stickerFile.renameTo(destStickerFile)
}

val displayName: String

// 冲突则跳过
@Parcelize
object SkipProxy : HandleImportedStickerProxy {
override fun handle(
stickerDao: StickerDao,
tagDao: TagDao,
importedStickerWithTags: StickerWithTags,
stickerFile: File,
): Boolean {
checkStickerWithTagsFormat(importedStickerWithTags)
val stickerUuid = importedStickerWithTags.sticker.uuid
// 冲突跳过
if (stickerDao.containsByUuid(stickerUuid) != 0) {
return false
}
moveFile(stickerFile)
stickerDao.innerAddSticker(importedStickerWithTags.sticker)
importedStickerWithTags.tags.forEach {
it.stickerUuid = stickerUuid
}
tagDao.deleteTags(stickerUuid)
tagDao.addTags(importedStickerWithTags.tags)
return true
}

override val displayName: String
get() = appContext.getString(R.string.handle_imported_sticker_proxy_skip)
}

// 冲突则覆盖
@Parcelize
object ReplaceProxy : HandleImportedStickerProxy {
override fun handle(
stickerDao: StickerDao,
tagDao: TagDao,
importedStickerWithTags: StickerWithTags,
stickerFile: File,
): Boolean {
checkStickerWithTagsFormat(importedStickerWithTags)
val stickerUuid = importedStickerWithTags.sticker.uuid
moveFile(stickerFile)
stickerDao.innerAddSticker(importedStickerWithTags.sticker)
importedStickerWithTags.tags.forEach {
it.stickerUuid = stickerUuid
}
tagDao.deleteTags(stickerUuid)
tagDao.addTags(importedStickerWithTags.tags)
return true
}

override val displayName: String
get() = appContext.getString(R.string.handle_imported_sticker_proxy_replace)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.skyd.rays.model.db.dao
package com.skyd.rays.model.db.dao.sticker

import androidx.room.*
import androidx.sqlite.db.SupportSQLiteQuery
Expand All @@ -13,6 +13,8 @@ import com.skyd.rays.model.bean.StickerBean.Companion.SHARE_COUNT_COLUMN
import com.skyd.rays.model.bean.StickerBean.Companion.STICKER_MD5_COLUMN
import com.skyd.rays.model.bean.StickerBean.Companion.UUID_COLUMN
import com.skyd.rays.model.bean.StickerWithTags
import com.skyd.rays.model.bean.StickerWithTagsAndFile
import com.skyd.rays.model.db.dao.TagDao
import com.skyd.rays.model.preference.CurrentStickerUuidPreference
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -142,12 +144,33 @@ interface StickerDao {
fun innerDeleteSticker(stickerUuid: String): Int

@Transaction
fun webDavImportData(stickerWithTagsList: List<StickerWithTags>) {
fun importDataFromExternal(stickerWithTagsList: List<StickerWithTags>) {
// 原始方案就是覆盖
stickerWithTagsList.forEach {
addStickerWithTags(stickerWithTags = it, updateModifyTime = false)
}
}

@Transaction
fun importDataFromExternal(
stickerWithTagsList: List<StickerWithTagsAndFile>,
proxy: HandleImportedStickerProxy,
): Int {
val hiltEntryPoint = EntryPointAccessors
.fromApplication(appContext, StickerDaoEntryPoint::class.java)
var updatedCount = 0
stickerWithTagsList.forEach {
val updated = proxy.handle(
stickerDao = this,
tagDao = hiltEntryPoint.tagDao,
importedStickerWithTags = it.stickerWithTags,
stickerFile = it.stickerFile,
)
if (updated) updatedCount++
}
return updatedCount
}

@Transaction
@Query("DELETE FROM $STICKER_TABLE_NAME")
fun innerDeleteAllStickers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.skyd.rays.ext.dataStore
import com.skyd.rays.ext.get
import com.skyd.rays.ext.md5
import com.skyd.rays.model.bean.StickerWithTags
import com.skyd.rays.model.db.dao.StickerDao
import com.skyd.rays.model.db.dao.sticker.StickerDao
import com.skyd.rays.model.preference.StickerClassificationModelPreference
import com.skyd.rays.model.preference.ai.ClassificationThresholdPreference
import com.skyd.rays.model.preference.ai.TextRecognizeThresholdPreference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.skyd.rays.model.respository

import com.skyd.rays.base.BaseData
import com.skyd.rays.base.BaseRepository
import com.skyd.rays.model.db.dao.StickerDao
import com.skyd.rays.model.db.dao.sticker.StickerDao
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.skyd.rays.model.bean.StickerBean
import com.skyd.rays.model.bean.StickerWithTags
import com.skyd.rays.model.bean.TagBean
import com.skyd.rays.model.db.dao.SearchDomainDao
import com.skyd.rays.model.db.dao.StickerDao
import com.skyd.rays.model.db.dao.sticker.StickerDao
import com.skyd.rays.model.preference.ExportStickerDirPreference
import com.skyd.rays.model.preference.search.IntersectSearchBySpacePreference
import com.skyd.rays.model.preference.search.UseRegexSearchPreference
Expand Down
Loading

0 comments on commit 37edb96

Please sign in to comment.