File tree Expand file tree Collapse file tree 15 files changed +156
-9
lines changed Expand file tree Collapse file tree 15 files changed +156
-9
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ dependencies {
4747 implementation ' com.google.android.material:material:1.4.0'
4848 implementation ' androidx.constraintlayout:constraintlayout:2.0.4'
4949 implementation ' androidx.legacy:legacy-support-v4:1.0.0'
50+ implementation ' androidx.preference:preference:1.1.1'
5051 testImplementation ' junit:junit:4.+'
5152 androidTestImplementation ' androidx.test.ext:junit:1.1.3'
5253 androidTestImplementation ' androidx.test.espresso:espresso-core:3.4.0'
@@ -74,9 +75,9 @@ dependencies {
7475 // Color picker
7576 implementation ' com.github.naz013:ColorSlider:2.0.6'
7677
77- // DataStore
78- implementation " androidx.datastore:datastore-preferences:1.0.0-rc02"
79-
8078 // Lottie Animation
8179 implementation " com.airbnb.android:lottie:3.7.2"
80+
81+ // Kotlin SharedPreference
82+ implementation ' androidx.preference:preference-ktx:1.1.1'
8283}
Original file line number Diff line number Diff line change 33 package =" com.codexo.notes" >
44
55 <application
6+ android : name =" .JottyApp"
67 android : allowBackup =" true"
78 android : icon =" @mipmap/ic_launcher"
89 android : label =" @string/app_name"
Original file line number Diff line number Diff line change 1+ package com.codexo.notes
2+
3+ import android.app.Application
4+ import android.content.SharedPreferences
5+ import androidx.preference.PreferenceManager
6+ import com.codexo.notes.utils.THEME_PREF_KEY
7+ import com.codexo.notes.utils.ThemeUtil
8+
9+ class JottyApp : Application () {
10+
11+ override fun onCreate () {
12+ super .onCreate()
13+
14+ val prefs: SharedPreferences = PreferenceManager .getDefaultSharedPreferences(this )
15+ val themePref = prefs.getString(THEME_PREF_KEY , ThemeUtil .DEFAULT_MODE )
16+ ThemeUtil .applyTheme(themePref!! )
17+ }
18+ }
Original file line number Diff line number Diff line change 11package com.codexo.notes.ui
22
3+ import android.content.SharedPreferences
34import android.os.Bundle
45import androidx.appcompat.app.AppCompatActivity
56import androidx.navigation.NavController
67import androidx.navigation.findNavController
78import androidx.navigation.fragment.NavHostFragment
89import androidx.navigation.fragment.findNavController
910import androidx.navigation.ui.setupActionBarWithNavController
11+ import androidx.preference.PreferenceManager
1012import com.codexo.notes.R
13+ import com.codexo.notes.utils.ThemeUtil
1114
1215class MainActivity : AppCompatActivity () {
1316 private lateinit var navController: NavController
Original file line number Diff line number Diff line change 1+ package com.codexo.notes.ui
2+
3+ import android.os.Bundle
4+ import androidx.preference.ListPreference
5+ import androidx.preference.PreferenceFragmentCompat
6+ import com.codexo.notes.R
7+ import com.codexo.notes.utils.THEME_PREF_KEY
8+ import com.codexo.notes.utils.ThemeUtil
9+
10+ class SettingsFragment : PreferenceFragmentCompat () {
11+
12+ override fun onCreate (savedInstanceState : Bundle ? ) {
13+ super .onCreate(savedInstanceState)
14+ setHasOptionsMenu(true )
15+ }
16+
17+ override fun onCreatePreferences (savedInstanceState : Bundle ? , rootKey : String? ) {
18+ setPreferencesFromResource(R .xml.root_preferences, rootKey)
19+ val themePreference: ListPreference ? = findPreference(THEME_PREF_KEY )
20+ themePreference?.setOnPreferenceChangeListener { _, newValue ->
21+ val themeOption: String = newValue as String
22+ ThemeUtil .applyTheme(themeOption)
23+ true
24+ }
25+ }
26+ }
Original file line number Diff line number Diff line change @@ -87,6 +87,10 @@ class NotesFragment : Fragment(R.layout.fragment_notes) {
8787 viewModel.sortByDateUpdated.observe(this , { notesAdapter.setData(it) })
8888 true
8989 }
90+ R .id.action_settings -> {
91+ findNavController().navigate(R .id.action_notesFragment_to_settingsFragment)
92+ true
93+ }
9094 R .id.action_about -> {
9195 findNavController().navigate(R .id.action_notesFragment_to_aboutFragment)
9296 true
Original file line number Diff line number Diff line change 11package com.codexo.notes.utils
22
3- const val REQUEST_CODE_STT = 1
3+ const val REQUEST_CODE_STT = 1
4+ const val THEME_PREF_KEY = " themePref"
Original file line number Diff line number Diff line change 1+ package com.codexo.notes.utils
2+
3+ import android.os.Build
4+ import androidx.appcompat.app.AppCompatDelegate
5+
6+ object ThemeUtil {
7+
8+ const val LIGHT_MODE = " light"
9+ const val DARK_MODE = " dark"
10+ const val DEFAULT_MODE = " default"
11+
12+
13+ fun applyTheme (themePref : String ) {
14+ when (themePref) {
15+ LIGHT_MODE -> {
16+ AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_NO )
17+ }
18+ DARK_MODE -> {
19+ AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_YES )
20+ }
21+ else -> {
22+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
23+ AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_FOLLOW_SYSTEM )
24+ } else {
25+ AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_AUTO_BATTERY )
26+ }
27+ }
28+ }
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 3939 <!-- android:checkable="true"-->
4040 <!-- android:title="@string/show_archived"-->
4141 <!-- app:showAsAction="never" />-->
42- <item
43- android : id =" @+id/action_about"
44- android : title =" @string/about"
45- app : showAsAction =" never" />
4642 <item
4743 android : id =" @+id/action_delete_all_notes"
4844 android : title =" @string/delete_all_notes"
4945 app : showAsAction =" never" />
46+ <item
47+ android : id =" @+id/action_settings"
48+ android : title =" @string/settings"
49+ app : showAsAction =" never" />
50+ <item
51+ android : id =" @+id/action_about"
52+ android : title =" @string/about"
53+ app : showAsAction =" never" />
5054
5155</menu >
You can’t perform that action at this time.
0 commit comments