|
|
|
@ -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 -> |
|
|
|
|