Skip to content

Commit

Permalink
Merge pull request MediaArea#485 from g-maxime/android-nightmode-auto
Browse files Browse the repository at this point in the history
Android GUI: Add "system default" uimode option
  • Loading branch information
JeromeMartinez authored Oct 21, 2020
2 parents b0843cd + a7c8ff0 commit f813a3e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,44 +219,75 @@ class ReportListActivity : AppCompatActivity(), ReportActivityListener {
}
}

private fun applyUiMode() {
private fun updatePreferences() {
val sharedPreferences = getDefaultSharedPreferences(this)
val key = getString(R.string.preferences_uimode_key)
if (sharedPreferences?.contains(key) == false) {
val oldSharedPreferences = getSharedPreferences(getString(R.string.preferences_key), Context.MODE_PRIVATE)
oldSharedPreferences.getString(key, "").let {
when (it) {
"ON" -> {
sharedPreferences.edit()?.putBoolean(key, true)?.apply()

when (sharedPreferences?.contains(key)) {
false -> {
val oldSharedPreferences = getSharedPreferences(getString(R.string.preferences_key), Context.MODE_PRIVATE)
if (oldSharedPreferences?.contains(key) == true) {
oldSharedPreferences.getString(key, "").let {
when (it) {
"ON" -> {
oldSharedPreferences.edit()?.remove(key)
sharedPreferences.edit()?.putString(key, "on")?.apply()
}
"OFF" -> {
oldSharedPreferences.edit()?.remove(key)
sharedPreferences.edit()?.putString(key, "off")?.apply()

}
else -> {
}
}
}
"OFF" -> {
sharedPreferences.edit()?.putBoolean(key, false)?.apply()
}
}
true -> {
try {
sharedPreferences.getBoolean(key, false).let {
when (it) {
false -> {
sharedPreferences.edit()?.remove(key)
sharedPreferences.edit()?.putString(key, "off")?.apply()
}
true -> {
sharedPreferences.edit()?.remove(key)
sharedPreferences.edit()?.putString(key, "on")?.apply()
}
}
}
else -> {}
}
catch(_: ClassCastException) {}
}
}
}

private fun applyUiMode() {
val sharedPreferences = getDefaultSharedPreferences(this)
val key = getString(R.string.preferences_uimode_key)

sharedPreferences?.getBoolean(getString(R.string.preferences_uimode_key), false).let {
sharedPreferences?.getString(getString(R.string.preferences_uimode_key), "system").let {
when (it) {
false -> {
"off" -> {
if (AppCompatDelegate.getDefaultNightMode()!=AppCompatDelegate.MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
recreate()
}
}
true -> {
"on" -> {
if (AppCompatDelegate.getDefaultNightMode()!=AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
recreate()
}
}
/* "AUTO" -> {
if (AppCompatDelegate.getDefaultNightMode()!=AppCompatDelegate.MODE_NIGHT_AUTO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO)
"system" -> {
if (AppCompatDelegate.getDefaultNightMode()!=AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
recreate()
}
} */
}
}
}
}
Expand Down Expand Up @@ -476,6 +507,8 @@ class ReportListActivity : AppCompatActivity(), ReportActivityListener {
setSupportActionBar(tool_bar)
tool_bar.title = title

updatePreferences()

subscriptionManager = SubscriptionManager.getInstance(application)
lifecycle.addObserver(subscriptionManager)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SettingsFragment : PreferenceFragmentCompat() {

//val subscribeButton: Preference? = findPreference(getString(R.string.preferences_subscribe_key))
val localeDropdown: DropDownPreference? = findPreference(getString(R.string.preferences_locale_key))
val uimodeSwitch: SwitchPreferenceCompat? = findPreference(getString(R.string.preferences_uimode_key))
val uimodeDropdown: DropDownPreference? = findPreference(getString(R.string.preferences_uimode_key))
val systemLanguageSwitch: SwitchPreferenceCompat? = findPreference(getString(R.string.preferences_report_translate_key))

/*subscribeButton?.setOnPreferenceClickListener {
Expand All @@ -49,7 +49,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
subscriptionManager.subscribed.observe(this, Observer {
if (it==true) {
//subscribeButton?.isVisible = false
uimodeSwitch?.isEnabled = true
uimodeDropdown?.isEnabled = true
localeDropdown?.isEnabled = true
systemLanguageSwitch?.isEnabled = true
}
Expand Down Expand Up @@ -91,16 +91,23 @@ class SettingsFragment : PreferenceFragmentCompat() {
true
}

uimodeSwitch?.setOnPreferenceChangeListener { _, newValue -> Boolean
when (newValue) {
false -> {
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
uimodeDropdown?.setOnPreferenceChangeListener { _, newValue -> Boolean
if (newValue is String) {
when (newValue) {
"off" -> {
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
}
}
true -> {
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
"on" -> {
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
}
"system" -> {
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions Source/GUI/Android/app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright (c) MediaArea.net SARL. All Rights Reserved.
Use of this source code is governed by a BSD-style license that can
be found in the License.html file in the root of the source tree.
-->

<resources>
<?xml version="1.0" encoding="UTF-8"?><resources>
<string-array name="uimodes">
<item>@string/default_setting_text</item>
<item>Off</item>
<item>On</item>
</string-array>
<string-array name="uimodes_values">
<item>system</item>
<item>off</item>
<item>on</item>
</string-array>
<string-array name="locales">
<item>@string/default_locale_text</item>
<item>@string/default_setting_text</item>
</string-array>
<string-array name="locales_values">
<item>system</item>
Expand Down
2 changes: 1 addition & 1 deletion Source/GUI/Android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<string name="text_output_desc" csv="Text">Text</string>
<string name="logo_desc">MediaArea logo</string>
<string name="delete_desc" csv="Delete">Delete</string>
<string name="default_locale_text" csv="Default_Setting">System default</string>
<string name="default_setting_text" csv="Default_Setting">System default</string>
<string name="export_title" csv="Export">Export</string>
<string name="open_title" csv="Open">Open</string>
<string name="save_title" csv="Save">Save</string>
Expand Down
8 changes: 6 additions & 2 deletions Source/GUI/Android/app/src/main/res/xml/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
android:defaultValue="true"
android:enabled="false" />

<SwitchPreferenceCompat
<DropDownPreference
android:key="@string/preferences_uimode_key"
android:title="@string/uimode_title"
android:enabled="false" />
android:entries="@array/uimodes"
android:entryValues="@array/uimodes_values"
android:defaultValue="system"
android:enabled="false"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
</PreferenceScreen>

0 comments on commit f813a3e

Please sign in to comment.