Browse code

Initial commit (losing old history)

Dario Rodriguez authored on 08/04/2026 17:25:38
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,62 @@
1
+/*
2
+ * Copyright (c) 2023 Samson Achiaga
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *     http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+package com.certified.audionote.ui
18
+
19
+import android.content.SharedPreferences
20
+import android.os.Bundle
21
+import androidx.appcompat.app.AppCompatDelegate
22
+import androidx.preference.PreferenceFragmentCompat
23
+import androidx.preference.PreferenceManager
24
+import com.certified.audionote.R
25
+
26
+class PreferenceFragment : PreferenceFragmentCompat(),
27
+    SharedPreferences.OnSharedPreferenceChangeListener {
28
+
29
+    override fun onCreate(savedInstanceState: Bundle?) {
30
+        super.onCreate(savedInstanceState)
31
+
32
+        PreferenceManager.getDefaultSharedPreferences(requireContext())
33
+            .registerOnSharedPreferenceChangeListener(this)
34
+    }
35
+
36
+    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
37
+        setPreferencesFromResource(R.xml.root_preferences, rootKey)
38
+    }
39
+
40
+    override fun onSharedPreferenceChanged(
41
+        sharedPreferences: SharedPreferences?,
42
+        key: String?
43
+    ) {
44
+        val darkModePreference = getString(R.string.key_theme)
45
+        key?.let {
46
+            if (it == darkModePreference) sharedPreferences?.let { pref ->
47
+                val darkModeValues = resources.getStringArray(R.array.pref_theme_values)
48
+                when (pref.getString(darkModePreference, darkModeValues[0])) {
49
+                    darkModeValues[0] -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
50
+                    darkModeValues[1] -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
51
+                    darkModeValues[2] -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
52
+                }
53
+            }
54
+        }
55
+    }
56
+
57
+    override fun onDestroyView() {
58
+        super.onDestroyView()
59
+        PreferenceManager.getDefaultSharedPreferences(requireContext())
60
+            .unregisterOnSharedPreferenceChangeListener(this)
61
+    }
62
+}
0 63
\ No newline at end of file