From 98e11facb26a4dc2a3d4fb84e8ed8baba4906bf7 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 31 Aug 2020 21:57:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E4=BD=93=E8=B7=9F=E9=9A=8F=E8=83=8C?= =?UTF-8?q?=E6=99=AF,=E6=AF=8F=E4=B8=AA=E8=83=8C=E6=99=AF=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E4=B8=80=E4=B8=AA=E5=AD=97=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/PreferKey.kt | 1 - .../java/io/legado/app/help/ReadBookConfig.kt | 15 ++++++-- .../ui/book/read/config/ReadStyleDialog.kt | 11 +++--- .../read/page/provider/ChapterProvider.kt | 24 ++++++++---- .../app/ui/widget/font/FontSelectDialog.kt | 38 ++++--------------- 5 files changed, 41 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index bf98afd11..c6e222849 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -22,7 +22,6 @@ object PreferKey { const val cleanCache = "cleanCache" const val saveTabPosition = "saveTabPosition" const val pageAnim = "pageAnim" - const val readBookFont = "readBookFont" const val fontFolder = "fontFolder" const val backupPath = "backupUri" const val restoreIgnore = "restoreIgnore" 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 c6c71598c..060430832 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -83,8 +83,10 @@ object ReadBookConfig { fun save() { Coroutine.async { - val json = GSON.toJson(configList) - FileUtils.createFileIfNotExist(configFilePath).writeText(json) + synchronized(this) { + val json = GSON.toJson(configList) + FileUtils.createFileIfNotExist(configFilePath).writeText(json) + } } } @@ -149,6 +151,12 @@ object ReadBookConfig { private val config get() = if (shareLayout) getConfig(5) else durConfig + var textFont: String + get() = config.textFont + set(value) { + config.textFont = value + } + var textBold: Int get() = config.textBold set(value) { @@ -296,6 +304,7 @@ object ReadBookConfig { private var darkStatusIconNight: Boolean = false,//晚上是否暗色状态栏 private var textColor: String = "#3E3D3B",//白天文字颜色 private var textColorNight: String = "#ADADAD",//夜间文字颜色 + var textFont: String = "",//字体 var textBold: Int = 0,//是否粗体字 0:正常, 1:粗体, 2:细体 var textSize: Int = 20,//文字大小 var letterSpacing: Float = 0.1f,//字间距 @@ -318,7 +327,7 @@ object ReadBookConfig { var footerPaddingRight: Int = 16, var footerPaddingTop: Int = 6, var showHeaderLine: Boolean = false, - var showFooterLine: Boolean = true + var showFooterLine: Boolean = true, ) { fun setBg(bgType: Int, bg: String) { if (AppConfig.isNightTheme) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt index 77b789b24..624f10b7a 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt @@ -9,7 +9,6 @@ import androidx.core.view.get import io.legado.app.R import io.legado.app.base.BaseDialogFragment import io.legado.app.constant.EventBus -import io.legado.app.constant.PreferKey import io.legado.app.help.ReadBookConfig import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.selector @@ -256,10 +255,12 @@ class ReadStyleDialog : BaseDialogFragment(), FontSelectDialog.CallBack { } override val curFontPath: String - get() = requireContext().getPrefString(PreferKey.readBookFont) ?: "" + get() = ReadBookConfig.textFont - override fun selectFile(path: String) { - requireContext().putPrefString(PreferKey.readBookFont, path) - postEvent(EventBus.UP_CONFIG, true) + override fun selectFont(path: String) { + if (path != ReadBookConfig.textFont) { + ReadBookConfig.textFont = path + postEvent(EventBus.UP_CONFIG, true) + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 849c69b84..f22b91933 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -1,14 +1,13 @@ package io.legado.app.ui.book.read.page.provider import android.graphics.Typeface +import android.net.Uri import android.os.Build import android.text.Layout import android.text.StaticLayout import android.text.TextPaint -import android.text.TextUtils import io.legado.app.App import io.legado.app.constant.AppPattern -import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.AppConfig @@ -382,18 +381,27 @@ object ChapterProvider { */ fun upStyle() { typeface = try { - val fontPath = App.INSTANCE.getPrefString(PreferKey.readBookFont) - if (!TextUtils.isEmpty(fontPath)) { - Typeface.createFromFile(fontPath) - } else { - when (AppConfig.systemTypefaces) { + val fontPath = ReadBookConfig.textFont + when { + fontPath.isContentPath() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { + val fd = App.INSTANCE.contentResolver + .openFileDescriptor(Uri.parse(fontPath), "r")!! + .fileDescriptor + Typeface.Builder(fd).build() + } + fontPath.isContentPath() -> { + Typeface.createFromFile(RealPathUtil.getPath(App.INSTANCE, Uri.parse(fontPath))) + } + fontPath.isNotEmpty() -> Typeface.createFromFile(fontPath) + else -> when (AppConfig.systemTypefaces) { 1 -> Typeface.SERIF 2 -> Typeface.MONOSPACE else -> Typeface.SANS_SERIF } } } catch (e: Exception) { - App.INSTANCE.removePref(PreferKey.readBookFont) + ReadBookConfig.textFont = "" + ReadBookConfig.save() Typeface.SANS_SERIF } // 字体统一处理 diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index 697c3f68c..f7aff548e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -15,7 +15,6 @@ import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseDialogFragment -import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.help.AppConfig import io.legado.app.help.permission.Permissions @@ -28,7 +27,6 @@ import io.legado.app.utils.* import kotlinx.android.synthetic.main.dialog_font_select.* import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import java.io.File import java.util.* @@ -170,34 +168,16 @@ class FontSelectDialog : BaseDialogFragment(), override fun onClick(docItem: DocItem) { execute { - fontFolder.listFiles()?.forEach { - it.delete() - } - if (docItem.isContentPath) { - val file = FileUtils.createFileIfNotExist(fontFolder, docItem.name) - @Suppress("BlockingMethodInNonBlockingContext") - requireActivity().contentResolver.openInputStream(docItem.uri)?.use { input -> - file.outputStream().use { output -> - input.copyTo(output) - } - } - callBack?.selectFile(file.path) - } else { - val file = File(docItem.uri.toString()) - file.copyTo(FileUtils.createFileIfNotExist(fontFolder, file.name), true) - .absolutePath.let { path -> - if (curFilePath != path) { - withContext(Main) { - callBack?.selectFile(path) - } - } - } - } + FileUtils.deleteFile(fontFolder.absolutePath) + callBack?.selectFont(docItem.uri.toString()) }.onSuccess { dialog?.dismiss() } } + /** + * 字体文件夹 + */ override fun onFilePicked(requestCode: Int, currentPath: String) { when (requestCode) { fontFolderRequestCode -> { @@ -231,11 +211,7 @@ class FontSelectDialog : BaseDialogFragment(), } private fun onDefaultFontChange() { - if (curFilePath == "") { - postEvent(EventBus.UP_CONFIG, true) - } else { - callBack?.selectFile("") - } + callBack?.selectFont("") } override val curFilePath: String get() = callBack?.curFontPath ?: "" @@ -244,7 +220,7 @@ class FontSelectDialog : BaseDialogFragment(), get() = (parentFragment as? CallBack) ?: (activity as? CallBack) interface CallBack { - fun selectFile(path: String) + fun selectFont(path: String) val curFontPath: String } } \ No newline at end of file