diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index c91e01648..3dfaf21fa 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -21,9 +21,6 @@ interface BookDao { @Query("SELECT bookUrl FROM books WHERE origin = '${BookType.local}'") fun observeLocalUri(): LiveData> - @Query("SELECT * FROM books WHERE origin <> '${BookType.local}' and type = 0") - fun observeDownload(): LiveData> - @Query("SELECT * FROM books WHERE (`group` & :group) > 0") fun observeByGroup(group: Int): LiveData> diff --git a/app/src/main/java/io/legado/app/ui/book/download/DownloadActivity.kt b/app/src/main/java/io/legado/app/ui/book/download/DownloadActivity.kt index b0f860527..6c8d63c26 100644 --- a/app/src/main/java/io/legado/app/ui/book/download/DownloadActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/download/DownloadActivity.kt @@ -12,10 +12,12 @@ import com.google.android.material.snackbar.Snackbar import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.AppConst import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter +import io.legado.app.data.entities.BookGroup import io.legado.app.help.BookHelp import io.legado.app.service.help.Download import io.legado.app.ui.filechooser.FileChooserDialog @@ -35,24 +37,44 @@ class DownloadActivity : VMBaseActivity(R.layout.activity_dow private val exportRequestCode = 32 private val exportBookPathKey = "exportBookPath" lateinit var adapter: DownloadAdapter - private var bookshelfLiveData: LiveData>? = null + private var groupLiveData: LiveData>? = null + private var booksLiveData: LiveData>? = null private var menu: Menu? = null private var exportPosition = -1 + private val groupList: ArrayList = arrayListOf() + private var groupId: Int = -1 override val viewModel: DownloadViewModel get() = getViewModel(DownloadViewModel::class.java) override fun onActivityCreated(savedInstanceState: Bundle?) { + groupId = intent.getIntExtra("groupId", -1) + title_bar.subtitle = intent.getStringExtra("groupName") ?: getString(R.string.all) initRecyclerView() - initLiveData() + initGroupData() + initBookData() } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.download, menu) - this.menu = menu return super.onCompatCreateOptionsMenu(menu) } + override fun onPrepareOptionsMenu(menu: Menu?): Boolean { + this.menu = menu + upMenu() + return super.onPrepareOptionsMenu(menu) + } + + private fun upMenu() { + menu?.findItem(R.id.menu_book_group)?.subMenu?.let { subMenu -> + subMenu.removeGroup(R.id.menu_group) + groupList.forEach { bookGroup -> + subMenu.add(R.id.menu_group, bookGroup.groupId, Menu.NONE, bookGroup.groupName) + } + } + } + override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_download -> launch(IO) { @@ -69,6 +91,21 @@ class DownloadActivity : VMBaseActivity(R.layout.activity_dow Download.stop(this@DownloadActivity) } } + R.id.menu_no_group -> { + title_bar.subtitle = getString(R.string.no_group) + groupId = AppConst.bookGroupNone.groupId + initBookData() + } + R.id.menu_all -> { + title_bar.subtitle = item.title + groupId = AppConst.bookGroupAll.groupId + initBookData() + } + else -> if (item.groupId == R.id.menu_group) { + title_bar.subtitle = item.title + groupId = item.itemId + initBookData() + } } return super.onCompatOptionsItemSelected(item) } @@ -79,21 +116,39 @@ class DownloadActivity : VMBaseActivity(R.layout.activity_dow recycler_view.adapter = adapter } - private fun initLiveData() { - bookshelfLiveData?.removeObservers(this) - bookshelfLiveData = App.db.bookDao().observeDownload() - bookshelfLiveData?.observe(this, Observer { list -> + private fun initBookData() { + booksLiveData?.removeObservers(this) + booksLiveData = when (groupId) { + AppConst.bookGroupAll.groupId -> App.db.bookDao().observeAll() + AppConst.bookGroupNone.groupId -> App.db.bookDao().observeNoGroup() + else -> App.db.bookDao().observeByGroup(groupId) + } + booksLiveData?.observe(this, Observer { list -> + val booksDownload = list.filter { + !it.isLocalBook() && it.isTxt() + } val books = when (getPrefInt(PreferKey.bookshelfSort)) { - 1 -> list.sortedByDescending { it.latestChapterTime } - 2 -> list.sortedBy { it.name } - 3 -> list.sortedBy { it.order } - else -> list.sortedByDescending { it.durChapterTime } + 1 -> booksDownload.sortedByDescending { it.latestChapterTime } + 2 -> booksDownload.sortedBy { it.name } + 3 -> booksDownload.sortedBy { it.order } + else -> booksDownload.sortedByDescending { it.durChapterTime } } adapter.setItems(books) initCacheSize(books) }) } + private fun initGroupData() { + groupLiveData?.removeObservers(this) + groupLiveData = App.db.bookGroupDao().liveDataAll() + groupLiveData?.observe(this, Observer { + groupList.clear() + groupList.addAll(it) + adapter.notifyDataSetChanged() + upMenu() + }) + } + private fun initCacheSize(books: List) { launch(IO) { books.forEach { book -> diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 9c657dd99..341a45eb5 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -91,7 +91,10 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b Pair("groupId", selectedGroup.groupId), Pair("groupName", selectedGroup.groupName) ) - R.id.menu_download -> startActivity() + R.id.menu_download -> startActivity( + Pair("groupId", selectedGroup.groupId), + Pair("groupName", selectedGroup.groupName) + ) } } diff --git a/app/src/main/res/menu/download.xml b/app/src/main/res/menu/download.xml index 452269d37..6c5b7a8f3 100644 --- a/app/src/main/res/menu/download.xml +++ b/app/src/main/res/menu/download.xml @@ -8,4 +8,25 @@ android:icon="@drawable/ic_play_24dp" app:showAsAction="ifRoom" /> + + + + + + + + + + + + + \ No newline at end of file