Skip to content

Commit

Permalink
Save night mode status
Browse files Browse the repository at this point in the history
  • Loading branch information
SeniorZhai committed Mar 11, 2022
1 parent 42d98bb commit 80baafa
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 51 deletions.
106 changes: 61 additions & 45 deletions app/src/main/java/one/mixin/android/ui/setting/AppearanceFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import one.mixin.android.vo.Fiats
import one.mixin.android.widget.theme.Coordinate
import one.mixin.android.widget.theme.NightModeSwitch.Companion.ANIM_DURATION
import one.mixin.android.widget.theme.ThemeActivity
import timber.log.Timber
import java.util.Locale

@AndroidEntryPoint
Expand All @@ -41,6 +42,23 @@ class AppearanceFragment : BaseFragment(R.layout.fragment_appearance) {
}

private val binding by viewBinding(FragmentAppearanceBinding::bind)
private val androidQ = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
private val androidS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
private val systemNightMode by lazy {
MixinApplication.appContext.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}
private val localNightModeState by lazy {
defaultSharedPreferences.getInt(
Constants.Theme.THEME_CURRENT_ID,
if (!androidQ) {
Constants.Theme.THEME_LIGHT_ID
} else {
Constants.Theme.THEME_AUTO_ID
}
)
}
private var lastNightModeState: Int? = null
private var switchTask: (() -> Unit)? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -52,58 +70,48 @@ class AppearanceFragment : BaseFragment(R.layout.fragment_appearance) {
nightModeSwitch.switch()
}
nightModeTv.setText(R.string.setting_theme)
nightModeSwitch.initState(
defaultSharedPreferences.getInt(
Constants.Theme.THEME_CURRENT_ID,
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
Constants.Theme.THEME_LIGHT_ID
} else {
Constants.Theme.THEME_AUTO_ID
}
)
)
nightModeSwitch.initState(localNightModeState)
nightModeSwitch.setOnSwitchListener { state ->
if (!isAdded) return@setOnSwitchListener
val currentId = defaultSharedPreferences.getInt(
Constants.Theme.THEME_CURRENT_ID,
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
Constants.Theme.THEME_LIGHT_ID
} else {
Constants.Theme.THEME_AUTO_ID
}
)
if (currentId == state) return@setOnSwitchListener

val currentNightMode = if (currentId == Constants.Theme.THEME_AUTO_ID) {
resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
} else currentId == Constants.Theme.THEME_NIGHT_ID

