Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Mar 9, 2025
1 parent e62d5b9 commit 0b18832
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 46 deletions.
15 changes: 9 additions & 6 deletions app/src/main/java/io/legado/app/help/AppWebDav.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import io.legado.app.utils.getPrefString
import io.legado.app.utils.isJson
import io.legado.app.utils.removePref
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx
import java.io.File
import kotlin.coroutines.coroutineContext

/**
* webDav初始化会访问网络,不要放到主线程
Expand Down Expand Up @@ -217,9 +219,8 @@ object AppWebDav {
WebDav(putUrl, it).upload(byteArray, "text/plain")
}
} catch (e: Exception) {
val msg = "WebDav导出\n${e.localizedMessage}"
AppLog.put(msg, e)
appCtx.toastOnUi(msg)
coroutineContext.ensureActive()
AppLog.put("WebDav导出失败\n${e.localizedMessage}", e, true)
}
}

Expand All @@ -232,9 +233,8 @@ object AppWebDav {
WebDav(putUrl, it).upload(uri, "text/plain")
}
} catch (e: Exception) {
val msg = "WebDav导出\n${e.localizedMessage}"
AppLog.put(msg, e)
appCtx.toastOnUi(msg)
coroutineContext.ensureActive()
AppLog.put("WebDav导出失败\n${e.localizedMessage}", e, true)
}
}

Expand All @@ -249,6 +249,7 @@ object AppWebDav {
WebDav(url, authorization).upload(json.toByteArray(), "application/json")
book.syncTime = System.currentTimeMillis()
} catch (e: Exception) {
coroutineContext.ensureActive()
AppLog.put("上传进度失败\n${e.localizedMessage}", e)
}
}
Expand All @@ -263,6 +264,7 @@ object AppWebDav {
WebDav(url, authorization).upload(json.toByteArray(), "application/json")
onSuccess?.invoke()
} catch (e: Exception) {
coroutineContext.ensureActive()
AppLog.put("上传进度失败\n${e.localizedMessage}", e)
}
}
Expand All @@ -289,6 +291,7 @@ object AppWebDav {
}
}
}.onFailure {
coroutineContext.ensureActive()
AppLog.put("获取书籍进度失败\n${it.localizedMessage}", it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
}
}
addRecyclerViewPreloader(AppConfig.mangaPreDownloadNum)
}
binding.webtoonFrame.run {
onTouchMiddle {
if (!binding.mangaMenu.isVisible) {
binding.mangaMenu.runMenuIn()
Expand Down Expand Up @@ -600,7 +602,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
}

private fun disabledClickScroller(disable: Boolean) {
binding.mRecyclerManga.disabledClickScroller = disable
binding.webtoonFrame.disabledClickScroller = disable
}

private fun upLayoutInDisplayCutoutMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.legado.app.ui.book.manga.recyclerview

import android.content.Context
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.GestureDetector
import android.view.MotionEvent
Expand Down Expand Up @@ -33,6 +34,45 @@ class WebtoonFrame : FrameLayout {
private val recycler: WebtoonRecyclerView?
get() = getChildAt(0) as? WebtoonRecyclerView

private val mcRect = RectF()
private val blRect = RectF()
private val brRect = RectF()

private var mTouchMiddle: (() -> Unit)? = null
fun onTouchMiddle(init: () -> Unit) = apply { this.mTouchMiddle = init }
private var mNextPage: (() -> Unit)? = null
fun onNextPage(init: () -> Unit) = apply { this.mNextPage = init }
private var mPrevPage: (() -> Unit)? = null
fun onPrevPage(init: () -> Unit) = apply { this.mPrevPage = init }

var disabledClickScroller = false

override fun onAttachedToWindow() {
super.onAttachedToWindow()
recycler?.tapListener = { ev ->
when {
mcRect.contains(ev.rawX, ev.rawY) -> {
mTouchMiddle?.invoke()
}

blRect.contains(ev.rawX, ev.rawY) && !disabledClickScroller -> {
mPrevPage?.invoke()
}

brRect.contains(ev.rawX, ev.rawY) && !disabledClickScroller -> {
mNextPage?.invoke()
}
}
}
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
mcRect.set(width * 0.33f, height * 0.33f, width * 0.66f, height * 0.66f)
blRect.set(0f, height * 0.66f, width * 0.33f, height.toFloat())
brRect.set(width * 0.66f, height * 0.66f, width.toFloat(), height.toFloat())
}

override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
if (!disableMangaScale) {
scaleDetector.onTouchEvent(ev)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,17 @@ class WebtoonRecyclerView @JvmOverloads constructor(
private var mNestedPreScrollListener: IComicPreScroll? = null
private val listener = GestureListener()
private val detector = Detector()
private val mcRect = RectF()
private val blRect = RectF()
private val brRect = RectF()

var doubleTapZoom = true
var tapListener: ((MotionEvent) -> Unit)? = null
var longTapListener: ((MotionEvent) -> Boolean)? = null
var disableMangaScaling = false
var disabledClickScroller = false

private var mTouchMiddle: (() -> Unit)? = null
fun onTouchMiddle(init: () -> Unit) = apply { this.mTouchMiddle = init }
private var mNextPage: (() -> Unit)? = null
fun onNextPage(init: () -> Unit) = apply { this.mNextPage = init }
private var mPrevPage: (() -> Unit)? = null
fun onPrevPage(init: () -> Unit) = apply { this.mPrevPage = init }

override fun onMeasure(widthSpec: Int, heightSpec: Int) {
halfWidth = MeasureSpec.getSize(widthSpec) / 2
halfHeight = MeasureSpec.getSize(heightSpec) / 2
if (!heightSet) {
originalHeight = MeasureSpec.getSize(heightSpec)
val width = MeasureSpec.getSize(widthSpec)
setClickArea(width, originalHeight)
heightSet = true
}
super.onMeasure(widthSpec, heightSpec)
Expand Down Expand Up @@ -105,12 +92,6 @@ class WebtoonRecyclerView @JvmOverloads constructor(
return super.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow, type)
}

private fun setClickArea(width: Int, height: Int) {
mcRect.set(width * 0.33f, height * 0.33f, width * 0.66f, height * 0.66f)
blRect.set(0f, height * 0.66f, width * 0.33f, height.toFloat())
brRect.set(width * 0.66f, height * 0.66f, width.toFloat(), height.toFloat())
}

private fun getPositionX(positionX: Float): Float {
if (currentScale < 1) {
return 0f
Expand Down Expand Up @@ -250,24 +231,7 @@ class WebtoonRecyclerView @JvmOverloads constructor(
inner class GestureListener : GestureDetectorWithLongTap.Listener() {

override fun onSingleTapConfirmed(ev: MotionEvent): Boolean {
when {
mcRect.contains(ev.rawX, ev.rawY) -> {
mTouchMiddle?.invoke()
}

blRect.contains(ev.rawX, ev.rawY) && !disabledClickScroller -> {
mPrevPage?.invoke()
}

brRect.contains(ev.rawX, ev.rawY) && !disabledClickScroller-> {
mNextPage?.invoke()
}

else -> {
tapListener?.invoke(ev)

}
}
tapListener?.invoke(ev)
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (!AppConfig.syncBookProgress) return
execute {
AppWebDav.getBookProgress(book)
?: throw NoStackTraceException("没有进度")
}.onError {
AppLog.put("拉取阅读进度失败《${book.name}\n${it.localizedMessage}", it)
}.onSuccess { progress ->
progress ?: return@onSuccess
if (progress.durChapterIndex < book.durChapterIndex ||
(progress.durChapterIndex == book.durChapterIndex
&& progress.durChapterPos < book.durChapterPos)
Expand All @@ -253,7 +253,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
AppLog.put("自动同步阅读进度成功《${book.name}${progress.durChapterTitle}")
}
}

}

/**
Expand Down

0 comments on commit 0b18832

Please sign in to comment.