diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 23f1177ef..b8179bdad 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -7,6 +7,7 @@ * 修复bug * 网络访问框架修改为RxHttp, 有bug及时反馈 * 优化进度同步 +* 换源界面添加分组选择 **2020/12/11** * 修复因修改进度同步导致的bug diff --git a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt index e2586e3e0..74d33dfeb 100644 --- a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt @@ -27,22 +27,27 @@ interface SearchBookDao { select t1.name, t1.author, t1.origin, t1.originName, t1.coverUrl, t1.bookUrl, t1.type, t1.time, t1.intro, t1.kind, t1.latestChapterTitle, t1.tocUrl, t1.variable, t1.wordCount, t2.customOrder as originOrder from searchBooks as t1 inner join book_sources as t2 on t1.origin = t2.bookSourceUrl - where t1.name = :name and t1.author = :author and t2.enabled = 1 + where t1.name = :name and t1.author = :author and t2.enabled = 1 and t2.bookSourceGroup like '%'||:sourceGroup||'%' order by t2.customOrder """ ) - fun getByNameAuthorEnable(name: String, author: String): List + fun getChangeSourceSearch(name: String, author: String, sourceGroup: String): List @Query( """ select t1.name, t1.author, t1.origin, t1.originName, t1.coverUrl, t1.bookUrl, t1.type, t1.time, t1.intro, t1.kind, t1.latestChapterTitle, t1.tocUrl, t1.variable, t1.wordCount, t2.customOrder as originOrder from searchBooks as t1 inner join book_sources as t2 on t1.origin = t2.bookSourceUrl - where t1.name = :name and t1.author = :author and originName like '%'||:key||'%' and t2.enabled = 1 + where t1.name = :name and t1.author = :author and originName like '%'||:key||'%' and t2.enabled = 1 and t2.bookSourceGroup like '%'||:sourceGroup||'%' order by t2.customOrder """ ) - fun getChangeSourceSearch(name: String, author: String, key: String): List + fun getChangeSourceSearch( + name: String, + author: String, + key: String, + sourceGroup: String + ): List @Query( """ diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt index 4a2865a54..59348ac86 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt @@ -1,29 +1,26 @@ package io.legado.app.ui.book.changesource import android.os.Bundle -import android.view.LayoutInflater -import android.view.MenuItem -import android.view.View -import android.view.ViewGroup +import android.view.* import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseDialogFragment +import io.legado.app.constant.AppPattern import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book import io.legado.app.data.entities.SearchBook import io.legado.app.databinding.DialogChangeSourceBinding import io.legado.app.help.AppConfig import io.legado.app.lib.theme.primaryColor +import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.widget.recycler.VerticalDivider -import io.legado.app.utils.applyTint -import io.legado.app.utils.getSize -import io.legado.app.utils.getViewModel -import io.legado.app.utils.putPrefBoolean +import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -47,6 +44,7 @@ class ChangeSourceDialog : BaseDialogFragment(), } private val binding by viewBinding(DialogChangeSourceBinding::bind) + private val groups = linkedSetOf() private var callBack: CallBack? = null private lateinit var viewModel: ChangeSourceViewModel lateinit var adapter: ChangeSourceAdapter @@ -90,6 +88,7 @@ class ChangeSourceDialog : BaseDialogFragment(), binding.toolBar.menu.findItem(R.id.menu_load_info)?.isChecked = AppConfig.changeSourceLoadInfo binding.toolBar.menu.findItem(R.id.menu_load_toc)?.isChecked = AppConfig.changeSourceLoadToc + } private fun initRecyclerView() { @@ -150,6 +149,13 @@ class ChangeSourceDialog : BaseDialogFragment(), adapter.setItems(it) diffResult.dispatchUpdatesTo(adapter) }) + App.db.bookSourceDao.liveGroupEnabled().observe(this, { + groups.clear() + it.map { group -> + groups.addAll(group.splitNotBlank(AppPattern.splitGroupRegex)) + } + upGroupMenu() + }) } private val stopMenuItem: MenuItem? @@ -166,6 +172,18 @@ class ChangeSourceDialog : BaseDialogFragment(), item.isChecked = !item.isChecked } R.id.menu_stop -> viewModel.stopSearch() + R.id.menu_source_manage -> startActivity() + else -> if (item?.groupId == R.id.source_group) { + if (!item.isChecked) { + item.isChecked = true + if (item.title.toString() == getString(R.string.all_source)) { + putPrefString("searchGroup", "") + } else { + putPrefString("searchGroup", item.title.toString()) + } + viewModel.stopSearch() + } + } } return false } @@ -184,6 +202,28 @@ class ChangeSourceDialog : BaseDialogFragment(), viewModel.disableSource(searchBook) } + /** + * 更新分组菜单 + */ + private fun upGroupMenu() { + val menu: Menu = binding.toolBar.menu + val selectedGroup = getPrefString("searchGroup") ?: "" + menu.removeGroup(R.id.source_group) + var item = menu.add(R.id.source_group, Menu.NONE, Menu.NONE, R.string.all_source) + if (selectedGroup == "") { + item?.isChecked = true + } + groups.sortedWith { o1, o2 -> + o1.cnCompare(o2) + }.map { + item = menu.add(R.id.source_group, Menu.NONE, Menu.NONE, it) + if (it == selectedGroup) { + item.isChecked = true + } + } + menu.setGroupCheckable(R.id.source_group, true, true) + } + interface CallBack { val oldBook: Book? fun changeTo(book: Book) diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index 20a3a159f..f52ad89d5 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -17,6 +17,7 @@ import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.model.webBook.WebBook import io.legado.app.utils.getPrefBoolean +import io.legado.app.utils.getPrefString import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.ExecutorCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher @@ -38,6 +39,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio private val searchBooks = CopyOnWriteArraySet() private var postTime = 0L private val sendRunnable = Runnable { upAdapter() } + private val searchGroup get() = App.INSTANCE.getPrefString("searchGroup") ?: "" @Volatile private var searchIndex = -1 @@ -60,7 +62,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio fun loadDbSearchBook() { execute { - App.db.searchBookDao.getByNameAuthorEnable(name, author).let { + searchBooks.clear() + upAdapter() + App.db.searchBookDao.getChangeSourceSearch(name, author, searchGroup).let { searchBooks.addAll(it) if (it.size <= 1) { upAdapter() @@ -98,7 +102,11 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio private fun startSearch() { execute { bookSourceList.clear() - bookSourceList.addAll(App.db.bookSourceDao.allEnabled) + if (searchGroup.isNullOrBlank()) { + bookSourceList.addAll(App.db.bookSourceDao.allEnabled) + } else { + bookSourceList.addAll(App.db.bookSourceDao.getEnabledByGroup(searchGroup)) + } searchStateData.postValue(true) initSearchPool() for (i in 0 until threadCount) { @@ -191,7 +199,8 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio if (key.isNullOrEmpty()) { loadDbSearchBook() } else { - val items = App.db.searchBookDao.getChangeSourceSearch(name, author, screenKey) + val items = + App.db.searchBookDao.getChangeSourceSearch(name, author, screenKey, searchGroup) searchBooks.clear() searchBooks.addAll(items) upAdapter() diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 122b9e541..dbabfd209 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -30,7 +30,6 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.startActivity -import java.text.Collator class SearchActivity : VMBaseActivity(), @@ -245,13 +244,14 @@ class SearchActivity : VMBaseActivity + o1.cnCompare(o2) + }.map { + item = menu?.add(R.id.source_group, Menu.NONE, Menu.NONE, it) + if (it == selectedGroup) { + item?.isChecked = true } + } menu?.setGroupCheckable(R.id.source_group, true, true) } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 71d3e4ce4..d82af267a 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -44,7 +44,6 @@ import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.toast import java.io.File -import java.text.Collator class BookSourceActivity : VMBaseActivity(), PopupMenu.OnMenuItemClickListener, @@ -342,10 +341,11 @@ class BookSourceActivity : VMBaseActivity + o1.cnCompare(o2) + }.map { + groupMenu?.add(R.id.source_group, Menu.NONE, Menu.NONE, it) + } } @SuppressLint("InflateParams") diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt index bc5e4e76b..0967a106e 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt @@ -22,11 +22,11 @@ import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.book.explore.ExploreShowActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity +import io.legado.app.utils.cnCompare import io.legado.app.utils.getViewModel import io.legado.app.utils.splitNotBlank import io.legado.app.utils.startActivity import io.legado.app.utils.viewbindingdelegate.viewBinding -import java.text.Collator /** * 发现界面 @@ -126,10 +126,11 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo private fun upGroupsMenu() { groupsMenu?.let { subMenu -> subMenu.removeGroup(R.id.menu_group_text) - groups.sortedWith(Collator.getInstance(java.util.Locale.CHINESE)) - .forEach { - subMenu.add(R.id.menu_group_text, Menu.NONE, Menu.NONE, it) - } + groups.sortedWith { o1, o2 -> + o1.cnCompare(o2) + }.forEach { + subMenu.add(R.id.menu_group_text, Menu.NONE, Menu.NONE, it) + } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 93430ee75..8e3c4f78b 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -40,8 +40,6 @@ import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.toast import java.io.File -import java.text.Collator -import java.util.* class RssSourceActivity : VMBaseActivity(), @@ -197,10 +195,11 @@ class RssSourceActivity : VMBaseActivity + o1.cnCompare(o2) + }.map { + groupMenu?.add(R.id.source_group, Menu.NONE, Menu.NONE, it) + } } private fun initLiveDataSource(key: String? = null) { diff --git a/app/src/main/res/menu/change_source.xml b/app/src/main/res/menu/change_source.xml index 277f93911..c4ed28e6c 100644 --- a/app/src/main/res/menu/change_source.xml +++ b/app/src/main/res/menu/change_source.xml @@ -18,6 +18,11 @@ app:showAsAction="always" tools:ignore="AlwaysShowAction" /> + +