feat: 换源添加是否获取目录的配置

pull/95/head
kunfei 5 years ago
parent dd0cfe226c
commit bd175725f7
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 35
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt
  3. 24
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt
  4. 19
      app/src/main/res/menu/change_source.xml
  5. 1
      app/src/main/res/values/strings.xml

@ -30,4 +30,5 @@ object PreferKey {
const val webDavUrl = "web_dav_url" const val webDavUrl = "web_dav_url"
const val webDavAccount = "web_dav_account" const val webDavAccount = "web_dav_account"
const val webDavPassword = "web_dav_password" const val webDavPassword = "web_dav_password"
const val changeSourceLoadToc = "changeSourceLoadToc"
} }

@ -3,22 +3,28 @@ package io.legado.app.ui.changesource
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.util.DisplayMetrics
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getVerticalDivider import io.legado.app.utils.getVerticalDivider
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.putPrefBoolean
import kotlinx.android.synthetic.main.dialog_change_source.* import kotlinx.android.synthetic.main.dialog_change_source.*
class ChangeSourceDialog : DialogFragment(), class ChangeSourceDialog : DialogFragment(),
Toolbar.OnMenuItemClickListener,
ChangeSourceViewModel.CallBack, ChangeSourceViewModel.CallBack,
ChangeSourceAdapter.CallBack { ChangeSourceAdapter.CallBack {
@ -64,19 +70,13 @@ class ChangeSourceDialog : DialogFragment(),
viewModel.searchStateData.observe(viewLifecycleOwner, Observer { viewModel.searchStateData.observe(viewLifecycleOwner, Observer {
refresh_progress_bar.isAutoLoading = it refresh_progress_bar.isAutoLoading = it
}) })
arguments?.let { bundle -> tool_bar.inflateMenu(R.menu.change_source)
bundle.getString("name")?.let { tool_bar.setOnMenuItemClickListener(this)
viewModel.name = it
}
bundle.getString("author")?.let {
viewModel.author = it
}
}
tool_bar.inflateMenu(R.menu.search_view)
showTitle() showTitle()
initRecyclerView() initRecyclerView()
initMenu()
initSearchView() initSearchView()
viewModel.initData() viewModel.loadDbSearchBook()
viewModel.search() viewModel.search()
} }
@ -85,6 +85,11 @@ class ChangeSourceDialog : DialogFragment(),
tool_bar.subtitle = getString(R.string.author_show, viewModel.author) tool_bar.subtitle = getString(R.string.author_show, viewModel.author)
} }
private fun initMenu() {
tool_bar.menu.findItem(R.id.menu_load_toc)?.isChecked =
getPrefBoolean(PreferKey.changeSourceLoadToc)
}
private fun initRecyclerView() { private fun initRecyclerView() {
changeSourceAdapter = ChangeSourceAdapter(requireContext(), this) changeSourceAdapter = ChangeSourceAdapter(requireContext(), this)
recycler_view.layoutManager = LinearLayoutManager(context) recycler_view.layoutManager = LinearLayoutManager(context)
@ -115,6 +120,16 @@ class ChangeSourceDialog : DialogFragment(),
}) })
} }
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_load_toc -> {
putPrefBoolean(PreferKey.changeSourceLoadToc, !item.isChecked)
item.isChecked = !item.isChecked
}
}
return false
}
override fun changeTo(searchBook: SearchBook) { override fun changeTo(searchBook: SearchBook) {
val book = searchBook.toBook() val book = searchBook.toBook()
callBack?.oldBook?.let { oldBook -> callBack?.oldBook?.let { oldBook ->

@ -1,16 +1,19 @@
package io.legado.app.ui.changesource package io.legado.app.ui.changesource
import android.app.Application import android.app.Application
import android.os.Bundle
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.utils.getPrefBoolean
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
@ -29,7 +32,18 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
private var screenKey: String = "" private var screenKey: String = ""
private val searchBooks = linkedSetOf<SearchBook>() private val searchBooks = linkedSetOf<SearchBook>()
fun initData() { fun initData(arguments: Bundle?) {
arguments?.let { bundle ->
bundle.getString("name")?.let {
name = it
}
bundle.getString("author")?.let {
author = it
}
}
}
fun loadDbSearchBook() {
execute { execute {
App.db.searchBookDao().getByNameAuthorEnable(name, author).let { App.db.searchBookDao().getByNameAuthorEnable(name, author).let {
searchBooks.addAll(it) searchBooks.addAll(it)
@ -64,11 +78,17 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
.onSuccess(IO) { .onSuccess(IO) {
it?.forEach { searchBook -> it?.forEach { searchBook ->
if (searchBook.name == name && searchBook.author == author) { if (searchBook.name == name && searchBook.author == author) {
if (context.getPrefBoolean(PreferKey.changeSourceLoadToc)) {
if (searchBook.tocUrl.isEmpty()) { if (searchBook.tocUrl.isEmpty()) {
loadBookInfo(searchBook.toBook()) loadBookInfo(searchBook.toBook())
} else { } else {
loadChapter(searchBook.toBook()) loadChapter(searchBook.toBook())
} }
} else {
App.db.searchBookDao().insert(searchBook)
searchBooks.add(searchBook)
upAdapter()
}
return@onSuccess return@onSuccess
} }
} }
@ -122,7 +142,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
execute { execute {
screenKey = key ?: "" screenKey = key ?: ""
if (key.isNullOrEmpty()) { if (key.isNullOrEmpty()) {
initData() loadDbSearchBook()
} else { } else {
App.db.searchBookDao() App.db.searchBookDao()
} }

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_search"
android:title="@string/search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"
tools:ignore="AlwaysShowAction" />
<item
android:id="@+id/menu_load_toc"
android:title="@string/load_toc"
android:checkable="true"
app:showAsAction="never" />
</menu>

@ -554,6 +554,7 @@
<string name="enable_selection">启用所选</string> <string name="enable_selection">启用所选</string>
<string name="disable_selection">禁用所选</string> <string name="disable_selection">禁用所选</string>
<string name="export_selection">导出所选</string> <string name="export_selection">导出所选</string>
<string name="load_toc">加载目录</string>
<string name="tts">TTS</string> <string name="tts">TTS</string>
<string name="web_dav_pw_s">输入你的WebDav授权密码</string> <string name="web_dav_pw_s">输入你的WebDav授权密码</string>
<string name="web_dav_url_s">输入你的服务器地址</string> <string name="web_dav_url_s">输入你的服务器地址</string>

Loading…
Cancel
Save