feat: 下载界面添加分组

pull/248/head
gedoor 4 years ago
parent eb39fb4436
commit 5932e6e6a2
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 77
      app/src/main/java/io/legado/app/ui/book/download/DownloadActivity.kt
  3. 5
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  4. 21
      app/src/main/res/menu/download.xml

@ -21,9 +21,6 @@ interface BookDao {
@Query("SELECT bookUrl FROM books WHERE origin = '${BookType.local}'")
fun observeLocalUri(): LiveData<List<String>>
@Query("SELECT * FROM books WHERE origin <> '${BookType.local}' and type = 0")
fun observeDownload(): LiveData<List<Book>>
@Query("SELECT * FROM books WHERE (`group` & :group) > 0")
fun observeByGroup(group: Int): LiveData<List<Book>>

@ -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<DownloadViewModel>(R.layout.activity_dow
private val exportRequestCode = 32
private val exportBookPathKey = "exportBookPath"
lateinit var adapter: DownloadAdapter
private var bookshelfLiveData: LiveData<List<Book>>? = null
private var groupLiveData: LiveData<List<BookGroup>>? = null
private var booksLiveData: LiveData<List<Book>>? = null
private var menu: Menu? = null
private var exportPosition = -1
private val groupList: ArrayList<BookGroup> = 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<DownloadViewModel>(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<DownloadViewModel>(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<Book>) {
launch(IO) {
books.forEach { book ->

@ -91,7 +91,10 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
Pair("groupId", selectedGroup.groupId),
Pair("groupName", selectedGroup.groupName)
)
R.id.menu_download -> startActivity<DownloadActivity>()
R.id.menu_download -> startActivity<DownloadActivity>(
Pair("groupId", selectedGroup.groupId),
Pair("groupName", selectedGroup.groupName)
)
}
}

@ -8,4 +8,25 @@
android:icon="@drawable/ic_play_24dp"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_book_group"
android:title="@string/group"
android:icon="@drawable/ic_groups"
app:showAsAction="always">
<menu>
<item
android:id="@+id/menu_no_group"
android:title="@string/no_group" />
<item
android:id="@+id/menu_all"
android:title="@string/all" />
</menu>
</item>
</menu>
Loading…
Cancel
Save