pull/34/head
kunfei 5 years ago
parent ac9852f71a
commit bc83e2a430
  1. 12
      app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt
  2. 18
      app/src/main/java/io/legado/app/help/BookHelp.kt
  3. 2
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  4. 57
      app/src/main/java/io/legado/app/help/storage/Restore.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<List<RssSource>>
@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)
}

@ -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 {

@ -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)
}

@ -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<Book>(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<BookSource>(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<RssSource>(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<ReplaceRule>(json)?.let {
App.db.replaceRuleDao().insert(*it.toTypedArray())
}
} catch (e: Exception) {
e.printStackTrace()
}
}
fun importYueDuData(context: Context) {

Loading…
Cancel
Save