mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
优化
This commit is contained in:
parent
d01f9942fb
commit
e001398e21
@ -1,27 +1,45 @@
|
|||||||
package io.legado.app.ui.book.explore
|
package io.legado.app.ui.book.explore
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import io.legado.app.R
|
import io.legado.app.R
|
||||||
import io.legado.app.base.VMBaseActivity
|
import io.legado.app.base.VMBaseActivity
|
||||||
|
import io.legado.app.constant.AppLog
|
||||||
|
import io.legado.app.data.appDb
|
||||||
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.databinding.ActivityExploreShowBinding
|
import io.legado.app.databinding.ActivityExploreShowBinding
|
||||||
|
import io.legado.app.databinding.DialogPageChoiceBinding
|
||||||
import io.legado.app.databinding.ViewLoadMoreBinding
|
import io.legado.app.databinding.ViewLoadMoreBinding
|
||||||
|
import io.legado.app.help.coroutine.Coroutine
|
||||||
|
import io.legado.app.lib.dialogs.alert
|
||||||
|
import io.legado.app.lib.theme.backgroundColor
|
||||||
|
import io.legado.app.model.webBook.WebBook
|
||||||
|
import io.legado.app.ui.book.group.GroupSelectDialog
|
||||||
import io.legado.app.ui.book.info.BookInfoActivity
|
import io.legado.app.ui.book.info.BookInfoActivity
|
||||||
|
import io.legado.app.ui.widget.dialog.WaitDialog
|
||||||
import io.legado.app.ui.widget.recycler.LoadMoreView
|
import io.legado.app.ui.widget.recycler.LoadMoreView
|
||||||
import io.legado.app.ui.widget.recycler.VerticalDivider
|
import io.legado.app.ui.widget.recycler.VerticalDivider
|
||||||
|
import io.legado.app.utils.showDialogFragment
|
||||||
import io.legado.app.utils.startActivity
|
import io.legado.app.utils.startActivity
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
|
import kotlinx.coroutines.Dispatchers.Main
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class ExploreShowActivity : VMBaseActivity<ActivityExploreShowBinding, ExploreShowViewModel>(),
|
class ExploreShowActivity : VMBaseActivity<ActivityExploreShowBinding, ExploreShowViewModel>(),
|
||||||
ExploreShowAdapter.CallBack {
|
ExploreShowAdapter.CallBack,
|
||||||
|
GroupSelectDialog.CallBack {
|
||||||
override val binding by viewBinding(ActivityExploreShowBinding::inflate)
|
override val binding by viewBinding(ActivityExploreShowBinding::inflate)
|
||||||
override val viewModel by viewModels<ExploreShowViewModel>()
|
override val viewModel by viewModels<ExploreShowViewModel>()
|
||||||
|
|
||||||
private val adapter by lazy { ExploreShowAdapter(this, this) }
|
private val adapter by lazy { ExploreShowAdapter(this, this) }
|
||||||
private val loadMoreView by lazy { LoadMoreView(this) }
|
private val loadMoreView by lazy { LoadMoreView(this) }
|
||||||
|
private val waitDialog by lazy {
|
||||||
|
WaitDialog(this)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||||
binding.titleBar.title = intent.getStringExtra("exploreName")
|
binding.titleBar.title = intent.getStringExtra("exploreName")
|
||||||
@ -98,4 +116,73 @@ class ExploreShowActivity : VMBaseActivity<ActivityExploreShowBinding, ExploreSh
|
|||||||
putExtra("bookUrl", book.bookUrl)
|
putExtra("bookUrl", book.bookUrl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
menuInflater.inflate(R.menu.explore_show, menu)
|
||||||
|
return super.onCompatCreateOptionsMenu(menu)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
when (item.itemId) {
|
||||||
|
R.id.menu_add_all_to_bookshelf -> addAllToBookshelf()
|
||||||
|
}
|
||||||
|
return super.onCompatOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addAllToBookshelf() {
|
||||||
|
showDialogFragment(GroupSelectDialog(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun upGroup(requestCode: Int, groupId: Long) {
|
||||||
|
|
||||||
|
alert("选择页数范围") {
|
||||||
|
val alertBinding = DialogPageChoiceBinding.inflate(layoutInflater).apply {
|
||||||
|
root.setBackgroundColor(root.context.backgroundColor)
|
||||||
|
}
|
||||||
|
customView { alertBinding.root }
|
||||||
|
yesButton {
|
||||||
|
alertBinding.run {
|
||||||
|
val start = editStart.text!!.toString().toInt()
|
||||||
|
val end = editEnd.text!!.toString().toInt()
|
||||||
|
addAllToBookshelf(start, end, groupId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
noButton()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addAllToBookshelf(start: Int, end: Int, groupId: Long) {
|
||||||
|
val job = Coroutine.async {
|
||||||
|
launch(Main) {
|
||||||
|
waitDialog.setText("加载列表中...")
|
||||||
|
waitDialog.show()
|
||||||
|
}
|
||||||
|
val searchBooks = viewModel.loadExploreBooks(start, end)
|
||||||
|
val books = searchBooks.map {
|
||||||
|
it.toBook()
|
||||||
|
}
|
||||||
|
launch(Main) {
|
||||||
|
waitDialog.setText("添加书架中...")
|
||||||
|
}
|
||||||
|
books.forEach {
|
||||||
|
if (isInBookshelf(it.name, it.author)) return@forEach
|
||||||
|
if (it.tocUrl.isEmpty()) {
|
||||||
|
val source = appDb.bookSourceDao.getBookSource(it.origin)!!
|
||||||
|
WebBook.getBookInfoAwait(source, it)
|
||||||
|
}
|
||||||
|
it.order = appDb.bookDao.minOrder - 1
|
||||||
|
it.group = groupId
|
||||||
|
it.save()
|
||||||
|
}
|
||||||
|
}.onError {
|
||||||
|
AppLog.put("添加书架出错\n${it.localizedMessage}", it)
|
||||||
|
}.onFinally {
|
||||||
|
waitDialog.dismiss()
|
||||||
|
}
|
||||||
|
waitDialog.setOnCancelListener {
|
||||||
|
job.cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -69,4 +69,22 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun loadExploreBooks(start: Int, end: Int): List<SearchBook> {
|
||||||
|
val source = bookSource
|
||||||
|
val url = exploreUrl
|
||||||
|
if (source == null || url == null) return emptyList()
|
||||||
|
val searchBooks = arrayListOf<SearchBook>()
|
||||||
|
val range = if (start <= end) {
|
||||||
|
start .. end
|
||||||
|
} else {
|
||||||
|
start downTo end
|
||||||
|
}
|
||||||
|
for (page in range) {
|
||||||
|
val books = WebBook.exploreBookAwait(source, url, page)
|
||||||
|
if (books.isEmpty()) break
|
||||||
|
searchBooks.addAll(books)
|
||||||
|
}
|
||||||
|
return searchBooks
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
74
app/src/main/res/layout/dialog_page_choice.xml
Normal file
74
app/src/main/res/layout/dialog_page_choice.xml
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:id="@+id/ll_content"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@color/background"
|
||||||
|
android:gravity="center"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:padding="16dp">
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1" />
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/edit_start"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_vertical"
|
||||||
|
android:background="@drawable/bg_edit"
|
||||||
|
android:hint="@string/start"
|
||||||
|
android:importantForAutofill="no"
|
||||||
|
android:inputType="number"
|
||||||
|
android:lines="1"
|
||||||
|
android:maxLength="5"
|
||||||
|
android:minWidth="60dp"
|
||||||
|
android:paddingLeft="5dp"
|
||||||
|
android:paddingTop="4dp"
|
||||||
|
android:paddingRight="5dp"
|
||||||
|
android:paddingBottom="4dp"
|
||||||
|
android:textColor="@color/primaryText"
|
||||||
|
android:textCursorDrawable="@drawable/shape_text_cursor"
|
||||||
|
android:textSize="14sp"
|
||||||
|
tools:ignore="TouchTargetSizeCheck,TextContrastCheck" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_vertical"
|
||||||
|
android:layout_marginLeft="5dp"
|
||||||
|
android:layout_marginRight="5dp"
|
||||||
|
android:text="@string/page_to"
|
||||||
|
android:textColor="@color/primaryText"
|
||||||
|
android:textSize="16sp" />
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/edit_end"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_vertical"
|
||||||
|
android:background="@drawable/bg_edit"
|
||||||
|
android:hint="@string/end"
|
||||||
|
android:importantForAutofill="no"
|
||||||
|
android:inputType="number"
|
||||||
|
android:lines="1"
|
||||||
|
android:maxLength="5"
|
||||||
|
android:minWidth="60dp"
|
||||||
|
android:paddingLeft="5dp"
|
||||||
|
android:paddingTop="4dp"
|
||||||
|
android:paddingRight="5dp"
|
||||||
|
android:paddingBottom="4dp"
|
||||||
|
android:textColor="@color/primaryText"
|
||||||
|
android:textCursorDrawable="@drawable/shape_text_cursor"
|
||||||
|
android:textSize="14sp"
|
||||||
|
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck,TextContrastCheck" />
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
8
app/src/main/res/menu/explore_show.xml
Normal file
8
app/src/main/res/menu/explore_show.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_add_all_to_bookshelf"
|
||||||
|
android:title="@string/add_all_to_bookshelf" />
|
||||||
|
|
||||||
|
</menu>
|
@ -1117,4 +1117,6 @@
|
|||||||
<string name="test">测试</string>
|
<string name="test">测试</string>
|
||||||
<string name="show_wait_up_count">显示等待更新数量</string>
|
<string name="show_wait_up_count">显示等待更新数量</string>
|
||||||
<string name="exit_app">退出软件</string>
|
<string name="exit_app">退出软件</string>
|
||||||
|
<string name="add_all_to_bookshelf">全部加入书架</string>
|
||||||
|
<string name="page_to">页至</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1120,4 +1120,6 @@
|
|||||||
<string name="test">测试</string>
|
<string name="test">测试</string>
|
||||||
<string name="show_wait_up_count">显示等待更新数量</string>
|
<string name="show_wait_up_count">显示等待更新数量</string>
|
||||||
<string name="exit_app">退出软件</string>
|
<string name="exit_app">退出软件</string>
|
||||||
|
<string name="add_all_to_bookshelf">全部加入书架</string>
|
||||||
|
<string name="page_to">页至</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1120,4 +1120,6 @@
|
|||||||
<string name="test">测试</string>
|
<string name="test">测试</string>
|
||||||
<string name="show_wait_up_count">显示等待更新数量</string>
|
<string name="show_wait_up_count">显示等待更新数量</string>
|
||||||
<string name="exit_app">退出软件</string>
|
<string name="exit_app">退出软件</string>
|
||||||
|
<string name="add_all_to_bookshelf">全部加入书架</string>
|
||||||
|
<string name="page_to">页至</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1117,4 +1117,6 @@
|
|||||||
<string name="test">测试</string>
|
<string name="test">测试</string>
|
||||||
<string name="show_wait_up_count">显示等待更新数量</string>
|
<string name="show_wait_up_count">显示等待更新数量</string>
|
||||||
<string name="exit_app">退出软件</string>
|
<string name="exit_app">退出软件</string>
|
||||||
|
<string name="add_all_to_bookshelf">全部加入书架</string>
|
||||||
|
<string name="page_to">页至</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1119,4 +1119,6 @@
|
|||||||
<string name="test">测试</string>
|
<string name="test">测试</string>
|
||||||
<string name="show_wait_up_count">显示等待更新数量</string>
|
<string name="show_wait_up_count">显示等待更新数量</string>
|
||||||
<string name="exit_app">退出软件</string>
|
<string name="exit_app">退出软件</string>
|
||||||
|
<string name="add_all_to_bookshelf">全部加入书架</string>
|
||||||
|
<string name="page_to">页至</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1119,4 +1119,6 @@
|
|||||||
<string name="test">测试</string>
|
<string name="test">测试</string>
|
||||||
<string name="show_wait_up_count">显示等待更新数量</string>
|
<string name="show_wait_up_count">显示等待更新数量</string>
|
||||||
<string name="exit_app">退出软件</string>
|
<string name="exit_app">退出软件</string>
|
||||||
|
<string name="add_all_to_bookshelf">全部加入书架</string>
|
||||||
|
<string name="page_to">页至</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1120,4 +1120,6 @@
|
|||||||
<string name="test">测试</string>
|
<string name="test">测试</string>
|
||||||
<string name="show_wait_up_count">显示等待更新数量</string>
|
<string name="show_wait_up_count">显示等待更新数量</string>
|
||||||
<string name="exit_app">退出软件</string>
|
<string name="exit_app">退出软件</string>
|
||||||
|
<string name="add_all_to_bookshelf">全部加入书架</string>
|
||||||
|
<string name="page_to">页至</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user