Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from gedoor:master #215

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.legado.app.base.adapter

import android.content.Context
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
Expand All @@ -23,17 +24,26 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
AsyncListDiffer(this, diffItemCallback).apply {
addListListener { _, _ ->
onCurrentListChanged()
if (keepScrollPosition) {
layoutManager?.onRestoreInstanceState(layoutState)
layoutState = null
}
}
}
}

private var itemClickListener: ((holder: ItemViewHolder, item: ITEM) -> Unit)? = null
private var itemLongClickListener: ((holder: ItemViewHolder, item: ITEM) -> Boolean)? = null

private var layoutManager: RecyclerView.LayoutManager? = null
private var layoutState: Parcelable? = null

var itemAnimation: ItemAnimation? = null

abstract val diffItemCallback: DiffUtil.ItemCallback<ITEM>

open val keepScrollPosition = false

fun setOnItemClickListener(listener: (holder: ItemViewHolder, item: ITEM) -> Unit) {
itemClickListener = listener
}
Expand All @@ -48,6 +58,9 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context

fun setItems(items: List<ITEM>?) {
kotlin.runCatching {
if (keepScrollPosition) {
layoutState = layoutManager?.onSaveInstanceState()
}
asyncListDiffer.submitList(items?.toMutableList())
}
}
Expand Down Expand Up @@ -161,6 +174,7 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
val manager = recyclerView.layoutManager
layoutManager = manager
if (manager is GridLayoutManager) {
manager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
Expand Down
15 changes: 9 additions & 6 deletions app/src/main/java/io/legado/app/help/glide/ImageLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.annotation.DrawableRes
import androidx.lifecycle.Lifecycle
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.request.RequestOptions
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.isDataUrl
Expand Down Expand Up @@ -54,15 +55,17 @@ object ImageLoader {
}

fun loadBitmap(context: Context, path: String?): RequestBuilder<Bitmap> {
val requestManager = Glide.with(context).asBitmap()
.apply(RequestOptions.decodeTypeOf(Any::class.java))
return when {
path.isNullOrEmpty() -> Glide.with(context).asBitmap().load(path)
path.isDataUrl() -> Glide.with(context).asBitmap().load(path)
path.isAbsUrl() -> Glide.with(context).asBitmap().load(path)
path.isContentScheme() -> Glide.with(context).asBitmap().load(Uri.parse(path))
path.isNullOrEmpty() -> requestManager.load(path)
path.isDataUrl() -> requestManager.load(path)
path.isAbsUrl() -> requestManager.load(path)
path.isContentScheme() -> requestManager.load(Uri.parse(path))
else -> kotlin.runCatching {
Glide.with(context).asBitmap().load(File(path))
requestManager.load(File(path))
}.getOrElse {
Glide.with(context).asBitmap().load(path)
requestManager.load(path)
}
}
}
Expand Down
32 changes: 31 additions & 1 deletion app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.MenuItem
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.PopupMenu
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.textfield.TextInputLayout
Expand Down Expand Up @@ -38,11 +39,13 @@ import io.legado.app.ui.file.HandleFileContract
import io.legado.app.utils.ACache
import io.legado.app.utils.FileDoc
import io.legado.app.utils.applyNavigationBarPadding
import io.legado.app.utils.applyOpenTint
import io.legado.app.utils.applyTint
import io.legado.app.utils.checkWrite
import io.legado.app.utils.cnCompare
import io.legado.app.utils.enableCustomExport
import io.legado.app.utils.flowWithLifecycleAndDatabaseChange
import io.legado.app.utils.iconItemOnLongClick
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.observeEvent
import io.legado.app.utils.parseToUri
Expand All @@ -65,6 +68,7 @@ import kotlin.math.max
* cache/download 缓存界面
*/
class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>(),
PopupMenu.OnMenuItemClickListener,
CacheAdapter.CallBack {

override val binding by viewBinding(ActivityCacheBookBinding::inflate)
Expand Down Expand Up @@ -120,6 +124,13 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()

override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.book_cache, menu)
menu.iconItemOnLongClick(R.id.menu_download) {
PopupMenu(this, it).apply {
inflate(R.menu.book_cache_download)
this.menu.applyOpenTint(this@CacheActivity)
setOnMenuItemClickListener(this@CacheActivity)
}.show()
}
return super.onCompatCreateOptionsMenu(menu)
}

