From 7824d938551ae4a87aac0c0335ab68a2e8f29867 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 3 Feb 2020 12:25:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/entities/Book.kt | 4 ++ .../io/legado/app/service/help/ReadBook.kt | 1 + .../app/ui/book/read/ReadBookActivity.kt | 37 +++++++++++++++---- app/src/main/res/menu/read_book.xml | 2 +- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index 27f9baec8..0c43860f0 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -53,6 +53,10 @@ data class Book( return origin == BookType.local } + fun isTxt(): Boolean { + return isLocalBook() && originName.endsWith(".txt", true) + } + @Ignore @IgnoredOnParcel override var variableMap: HashMap? = null diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index 770b21e18..f5227f731 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -39,6 +39,7 @@ object ReadBook { durChapterIndex = book.durChapterIndex durPageIndex = book.durChapterPos isLocalBook = book.origin == BookType.local + webBook = null App.db.bookSourceDao().getBookSource(book.origin)?.let { webBook = WebBook(it) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index ee387edb8..60057a622 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -13,7 +13,9 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.EditText +import androidx.core.view.get import androidx.core.view.isVisible +import androidx.core.view.size import androidx.lifecycle.Observer import com.jaredrummler.android.colorpicker.ColorPickerDialogListener import io.legado.app.App @@ -67,7 +69,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo private val requestCodeChapterList = 568 private val requestCodeEditSource = 111 private val requestCodeReplace = 312 - private var replaceUseMenu: MenuItem? = null + private var menu: Menu? = null override val viewModel: ReadBookViewModel get() = getViewModel(ReadBookViewModel::class.java) @@ -147,16 +149,34 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.read_book, menu) - replaceUseMenu = menu.findItem(R.id.menu_enable_replace) - upMenu() return super.onCompatCreateOptionsMenu(menu) } + override fun onPrepareOptionsMenu(menu: Menu?): Boolean { + this.menu = menu + upMenu() + return super.onPrepareOptionsMenu(menu) + } + private fun upMenu() { - ReadBook.book?.let { - replaceUseMenu?.isChecked = it.useReplaceRule + menu?.let { menu -> + ReadBook.book?.let { book -> + val onLine = !book.isLocalBook() + for (i in 0 until menu.size) { + val item = menu[i] + when (item.groupId) { + R.id.menu_group_on_line -> item.isVisible = onLine + R.id.menu_group_local -> item.isVisible = !onLine + R.id.menu_group_text -> item.isVisible = book.isTxt() + R.id.menu_group_login -> + item.isVisible = !ReadBook.webBook?.bookSource?.loginUrl.isNullOrEmpty() + else -> if (item.itemId == R.id.menu_enable_replace) { + item.isChecked = book.useReplaceRule + } + } + } + } } - } /** @@ -221,7 +241,8 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo ReadBook.durChapterIndex, ReadBook.durPageIndex, textChapter!!.title, - editContent) + editContent + ) App.db.bookmarkDao().insert(bookmark) } } @@ -237,7 +258,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } R.id.menu_enable_replace -> ReadBook.book?.let { it.useReplaceRule = !it.useReplaceRule - replaceUseMenu?.isChecked = it.useReplaceRule + menu?.findItem(R.id.menu_enable_replace)?.isChecked = it.useReplaceRule } } return super.onCompatOptionsItemSelected(item) diff --git a/app/src/main/res/menu/read_book.xml b/app/src/main/res/menu/read_book.xml index a67fd0fb6..b27cd41ff 100644 --- a/app/src/main/res/menu/read_book.xml +++ b/app/src/main/res/menu/read_book.xml @@ -3,7 +3,7 @@ xmlns:tools="http://schemas.android.com/tools" tools:context=".ui.main.MainActivity"> - +