Skip to content

Commit 39a3b01

Browse files
committed
✨ change theme setting done
1 parent 94ef24c commit 39a3b01

File tree

15 files changed

+156
-9
lines changed

15 files changed

+156
-9
lines changed

.idea/misc.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff 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
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

app/src/main/java/com/codexo/notes/ui/MainActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.codexo.notes.ui
22

3+
import android.content.SharedPreferences
34
import android.os.Bundle
45
import androidx.appcompat.app.AppCompatActivity
56
import androidx.navigation.NavController
67
import androidx.navigation.findNavController
78
import androidx.navigation.fragment.NavHostFragment
89
import androidx.navigation.fragment.findNavController
910
import androidx.navigation.ui.setupActionBarWithNavController
11+
import androidx.preference.PreferenceManager
1012
import com.codexo.notes.R
13+
import com.codexo.notes.utils.ThemeUtil
1114

1215
class MainActivity : AppCompatActivity() {
1316
private lateinit var navController: NavController
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

app/src/main/java/com/codexo/notes/ui/notes/NotesFragment.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package 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"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

app/src/main/res/menu/menu_fragment_notes.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@
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>

0 commit comments

Comments
 (0)