From ab2072fcf99d57c6d86e33d5142c3e34c1908d42 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 24 Aug 2019 20:36:21 +0800 Subject: [PATCH] up --- .../java/io/legado/app/help/ReadBookConfig.kt | 43 +++++++++++-------- .../java/io/legado/app/ui/readbook/Help.kt | 26 +++++++++++ .../io/legado/app/ui/readbook/ReadMenu.kt | 6 ++- .../res/layout/dialog_read_book_style.xml | 18 +++++++- app/src/main/res/values/strings.xml | 2 +- 5 files changed, 75 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt index 0dc5e2f18..d3f6c69a3 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -17,31 +17,37 @@ import java.io.IOException * 阅读界面配置 */ object ReadBookConfig { - private val configList: ArrayList by lazy { - val list: ArrayList = arrayListOf() - val configFile = File(App.INSTANCE.filesDir.absolutePath + File.separator + "config") + const val fileName = "readConfig.json" + private val configList: ArrayList = arrayListOf() + .apply { + upConfig(this) + } + + var styleSelect + get() = App.INSTANCE.getPrefInt("readStyleSelect") + set(value) = App.INSTANCE.putPrefInt("readStyleSelect", value) + var bg: Drawable? = null + + fun getConfig(index: Int = styleSelect): Config { + return configList[index] + } + + fun upConfig(list: ArrayList = configList) { + val configFile = File(App.INSTANCE.filesDir.absolutePath + File.separator + fileName) val json = if (configFile.exists()) { String(configFile.readBytes()) } else { - String(App.INSTANCE.assets.open("readConfig.json").readBytes()) + String(App.INSTANCE.assets.open(fileName).readBytes()) } try { GSON.fromJsonArray(json)?.let { + list.clear() list.addAll(it) } } catch (e: Exception) { + list.clear() list.addAll(getOnError()) } - list - } - - var styleSelect - get() = App.INSTANCE.getPrefInt("readStyleSelect") - set(value) = App.INSTANCE.putPrefInt("readStyleSelect", value) - var bg: Drawable? = null - - fun getConfig(index: Int = styleSelect): Config { - return configList[index] } fun upBg() { @@ -50,7 +56,7 @@ object ReadBookConfig { fun save() { val json = GSON.toJson(configList) - val configFile = File(App.INSTANCE.filesDir.absolutePath + File.separator + "config") + val configFile = File(App.INSTANCE.filesDir.absolutePath + File.separator + fileName) //获取流并存储 try { BufferedWriter(FileWriter(configFile)).use { writer -> @@ -64,7 +70,7 @@ object ReadBookConfig { fun reset() { try { - val json = String(App.INSTANCE.assets.open("readConfig.json").readBytes()) + val json = String(App.INSTANCE.assets.open(fileName).readBytes()) GSON.fromJsonArray(json)?.let { configList.clear() configList.addAll(it) @@ -109,7 +115,10 @@ object ReadBookConfig { when (bgType) { 0 -> bgDrawable = ColorDrawable(Color.parseColor(bgStr)) 1 -> bgDrawable = - Drawable.createFromStream(App.INSTANCE.assets.open("bg" + File.separator + bgStr), "bg") + Drawable.createFromStream( + App.INSTANCE.assets.open("bg" + File.separator + bgStr), + "bg" + ) else -> runCatching { bgDrawable = Drawable.createFromPath(bgStr) } diff --git a/app/src/main/java/io/legado/app/ui/readbook/Help.kt b/app/src/main/java/io/legado/app/ui/readbook/Help.kt index 3f5a4cd2f..13bdebf50 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/Help.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/Help.kt @@ -1,12 +1,18 @@ package io.legado.app.ui.readbook +import android.app.Activity import android.view.View +import android.view.View.NO_ID +import android.view.ViewGroup import android.view.Window import io.legado.app.App import io.legado.app.utils.getPrefBoolean + object Help { + private const val NAVIGATION = "navigationBarBackground" + fun upSystemUiVisibility(window: Window, hide: Boolean = true) { var flag = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE @@ -28,4 +34,24 @@ object Help { window.decorView.systemUiVisibility = flag } + // 该方法需要在View完全被绘制出来之后调用,否则判断不了 + //在比如 onWindowFocusChanged()方法中可以得到正确的结果 + fun isNavigationBarExist(activity: Activity?): Boolean { + activity?.let { + val vp = it.window.decorView as? ViewGroup + if (vp != null) { + for (i in 0 until vp.childCount) { + vp.getChildAt(i).context.packageName + if (vp.getChildAt(i).id != NO_ID && NAVIGATION == activity.resources.getResourceEntryName( + vp.getChildAt(i).id + ) + ) { + return true + } + } + } + } + return false + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadMenu.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadMenu.kt index 7ed2418b2..81bdb2616 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadMenu.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadMenu.kt @@ -182,7 +182,11 @@ class ReadMenu : FrameLayout { override fun onAnimationEnd(animation: Animation) { vw_menu_bg.onClick { runMenuOut() } val lp = vwNavigationBar.layoutParams - lp.height = if (context.getPrefBoolean("hideNavigationBar")) + lp.height = + if (context.getPrefBoolean("hideNavigationBar") and Help.isNavigationBarExist( + activity + ) + ) context.getNavigationBarHeight() else 0 vwNavigationBar.layoutParams = lp } diff --git a/app/src/main/res/layout/dialog_read_book_style.xml b/app/src/main/res/layout/dialog_read_book_style.xml index 83fc3f585..76b32aa5b 100644 --- a/app/src/main/res/layout/dialog_read_book_style.xml +++ b/app/src/main/res/layout/dialog_read_book_style.xml @@ -14,6 +14,22 @@ android:background="@color/btn_bg_press" app:layout_constraintTop_toTopOf="parent" /> + + + + + app:layout_constraintTop_toBottomOf="@+id/vw_bg_fg1" /> 音量键翻页 点击翻页 点击总是翻下一页 - 翻页动画 + 翻页动画 屏幕超时 返回 菜单