Skip to content

Commit

Permalink
move setNightMode to ThemeUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
tsynik committed Nov 17, 2023
1 parent 8959d3f commit 0e5172b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
12 changes: 3 additions & 9 deletions app/src/main/java/ru/yourok/torrserve/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import android.os.Looper
import android.os.PowerManager
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.multidex.MultiDexApplication
import ru.yourok.torrserve.atv.Utils
import ru.yourok.torrserve.settings.Settings
import ru.yourok.torrserve.utils.ThemeUtil


class App : MultiDexApplication() {

Expand Down Expand Up @@ -87,12 +86,7 @@ class App : MultiDexApplication() {
// Track activities
registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks)
// DayNight Auto ON/OFF
when (Settings.getTheme()) {
"dark", "black" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
else -> if (Utils.isTvBox()) AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_TIME) else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) // phones
}
ThemeUtil.setNightMode()

val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TorrServe:WakeLock")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import ru.yourok.torrserve.ui.fragments.main.update.apk.ApkUpdateFragment
import ru.yourok.torrserve.ui.fragments.main.update.apk.UpdaterApk
import ru.yourok.torrserve.ui.fragments.speedtest.SpeedTest
import ru.yourok.torrserve.utils.Accessibility
import ru.yourok.torrserve.utils.ThemeUtil
import ru.yourok.torrserve.utils.ThemeUtil.Companion.isDarkMode


Expand Down Expand Up @@ -136,6 +137,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
summary = "$summary (${darkMode})"
setOnPreferenceChangeListener { _, newValue ->
Settings.setTheme(newValue.toString())
ThemeUtil.setNightMode()
requireActivity().recreate()
true
}
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/ru/yourok/torrserve/utils/ThemeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import android.util.TypedValue
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import ru.yourok.torrserve.BuildConfig
import ru.yourok.torrserve.R
import ru.yourok.torrserve.app.App
import ru.yourok.torrserve.atv.Utils
import ru.yourok.torrserve.settings.Settings
import ru.yourok.torrserve.settings.Settings.getTheme

class ThemeUtil {
Expand Down Expand Up @@ -41,8 +44,7 @@ class ThemeUtil {
companion object {
val selectedTheme: Int
get() {
val theme = getTheme()
return when (theme) {
return when (getTheme()) {
"light" -> R.style.Theme_TorrServe_Light
"dark" -> R.style.Theme_TorrServe_Dark
"black" -> R.style.Theme_TorrServe_Black
Expand All @@ -69,5 +71,14 @@ class ThemeUtil {
darkModeFlag == Configuration.UI_MODE_NIGHT_YES
}
}

fun setNightMode() {
when (getTheme()) {
"dark", "black" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
else -> if (Utils.isTvBox()) AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_TIME) else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) // phones
}
}
}
}

0 comments on commit 0e5172b

Please sign in to comment.