Expand Down Expand Up @@ -158,7 +169,8 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
*/
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_download -> {
R.id.menu_download,
R.id.menu_download_after -> {
if (!CacheBook.isRun) {
adapter.getItems().forEach { book ->
CacheBook.start(
Expand All @@ -172,6 +184,20 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
CacheBook.stop(this@CacheActivity)
}
}
R.id.menu_download_all -> {
if (!CacheBook.isRun) {
adapter.getItems().forEach { book ->
CacheBook.start(
this@CacheActivity,
book,
0,
book.lastChapterIndex
)
}
} else {
CacheBook.stop(this@CacheActivity)
}
}

R.id.menu_export_all -> exportAll()
R.id.menu_enable_replace -> AppConfig.exportUseReplace = !item.isChecked
Expand All @@ -198,6 +224,10 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
return super.onCompatOptionsItemSelected(item)
}

override fun onMenuItemClick(item: MenuItem): Boolean {
return onCompatOptionsItemSelected(item)
}

private fun initRecyclerView() {
binding.recyclerView.layoutManager = layoutManager
binding.recyclerView.adapter = adapter
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
private var historyFlowJob: Job? = null
private var booksFlowJob: Job? = null
private var precisionSearchMenuItem: MenuItem? = null
private var isManualStopSearch = false

override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.llInputHelp.setBackgroundColor(backgroundColor)
Expand Down Expand Up @@ -180,6 +181,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onQueryTextSubmit(query: String): Boolean {
searchView.clearFocus()
query.trim().let { searchKey ->
isManualStopSearch = false
viewModel.saveSearchKey(searchKey)
viewModel.searchKey = ""
viewModel.search(searchKey)
Expand Down Expand Up @@ -234,6 +236,14 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
}
}
})
binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (!recyclerView.canScrollVertically(1)) {
scrollToBottom()
}
}
})
}

private fun initOtherView() {
Expand All @@ -244,6 +254,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
.create()
binding.fbStartStop.setOnClickListener {
if (viewModel.isSearchLiveData.value == true) {
isManualStopSearch = true
viewModel.stop()
binding.refreshProgressBar.isAutoLoading = false
} else {
Expand Down Expand Up @@ -295,6 +306,21 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
}
}

/**
* 滚动到底部事件
*/
private fun scrollToBottom() {
if (isManualStopSearch) {
return
}
if (viewModel.isSearchLiveData.value == false
&& viewModel.searchKey.isNotEmpty()
&& viewModel.hasMore
) {
viewModel.search("")
}
}

/**
* 打开关闭输入帮助
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import io.legado.app.utils.visible
class SearchAdapter(context: Context, val callBack: CallBack) :
DiffRecyclerAdapter<SearchBook, ItemSearchBinding>(context) {

override val keepScrollPosition = true

override val diffItemCallback: DiffUtil.ItemCallback<SearchBook>
get() = object : DiffUtil.ItemCallback<SearchBook>() {

Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/menu/book_cache_download.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_download_after"
android:icon="@drawable/ic_bubble_chart"
android:title="@string/menu_download_after"
app:showAsAction="never" />

<item
android:id="@+id/menu_download_all"
android:icon="@drawable/ic_bubble_chart"
android:title="@string/menu_download_all"
app:showAsAction="never" />

</menu>
2 changes: 2 additions & 0 deletions app/src/main/res/values-es-rES/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1178,4 +1178,6 @@
<string name="read_aloud_by_media_button_summary">通过耳机按键来启动朗读</string>
<string name="show_same_source">显示重复书源</string>
<string name="theme_config">主题配置</string>
<string name="menu_download_after">Descargar el siguiente capítulo</string>
<string name="menu_download_all">Descargar todos los capítulos</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1181,4 +1181,6 @@
<string name="read_aloud_by_media_button_summary">通过耳机按键来启动朗读</string>
<string name="show_same_source">显示重复书源</string>
<string name="theme_config">主题配置</string>
<string name="menu_download_after">下载之后章节</string>
<string name="menu_download_all">下载全部章节</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1181,4 +1181,6 @@
<string name="read_aloud_by_media_button_summary">通过耳机按键来启动朗读</string>
<string name="show_same_source">显示重复书源</string>
<string name="theme_config">主题配置</string>
<string name="menu_download_after">Faça o download do próximo capítulo</string>
<string name="menu_download_all">Download de todos os capítulos</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1177,4 +1177,6 @@ Còn </string>
<string name="read_aloud_by_media_button_summary">通过耳机按键来启动朗读</string>
<string name="show_same_source">显示重复书源</string>
<string name="theme_config">主题配置</string>
<string name="menu_download_after">Tải xuống chương tiếp theo</string>
<string name="menu_download_all">Tải xuống tất cả các chương</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1178,4 +1178,6 @@
<string name="read_aloud_by_media_button_summary">通过耳机按键来启动朗读</string>
<string name="show_same_source">显示重复书源</string>
<string name="theme_config">主题配置</string>
<string name="menu_download_after">下載之後章節</string>
<string name="menu_download_all">下載全部章節</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1180,4 +1180,6 @@
<string name="read_aloud_by_media_button_summary">通过耳机按键来启动朗读</string>
<string name="show_same_source">显示重复书源</string>
<string name="theme_config">主题配置</string>
<string name="menu_download_after">下載之後章節</string>
<string name="menu_download_all">下載全部章節</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1180,4 +1180,6 @@
<string name="read_aloud_by_media_button_summary">通过耳机按键来启动朗读</string>
<string name="show_same_source">显示重复书源</string>
<string name="theme_config">主题配置</string>
<string name="menu_download_after">下载之后章节</string>
<string name="menu_download_all">下载全部章节</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1181,4 +1181,6 @@
<string name="read_aloud_by_media_button_summary">通过耳机按键来启动朗读</string>
<string name="show_same_source">显示重复书源</string>
<string name="theme_config">主题配置</string>
<string name="menu_download_after">Download the chapter after</string>
<string name="menu_download_all">Download all chapter</string>
</resources>
Loading