Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Feb 13, 2024
1 parent d115910 commit 7fbc23a
Show file tree
Hide file tree
Showing 17 changed files with 343 additions and 96 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/io/legado/app/help/config/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
}
}

val textSelectAble: Boolean
get() = appCtx.getPrefBoolean(PreferKey.textSelectAble, true)

val isTransparentStatusBar: Boolean
get() = appCtx.getPrefBoolean(PreferKey.transparentStatusBar, true)

Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.legado.app.help.coroutine

import android.os.Looper
import io.legado.app.utils.printOnDebug
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CompletionHandler
Expand All @@ -15,6 +16,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import java.util.concurrent.Executors
import kotlin.coroutines.CoroutineContext

/**
Expand All @@ -33,6 +35,9 @@ class Coroutine<T>(
companion object {

private val DEFAULT = MainScope()
private val launchExecutor = Executors.newSingleThreadExecutor()
private val mainThread = Looper.getMainLooper().thread
private val isMainThread inline get() = mainThread === Thread.currentThread()

fun <T> async(
scope: CoroutineScope = DEFAULT,
Expand All @@ -46,7 +51,7 @@ class Coroutine<T>(

}

private val job: Job
private val job: Job by lazy { executeInternal(context, block) }

private var start: VoidCallback? = null
private var success: Callback<T>? = null
Expand All @@ -67,7 +72,13 @@ class Coroutine<T>(
get() = job.isCompleted

init {
this.job = executeInternal(context, block)
if (context == Dispatchers.Main.immediate && isMainThread) {
job
} else {
launchExecutor.execute {
job
}
}
}

fun timeout(timeMillis: () -> Long): Coroutine<T> {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/io/legado/app/model/ReadBook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ object ReadBook : CoroutineScope by MainScope() {
textChapter.getPage(index - 2)?.recyclePictures()
}
if (index < pageIndex) {
textChapter.getPage(index + 2)?.recyclePictures()
textChapter.getPage(index + 3)?.recyclePictures()
}
}
durChapterPos = curTextChapter?.getReadLength(index) ?: index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ abstract class BaseReadBookActivity :
override val binding by viewBinding(ActivityBookReadBinding::inflate)
override val viewModel by viewModels<ReadBookViewModel>()
var bottomDialog = 0
set(value) {
if (field != value) {
field = value
onBottomDialogChange()
}
}
private val selectBookFolderResult = registerForActivityResult(HandleFileContract()) {
it.uri?.let { uri ->
ReadBook.book?.let { book ->
Expand Down Expand Up @@ -94,6 +100,21 @@ abstract class BaseReadBookActivity :
}
}

private fun onBottomDialogChange() {
when (bottomDialog) {
0 -> onMenuHide()
1 -> onMenuShow()
}
}

open fun onMenuShow() {

}

open fun onMenuHide() {

}

fun showPaddingConfig() {
showDialogFragment<PaddingConfigDialog>()
}
Expand Down
67 changes: 13 additions & 54 deletions app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ import io.legado.app.utils.ACache
import io.legado.app.utils.Debounce
import io.legado.app.utils.LogUtils
import io.legado.app.utils.StartActivityContract
import io.legado.app.utils.SyncedRenderer
import io.legado.app.utils.applyOpenTint
import io.legado.app.utils.buildMainHandler
import io.legado.app.utils.getPrefBoolean
Expand Down Expand Up @@ -201,8 +200,7 @@ class ReadBookActivity : BaseReadBookActivity(),
}
override val isInitFinish: Boolean get() = viewModel.isInitFinish
override val isScroll: Boolean get() = binding.readView.isScroll
override var autoPageProgress = 0
override var isAutoPage = false
private val isAutoPage get() = binding.readView.isAutoPage
override var isShowingSearchResult = false
override var isSelectingSearchResult = false
set(value) {
Expand All @@ -220,8 +218,6 @@ class ReadBookActivity : BaseReadBookActivity(),
private var bookChanged = false
private var pageChanged = false
private var reloadContent = false
private val autoPageRenderer by lazy { SyncedRenderer { doAutoPage(it) } }
private var autoPageScrollOffset = 0.0
private val handler by lazy { buildMainHandler() }
private val screenOffRunnable by lazy { Runnable { keepScreenOn(false) } }

Expand Down Expand Up @@ -934,6 +930,7 @@ class ReadBookActivity : BaseReadBookActivity(),
ReadAloud.upTtsProgress(this)
}
loadStates = true
binding.readView.onContentLoadFinish()
}

/**
Expand All @@ -945,9 +942,6 @@ class ReadBookActivity : BaseReadBookActivity(),
success: (() -> Unit)?
) {
lifecycleScope.launch {
if (relativePosition == 0) {
autoPageProgress = 0
}
binding.readView.upContent(relativePosition, resetPageOffset)
upSeekBarProgress()
loadStates = false
Expand All @@ -970,8 +964,7 @@ class ReadBookActivity : BaseReadBookActivity(),
*/
override fun pageChanged() {
pageChanged = true
lifecycleScope.launch {
autoPageProgress = 0
handler.post {
upSeekBarProgress()
startBackupJob()
}
Expand Down Expand Up @@ -1053,8 +1046,7 @@ class ReadBookActivity : BaseReadBookActivity(),
if (isAutoPage) {
autoPageStop()
} else {
isAutoPage = true
autoPagePlus()
binding.readView.autoPager.start()
binding.readMenu.setAutoPage(true)
screenTimeOut = -1L
screenOffTimerStart()
Expand All @@ -1063,53 +1055,12 @@ class ReadBookActivity : BaseReadBookActivity(),

override fun autoPageStop() {
if (isAutoPage) {
isAutoPage = false
autoPageRenderer.stop()
binding.readView.invalidate()
binding.readView.clearNextPageBitmap()
binding.readView.autoPager.stop()
binding.readMenu.setAutoPage(false)
upScreenTimeOut()
}
}

private fun autoPagePlus() {
autoPageProgress = 0
autoPageScrollOffset = 0.0
autoPageRenderer.start()
}

private fun doAutoPage(frameTime: Double) {
if (menuLayoutIsVisible) {
return
}
if (binding.readView.run { isScroll && pageDelegate?.isRunning == true }) {
return
}
val readTime = ReadBookConfig.autoReadSpeed * 1000.0
val height = binding.readView.height
autoPageScrollOffset += height / readTime * frameTime
if (autoPageScrollOffset < 1) {
return
}
val scrollOffset = autoPageScrollOffset.toInt()
autoPageScrollOffset -= scrollOffset
if (binding.readView.isScroll) {
binding.readView.curPage.scroll(-scrollOffset)
} else {
autoPageProgress += scrollOffset
if (autoPageProgress >= height) {
autoPageProgress = 0
if (!binding.readView.fillPage(PageDirection.NEXT)) {
autoPageStop()
} else {
binding.readView.clearNextPageBitmap()
}
} else {
binding.readView.invalidate()
}
}
}

override fun openSourceEditActivity() {
ReadBook.bookSource?.let {
sourceEditActivity.launch {
Expand Down Expand Up @@ -1414,6 +1365,14 @@ class ReadBookActivity : BaseReadBookActivity(),
skipToSearch(searchResult)
}

override fun onMenuShow() {
binding.readView.autoPager.pause()
}

override fun onMenuHide() {
binding.readView.autoPager.resume()
}

/* 全文搜索跳转 */
private fun skipToSearch(searchResult: SearchResult) {
val previousResult = binding.searchMenu.previousSearchResult
Expand Down
35 changes: 32 additions & 3 deletions app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,38 @@ import io.legado.app.help.config.LocalConfig
import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ThemeConfig
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.*
import io.legado.app.lib.theme.Selector
import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.bottomBackground
import io.legado.app.lib.theme.buttonDisabledColor
import io.legado.app.lib.theme.getPrimaryTextColor
import io.legado.app.lib.theme.primaryColor
import io.legado.app.lib.theme.primaryTextColor
import io.legado.app.model.ReadBook
import io.legado.app.ui.book.info.BookInfoActivity
import io.legado.app.ui.browser.WebViewActivity
import io.legado.app.ui.widget.seekbar.SeekBarChangeListener
import io.legado.app.utils.*
import splitties.views.*
import io.legado.app.utils.ColorUtils
import io.legado.app.utils.ConstraintModify
import io.legado.app.utils.activity
import io.legado.app.utils.dpToPx
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.gone
import io.legado.app.utils.invisible
import io.legado.app.utils.loadAnimation
import io.legado.app.utils.modifyBegin
import io.legado.app.utils.navigationBarGravity
import io.legado.app.utils.navigationBarHeight
import io.legado.app.utils.openUrl
import io.legado.app.utils.putPrefBoolean
import io.legado.app.utils.startActivity
import io.legado.app.utils.visible
import splitties.views.bottomPadding
import splitties.views.leftPadding
import splitties.views.onClick
import splitties.views.onLongClick
import splitties.views.padding
import splitties.views.rightPadding

/**
* 阅读界面菜单
Expand Down Expand Up @@ -273,6 +298,7 @@ class ReadMenu @JvmOverloads constructor(
}

fun runMenuIn(anim: Boolean = !AppConfig.isEInkMode) {
callBack.onMenuShow()
this.visible()
binding.titleBar.visible()
binding.bottomMenu.visible()
Expand All @@ -286,6 +312,7 @@ class ReadMenu @JvmOverloads constructor(
}

fun runMenuOut(anim: Boolean = !AppConfig.isEInkMode, onMenuOutEnd: (() -> Unit)? = null) {
callBack.onMenuHide()
this.onMenuOutEnd = onMenuOutEnd
if (this.isVisible) {
if (anim) {
Expand Down Expand Up @@ -558,6 +585,8 @@ class ReadMenu @JvmOverloads constructor(
fun payAction()
fun disableSource()
fun skipToChapter(index: Int)
fun onMenuShow()
fun onMenuHide()
}

}
10 changes: 9 additions & 1 deletion app/src/main/java/io/legado/app/ui/book/read/SearchMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import io.legado.app.lib.theme.bottomBackground
import io.legado.app.lib.theme.getPrimaryTextColor
import io.legado.app.model.ReadBook
import io.legado.app.ui.book.searchContent.SearchResult
import io.legado.app.utils.*
import io.legado.app.utils.ColorUtils
import io.legado.app.utils.activity
import io.legado.app.utils.invisible
import io.legado.app.utils.loadAnimation
import io.legado.app.utils.navigationBarGravity
import io.legado.app.utils.navigationBarHeight
import io.legado.app.utils.visible
import splitties.views.bottomPadding
import splitties.views.leftPadding
import splitties.views.padding
Expand Down Expand Up @@ -235,6 +241,8 @@ class SearchMenu @JvmOverloads constructor(
fun exitSearchMenu()
fun showMenuBar()
fun navigateToSearch(searchResult: SearchResult, index: Int)
fun onMenuShow()
fun onMenuHide()
}

}
Loading

0 comments on commit 7fbc23a

Please sign in to comment.