Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Feb 12, 2024
1 parent b79f59a commit 5ac5f8d
Show file tree
Hide file tree
Showing 23 changed files with 734 additions and 307 deletions.
17 changes: 16 additions & 1 deletion app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,22 @@ private constructor(private val mContext: Context) : ThemeStoreInterface {
.apply()
}

companion object {
companion object : SharedPreferences.OnSharedPreferenceChangeListener {

init {
prefs(appCtx).registerOnSharedPreferenceChangeListener(this)
}

var accentColor = accentColor()

override fun onSharedPreferenceChanged(
sharedPreferences: SharedPreferences?,
key: String?
) {
when (key) {
ThemeStorePrefKeys.KEY_ACCENT_COLOR -> accentColor = accentColor()
}
}

fun editTheme(context: Context): ThemeStore {
return ThemeStore(context)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/io/legado/app/model/ImageProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,9 @@ object ImageProvider {
}.getOrDefault(errorBitmap)
}

fun clear() {
bitmapLruCache.evictAll()
BitmapCache.clear()
}

}
28 changes: 20 additions & 8 deletions app/src/main/java/io/legado/app/model/ReadBook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ object ReadBook : CoroutineScope by MainScope() {
return hasPrevPage
}

fun moveToNextChapter(upContent: Boolean): Boolean {
fun moveToNextChapter(upContent: Boolean, upContentInPlace: Boolean = true): Boolean {
if (durChapterIndex < chapterSize - 1) {
durChapterPos = 0
durChapterIndex++
Expand All @@ -213,11 +213,11 @@ object ReadBook : CoroutineScope by MainScope() {
nextTextChapter = null
if (curTextChapter == null) {
AppLog.putDebug("moveToNextChapter-章节未加载,开始加载")
callBack?.upContent()
if (upContentInPlace) callBack?.upContent(resetPageOffset = false)
loadContent(durChapterIndex, upContent, resetPageOffset = false)
} else if (upContent) {
} else if (upContent && upContentInPlace) {
AppLog.putDebug("moveToNextChapter-章节已加载,刷新视图")
callBack?.upContent()
callBack?.upContent(resetPageOffset = false)
}
loadContent(durChapterIndex.plus(1), upContent, false)
saveRead()
Expand All @@ -233,7 +233,8 @@ object ReadBook : CoroutineScope by MainScope() {

fun moveToPrevChapter(
upContent: Boolean,
toLast: Boolean = true
toLast: Boolean = true,
upContentInPlace: Boolean = true
): Boolean {
if (durChapterIndex > 0) {
durChapterPos = if (toLast) prevTextChapter?.lastReadLength ?: Int.MAX_VALUE else 0
Expand All @@ -242,10 +243,10 @@ object ReadBook : CoroutineScope by MainScope() {
curTextChapter = prevTextChapter
prevTextChapter = null
if (curTextChapter == null) {
callBack?.upContent()
if (upContentInPlace) callBack?.upContent(resetPageOffset = false)
loadContent(durChapterIndex, upContent, resetPageOffset = false)
} else if (upContent) {
callBack?.upContent()
} else if (upContent && upContentInPlace) {
callBack?.upContent(resetPageOffset = false)
}
loadContent(durChapterIndex.minus(1), upContent, false)
saveRead()
Expand All @@ -267,6 +268,16 @@ object ReadBook : CoroutineScope by MainScope() {
}

fun setPageIndex(index: Int) {
val textChapter = curTextChapter
if (textChapter != null) {
val pageIndex = durPageIndex
if (index > pageIndex) {
textChapter.getPage(index - 2)?.recyclePictures()
}
if (index < pageIndex) {
textChapter.getPage(index + 2)?.recyclePictures()
}
}
durChapterPos = curTextChapter?.getReadLength(index) ?: index
saveRead(true)
curPageChanged(true)
Expand Down Expand Up @@ -595,6 +606,7 @@ object ReadBook : CoroutineScope by MainScope() {
coroutineContext.cancelChildren()
downloadedChapters.clear()
downloadFailChapters.clear()
ImageProvider.clear()
}

interface CallBack {
Expand Down
30 changes: 12 additions & 18 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 @@ -97,6 +97,7 @@ 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
import io.legado.app.utils.getPrefString
import io.legado.app.utils.hexString
Expand Down Expand Up @@ -218,9 +219,10 @@ class ReadBookActivity : BaseReadBookActivity(),
private val prevPageDebounce by lazy { Debounce { keyPage(PageDirection.PREV) } }
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) } }

//恢复跳转前进度对话框的交互结果
private var confirmRestoreProcess: Boolean? = null
Expand Down Expand Up @@ -265,22 +267,14 @@ class ReadBookActivity : BaseReadBookActivity(),
override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
viewModel.initData(intent) {
initDataSuccess()
upMenu()
}
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
viewModel.initData(intent ?: return) {
initDataSuccess()
}
}

private fun initDataSuccess() {
upMenu()
if (reloadContent) {
reloadContent = false
ReadBook.loadContent(resetPageOffset = false)
upMenu()
}
}

Expand Down Expand Up @@ -1532,8 +1526,6 @@ class ReadBookActivity : BaseReadBookActivity(),
if (it) { // 更新内容排版布局
if (isInitFinish) {
ReadBook.loadContent(resetPageOffset = false)
} else {
reloadContent = true
}
} else {
readView.upContent(resetPageOffset = false)
Expand Down Expand Up @@ -1582,6 +1574,9 @@ class ReadBookActivity : BaseReadBookActivity(),
observeEvent<Boolean>(EventBus.UP_SEEK_BAR) {
binding.readMenu.upSeekBar()
}
observeEvent<String>(EventBus.RECREATE) {
binding.readView.invalidateTextPage()
}
}

private fun upScreenTimeOut() {
Expand All @@ -1594,17 +1589,16 @@ class ReadBookActivity : BaseReadBookActivity(),
* 重置黑屏时间
*/
override fun screenOffTimerStart() {
keepScreenJon?.cancel()
keepScreenJon = lifecycleScope.launch {
handler.post {
if (screenTimeOut < 0) {
keepScreenOn(true)
return@launch
return@post
}
val t = screenTimeOut - sysScreenOffTime
if (t > 0) {
keepScreenOn(true)
delay(screenTimeOut)
keepScreenOn(false)
handler.removeCallbacks(screenOffRunnable)
handler.postDelayed(screenOffRunnable, screenTimeOut)
} else {
keepScreenOn(false)
}
Expand Down
Loading

0 comments on commit 5ac5f8d

Please sign in to comment.