if (lastNightModeState == null) lastNightModeState = localNightModeState
if (!isAdded || state == lastNightModeState) return@setOnSwitchListener
Timber.e("$state $lastNightModeState")
val currentNightMode = if (lastNightModeState == Constants.Theme.THEME_AUTO_ID) {
systemNightMode
} else lastNightModeState == Constants.Theme.THEME_NIGHT_ID
lastNightModeState = state
val targetNightMode = if (state == Constants.Theme.THEME_AUTO_ID) {
MixinApplication.appContext.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
systemNightMode
} else state == Constants.Theme.THEME_NIGHT_ID
if (currentNightMode != targetNightMode) {
(requireActivity() as ThemeActivity).run {
changeTheme(
getViewCoordinates(nightModeSwitch),
ANIM_DURATION,
!targetNightMode
) {
AppCompatDelegate.setDefaultNightMode(
when (state) {
Constants.Theme.THEME_LIGHT_ID -> AppCompatDelegate.MODE_NIGHT_NO
Constants.Theme.THEME_NIGHT_ID -> AppCompatDelegate.MODE_NIGHT_YES
Constants.Theme.THEME_AUTO_ID -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
else -> AppCompatDelegate.MODE_NIGHT_NO
}
)
defaultSharedPreferences.putInt(Constants.Theme.THEME_CURRENT_ID, state)
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) {
requireActivity().recreate()
Timber.e("targetNightMode:$targetNightMode currentNightMode:${currentNightMode}")
switchTask = if (lastNightModeState != localNightModeState) {
{
Timber.e("switchTask")
AppCompatDelegate.setDefaultNightMode(
when (state) {
Constants.Theme.THEME_LIGHT_ID -> AppCompatDelegate.MODE_NIGHT_NO
Constants.Theme.THEME_NIGHT_ID -> AppCompatDelegate.MODE_NIGHT_YES
Constants.Theme.THEME_AUTO_ID -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
else -> AppCompatDelegate.MODE_NIGHT_NO
}
)
defaultSharedPreferences.putInt(Constants.Theme.THEME_CURRENT_ID, state)
requireActivity().recreate()
}
} else {
null
}
(requireActivity() as ThemeActivity).run {
changeTheme(
getViewCoordinates(nightModeSwitch),
ANIM_DURATION,
!targetNightMode
) {
if (androidS) {
switchTask?.invoke()
}
}
syncTheme(targetNightMode)
}
syncTheme(targetNightMode)
}
val language = Lingver.getInstance().getLanguage()
val languageNames = resources.getStringArray(R.array.language_names)
Expand Down Expand Up @@ -147,6 +155,13 @@ class AppearanceFragment : BaseFragment(R.layout.fragment_appearance) {
}
}

override fun onDestroy() {
if (!androidS) {
switchTask?.invoke()
}
super.onDestroy()
}

private fun syncTheme(isNight: Boolean) {
binding.apply {
val bgWindow = if (isNight) {
Expand Down Expand Up @@ -187,6 +202,7 @@ class AppearanceFragment : BaseFragment(R.layout.fragment_appearance) {
currencyTv.textColorResource = textPrimary
currentTv.textColorResource = textMinor

Timber.e("isNight $isNight")
val window = requireActivity().window
SystemUIManager.lightUI(window, !isNight)
SystemUIManager.setSystemUiColor(window, requireContext().getColor(bgColor))
Expand Down
22 changes: 16 additions & 6 deletions app/src/main/java/one/mixin/android/util/SystemUIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.Window
import android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
import android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
import android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
import androidx.core.view.ViewCompat
import one.mixin.android.extension.supportsPie

@SuppressLint("InlinedApi")
Expand Down Expand Up @@ -72,13 +73,22 @@ object SystemUIManager {
window.decorView.systemUiVisibility = flag
}

fun lightUI(window: Window, light: Boolean) {
if (light) {
window.decorView.systemUiVisibility =
window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
fun lightUI(window: Window, lightIcon: Boolean) {
val controller = ViewCompat.getWindowInsetsController(window.decorView)
if (controller != null) {
controller.apply {
isAppearanceLightStatusBars = lightIcon
isAppearanceLightNavigationBars = lightIcon
}
} else {
window.decorView.systemUiVisibility =
window.decorView.systemUiVisibility xor View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR xor View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
// Can only be executed once and cannot be used as a switch
if (lightIcon) {
window.decorView.systemUiVisibility =
window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
} else {
window.decorView.systemUiVisibility =
window.decorView.systemUiVisibility xor View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR xor View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import one.mixin.android.Constants
import one.mixin.android.R
import one.mixin.android.databinding.NightModeButtonLayoutBinding
import one.mixin.android.extension.dp
import timber.log.Timber

class NightModeSwitch : RelativeLayout {
companion object {
Expand Down Expand Up @@ -46,6 +47,7 @@ class NightModeSwitch : RelativeLayout {

@SuppressLint("UseCompatLoadingForDrawables")
fun initState(state: Int) {
Timber.e("initState $state")
this.state = state
when (state) {
Constants.Theme.THEME_LIGHT_ID -> {
Expand All @@ -68,6 +70,7 @@ class NightModeSwitch : RelativeLayout {

@SuppressLint("ClickableViewAccessibility")
private fun init() {
Timber.e("init $androidQ")
switchRL.layoutParams.width = if (androidQ) {
(111).dp
} else {
Expand Down

0 comments on commit 80baafa

Please sign in to comment.