Skip to content

Commit

Permalink
添加全局数据导出与导入功能
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzz-yyhyy committed Oct 6, 2024
1 parent 8321175 commit 00b70fd
Show file tree
Hide file tree
Showing 66 changed files with 2,519 additions and 454 deletions.
4 changes: 2 additions & 2 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/dictionaries/NightFish.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import indi.dmzz_yyhyy.lightnovelreader.data.book.BookVolumes
import indi.dmzz_yyhyy.lightnovelreader.data.book.ChapterContent
import indi.dmzz_yyhyy.lightnovelreader.data.book.UserReadingData
import indi.dmzz_yyhyy.lightnovelreader.data.bookshelf.BookshelfRepository
import indi.dmzz_yyhyy.lightnovelreader.data.json.AppUserDataContent
import indi.dmzz_yyhyy.lightnovelreader.data.json.BookUserData
import indi.dmzz_yyhyy.lightnovelreader.data.local.LocalBookDataSource
import indi.dmzz_yyhyy.lightnovelreader.data.web.WebBookDataSource
import indi.dmzz_yyhyy.lightnovelreader.data.work.CacheBookWork
Expand Down Expand Up @@ -92,6 +94,9 @@ class BookRepository @Inject constructor(
fun getUserReadingData(bookId: Int): Flow<UserReadingData> =
localBookDataSource.getUserReadingData(bookId).map { it }

fun getAllUserReadingData(): List<UserReadingData> =
localBookDataSource.getAllUserReadingData()

fun updateUserReadingData(id: Int, update: (UserReadingData) -> UserReadingData) {
localBookDataSource.updateUserReadingData(id, update)
}
Expand All @@ -111,4 +116,23 @@ class BookRepository @Inject constructor(
}

fun isCacheBookWorkFlow(workId: UUID) = workManager.getWorkInfoByIdFlow(workId)

fun importUserReadingData(data: AppUserDataContent): Boolean {
val userReadingDataList: List<BookUserData> = data.bookUserData ?: return false
userReadingDataList.forEach { bookUserData ->
localBookDataSource.updateUserReadingData(bookUserData.id) {
UserReadingData(
id = bookUserData.id,
lastReadTime = if (bookUserData.lastReadTime.isAfter(it.lastReadTime)) bookUserData.lastReadTime else it.lastReadTime,
totalReadTime = if (bookUserData.totalReadTime > it.totalReadTime) bookUserData.totalReadTime else it.totalReadTime,
readingProgress = if (bookUserData.readingProgress > it.readingProgress) bookUserData.readingProgress else it.readingProgress,
lastReadChapterId = bookUserData.lastReadChapterId,
lastReadChapterTitle = bookUserData.lastReadChapterTitle,
lastReadChapterProgress = bookUserData.lastReadChapterProgress,
readCompletedChapterIds = (bookUserData.readCompletedChapterIds + it.readCompletedChapterIds).distinct()
)
}
}
return true
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package indi.dmzz_yyhyy.lightnovelreader.data

import indi.dmzz_yyhyy.lightnovelreader.data.json.AppUserDataContent
import indi.dmzz_yyhyy.lightnovelreader.data.local.room.dao.UserDataDao
import indi.dmzz_yyhyy.lightnovelreader.data.userdata.BooleanUserData
import indi.dmzz_yyhyy.lightnovelreader.data.userdata.FloatUserData
Expand All @@ -18,4 +19,17 @@ class UserDataRepository @Inject constructor(
fun booleanUserData(path: String): BooleanUserData = BooleanUserData(path, userDataDao)
fun intListUserData(path: String): IntListUserData = IntListUserData(path, userDataDao)
fun stringListUserData(path: String): StringListUserData = StringListUserData(path, userDataDao)

fun importUserData(data: AppUserDataContent): Boolean {
val userDataList = data.userData ?: return false
userDataList.forEach {
userDataDao.update(
path = it.path,
group = it.group,
type = it.type,
value = it.value
)
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import androidx.work.OneTimeWorkRequest
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.workDataOf
import com.google.gson.JsonSyntaxException
import indi.dmzz_yyhyy.lightnovelreader.data.book.BookInformation
import indi.dmzz_yyhyy.lightnovelreader.data.json.AppUserDataJson
import indi.dmzz_yyhyy.lightnovelreader.data.json.AppUserDataContent
import indi.dmzz_yyhyy.lightnovelreader.data.json.AppUserDataJsonBuilder
import indi.dmzz_yyhyy.lightnovelreader.data.json.toJsonData
import indi.dmzz_yyhyy.lightnovelreader.data.loacltion.room.converter.LocalDataTimeConverter.dateToString
Expand Down Expand Up @@ -264,37 +263,27 @@ class BookshelfRepository @Inject constructor(
return workRequest
}

fun importBookshelfFromJsonData(stringJson: String): Boolean {
try {
val appUserDataJson = AppUserDataJson.fromJson(stringJson)
val data =
appUserDataJson.data.firstOrNull { it.webDataSourceId == webBookDataSource.id }
?: return false
val bookshelfDataList = data.bookshelf ?: return false
val bookshelfBookMetadataList = data.bookShelfBookMetadata ?: return false
val allBookshelfIds = getAllBookshelfIds()
bookshelfDataList.forEach { bookshelf ->
if (allBookshelfIds.contains(bookshelf.id)) return@forEach
bookshelfDao.createBookshelf(
BookshelfEntity(
id = bookshelf.id,
name = bookshelf.name,
sortType = bookshelf.sortType.key,
autoCache = bookshelf.autoCache,
systemUpdateReminder = bookshelf.systemUpdateReminder,
allBookIds = bookshelf.allBookIds,
pinnedBookIds = bookshelf.pinnedBookIds,
updatedBookIds = bookshelf.updatedBookIds,
)
fun importBookshelf(data: AppUserDataContent): Boolean {
val bookshelfDataList = data.bookshelf ?: return false
val bookshelfBookMetadataList = data.bookShelfBookMetadata ?: return false
val allBookshelfIds = getAllBookshelfIds()
bookshelfDataList.forEach { bookshelf ->
if (allBookshelfIds.contains(bookshelf.id)) return@forEach
bookshelfDao.createBookshelf(
BookshelfEntity(
id = bookshelf.id,
name = bookshelf.name,
sortType = bookshelf.sortType.key,
autoCache = bookshelf.autoCache,
systemUpdateReminder = bookshelf.systemUpdateReminder,
allBookIds = bookshelf.allBookIds,
pinnedBookIds = bookshelf.pinnedBookIds,
updatedBookIds = bookshelf.updatedBookIds,
)
}
bookshelfBookMetadataList.forEach {
bookshelfDao.addBookshelfMetadata(it.id, it.lastUpdate, it.bookShelfIds)
}
)
}
catch (e: JsonSyntaxException) {
e.printStackTrace()
return false
bookshelfBookMetadataList.forEach {
bookshelfDao.addBookshelfMetadata(it.id, it.lastUpdate, it.bookShelfIds)
}
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import indi.dmzz_yyhyy.lightnovelreader.data.bookshelf.BookshelfSortType
import java.time.LocalDateTime

data class AppUserDataJson(
val type: String,
val id: Int? = null,
val data: List<AppUserDataContent>
) {
companion object {
val gson: Gson = GsonBuilder()
.serializeSpecialFloatingPointValues()
.registerTypeAdapter(LocalDateTime::class.java, LocalTimeDataTypeAdapter)
.registerTypeAdapter(BookshelfSortType::class.java, BookshelfSortTypeTypeAdapter)
.create()
Expand All @@ -35,11 +37,12 @@ data class AppUserDataContent(
val userData: List<UserDataData>? = null,
)

class AppUserDataJsonBuilder() {
class AppUserDataJsonBuilder {
private var id: Int? = null
private var data: MutableList<AppUserDataContent> = mutableListOf()

fun build(): AppUserDataJson = AppUserDataJson(
type = "light novel reader data file",
id = id,
data = data
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package indi.dmzz_yyhyy.lightnovelreader.data.json

import com.google.gson.annotations.SerializedName
import indi.dmzz_yyhyy.lightnovelreader.data.book.UserReadingData
import java.time.LocalDateTime

data class BookUserData(
Expand All @@ -19,4 +20,16 @@ data class BookUserData(
val lastReadChapterProgress: Float,
@SerializedName("read_completed_chapter_ids")
val readCompletedChapterIds: List<Int>,
)
)

fun UserReadingData.toJsonData() =
BookUserData(
id = this.id,
lastReadTime = this.lastReadTime,
totalReadTime = this.totalReadTime,
readingProgress = this.readingProgress,
lastReadChapterId = this.lastReadChapterId,
lastReadChapterTitle = this.lastReadChapterTitle,
lastReadChapterProgress = this.lastReadChapterProgress,
readCompletedChapterIds = this.readCompletedChapterIds,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package indi.dmzz_yyhyy.lightnovelreader.data.json

import indi.dmzz_yyhyy.lightnovelreader.data.local.room.entity.UserDataEntity

data class UserDataData(
val path: String,
val group: String,
val type: String,
val value: String
)
)

fun UserDataEntity.toJsonData() =
UserDataData(
path = this.path,
group = this.group,
type = this.type,
value = this.value
)
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,17 @@ class LocalBookDataSource @Inject constructor(
return@let data
})
}
fun getAllUserReadingData(): List<UserReadingData> =
userReadingDataDao.getAll().map {
UserReadingData(
it.id,
it.lastReadTime,
it.totalReadTime,
it.readingProgress,
it.lastReadChapterId,
it.lastReadChapterTitle,
it.lastReadChapterProgress,
it.readCompletedChapterIds
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package indi.dmzz_yyhyy.lightnovelreader.data.local.room.dao

import androidx.room.Dao
import androidx.room.Query
import indi.dmzz_yyhyy.lightnovelreader.data.local.room.entity.UserDataEntity
import kotlinx.coroutines.flow.Flow

@Dao
Expand All @@ -13,4 +14,8 @@ interface UserDataDao {
fun get(path: String): String?
@Query("select value from user_data where path = :path")
fun getFlow(path: String): Flow<String?>
@Query("select * from user_data where path = :path")
fun getEntity(path: String): UserDataEntity?
@Query("select * from user_data where `group` = :group")
fun getGroupValues(group: String): List<UserDataEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ sealed class UserDataPath(
data object DynamicColors : UserDataPath("dynamic_color", Display)
data object AppLocale : UserDataPath("app_locale", Display)
}
data object Data: UserDataPath("data", Settings) {
data object WebDataSourceId : UserDataPath("web_data_source_id", Data)
}
data object Reader : UserDataPath("reader", Settings) {
data object FontSize : LinkUserData(Reader.FontSize)
data object FontLineHeight : LinkUserData(Reader.FontLineHeight)
data object KeepScreenOn : LinkUserData(Reader.KeepScreenOn)
}
}
data object Bookshelf : UserDataPath("bookshelf") {
}
}

open class LinkUserData(
Expand Down
Loading

0 comments on commit 00b70fd

Please sign in to comment.