From dd83e98d08175aaa94bdac14f1e04dbaef2aa8eb Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 11 Sep 2020 14:26:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=8C=E6=99=AF=E9=85=8D=E7=BD=AE=E8=87=AA?= =?UTF-8?q?=E7=94=B1=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/readConfig.json | 10 -- .../java/io/legado/app/help/ReadBookConfig.kt | 107 +++++++++-------- .../java/io/legado/app/help/storage/Backup.kt | 4 + .../io/legado/app/help/storage/Restore.kt | 13 +- .../ui/book/read/config/BgTextConfigDialog.kt | 4 - .../ui/book/read/config/ReadStyleDialog.kt | 112 +++++++----------- .../main/res/layout/dialog_read_bg_text.xml | 6 +- .../res/layout/dialog_read_book_style.xml | 84 +------------ app/src/main/res/layout/item_bg_image.xml | 6 +- app/src/main/res/layout/item_read_style.xml | 12 ++ 10 files changed, 145 insertions(+), 213 deletions(-) create mode 100644 app/src/main/res/layout/item_read_style.xml diff --git a/app/src/main/assets/readConfig.json b/app/src/main/assets/readConfig.json index c94ecda3b..968141a0c 100644 --- a/app/src/main/assets/readConfig.json +++ b/app/src/main/assets/readConfig.json @@ -1,14 +1,4 @@ [ - { - "bgStr": "#FFFFFF", - "bgStrNight": "#000000", - "textColor": "#000000", - "textColorNight": "#FFFFFF", - "bgType": 0, - "bgTypeNight": 0, - "darkStatusIcon": true, - "darkStatusIconNight": false - }, { "bgStr": "#FFFFFF", "bgStrNight": "#000000", 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 0472bed7c..8eadae8ed 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -21,18 +21,21 @@ import java.io.File @Keep object ReadBookConfig { const val configFileName = "readConfig.json" + const val shareConfigFileName = "shareReadConfig.json" val configFilePath = FileUtils.getPath(App.INSTANCE.filesDir, configFileName) + val shareConfigFilePath = FileUtils.getPath(App.INSTANCE.filesDir, shareConfigFileName) val configList: ArrayList = arrayListOf() + lateinit var shareConfig: Config private val defaultConfigs by lazy { val json = String(App.INSTANCE.assets.open(configFileName).readBytes()) GSON.fromJsonArray(json)!! } var durConfig - get() = getConfig(styleSelect + 1) + get() = getConfig(styleSelect) set(value) { - configList[styleSelect + 1] = value + configList[styleSelect] = value if (shareLayout) { - configList[5] = value + shareConfig = value } upBg() } @@ -42,7 +45,8 @@ object ReadBookConfig { val textColor: Int get() = durConfig.textColor() init { - upConfig() + initConfigs() + initShareConfig() } @Synchronized @@ -50,30 +54,38 @@ object ReadBookConfig { if (configList.size < 5) { resetAll() } - if (configList.size < 6) { - configList.add(0, Config()) - } - return configList[index] + return configList.getOrNull(index) ?: configList[0] } - fun upConfig() { - (getConfigs() ?: defaultConfigs).let { + fun initConfigs() { + val configFile = File(configFilePath) + var configs: List? = null + if (configFile.exists()) { + try { + val json = configFile.readText() + configs = GSON.fromJsonArray(json) + } catch (e: Exception) { + e.printStackTrace() + } + } + (configs ?: defaultConfigs).let { configList.clear() configList.addAll(it) } } - private fun getConfigs(): List? { + fun initShareConfig() { val configFile = File(configFilePath) + var c: Config? = null if (configFile.exists()) { try { val json = configFile.readText() - return GSON.fromJsonArray(json) + c = GSON.fromJsonObject(json) } catch (e: Exception) { e.printStackTrace() } } - return null + shareConfig = c ?: configList.getOrNull(5) ?: Config() } fun upBg() { @@ -160,7 +172,7 @@ object ReadBookConfig { var hideStatusBar = App.INSTANCE.getPrefBoolean(PreferKey.hideStatusBar) var hideNavigationBar = App.INSTANCE.getPrefBoolean(PreferKey.hideNavigationBar) - val config get() = if (shareLayout) getConfig(0) else durConfig + val config get() = if (shareLayout) shareConfig else durConfig var textFont: String get() = config.textFont @@ -308,39 +320,38 @@ object ReadBookConfig { fun getExportConfig(): Config { val exportConfig = GSON.fromJsonObject(GSON.toJson(durConfig))!! if (shareLayout) { - val shearConfig = getConfig(0) - exportConfig.textFont = shearConfig.textFont - exportConfig.textBold = shearConfig.textBold - exportConfig.textSize = shearConfig.textSize - exportConfig.letterSpacing = shearConfig.letterSpacing - exportConfig.lineSpacingExtra = shearConfig.lineSpacingExtra - exportConfig.paragraphSpacing = shearConfig.paragraphSpacing - exportConfig.titleMode = shearConfig.titleMode - exportConfig.titleSize = shearConfig.titleSize - exportConfig.titleTopSpacing = shearConfig.titleTopSpacing - exportConfig.titleBottomSpacing = shearConfig.titleBottomSpacing - exportConfig.paddingBottom = shearConfig.paddingBottom - exportConfig.paddingLeft = shearConfig.paddingLeft - exportConfig.paddingRight = shearConfig.paddingRight - exportConfig.paddingTop = shearConfig.paddingTop - exportConfig.headerPaddingBottom = shearConfig.headerPaddingBottom - exportConfig.headerPaddingLeft = shearConfig.headerPaddingLeft - exportConfig.headerPaddingRight = shearConfig.headerPaddingRight - exportConfig.headerPaddingTop = shearConfig.headerPaddingTop - exportConfig.footerPaddingBottom = shearConfig.footerPaddingBottom - exportConfig.footerPaddingLeft = shearConfig.footerPaddingLeft - exportConfig.footerPaddingRight = shearConfig.footerPaddingRight - exportConfig.footerPaddingTop = shearConfig.footerPaddingTop - exportConfig.showHeaderLine = shearConfig.showHeaderLine - exportConfig.showFooterLine = shearConfig.showFooterLine - exportConfig.tipHeaderLeft = shearConfig.tipHeaderLeft - exportConfig.tipHeaderMiddle = shearConfig.tipHeaderMiddle - exportConfig.tipHeaderRight = shearConfig.tipHeaderRight - exportConfig.tipFooterLeft = shearConfig.tipFooterLeft - exportConfig.tipFooterMiddle = shearConfig.tipFooterMiddle - exportConfig.tipFooterRight = shearConfig.tipFooterRight - exportConfig.hideHeader = shearConfig.hideHeader - exportConfig.hideFooter = shearConfig.hideFooter + exportConfig.textFont = shareConfig.textFont + exportConfig.textBold = shareConfig.textBold + exportConfig.textSize = shareConfig.textSize + exportConfig.letterSpacing = shareConfig.letterSpacing + exportConfig.lineSpacingExtra = shareConfig.lineSpacingExtra + exportConfig.paragraphSpacing = shareConfig.paragraphSpacing + exportConfig.titleMode = shareConfig.titleMode + exportConfig.titleSize = shareConfig.titleSize + exportConfig.titleTopSpacing = shareConfig.titleTopSpacing + exportConfig.titleBottomSpacing = shareConfig.titleBottomSpacing + exportConfig.paddingBottom = shareConfig.paddingBottom + exportConfig.paddingLeft = shareConfig.paddingLeft + exportConfig.paddingRight = shareConfig.paddingRight + exportConfig.paddingTop = shareConfig.paddingTop + exportConfig.headerPaddingBottom = shareConfig.headerPaddingBottom + exportConfig.headerPaddingLeft = shareConfig.headerPaddingLeft + exportConfig.headerPaddingRight = shareConfig.headerPaddingRight + exportConfig.headerPaddingTop = shareConfig.headerPaddingTop + exportConfig.footerPaddingBottom = shareConfig.footerPaddingBottom + exportConfig.footerPaddingLeft = shareConfig.footerPaddingLeft + exportConfig.footerPaddingRight = shareConfig.footerPaddingRight + exportConfig.footerPaddingTop = shareConfig.footerPaddingTop + exportConfig.showHeaderLine = shareConfig.showHeaderLine + exportConfig.showFooterLine = shareConfig.showFooterLine + exportConfig.tipHeaderLeft = shareConfig.tipHeaderLeft + exportConfig.tipHeaderMiddle = shareConfig.tipHeaderMiddle + exportConfig.tipHeaderRight = shareConfig.tipHeaderRight + exportConfig.tipFooterLeft = shareConfig.tipFooterLeft + exportConfig.tipFooterMiddle = shareConfig.tipFooterMiddle + exportConfig.tipFooterRight = shareConfig.tipFooterRight + exportConfig.hideHeader = shareConfig.hideHeader + exportConfig.hideFooter = shareConfig.hideFooter } return exportConfig } @@ -365,7 +376,7 @@ object ReadBookConfig { var textSize: Int = 20,//文字大小 var letterSpacing: Float = 0.1f,//字间距 var lineSpacingExtra: Int = 12,//行间距 - var paragraphSpacing: Int = 4,//段距 + var paragraphSpacing: Int = 2,//段距 var titleMode: Int = 0,//标题居中 var titleSize: Int = 0, var titleTopSpacing: Int = 0, diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index b52688846..d1437ce7c 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -35,6 +35,7 @@ object Backup { "readRecord.json", "httpTTS.json", ReadBookConfig.configFileName, + ReadBookConfig.shareConfigFileName, ThemeConfig.configFileName, "config.xml" ) @@ -67,6 +68,9 @@ object Backup { FileUtils.createFileIfNotExist(backupPath + File.separator + ReadBookConfig.configFileName) .writeText(it) } + GSON.toJson(ReadBookConfig.shareConfig).let { + FileUtils.createFileIfNotExist(backupPath + File.separator + ReadBookConfig.shareConfigFileName) + } GSON.toJson(ThemeConfig.configList).let { FileUtils.createFileIfNotExist(backupPath + File.separator + ThemeConfig.configFileName) .writeText(it) diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 8f89a45fe..18880822e 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -177,7 +177,18 @@ object Restore { if (file.exists()) { FileUtils.deleteFile(ReadBookConfig.configFilePath) file.copyTo(File(ReadBookConfig.configFilePath)) - ReadBookConfig.upConfig() + ReadBookConfig.initConfigs() + } + } catch (e: Exception) { + e.printStackTrace() + } + try { + val file = + FileUtils.createFileIfNotExist(path + File.separator + ReadBookConfig.shareConfigFileName) + if (file.exists()) { + FileUtils.deleteFile(ReadBookConfig.shareConfigFilePath) + file.copyTo(File(ReadBookConfig.shareConfigFilePath)) + ReadBookConfig.initShareConfig() } } catch (e: Exception) { e.printStackTrace() diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 7eb4ffcef..46e5dbd0f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -11,8 +11,6 @@ import android.os.Bundle import android.util.DisplayMetrics import android.view.* import androidx.documentfile.provider.DocumentFile -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView import com.jaredrummler.android.colorpicker.ColorPickerDialog import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -111,8 +109,6 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack { private fun initData() = with(ReadBookConfig.durConfig) { sw_dark_status_icon.isChecked = statusIconDark() adapter = BgAdapter(requireContext()) - recycler_view.layoutManager = - LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false) recycler_view.adapter = adapter val headerView = LayoutInflater.from(requireContext()) .inflate(R.layout.item_bg_image, recycler_view, false) 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 689752171..5696a638d 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 @@ -8,6 +8,8 @@ import android.view.* import androidx.core.view.get import io.legado.app.R import io.legado.app.base.BaseDialogFragment +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.constant.EventBus import io.legado.app.help.ReadBookConfig import io.legado.app.lib.dialogs.alert @@ -21,6 +23,7 @@ import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_book_read.* import kotlinx.android.synthetic.main.dialog_read_book_style.* import kotlinx.android.synthetic.main.dialog_title_config.view.* +import kotlinx.android.synthetic.main.item_read_style.view.* import org.jetbrains.anko.sdk27.listeners.onCheckedChange import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onLongClick @@ -28,6 +31,7 @@ import org.jetbrains.anko.sdk27.listeners.onLongClick class ReadStyleDialog : BaseDialogFragment(), FontSelectDialog.CallBack { val callBack get() = activity as? ReadBookActivity + lateinit var styleAdapter: StyleAdapter override fun onStart() { super.onStart() @@ -82,6 +86,8 @@ class ReadStyleDialog : BaseDialogFragment(), FontSelectDialog.CallBack { } dsb_line_size.valueFormat = { ((it - 10) / 10f).toString() } dsb_paragraph_spacing.valueFormat = { (it / 10f).toString() } + styleAdapter = StyleAdapter() + rv_style.adapter = styleAdapter } private fun initData() { @@ -92,8 +98,7 @@ class ReadStyleDialog : BaseDialogFragment(), FontSelectDialog.CallBack { } } upStyle() - setBg() - upBg() + styleAdapter.setItems(ReadBookConfig.configList) } private fun initViewEvent() { @@ -152,16 +157,6 @@ class ReadStyleDialog : BaseDialogFragment(), FontSelectDialog.CallBack { ReadBookConfig.paragraphSpacing = it postEvent(EventBus.UP_CONFIG, true) } - bg0.onClick { changeBg(0) } - bg0.onLongClick { showBgTextConfig(0) } - bg1.onClick { changeBg(1) } - bg1.onLongClick { showBgTextConfig(1) } - bg2.onClick { changeBg(2) } - bg2.onLongClick { showBgTextConfig(2) } - bg3.onClick { changeBg(3) } - bg3.onLongClick { showBgTextConfig(3) } - bg4.onClick { changeBg(4) } - bg4.onLongClick { showBgTextConfig(4) } } @SuppressLint("InflateParams") @@ -195,11 +190,13 @@ class ReadStyleDialog : BaseDialogFragment(), FontSelectDialog.CallBack { } private fun changeBg(index: Int) { - if (ReadBookConfig.styleSelect != index) { + val oldIndex = ReadBookConfig.styleSelect + if (index != oldIndex) { ReadBookConfig.styleSelect = index ReadBookConfig.upBg() upStyle() - upBg() + styleAdapter.notifyItemChanged(oldIndex) + styleAdapter.notifyItemChanged(index) postEvent(EventBus.UP_CONFIG, true) } } @@ -220,59 +217,6 @@ class ReadStyleDialog : BaseDialogFragment(), FontSelectDialog.CallBack { } } - private fun setBg() = ReadBookConfig.apply { - bg0.setTextColor(getConfig(0).textColor()) - bg1.setTextColor(getConfig(1).textColor()) - bg2.setTextColor(getConfig(2).textColor()) - bg3.setTextColor(getConfig(3).textColor()) - bg4.setTextColor(getConfig(4).textColor()) - for (i in 0..4) { - val iv = when (i) { - 1 -> bg1 - 2 -> bg2 - 3 -> bg3 - 4 -> bg4 - else -> bg0 - } - iv.setImageDrawable(getConfig(i).bgDrawable(100, 150)) - } - } - - private fun upBg() = ReadBookConfig.apply { - bg0.borderColor = getConfig(0).textColor() - bg0.setTextBold(false) - bg1.borderColor = getConfig(1).textColor() - bg1.setTextBold(false) - bg2.borderColor = getConfig(2).textColor() - bg2.setTextBold(false) - bg3.borderColor = getConfig(3).textColor() - bg3.setTextBold(false) - bg4.borderColor = getConfig(4).textColor() - bg4.setTextBold(false) - when (styleSelect) { - 1 -> { - bg1.borderColor = accentColor - bg1.setTextBold(true) - } - 2 -> { - bg2.borderColor = accentColor - bg2.setTextBold(true) - } - 3 -> { - bg3.borderColor = accentColor - bg3.setTextBold(true) - } - 4 -> { - bg4.borderColor = accentColor - bg4.setTextBold(true) - } - else -> { - bg0.borderColor = accentColor - bg0.setTextBold(true) - } - } - } - override val curFontPath: String get() = ReadBookConfig.textFont @@ -282,4 +226,38 @@ class ReadStyleDialog : BaseDialogFragment(), FontSelectDialog.CallBack { postEvent(EventBus.UP_CONFIG, true) } } + + inner class StyleAdapter : + SimpleRecyclerAdapter(requireContext(), R.layout.item_read_style) { + + override fun convert( + holder: ItemViewHolder, + item: ReadBookConfig.Config, + payloads: MutableList + ) { + holder.itemView.apply { + iv_style.setTextColor(item.textColor()) + iv_style.setImageDrawable(item.bgDrawable(100, 150)) + if (ReadBookConfig.styleSelect == holder.layoutPosition) { + iv_style.borderColor = accentColor + iv_style.setTextBold(true) + } else { + iv_style.borderColor = item.textColor() + iv_style.setTextBold(false) + } + } + } + + override fun registerListener(holder: ItemViewHolder) { + holder.itemView.apply { + iv_style.onClick { + changeBg(holder.layoutPosition) + } + iv_style.onLongClick { + showBgTextConfig(holder.layoutPosition) + } + } + } + + } } \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_read_bg_text.xml b/app/src/main/res/layout/dialog_read_bg_text.xml index cc47ac88d..0be5daf57 100644 --- a/app/src/main/res/layout/dialog_read_bg_text.xml +++ b/app/src/main/res/layout/dialog_read_bg_text.xml @@ -112,7 +112,9 @@ android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="100dp" - android:padding="6dp" /> - + android:orientation="horizontal" + android:padding="6dp" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + tools:listitem="@layout/item_bg_image" /> \ No newline at end of file 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 2b0e7fd4a..1d0fcd6a8 100644 --- a/app/src/main/res/layout/dialog_read_book_style.xml +++ b/app/src/main/res/layout/dialog_read_book_style.xml @@ -1,6 +1,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + tool:listitem="@layout/item_read_style" /> \ No newline at end of file diff --git a/app/src/main/res/layout/item_bg_image.xml b/app/src/main/res/layout/item_bg_image.xml index 2530fb894..4bf9cf5fe 100644 --- a/app/src/main/res/layout/item_bg_image.xml +++ b/app/src/main/res/layout/item_bg_image.xml @@ -1,7 +1,8 @@ @@ -11,7 +12,8 @@ android:layout_height="0dp" android:layout_weight="1" android:scaleType="fitCenter" - android:contentDescription="@string/bg_image" /> + android:contentDescription="@string/bg_image" + tool:src="@drawable/ic_image" /> + \ No newline at end of file