From fb860c23490b57c88317cadb0181d3daa7cf3a08 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 8 Sep 2020 15:58:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E9=A2=98=E5=AF=BC=E5=85=A5=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/ThemeConfig.kt | 20 +++++++++++------- .../source/edit/BookSourceEditActivity.kt | 11 ++-------- .../legado/app/ui/config/ThemeListDialog.kt | 21 ++++++++++++++++++- .../rss/source/edit/RssSourceEditActivity.kt | 11 ++-------- .../io/legado/app/utils/ContextExtensions.kt | 10 +++++++++ app/src/main/res/menu/theme_list.xml | 10 +++++++++ 6 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 app/src/main/res/menu/theme_list.xml diff --git a/app/src/main/java/io/legado/app/help/ThemeConfig.kt b/app/src/main/java/io/legado/app/help/ThemeConfig.kt index 1cea05cbd..91342bfc9 100644 --- a/app/src/main/java/io/legado/app/help/ThemeConfig.kt +++ b/app/src/main/java/io/legado/app/help/ThemeConfig.kt @@ -40,7 +40,13 @@ object ThemeConfig { } } - fun addConfig(newConfig: Config) { + fun addConfig(json: String) { + GSON.fromJsonObject(json)?.let { + addConfig(it) + } + } + + private fun addConfig(newConfig: Config) { configList.forEachIndexed { index, config -> if (newConfig.themeName == config.themeName) { configList[index] = newConfig @@ -125,12 +131,12 @@ object ThemeConfig { } class Config( - var themeName: String = "典雅蓝", - var isNightTheme: Boolean = false, - var primaryColor: String = "#03A9F4", - var accentColor: String = "#AD1457", - var backgroundColor: String = "#F5F5F5", - var bottomBackground: String = "#EEEEEE" + var themeName: String, + var isNightTheme: Boolean, + var primaryColor: String, + var accentColor: String, + var backgroundColor: String, + var bottomBackground: String ) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 457a2b0ba..8a7deb2ab 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -1,9 +1,6 @@ package io.legado.app.ui.book.source.edit import android.app.Activity -import android.content.ClipData -import android.content.ClipboardManager -import android.content.Context import android.content.Intent import android.graphics.Rect import android.net.Uri @@ -28,10 +25,7 @@ import io.legado.app.ui.book.source.debug.BookSourceDebugActivity import io.legado.app.ui.login.SourceLogin import io.legado.app.ui.qrcode.QrCodeActivity import io.legado.app.ui.widget.KeyboardToolPop -import io.legado.app.utils.GSON -import io.legado.app.utils.applyTint -import io.legado.app.utils.getViewModel -import io.legado.app.utils.shareWithQr +import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_book_source_edit.* import org.jetbrains.anko.* import kotlin.math.abs @@ -82,8 +76,7 @@ class BookSourceEditActivity : } R.id.menu_copy_source -> getSource().let { source -> GSON.toJson(source)?.let { sourceStr -> - val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager - clipboard?.setPrimaryClip(ClipData.newPlainText(null, sourceStr)) + sendToClip(sourceStr) } } R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) } diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeListDialog.kt b/app/src/main/java/io/legado/app/ui/config/ThemeListDialog.kt index b7cf85e19..ceb96fe02 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeListDialog.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeListDialog.kt @@ -3,8 +3,10 @@ package io.legado.app.ui.config import android.os.Bundle import android.util.DisplayMetrics import android.view.LayoutInflater +import android.view.MenuItem import android.view.View import android.view.ViewGroup +import androidx.appcompat.widget.Toolbar import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -18,12 +20,13 @@ import io.legado.app.lib.theme.primaryColor import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.GSON import io.legado.app.utils.applyTint +import io.legado.app.utils.getClipText import kotlinx.android.synthetic.main.dialog_recycler_view.* import kotlinx.android.synthetic.main.item_theme_config.view.* import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.share -class ThemeListDialog : BaseDialogFragment() { +class ThemeListDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { private lateinit var adapter: Adapter @@ -46,6 +49,7 @@ class ThemeListDialog : BaseDialogFragment() { tool_bar.setBackgroundColor(primaryColor) tool_bar.setTitle(R.string.theme_list) initView() + initMenu() initData() } @@ -56,10 +60,25 @@ class ThemeListDialog : BaseDialogFragment() { recycler_view.adapter = adapter } + private fun initMenu() { + tool_bar.setOnMenuItemClickListener(this) + } + fun initData() { adapter.setItems(ThemeConfig.configList) } + override fun onMenuItemClick(item: MenuItem?): Boolean { + when (item?.itemId) { + R.id.menu_import -> { + requireContext().getClipText()?.let { + ThemeConfig.addConfig(it) + } + } + } + return true + } + fun delete(index: Int) { alert(R.string.delete, R.string.sure_del) { okButton { diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt index bd070639a..5072861ec 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt @@ -1,9 +1,6 @@ package io.legado.app.ui.rss.source.edit import android.app.Activity -import android.content.ClipData -import android.content.ClipboardManager -import android.content.Context import android.content.Intent import android.graphics.Rect import android.os.Bundle @@ -23,10 +20,7 @@ import io.legado.app.lib.theme.ATH import io.legado.app.ui.qrcode.QrCodeActivity import io.legado.app.ui.rss.source.debug.RssSourceDebugActivity import io.legado.app.ui.widget.KeyboardToolPop -import io.legado.app.utils.GSON -import io.legado.app.utils.applyTint -import io.legado.app.utils.getViewModel -import io.legado.app.utils.shareWithQr +import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_rss_source_edit.* import org.jetbrains.anko.* import kotlin.math.abs @@ -99,8 +93,7 @@ class RssSourceEditActivity : } R.id.menu_copy_source -> { GSON.toJson(getRssSource())?.let { sourceStr -> - val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager - clipboard?.setPrimaryClip(ClipData.newPlainText(null, sourceStr)) + sendToClip(sourceStr) } } R.id.menu_qr_code_camera -> startActivityForResult(qrRequestCode) diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 272c8d02d..489f5d508 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -146,6 +146,16 @@ fun Context.sendToClip(text: String) { } } +fun Context.getClipText(): String? { + val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager? + clipboard?.primaryClip?.let { + if (it.itemCount > 0) { + return it.getItemAt(0).text.toString().trim() + } + } + return null +} + /** * 系统是否暗色主题 */ diff --git a/app/src/main/res/menu/theme_list.xml b/app/src/main/res/menu/theme_list.xml new file mode 100644 index 000000000..bd7f6194d --- /dev/null +++ b/app/src/main/res/menu/theme_list.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file