Browse Source

dark mode toggles work, and it is even stateful (saves user preference in the system)

Tareef 4 years ago
parent
commit
8def9196a6

+ 2 - 1
app/src/main/AndroidManifest.xml

@@ -9,7 +9,8 @@
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/Theme.PWM">
-        <activity android:name=".MainActivity">
+        <activity
+            android:name=".MainActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 

+ 52 - 6
app/src/main/java/com/tarfeef101/pwm/MainActivity.kt

@@ -1,6 +1,8 @@
 package com.tarfeef101.pwm
 
 import android.annotation.SuppressLint
+import android.content.Context
+import android.content.res.Configuration
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
 import android.widget.RadioButton
@@ -10,16 +12,36 @@ import android.widget.TextView
 import androidx.appcompat.app.AppCompatDelegate
 import kotlinx.android.synthetic.main.activity_main.*
 
+@SuppressLint("SetTextI18n")
 class MainActivity : AppCompatActivity()
 {
-    @SuppressLint("SetTextI18n")
     override fun onCreate(savedInstanceState: Bundle?)
     {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
 
+        // load shared preferences file
+        val pref = this.getPreferences(Context.MODE_PRIVATE)
+        // retrieve user dark mode setting
+        val last_dark_setting = pref.getInt("dark", -1)
+        // set radio button based on this
+        if (last_dark_setting == -1)
+        {
+            // no preference, use default
+            themeRadioGroup.check(radioButtonDefault.id)
+        }
+        else if (last_dark_setting == 0)
+        {
+            // user wants light mode, loser that they are
+            themeRadioGroup.check(radioButtonLight.id)
+        }
+        else
+        {
+            // we have a dark moder!
+            themeRadioGroup.check(radioButtonDark.id)
+        }
+
         // define objects, set text values for em
-        themeRadioGroup.check(radioButtonDefault.id)
         radioButtonDefault.setText("System Default")
         radioButtonDark.setText("Dark")
         radioButtonLight.setText("Light")
@@ -30,14 +52,38 @@ class MainActivity : AppCompatActivity()
             val active = findViewById<RadioButton>(themeRadioGroup.checkedRadioButtonId)
             when (active)
             {
-                radioButtonDefault -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
-                radioButtonDark -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
-                radioButtonLight -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
+                radioButtonDefault ->
+                {
+                    with (pref.edit())
+                    {
+                        putInt("dark", -1)
+                        apply()
+                    }
+                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
+                }
+                radioButtonDark ->
+                {
+                    with (pref.edit())
+                    {
+                        putInt("dark", 1)
+                        apply()
+                    }
+                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
+                }
+                radioButtonLight ->
+                {
+                    with (pref.edit())
+                    {
+                        putInt("dark", 0)
+                        apply()
+                    }
+                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
+                }
             }
             themeStatus.setText(active.getText())
         }
 
-        // invoke the above on app startup, and set the listener to run from then on
+        // set the listener for changes to the radio button group
         setTheme()
         themeRadioGroup.setOnCheckedChangeListener { _, _ -> setTheme()}
     }