diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 8565ceb9e..fed02381c 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -15,9 +15,9 @@ import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdWeb import io.legado.app.data.AppDatabase import io.legado.app.help.ActivityHelp +import io.legado.app.help.AppConfig import io.legado.app.help.CrashHandler import io.legado.app.help.ReadBookConfig -import io.legado.app.help.isNightTheme import io.legado.app.lib.theme.ThemeStore import io.legado.app.ui.book.read.page.ChapterProvider import io.legado.app.utils.getCompatColor @@ -50,7 +50,7 @@ class App : Application() { } if (!ThemeStore.isConfigured(this, versionCode)) applyTheme() - initNightTheme() + initNightMode() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannelId() @@ -67,7 +67,7 @@ class App : Application() { * 更新主题 */ fun applyTheme() { - if (isNightTheme) { + if (AppConfig.isNightTheme) { ThemeStore.editTheme(this) .primaryColor( getPrefInt("colorPrimaryNight", getCompatColor(R.color.shine_color)) @@ -98,11 +98,11 @@ class App : Application() { fun applyDayNight() { ReadBookConfig.upBg() applyTheme() - initNightTheme() + initNightMode() } - private fun initNightTheme() { - val targetMode = if (isNightTheme) { + private fun initNightMode() { + val targetMode = if (AppConfig.isNightTheme) { AppCompatDelegate.MODE_NIGHT_YES } else { AppCompatDelegate.MODE_NIGHT_NO diff --git a/app/src/main/java/io/legado/app/constant/Theme.kt b/app/src/main/java/io/legado/app/constant/Theme.kt index 975eaa802..b8587997b 100644 --- a/app/src/main/java/io/legado/app/constant/Theme.kt +++ b/app/src/main/java/io/legado/app/constant/Theme.kt @@ -1,14 +1,13 @@ package io.legado.app.constant -import io.legado.app.App -import io.legado.app.help.isNightTheme +import io.legado.app.help.AppConfig enum class Theme { Dark, Light, Auto; companion object { fun getTheme(): Theme { - return if (App.INSTANCE.isNightTheme) { + return if (AppConfig.isNightTheme) { Dark } else Light } diff --git a/app/src/main/java/io/legado/app/help/AppConfig.kt b/app/src/main/java/io/legado/app/help/AppConfig.kt new file mode 100644 index 000000000..8190cfd79 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -0,0 +1,41 @@ +package io.legado.app.help + +import io.legado.app.App +import io.legado.app.constant.PreferKey +import io.legado.app.utils.getPrefBoolean +import io.legado.app.utils.getPrefInt +import io.legado.app.utils.putPrefBoolean +import io.legado.app.utils.putPrefInt + +object AppConfig { + + var isNightTheme: Boolean + get() = App.INSTANCE.getPrefBoolean("isNightTheme") + set(value) { + App.INSTANCE.putPrefBoolean("isNightTheme", value) + } + + var isTransparentStatusBar: Boolean + get() = App.INSTANCE.getPrefBoolean("transparentStatusBar") + set(value) { + App.INSTANCE.putPrefBoolean("transparentStatusBar", value) + } + + var isShowRSS: Boolean + get() = App.INSTANCE.getPrefBoolean(PreferKey.showRss) + set(value) { + App.INSTANCE.putPrefBoolean(PreferKey.showRss, value) + } + + var threadCount: Int + get() = App.INSTANCE.getPrefInt(PreferKey.threadCount) + set(value) { + App.INSTANCE.putPrefInt(PreferKey.threadCount, value) + } + + var autoDarkMode: Boolean + get() = App.INSTANCE.getPrefBoolean(PreferKey.autoDarkMode) + set(value) { + App.INSTANCE.putPrefBoolean(PreferKey.autoDarkMode, value) + } +} diff --git a/app/src/main/java/io/legado/app/help/PrefExtensions.kt b/app/src/main/java/io/legado/app/help/PrefExtensions.kt deleted file mode 100644 index 4b8daf1e4..000000000 --- a/app/src/main/java/io/legado/app/help/PrefExtensions.kt +++ /dev/null @@ -1,21 +0,0 @@ -package io.legado.app.help - -import android.content.Context -import io.legado.app.constant.PreferKey -import io.legado.app.utils.getPrefBoolean -import io.legado.app.utils.getPrefInt - -val Context.isNightTheme: Boolean - get() = getPrefBoolean("isNightTheme") - -val Context.isTransparentStatusBar: Boolean - get() = getPrefBoolean("transparentStatusBar", true) - -val Context.isShowRSS: Boolean - get() = getPrefBoolean(PreferKey.showRss, true) - -val Context.threadCount: Int - get() = getPrefInt(PreferKey.threadCount, 16) - -val Context.autoDarkMode: Boolean - get() = getPrefBoolean(PreferKey.autoDarkMode, true) \ No newline at end of file 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 328433c3c..311093c13 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -111,7 +111,7 @@ object ReadBookConfig { var textSize: Int = 15 ) { fun setBg(bgType: Int, bg: String) { - if (App.INSTANCE.isNightTheme) { + if (AppConfig.isNightTheme) { bgTypeNight = bgType bgStrNight = bg } else { @@ -121,7 +121,7 @@ object ReadBookConfig { } fun setTextColor(color: Int) { - if (App.INSTANCE.isNightTheme) { + if (AppConfig.isNightTheme) { textColorNight = "#${color.hexString}" } else { textColor = "#${color.hexString}" @@ -129,7 +129,7 @@ object ReadBookConfig { } fun setStatusIconDark(isDark: Boolean) { - if (App.INSTANCE.isNightTheme) { + if (AppConfig.isNightTheme) { darkStatusIconNight = isDark } else { darkStatusIcon = isDark @@ -137,7 +137,7 @@ object ReadBookConfig { } fun statusIconDark(): Boolean { - return if (App.INSTANCE.isNightTheme) { + return if (AppConfig.isNightTheme) { darkStatusIconNight } else { darkStatusIcon @@ -145,17 +145,17 @@ object ReadBookConfig { } fun textColor(): Int { - return if (App.INSTANCE.isNightTheme) Color.parseColor(textColorNight) + return if (AppConfig.isNightTheme) Color.parseColor(textColorNight) else Color.parseColor(textColor) } fun bgStr(): String { - return if (App.INSTANCE.isNightTheme) bgStrNight + return if (AppConfig.isNightTheme) bgStrNight else bgStr } fun bgType(): Int { - return if (App.INSTANCE.isNightTheme) bgTypeNight + return if (AppConfig.isNightTheme) bgTypeNight else bgType } diff --git a/app/src/main/java/io/legado/app/lib/theme/ATH.kt b/app/src/main/java/io/legado/app/lib/theme/ATH.kt index 91c7fcda2..c625e1807 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ATH.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ATH.kt @@ -16,8 +16,7 @@ import androidx.recyclerview.widget.RecyclerView import androidx.viewpager.widget.ViewPager import com.google.android.material.bottomnavigation.BottomNavigationView import io.legado.app.R -import io.legado.app.help.isNightTheme -import io.legado.app.help.isTransparentStatusBar +import io.legado.app.help.AppConfig import io.legado.app.utils.getCompatColor import kotlinx.android.synthetic.main.activity_main.view.* import org.jetbrains.anko.backgroundColor @@ -37,7 +36,7 @@ object ATH { } fun setStatusBarColorAuto(activity: Activity, fullScreen: Boolean) { - val isTransparentStatusBar = activity.isTransparentStatusBar + val isTransparentStatusBar = AppConfig.isTransparentStatusBar setStatusBarColor( activity, ThemeStore.statusBarColor(activity, isTransparentStatusBar), @@ -131,14 +130,14 @@ object ATH { fun setTint( view: View, @ColorInt color: Int, - isDark: Boolean = view.context.isNightTheme + isDark: Boolean = AppConfig.isNightTheme ) { TintHelper.setTintAuto(view, color, false, isDark) } fun setBackgroundTint( view: View, @ColorInt color: Int, - isDark: Boolean = view.context.isNightTheme + isDark: Boolean = AppConfig.isNightTheme ) { TintHelper.setTintAuto(view, color, true, isDark) } @@ -206,7 +205,7 @@ object ATH { .setSelectedColor(ThemeStore.accentColor(bottom_navigation_view.context)).create() itemIconTintList = colorStateList itemTextColor = colorStateList - itemBackgroundResource = when(context.isNightTheme) { + itemBackgroundResource = when (AppConfig.isNightTheme) { true -> R.drawable.item_bg_dark false -> R.drawable.item_bg_light } diff --git a/app/src/main/java/io/legado/app/service/DownloadService.kt b/app/src/main/java/io/legado/app/service/DownloadService.kt index 6770f7db7..9c2e7a2f3 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -9,10 +9,10 @@ import io.legado.app.base.BaseService import io.legado.app.constant.Action import io.legado.app.constant.AppConst import io.legado.app.constant.Bus +import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp import io.legado.app.help.IntentHelp import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.threadCount import io.legado.app.model.WebBook import io.legado.app.utils.postEvent import kotlinx.coroutines.Dispatchers.IO @@ -21,7 +21,7 @@ import java.util.concurrent.Executors class DownloadService : BaseService() { private var searchPool = - Executors.newFixedThreadPool(App.INSTANCE.threadCount).asCoroutineDispatcher() + Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() private var tasks: ArrayList> = arrayListOf() private val handler = Handler() private var runnable: Runnable = Runnable { upDownload() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/Help.kt b/app/src/main/java/io/legado/app/ui/book/read/Help.kt index 9d1be9f0c..939377cb1 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/Help.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/Help.kt @@ -9,8 +9,8 @@ import android.view.Window import android.view.WindowManager import io.legado.app.App import io.legado.app.constant.PreferKey +import io.legado.app.help.AppConfig import io.legado.app.help.ReadBookConfig -import io.legado.app.help.isTransparentStatusBar import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ThemeStore import io.legado.app.utils.getPrefBoolean @@ -43,7 +43,7 @@ object Help { } else { ATH.setLightStatusBarAuto( activity, - ThemeStore.statusBarColor(activity, activity.isTransparentStatusBar) + ThemeStore.statusBarColor(activity, AppConfig.isTransparentStatusBar) ) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt index 972f89cb7..0583056cd 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt @@ -11,7 +11,7 @@ import androidx.core.view.isVisible import io.legado.app.App import io.legado.app.R import io.legado.app.constant.PreferKey -import io.legado.app.help.isNightTheme +import io.legado.app.help.AppConfig import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.buttonDisabledColor import io.legado.app.service.help.ReadBook @@ -42,7 +42,7 @@ class ReadMenu : FrameLayout { init { callBack = activity as? CallBack inflate(context, R.layout.view_read_menu, this) - if (context.isNightTheme) { + if (AppConfig.isNightTheme) { fabNightTheme.setImageResource(R.drawable.ic_daytime) } else { fabNightTheme.setImageResource(R.drawable.ic_brightness) @@ -145,7 +145,7 @@ class ReadMenu : FrameLayout { //夜间模式 fabNightTheme.onClick { - context.putPrefBoolean("isNightTheme", !context.isNightTheme) + context.putPrefBoolean("isNightTheme", !AppConfig.isNightTheme) App.INSTANCE.applyDayNight() } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index 00148a7fd..125204062 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -6,8 +6,8 @@ import io.legado.app.base.BaseViewModel import io.legado.app.constant.PreferKey import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchKeyword +import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.threadCount import io.legado.app.model.WebBook import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefString @@ -18,7 +18,7 @@ import java.util.concurrent.Executors class SearchViewModel(application: Application) : BaseViewModel(application) { private var searchPool = - Executors.newFixedThreadPool(context.threadCount).asCoroutineDispatcher() + Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() private var task: Coroutine<*>? = null var callBack: CallBack? = null var searchKey: String = "" diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt index 81dfcebe9..3b16a4359 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt @@ -8,8 +8,8 @@ import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book import io.legado.app.data.entities.SearchBook +import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.threadCount import io.legado.app.model.WebBook import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -20,7 +20,7 @@ import java.util.concurrent.Executors class ChangeSourceViewModel(application: Application) : BaseViewModel(application) { private var searchPool = - Executors.newFixedThreadPool(context.threadCount).asCoroutineDispatcher() + Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() var callBack: CallBack? = null val searchStateData = MutableLiveData() var name: String = "" diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index 07a5a641d..2645002fe 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -12,8 +12,8 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.constant.Bus import io.legado.app.constant.PreferKey +import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp -import io.legado.app.help.threadCount import io.legado.app.lib.theme.ATH import io.legado.app.receiver.SharedReceiverActivity import io.legado.app.ui.filechooser.FileChooserDialog @@ -61,7 +61,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), .setTitle(getString(R.string.threads_num_title)) .setMaxValue(999) .setMinValue(1) - .setValue(requireContext().threadCount) + .setValue(AppConfig.threadCount) .show { requireContext().putPrefInt(PreferKey.threadCount, it) findPreference(PreferKey.threadCount)?.summary = @@ -125,7 +125,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), PreferKey.downloadPath -> getPrefString(PreferKey.downloadPath) ?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath ?: App.INSTANCE.cacheDir.absolutePath - PreferKey.threadCount -> requireContext().threadCount + PreferKey.threadCount -> AppConfig.threadCount else -> getPrefString(key) ?: "" } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 0b47b24f6..bdaf72031 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -16,8 +16,8 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus import io.legado.app.constant.PreferKey +import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.Coroutine -import io.legado.app.help.isShowRSS import io.legado.app.help.storage.Backup import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService @@ -47,7 +47,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager_main.addOnPageChangeListener(this) bottom_navigation_view.setOnNavigationItemSelectedListener(this) - bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = isShowRSS + bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS upVersion() } @@ -68,10 +68,10 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), fragmentList.add(RssFragment()) fragmentList.add(MyFragment()) } - if (isShowRSS && fragmentList.size < 4) { + if (AppConfig.isShowRSS && fragmentList.size < 4) { fragmentList.add(2, RssFragment()) } - if (!isShowRSS && fragmentList.size == 4) { + if (!AppConfig.isShowRSS && fragmentList.size == 4) { fragmentList.removeAt(2) } } @@ -89,7 +89,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), pagePosition = position when (position) { 0, 1, 3 -> bottom_navigation_view.menu.getItem(position).isChecked = true - 2 -> if (isShowRSS) { + 2 -> if (AppConfig.isShowRSS) { bottom_navigation_view.menu.getItem(position).isChecked = true } else { bottom_navigation_view.menu.getItem(3).isChecked = true @@ -147,10 +147,10 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), recreate() } observeEvent(Bus.SHOW_RSS) { - bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = isShowRSS + bottom_navigation_view.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS upFragmentList() view_pager_main.adapter?.notifyDataSetChanged() - if (isShowRSS) { + if (AppConfig.isShowRSS) { view_pager_main.setCurrentItem(3, false) } } @@ -168,7 +168,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } override fun getCount(): Int { - return if (isShowRSS) 4 else 3 + return if (AppConfig.isShowRSS) 4 else 3 } }