2020-03-20 15:36:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
package com.wireguard.android.activity
|
|
|
|
|
|
|
|
import android.os.Build
|
|
|
|
import android.os.Bundle
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
2020-09-18 14:03:48 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import com.wireguard.android.util.UserKnobs
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
2020-03-20 15:36:48 +01:00
|
|
|
|
2020-09-18 14:03:48 +02:00
|
|
|
abstract class ThemeChangeAwareActivity : AppCompatActivity() {
|
2020-03-20 15:36:48 +01:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
2020-09-18 14:03:48 +02:00
|
|
|
UserKnobs.darkTheme.onEach {
|
|
|
|
val newMode = if (it) {
|
2020-03-20 15:36:48 +01:00
|
|
|
AppCompatDelegate.MODE_NIGHT_YES
|
|
|
|
} else {
|
|
|
|
AppCompatDelegate.MODE_NIGHT_NO
|
2020-09-18 14:03:48 +02:00
|
|
|
}
|
|
|
|
if (AppCompatDelegate.getDefaultNightMode() != newMode) {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(newMode)
|
|
|
|
recreate()
|
|
|
|
}
|
|
|
|
}.launchIn(lifecycleScope)
|
2020-03-20 15:36:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|