feat: 优化代码

pull/123/head
kunfei 5 years ago
parent 909f31a5f2
commit a7b574af4d
  1. 8
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  2. 38
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  3. 21
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt
  4. 25
      app/src/main/java/io/legado/app/utils/UriExtensions.kt

@ -50,7 +50,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
get() = getViewModel(BookSourceViewModel::class.java)
private val importRecordKey = "bookSourceRecordKey"
private val qrRequestCode = 101
private val importSource = 132
private val importRequestCode = 132
private val exportRequestCode = 65
private lateinit var adapter: BookSourceAdapter
private var bookSourceLiveDate: LiveData<List<BookSource>>? = null
@ -84,7 +84,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
R.id.menu_group_manage ->
GroupManageDialog().show(supportFragmentManager, "groupManage")
R.id.menu_import_source_local -> FilePicker
.selectFile(this, importSource, "text/*", arrayOf("txt", "json"))
.selectFile(this, importRequestCode, "text/*", arrayOf("txt", "json"))
R.id.menu_import_source_onLine -> showImportDialog()
}
if (item.groupId == R.id.source_group) {
@ -278,7 +278,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
adapter.getSelection(),
File(currentPath)
)
importSource -> {
importRequestCode -> {
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show()
viewModel.importSourceFromFilePath(currentPath) { msg ->
title_bar.snackbar(msg)
@ -298,7 +298,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
}
}
}
importSource -> if (resultCode == Activity.RESULT_OK) {
importRequestCode -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
try {
uri.readText(this)?.let {

@ -9,6 +9,7 @@ import android.view.MenuItem
import android.view.SubMenu
import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.SearchView
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil
@ -37,6 +38,7 @@ import kotlinx.android.synthetic.main.view_search.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.toast
import java.io.File
class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_rss_source),
@ -48,7 +50,8 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
get() = getViewModel(RssSourceViewModel::class.java)
private val importRecordKey = "rssSourceRecordKey"
private val qrRequestCode = 101
private val importSource = 124
private val importRequestCode = 124
private val exportRequestCode = 65
private lateinit var adapter: RssSourceAdapter
private var sourceLiveData: LiveData<List<RssSource>>? = null
private var groups = hashSetOf<String>()
@ -78,7 +81,7 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
when (item.itemId) {
R.id.menu_add -> startActivity<RssSourceEditActivity>()
R.id.menu_import_source_local -> FilePicker
.selectFile(this, importSource, "text/*", arrayOf("txt", "json"))
.selectFile(this, importRequestCode, "text/*", arrayOf("txt", "json"))
R.id.menu_import_source_onLine -> showImportDialog()
R.id.menu_import_source_qr -> startActivityForResult<QrCodeActivity>(qrRequestCode)
R.id.menu_group_manage -> GroupManageDialog()
@ -95,7 +98,7 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
R.id.menu_enable_selection -> viewModel.enableSelection(adapter.getSelection())
R.id.menu_disable_selection -> viewModel.disableSelection(adapter.getSelection())
R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelection())
R.id.menu_export_selection -> viewModel.exportSelection(adapter.getSelection())
R.id.menu_export_selection -> FilePicker.selectFolder(this, exportRequestCode)
R.id.menu_check_source -> {
}
}
@ -248,18 +251,24 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
if (requestCode == importSource) {
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show()
viewModel.importSourceFromFilePath(currentPath) { msg ->
title_bar.snackbar(msg)
when (requestCode) {
importRequestCode -> {
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show()
viewModel.importSourceFromFilePath(currentPath) { msg ->
title_bar.snackbar(msg)
}
}
exportRequestCode -> viewModel.exportSelection(
adapter.getSelection(),
File(currentPath)
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
importSource -> if (resultCode == Activity.RESULT_OK) {
importRequestCode -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
try {
uri.readText(this)?.let {
@ -282,6 +291,19 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
}
}
}
exportRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.toString().isContentPath()) {
DocumentFile.fromTreeUri(this, uri)?.let {
viewModel.exportSelection(adapter.getSelection(), it)
}
} else {
uri.path?.let {
viewModel.exportSelection(adapter.getSelection(), File(it))
}
}
}
}
}
}

@ -2,6 +2,7 @@ package io.legado.app.ui.rss.source.manage
import android.app.Application
import android.text.TextUtils
import androidx.documentfile.provider.DocumentFile
import com.jayway.jsonpath.JsonPath
import io.legado.app.App
import io.legado.app.R
@ -67,12 +68,24 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
}
}
fun exportSelection(sources: LinkedHashSet<RssSource>) {
fun exportSelection(sources: LinkedHashSet<RssSource>, file: File) {
execute {
val json = GSON.toJson(sources)
val file =
FileUtils.createFileIfNotExist(Backup.exportPath + File.separator + "exportRssSource.json")
file.writeText(json)
FileUtils.createFileIfNotExist(file, "exportRssSource.json")
.writeText(json)
}.onSuccess {
context.toast("成功导出至\n${Backup.exportPath}")
}.onError {
context.toast("导出失败\n${it.localizedMessage}")
}
}
fun exportSelection(sources: LinkedHashSet<RssSource>, doc: DocumentFile) {
execute {
val json = GSON.toJson(sources)
doc.findFile("exportBookSource.json")?.delete()
doc.createFile("", "exportBookSource.json")
?.writeText(context, json)
}.onSuccess {
context.toast("成功导出至\n${Backup.exportPath}")
}.onError {

@ -19,19 +19,17 @@ fun Uri.readBytes(context: Context): ByteArray? {
@Throws(Exception::class)
fun Uri.readText(context: Context): String? {
if (this.toString().isContentPath()) {
return DocumentUtils.readText(context, this)
} else {
val path = RealPathUtil.getPath(context, this)
if (path?.isNotEmpty() == true) {
return File(path).readText()
}
readBytes(context)?.let {
return String(it)
}
return null
}
@Throws(Exception::class)
fun Uri.writeBytes(context: Context, byteArray: ByteArray): Boolean {
fun Uri.writeBytes(
context: Context,
byteArray: ByteArray
): Boolean {
if (this.toString().isContentPath()) {
return DocumentUtils.writeBytes(context, byteArray, this)
} else {
@ -46,14 +44,5 @@ fun Uri.writeBytes(context: Context, byteArray: ByteArray): Boolean {
@Throws(Exception::class)
fun Uri.writeText(context: Context, text: String): Boolean {
if (this.toString().isContentPath()) {
return DocumentUtils.writeText(context, text, this)
} else {
val path = RealPathUtil.getPath(context, this)
if (path?.isNotEmpty() == true) {
File(path).writeText(text)
return true
}
}
return false
return writeBytes(context, text.toByteArray())
}
Loading…
Cancel
Save