pull/1295/head
gedoor 3 years ago
parent 65d943dc91
commit 1e92e546a7
  1. 23
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt
  2. 29
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleViewModel.kt
  3. 23
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  4. 27
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt

@ -10,7 +10,6 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.SearchView
import androidx.documentfile.provider.DocumentFile
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
@ -39,7 +38,6 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.io.File
/**
* 替换规则管理
@ -79,15 +77,16 @@ class ReplaceRuleActivity : VMBaseActivity<ActivityReplaceRuleBinding, ReplaceRu
}
private val exportDir = registerForActivityResult(HandleFileContract()) { uri ->
uri ?: return@registerForActivityResult
if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(this, uri)?.let {
viewModel.exportSelection(adapter.selection, it)
}
} else {
uri.path?.let {
viewModel.exportSelection(adapter.selection, File(it))
alert(R.string.export_success) {
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editView.hint = getString(R.string.path)
editView.setText(uri.toString())
}
customView { alertBinding.root }
okButton {
sendToClip(uri.toString())
}
}.show()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
@ -231,7 +230,11 @@ class ReplaceRuleActivity : VMBaseActivity<ActivityReplaceRuleBinding, ReplaceRu
R.id.menu_disable_selection -> viewModel.disableSelection(adapter.selection)
R.id.menu_top_sel -> viewModel.topSelect(adapter.selection)
R.id.menu_bottom_sel -> viewModel.bottomSelect(adapter.selection)
R.id.menu_export_selection -> exportDir.launch(null)
R.id.menu_export_selection -> exportDir.launch {
mode = HandleFileContract.EXPORT
fileName = "exportReplaceRule.json"
file = GSON.toJson(adapter.selection).toByteArray()
}
}
return false
}

@ -2,12 +2,10 @@ package io.legado.app.ui.replace
import android.app.Application
import android.text.TextUtils
import androidx.documentfile.provider.DocumentFile
import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.utils.*
import java.io.File
import io.legado.app.utils.splitNotBlank
class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) {
@ -93,31 +91,6 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
}
}
fun exportSelection(sources: LinkedHashSet<ReplaceRule>, file: File) {
execute {
val json = GSON.toJson(sources)
FileUtils.createFileIfNotExist(file, "exportReplaceRule.json")
.writeText(json)
}.onSuccess {
context.toastOnUi("成功导出至\n${file.absolutePath}")
}.onError {
context.toastOnUi("导出失败\n${it.localizedMessage}")
}
}
fun exportSelection(sources: LinkedHashSet<ReplaceRule>, doc: DocumentFile) {
execute {
val json = GSON.toJson(sources)
doc.findFile("exportReplaceRule.json")?.delete()
doc.createFile("", "exportReplaceRule.json")
?.writeText(context, json)
}.onSuccess {
context.toastOnUi("成功导出至\n${doc.uri.path}")
}.onError {
context.toastOnUi("导出失败\n${it.localizedMessage}")
}
}
fun addGroup(group: String) {
execute {
val sources = appDb.replaceRuleDao.noGroup

@ -9,7 +9,6 @@ import android.view.SubMenu
import androidx.activity.viewModels
import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.SearchView
import androidx.documentfile.provider.DocumentFile
import androidx.recyclerview.widget.ItemTouchHelper
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
@ -35,7 +34,6 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.io.File
/**
* 订阅源管理
@ -67,15 +65,16 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
}
private val exportDir = registerForActivityResult(HandleFileContract()) { uri ->
uri ?: return@registerForActivityResult
if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(this, uri)?.let {
viewModel.exportSelection(adapter.selection, it)
}
} else {
uri.path?.let {
viewModel.exportSelection(adapter.selection, File(it))
alert(R.string.export_success) {
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editView.hint = getString(R.string.path)
editView.setText(uri.toString())
}
customView { alertBinding.root }
okButton {
sendToClip(uri.toString())
}
}.show()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
@ -126,7 +125,11 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
R.id.menu_enable_selection -> viewModel.enableSelection(adapter.selection)
R.id.menu_disable_selection -> viewModel.disableSelection(adapter.selection)
R.id.menu_del_selection -> viewModel.delSelection(adapter.selection)
R.id.menu_export_selection -> exportDir.launch(null)
R.id.menu_export_selection -> exportDir.launch {
mode = HandleFileContract.EXPORT
fileName = "exportRssSource.json"
file = GSON.toJson(adapter.selection).toByteArray()
}
R.id.menu_top_sel -> viewModel.topSource(*adapter.selection.toTypedArray())
R.id.menu_bottom_sel -> viewModel.bottomSource(*adapter.selection.toTypedArray())
}

@ -4,14 +4,12 @@ import android.app.Application
import android.content.Intent
import android.text.TextUtils
import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst
import io.legado.app.data.appDb
import io.legado.app.data.entities.RssSource
import io.legado.app.help.DefaultData
import io.legado.app.utils.*
import java.io.File
class RssSourceViewModel(application: Application) : BaseViewModel(application) {
@ -79,31 +77,6 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application)
}
}
fun exportSelection(sources: List<RssSource>, file: File) {
execute {
val json = GSON.toJson(sources)
FileUtils.createFileIfNotExist(file, "exportRssSource.json")
.writeText(json)
}.onSuccess {
context.toastOnUi("成功导出至\n${file.absolutePath}")
}.onError {
context.toastOnUi("导出失败\n${it.localizedMessage}")
}
}
fun exportSelection(sources: List<RssSource>, doc: DocumentFile) {
execute {
val json = GSON.toJson(sources)
doc.findFile("exportRssSource.json")?.delete()
doc.createFile("", "exportRssSource.json")
?.writeText(context, json)
}.onSuccess {
context.toastOnUi("成功导出至\n${doc.uri.path}")
}.onError {
context.toastOnUi("导出失败\n${it.localizedMessage}")
}
}
fun shareSelection(sources: List<RssSource>, success: ((intent: Intent) -> Unit)) {
execute {
val tmpSharePath = "${context.filesDir}/shareRssSource.json"

Loading…
Cancel
Save