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 5414ba3e8..6c856df14 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 @@ -18,7 +18,6 @@ import androidx.core.view.size import com.jaredrummler.android.colorpicker.ColorPickerDialogListener import io.legado.app.BuildConfig import io.legado.app.R -import io.legado.app.base.VMBaseActivity import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.constant.Status @@ -68,7 +67,7 @@ import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.toast -class ReadBookActivity : VMBaseActivity(R.layout.activity_book_read), +class ReadBookActivity : ReadBookBaseActivity(), View.OnTouchListener, PageView.CallBack, TextActionMenu.CallBack, @@ -87,15 +86,12 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo private var menu: Menu? = null private var textActionMenu: TextActionMenu? = null - override val viewModel: ReadBookViewModel - get() = getViewModel(ReadBookViewModel::class.java) - override val scope: CoroutineScope get() = this override val isInitFinish: Boolean get() = viewModel.isInitFinish override val isScroll: Boolean get() = page_view.isScroll private val mHandler = Handler(Looper.getMainLooper()) private val keepScreenRunnable: Runnable = - Runnable { ReadBookActivityHelp.keepScreenOn(window, false) } + Runnable { keepScreenOn(window, false) } private val autoPageRunnable: Runnable = Runnable { autoPagePlus() } override var autoPageProgress = 0 override var isAutoPage = false @@ -105,14 +101,8 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo override val pageFactory: TextPageFactory get() = page_view.pageFactory override val headerHeight: Int get() = page_view.curPage.headerHeight - override fun onCreate(savedInstanceState: Bundle?) { - ReadBook.msg = null - ReadBookActivityHelp.setOrientation(this) - super.onCreate(savedInstanceState) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { - ReadBookActivityHelp.upLayoutInDisplayCutoutMode(window) + upLayoutInDisplayCutoutMode() initView() upScreenTimeOut() ReadBook.callBack = this @@ -198,14 +188,6 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } } - fun showPaddingConfig() { - PaddingConfigDialog().show(supportFragmentManager, "paddingConfig") - } - - fun showBgTextConfig() { - BgTextConfigDialog().show(supportFragmentManager, "bgTextConfig") - } - override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.read_book, menu) return super.onCompatCreateOptionsMenu(menu) @@ -257,8 +239,8 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo viewModel.refreshContent(it) } } - R.id.menu_download -> ReadBookActivityHelp.showDownloadDialog(this) - R.id.menu_add_bookmark -> ReadBookActivityHelp.showBookMark(this) + R.id.menu_download -> showDownloadDialog() + R.id.menu_add_bookmark -> showBookMark() R.id.menu_copy_text -> TextDialog.show(supportFragmentManager, ReadBook.curTextChapter?.getContent()) R.id.menu_update_toc -> ReadBook.book?.let { @@ -274,7 +256,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo menu?.findItem(R.id.menu_re_segment)?.isChecked = it.getReSegment() ReadBook.loadContent(false) } - R.id.menu_page_anim -> ReadBookActivityHelp.showPageAnimConfig(this) { + R.id.menu_page_anim -> showPageAnimConfig { page_view.upPageAnim() } R.id.menu_book_info -> ReadBook.book?.let { @@ -293,7 +275,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo Pair("loginUrl", it.loginUrl) ) } - R.id.menu_set_charset -> ReadBookActivityHelp.showCharsetConfig(this) + R.id.menu_set_charset -> showCharsetConfig() } return super.onCompatOptionsItemSelected(item) } @@ -324,13 +306,13 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo */ override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { when { - ReadBookActivityHelp.isPrevKey(this, keyCode) -> { + isPrevKey(keyCode) -> { if (keyCode != KeyEvent.KEYCODE_UNKNOWN) { page_view.pageDelegate?.keyTurnPage(PageDelegate.Direction.PREV) return true } } - ReadBookActivityHelp.isNextKey(this, keyCode) -> { + isNextKey(keyCode) -> { if (keyCode != KeyEvent.KEYCODE_UNKNOWN) { page_view.pageDelegate?.keyTurnPage(PageDelegate.Direction.NEXT) return true @@ -736,7 +718,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo * 更新状态栏,导航栏 */ override fun upSystemUiVisibility() { - ReadBookActivityHelp.upSystemUiVisibility(this, isInMultiWindow, !read_menu.isVisible) + upSystemUiVisibility(isInMultiWindow, !read_menu.isVisible) upNavigationBarColor() } @@ -950,16 +932,16 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo */ override fun screenOffTimerStart() { if (screenTimeOut < 0) { - ReadBookActivityHelp.keepScreenOn(window, true) + keepScreenOn(window, true) return } val t = screenTimeOut - sysScreenOffTime if (t > 0) { mHandler.removeCallbacks(keepScreenRunnable) - ReadBookActivityHelp.keepScreenOn(window, true) + keepScreenOn(window, true) mHandler.postDelayed(keepScreenRunnable, screenTimeOut) } else { - ReadBookActivityHelp.keepScreenOn(window, false) + keepScreenOn(window, false) } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivityHelp.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt similarity index 80% rename from app/src/main/java/io/legado/app/ui/book/read/ReadBookActivityHelp.kt rename to app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt index 6d2baa4ce..59706d338 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivityHelp.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt @@ -1,14 +1,14 @@ package io.legado.app.ui.book.read import android.annotation.SuppressLint -import android.app.Activity -import android.content.Context import android.content.pm.ActivityInfo import android.os.Build +import android.os.Bundle import android.view.* import android.widget.EditText import io.legado.app.App import io.legado.app.R +import io.legado.app.base.VMBaseActivity import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Bookmark import io.legado.app.help.AppConfig @@ -20,27 +20,60 @@ import io.legado.app.lib.theme.ThemeStore import io.legado.app.lib.theme.backgroundColor import io.legado.app.service.help.CacheBook import io.legado.app.service.help.ReadBook +import io.legado.app.ui.book.read.config.BgTextConfigDialog +import io.legado.app.ui.book.read.config.PaddingConfigDialog import io.legado.app.ui.widget.text.AutoCompleteTextView import io.legado.app.utils.applyTint import io.legado.app.utils.getPrefString +import io.legado.app.utils.getViewModel import io.legado.app.utils.requestInputMethod import kotlinx.android.synthetic.main.dialog_download_choice.view.* import kotlinx.android.synthetic.main.dialog_edit_text.view.* -import org.jetbrains.anko.layoutInflater +abstract class ReadBookBaseActivity : + VMBaseActivity(R.layout.activity_book_read) { + + override val viewModel: ReadBookViewModel + get() = getViewModel(ReadBookViewModel::class.java) + + + override fun onCreate(savedInstanceState: Bundle?) { + ReadBook.msg = null + setOrientation() + super.onCreate(savedInstanceState) + } + + fun showPaddingConfig() { + PaddingConfigDialog().show(supportFragmentManager, "paddingConfig") + } + + fun showBgTextConfig() { + BgTextConfigDialog().show(supportFragmentManager, "bgTextConfig") + } + + /** + * 屏幕方向 + */ + @SuppressLint("SourceLockedOrientationActivity") + fun setOrientation() { + when (AppConfig.requestedDirection) { + "0" -> requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + "1" -> requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + "2" -> requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + "3" -> requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR + } + } -object ReadBookActivityHelp { /** * 更新状态栏,导航栏 */ fun upSystemUiVisibility( - activity: Activity, isInMultiWindow: Boolean, toolBarHide: Boolean = true ) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - activity.window.insetsController?.let { + window.insetsController?.let { if (toolBarHide) { if (ReadBookConfig.hideStatusBar) { it.hide(WindowInsets.Type.statusBars()) @@ -54,20 +87,19 @@ object ReadBookActivityHelp { } } } - upSystemUiVisibilityO(activity, isInMultiWindow, toolBarHide) + upSystemUiVisibilityO(isInMultiWindow, toolBarHide) if (toolBarHide) { - ATH.setLightStatusBar(activity, ReadBookConfig.durConfig.curStatusIconDark()) + ATH.setLightStatusBar(this, ReadBookConfig.durConfig.curStatusIconDark()) } else { ATH.setLightStatusBarAuto( - activity, - ThemeStore.statusBarColor(activity, AppConfig.isTransparentStatusBar) + this, + ThemeStore.statusBarColor(this, AppConfig.isTransparentStatusBar) ) } } @Suppress("DEPRECATION") private fun upSystemUiVisibilityO( - activity: Activity, isInMultiWindow: Boolean, toolBarHide: Boolean = true ) { @@ -88,20 +120,7 @@ object ReadBookActivityHelp { flag = flag or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION } } - activity.window.decorView.systemUiVisibility = flag - } - - /** - * 屏幕方向 - */ - @SuppressLint("SourceLockedOrientationActivity") - fun setOrientation(activity: Activity) = activity.apply { - when (AppConfig.requestedDirection) { - "0" -> requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED - "1" -> requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT - "2" -> requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE - "3" -> requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR - } + window.decorView.systemUiVisibility = flag } @@ -119,7 +138,7 @@ object ReadBookActivityHelp { /** * 适配刘海 */ - fun upLayoutInDisplayCutoutMode(window: Window) { + fun upLayoutInDisplayCutoutMode() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && AppConfig.readBodyToLh) { window.attributes = window.attributes.apply { layoutInDisplayCutoutMode = @@ -129,12 +148,13 @@ object ReadBookActivityHelp { } @SuppressLint("InflateParams") - fun showDownloadDialog(context: Context) { + fun showDownloadDialog() { ReadBook.book?.let { book -> - context.alert(titleResource = R.string.offline_cache) { + alert(titleResource = R.string.offline_cache) { var view: View? = null customView { - LayoutInflater.from(context).inflate(R.layout.dialog_download_choice, null) + LayoutInflater.from(this@ReadBookBaseActivity) + .inflate(R.layout.dialog_download_choice, null) .apply { view = this setBackgroundColor(context.backgroundColor) @@ -155,10 +175,10 @@ object ReadBookActivityHelp { } @SuppressLint("InflateParams") - fun showBookMark(context: Context) = with(context) { + fun showBookMark() { val book = ReadBook.book ?: return val textChapter = ReadBook.curTextChapter ?: return - context.alert(title = getString(R.string.bookmark_add)) { + alert(title = getString(R.string.bookmark_add)) { var editText: EditText? = null message = book.name + " • " + textChapter.title customView { @@ -188,7 +208,7 @@ object ReadBookActivityHelp { } @SuppressLint("InflateParams") - fun showCharsetConfig(context: Context) = with(context) { + fun showCharsetConfig() { val charsets = arrayListOf("UTF-8", "GB2312", "GBK", "Unicode", "UTF-16", "UTF-16LE", "ASCII") alert(R.string.set_charset) { @@ -210,7 +230,7 @@ object ReadBookActivityHelp { }.show().applyTint() } - fun showPageAnimConfig(context: Context, success: () -> Unit) = with(context) { + fun showPageAnimConfig(success: () -> Unit) { val items = arrayListOf() items.add(getString(R.string.btn_default_s)) items.add(getString(R.string.page_anim_cover)) @@ -224,13 +244,13 @@ object ReadBookActivityHelp { } } - fun isPrevKey(context: Context, keyCode: Int): Boolean { - val prevKeysStr = context.getPrefString(PreferKey.prevKeys) + fun isPrevKey(keyCode: Int): Boolean { + val prevKeysStr = getPrefString(PreferKey.prevKeys) return prevKeysStr?.split(",")?.contains(keyCode.toString()) ?: false } - fun isNextKey(context: Context, keyCode: Int): Boolean { - val nextKeysStr = context.getPrefString(PreferKey.nextKeys) + fun isNextKey(keyCode: Int): Boolean { + val nextKeysStr = getPrefString(PreferKey.nextKeys) return nextKeysStr?.split(",")?.contains(keyCode.toString()) ?: false } } \ No newline at end of file