From bc83e2a4306423b474fd23bb0205b47caff04287 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 15 Sep 2019 19:56:42 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/RssSourceDao.kt | 12 +++- .../main/java/io/legado/app/help/BookHelp.kt | 18 +----- .../java/io/legado/app/help/storage/Backup.kt | 2 +- .../io/legado/app/help/storage/Restore.kt | 57 +++++++++++++++++-- 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt index 0d3e6e575..999898598 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt @@ -1,8 +1,7 @@ package io.legado.app.data.dao import androidx.lifecycle.LiveData -import androidx.room.Dao -import androidx.room.Query +import androidx.room.* import io.legado.app.data.entities.RssSource @Dao @@ -16,4 +15,13 @@ interface RssSourceDao { @Query("SELECT * FROM rssSources where enabled = 1") fun liveEnabled(): LiveData> + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(vararg rssSource: RssSource) + + @Update + fun update(vararg rssSource: RssSource) + + @Query("delete from rssSources where sourceUrl = :sourceUrl") + fun delete(sourceUrl: String) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index d32e4d077..dd68c455b 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -7,10 +7,7 @@ import io.legado.app.data.entities.ReplaceRule import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefString import io.legado.app.utils.similarity -import java.io.BufferedWriter import java.io.File -import java.io.FileWriter -import java.io.IOException import kotlin.math.max import kotlin.math.min @@ -29,21 +26,10 @@ object BookHelp { } fun saveContent(book: Book, bookChapter: BookChapter, content: String) { - if (content.isEmpty()) { - return - } + if (content.isEmpty()) return val filePath = getChapterPath(book, bookChapter) val file = FileHelp.getFile(filePath) - //获取流并存储 - try { - BufferedWriter(FileWriter(file)).use { writer -> - writer.write(content) - writer.flush() - } - } catch (e: IOException) { - e.printStackTrace() - } - + file.writeText(content) } fun hasContent(book: Book, bookChapter: BookChapter): Boolean { diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index fe89fb3e1..ac381ec52 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -56,7 +56,7 @@ object Backup { private fun backupRssSource(path: String) { val json = GSON.toJson(App.db.rssSourceDao().all) - val file = FileHelp.getFile(path + File.separator + "cssSource.json") + val file = FileHelp.getFile(path + File.separator + "rssSource.json") file.writeText(json) } diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 335dd2497..43437a46b 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -2,14 +2,20 @@ package io.legado.app.help.storage import android.content.Context import android.util.Log +import androidx.appcompat.app.AppCompatActivity import com.jayway.jsonpath.Configuration import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.Option import io.legado.app.App +import io.legado.app.R import io.legado.app.constant.AppConst import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.ReplaceRule +import io.legado.app.data.entities.RssSource +import io.legado.app.help.FileHelp +import io.legado.app.help.permission.Permissions +import io.legado.app.help.permission.PermissionsCompat import io.legado.app.utils.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast @@ -23,12 +29,55 @@ object Restore { .build() ) - fun restore() { - + fun restore(activity: AppCompatActivity) { + PermissionsCompat.Builder(activity) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + val path = + FileUtils.getSdCardPath() + File.separator + "YueDu" + File.separator + "legadoBackUp" + restore(path) + } + .request() } - fun importFromGithub() { - + fun restore(path: String) { + try { + val file = FileHelp.getFile(path + File.separator + "bookshelf.json") + val json = file.readText() + GSON.fromJsonArray(json)?.let { + App.db.bookDao().insert(*it.toTypedArray()) + } + } catch (e: Exception) { + e.printStackTrace() + } + try { + val file = FileHelp.getFile(path + File.separator + "bookSource.json") + val json = file.readText() + GSON.fromJsonArray(json)?.let { + App.db.bookSourceDao().insert(*it.toTypedArray()) + } + } catch (e: Exception) { + e.printStackTrace() + } + try { + val file = FileHelp.getFile(path + File.separator + "rssSource.json") + val json = file.readText() + GSON.fromJsonArray(json)?.let { + App.db.rssSourceDao().insert(*it.toTypedArray()) + } + } catch (e: Exception) { + e.printStackTrace() + } + try { + val file = FileHelp.getFile(path + File.separator + "replaceRule.json") + val json = file.readText() + GSON.fromJsonArray(json)?.let { + App.db.replaceRuleDao().insert(*it.toTypedArray()) + } + } catch (e: Exception) { + e.printStackTrace() + } } fun importYueDuData(context: Context) {