优化导入,导入前可以选择需导入那些源

pull/306/head
gedoor 4 years ago
parent b7a77f99b6
commit 9203e3697e
  1. 6
      app/src/main/java/io/legado/app/lib/dialogs/AlertBuilder.kt
  2. 10
      app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt
  3. 148
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt
  4. 18
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt
  5. 12
      app/src/main/res/layout/dialog_recycler_view.xml
  6. 21
      app/src/main/res/layout/item_source_import.xml
  7. 10
      app/src/main/res/menu/import_source.xml

@ -86,6 +86,12 @@ interface AlertBuilder<out D : DialogInterface> {
onClick: (dialog: DialogInterface, which: Int, isChecked: Boolean) -> Unit
)
fun singleChoiceItems(
items: Array<String>,
checkedItem: Int = 0,
onClick: ((dialog: DialogInterface, which: Int) -> Unit)? = null
)
fun build(): D
fun show(): D
}

@ -124,6 +124,16 @@ internal class AndroidAlertBuilder(override val ctx: Context) : AlertBuilder<Ale
}
}
override fun singleChoiceItems(
items: Array<String>,
checkedItem: Int,
onClick: ((dialog: DialogInterface, which: Int) -> Unit)?
) {
builder.setSingleChoiceItems(items, checkedItem) { dialog, which ->
onClick?.invoke(dialog, which)
}
}
override fun build(): AlertDialog = builder.create()
override fun show(): AlertDialog = builder.show()

@ -1,19 +1,34 @@
package io.legado.app.ui.association
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
import io.legado.app.base.VMBaseActivity
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.constant.Theme
import io.legado.app.data.entities.BookSource
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.SourceHelp
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.okButton
import io.legado.app.utils.applyTint
import io.legado.app.utils.getViewModel
import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.activity_translucence.*
import kotlinx.android.synthetic.main.dialog_recycler_view.*
import kotlinx.android.synthetic.main.item_source_import.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.toast
class ImportBookSourceActivity : VMBaseActivity<ImportBookSourceViewModel>(
@ -32,7 +47,7 @@ class ImportBookSourceActivity : VMBaseActivity<ImportBookSourceViewModel>(
})
viewModel.successLiveData.observe(this, Observer {
rotate_loading.hide()
successDialog(it)
successDialog()
})
initData()
}
@ -78,16 +93,131 @@ class ImportBookSourceActivity : VMBaseActivity<ImportBookSourceViewModel>(
}
}
private fun successDialog(allSource: ArrayList<BookSource>) {
alert("解析结果", "${allSource.size}个书源,是否确认导入?") {
okButton {
SourceHelp.insertBookSource(*allSource.toTypedArray())
private fun successDialog() {
val bundle = Bundle()
val allSourceKey = IntentDataHelp.putData(viewModel.allSources, "source")
bundle.putString("allSourceKey", allSourceKey)
val checkStatusKey = IntentDataHelp.putData(viewModel.sourceCheckState, "check")
bundle.putString("checkStatusKey", checkStatusKey)
val selectStatusKey = IntentDataHelp.putData(viewModel.selectStatus, "select")
bundle.putString("selectStatusKey", selectStatusKey)
SourcesDialog().apply {
arguments = bundle
}.show(supportFragmentManager, "SourceDialog")
}
noButton {
class SourcesDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
lateinit var adapter: SourcesAdapter
override fun onStart() {
super.onStart()
val dm = DisplayMetrics()
activity?.windowManager?.defaultDisplay?.getMetrics(dm)
dialog?.window?.setLayout(
(dm.widthPixels * 0.9).toInt(),
ViewGroup.LayoutParams.WRAP_CONTENT
)
}
}.show().applyTint().setOnDismissListener {
finish()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.dialog_recycler_view, container)
}
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
tool_bar.title = "导入书源"
initMenu()
arguments?.let {
adapter = SourcesAdapter(requireContext())
val allSources =
IntentDataHelp.getData<ArrayList<BookSource>>(it.getString("allSourceKey"))
adapter.sourceCheckState =
IntentDataHelp.getData<ArrayList<Boolean>>(it.getString("checkStatusKey"))!!
adapter.selectStatus =
IntentDataHelp.getData<ArrayList<Boolean>>(it.getString("selectStatusKey"))!!
recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.adapter = adapter
adapter.setItems(allSources)
tv_cancel.visible()
tv_cancel.onClick {
dismiss()
}
tv_ok.visible()
tv_ok.onClick {
importSelect()
dismiss()
}
}
}
private fun initMenu() {
tool_bar.setOnMenuItemClickListener(this)
tool_bar.inflateMenu(R.menu.import_source)
}
override fun onMenuItemClick(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_select_all -> {
adapter.selectStatus.forEachIndexed { index, b ->
if (!b) {
adapter.selectStatus[index] = true
}
}
adapter.notifyDataSetChanged()
}
}
return false
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
activity?.finish()
}
private fun importSelect() {
val selectSource = arrayListOf<BookSource>()
adapter.selectStatus.forEachIndexed { index, b ->
if (b) {
selectSource.add(adapter.getItem(index)!!)
}
}
SourceHelp.insertBookSource(*selectSource.toTypedArray())
}
}
class SourcesAdapter(context: Context) :
SimpleRecyclerAdapter<BookSource>(context, R.layout.item_source_import) {
lateinit var sourceCheckState: ArrayList<Boolean>
lateinit var selectStatus: ArrayList<Boolean>
override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) {
holder.itemView.apply {
cb_source_name.isChecked = selectStatus[holder.layoutPosition]
cb_source_name.text = item.bookSourceName
tv_source_state.text = if (sourceCheckState[holder.layoutPosition]) {
"已存在"
} else {
"新书源"
}
cb_source_name.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed) {
selectStatus[holder.layoutPosition] = isChecked
}
}
}
}
override fun registerListener(holder: ItemViewHolder) {
}
}
}

@ -5,6 +5,7 @@ import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData
import com.jayway.jsonpath.JsonPath
import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.BookSource
import io.legado.app.help.http.HttpHelper
@ -20,7 +21,9 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
val errorLiveData = MutableLiveData<String>()
val successLiveData = MutableLiveData<ArrayList<BookSource>>()
private val allSources = arrayListOf<BookSource>()
val allSources = arrayListOf<BookSource>()
val sourceCheckState = arrayListOf<Boolean>()
val selectStatus = arrayListOf<Boolean>()
fun importSourceFromFilePath(path: String) {
execute {
@ -83,7 +86,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
it.printStackTrace()
errorLiveData.postValue(it.localizedMessage ?: "")
}.onSuccess {
successLiveData.postValue(allSources)
comparisonSource()
}
}
@ -102,4 +105,15 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
}
}
private fun comparisonSource() {
execute {
allSources.forEach {
val has = App.db.bookSourceDao().getBookSource(it.bookSourceUrl) != null
sourceCheckState.add(has)
selectStatus.add(!has)
}
successLiveData.postValue(allSources)
}
}
}

@ -25,7 +25,17 @@
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
<io.legado.app.ui.widget.text.AccentTextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:padding="12dp"
android:text="@string/cancel"
android:visibility="gone"
tools:ignore="RtlHardcoded" />
<io.legado.app.ui.widget.text.AccentTextView
android:id="@+id/tv_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<io.legado.app.lib.theme.view.ATECheckBox
android:id="@+id/cb_source_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1" />
<TextView
android:id="@+id/tv_source_state"
android:layout_width="60dp"
android:layout_height="wrap_content" />
</LinearLayout>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_select_all"
android:title="@string/select_all"
app:showAsAction="always" />
</menu>
Loading…
Cancel
Save