Day/night UI mode settings selector.
parent
4f97989154
commit
3a6fe98f1f
@ -0,0 +1,35 @@
|
|||||||
|
package org.thoughtcrime.securesms.loki.dialogs
|
||||||
|
|
||||||
|
import android.app.Dialog
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.appcompat.app.AlertDialog
|
||||||
|
import androidx.fragment.app.DialogFragment
|
||||||
|
import org.thoughtcrime.securesms.loki.utilities.UiMode
|
||||||
|
import org.thoughtcrime.securesms.loki.utilities.UiModeUtilities
|
||||||
|
|
||||||
|
//TODO Use localized string resources.
|
||||||
|
class ChangeUiModeDialog : DialogFragment() {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
public const val TAG = "ChangeUiModeDialog"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||||
|
val context = requireContext()
|
||||||
|
|
||||||
|
val displayNameList = UiMode.values().map { it.displayName }.toTypedArray()
|
||||||
|
val activeUiMode = UiModeUtilities.getUserSelectedUiMode(context)
|
||||||
|
|
||||||
|
return AlertDialog.Builder(context)
|
||||||
|
.setSingleChoiceItems(displayNameList, activeUiMode.ordinal) { _, selectedItemIdx: Int ->
|
||||||
|
val uiMode = UiMode.values()[selectedItemIdx]
|
||||||
|
UiModeUtilities.setUserSelectedUiMode(context, uiMode)
|
||||||
|
dismiss()
|
||||||
|
requireActivity().recreate()
|
||||||
|
}
|
||||||
|
.setTitle("Application theme")
|
||||||
|
.setNegativeButton("Cancel") { _, _ -> dismiss() }
|
||||||
|
.create()
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package org.thoughtcrime.securesms.loki.utilities
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.res.Configuration
|
||||||
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Day/night UI mode related utilities.
|
||||||
|
* @see <a href="https://developer.android.com/guide/topics/ui/look-and-feel/darktheme">Official Documentation</a>
|
||||||
|
*/
|
||||||
|
object UiModeUtilities {
|
||||||
|
private const val PREF_FILE_NAME = "UiModeUtilities"
|
||||||
|
private const val PREF_KEY_SELECTED_UI_MODE = "SELECTED_UI_MODE"
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
public fun setUserSelectedUiMode(context: Context, uiMode: UiMode) {
|
||||||
|
val prefs = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE)
|
||||||
|
prefs.edit()
|
||||||
|
.putString(PREF_KEY_SELECTED_UI_MODE, uiMode.name)
|
||||||
|
.apply()
|
||||||
|
AppCompatDelegate.setDefaultNightMode(uiMode.nightModeValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
public fun getUserSelectedUiMode(context: Context): UiMode {
|
||||||
|
val prefs = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE)
|
||||||
|
val selectedUiModeName = prefs.getString(PREF_KEY_SELECTED_UI_MODE, UiMode.SYSTEM_DEFAULT.name)!!
|
||||||
|
var selectedUiMode: UiMode
|
||||||
|
try {
|
||||||
|
selectedUiMode = UiMode.valueOf(selectedUiModeName)
|
||||||
|
} catch (e: IllegalArgumentException) {
|
||||||
|
// Cannot recognize UiMode constant from the given string.
|
||||||
|
selectedUiMode = UiMode.SYSTEM_DEFAULT
|
||||||
|
}
|
||||||
|
return selectedUiMode
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
public fun setupUiModeToUserSelected(context: Context) {
|
||||||
|
val selectedUiMode = getUserSelectedUiMode(context)
|
||||||
|
setUserSelectedUiMode(context, selectedUiMode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO Use localized string resources.
|
||||||
|
enum class UiMode(
|
||||||
|
val displayName: String,
|
||||||
|
val uiModeNightFlag: Int,
|
||||||
|
val nightModeValue: Int) {
|
||||||
|
|
||||||
|
DAY ("Day",
|
||||||
|
Configuration.UI_MODE_NIGHT_NO,
|
||||||
|
AppCompatDelegate.MODE_NIGHT_NO),
|
||||||
|
|
||||||
|
NIGHT ("Night",
|
||||||
|
Configuration.UI_MODE_NIGHT_YES,
|
||||||
|
AppCompatDelegate.MODE_NIGHT_YES),
|
||||||
|
|
||||||
|
SYSTEM_DEFAULT ("System default",
|
||||||
|
Configuration.UI_MODE_NIGHT_UNDEFINED,
|
||||||
|
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
|
||||||
|
}
|
Loading…
Reference in New Issue