Skip to content

Commit 74277b9

Browse files
committed
🔥 EyuCoder#9 added
1 parent 7683d4c commit 74277b9

File tree

7 files changed

+91
-35
lines changed

7 files changed

+91
-35
lines changed

app/src/main/java/com/codexo/notes/data/PreferenceManager.kt renamed to app/src/main/java/com/codexo/notes/data/PrefsManager.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.codexo.notes.data
22

33
import android.content.Context
4+
import androidx.preference.PreferenceManager
45
import com.codexo.notes.utils.SortBy
6+
import com.codexo.notes.utils.VIEW_PREF_KEY
57

6-
class PreferenceManager(private val context: Context) {
8+
class PrefsManager(private val context: Context) {
79

810
companion object {
911
private const val PREFS_NAME = "JottyNotes"
@@ -38,4 +40,12 @@ class PreferenceManager(private val context: Context) {
3840
}
3941
return false
4042
}
43+
44+
fun getViewStyle(): String? {
45+
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
46+
if (prefs.contains(VIEW_PREF_KEY)) {
47+
return prefs.getString(VIEW_PREF_KEY, "grid")
48+
}
49+
return null
50+
}
4151
}

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ import android.view.Menu
66
import android.view.MenuInflater
77
import android.view.MenuItem
88
import android.view.View
9+
import android.widget.Toast
910
import androidx.appcompat.widget.SearchView
1011
import androidx.core.view.isVisible
1112
import androidx.fragment.app.Fragment
1213
import androidx.fragment.app.viewModels
1314
import androidx.navigation.fragment.findNavController
15+
import androidx.recyclerview.widget.LinearLayoutManager
1416
import androidx.recyclerview.widget.StaggeredGridLayoutManager
1517
import com.codexo.notes.R
1618
import com.codexo.notes.adapters.NotesAdapter
1719
import com.codexo.notes.data.Note
18-
import com.codexo.notes.data.PreferenceManager
20+
import com.codexo.notes.data.PrefsManager
1921
import com.codexo.notes.databinding.FragmentNotesBinding
2022
import com.codexo.notes.ui.SharedViewModel
2123
import com.codexo.notes.utils.SortBy
2224
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2325
import com.google.android.material.snackbar.Snackbar
26+
import kotlinx.android.synthetic.main.fragment_notes.*
2427

2528
class NotesFragment : Fragment(R.layout.fragment_notes), NotesAdapter.OnItemClickListener {
2629

@@ -31,18 +34,16 @@ class NotesFragment : Fragment(R.layout.fragment_notes), NotesAdapter.OnItemClic
3134
private val binding
3235
get() = _binding
3336
private val notesAdapter = NotesAdapter(this)
34-
private val prefs: PreferenceManager by lazy { PreferenceManager(requireContext()) }
37+
private val prefs: PrefsManager by lazy { PrefsManager(requireContext()) }
3538

3639
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
3740
super.onViewCreated(view, savedInstanceState)
3841
_binding = FragmentNotesBinding.bind(view)
3942

40-
Log.d(TAG, "onViewCreated:${SortBy.CREATED_AT.colName}")
41-
4243
binding?.apply {
4344
rvNotes.apply {
4445
adapter = notesAdapter
45-
layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
46+
layoutStyle()
4647
setHasFixedSize(true)
4748
}
4849
}
@@ -55,6 +56,7 @@ class NotesFragment : Fragment(R.layout.fragment_notes), NotesAdapter.OnItemClic
5556
animationView.playAnimation()
5657
}
5758
}
59+
Log.d(TAG, "onViewCreated:${prefs.getViewStyle()}")
5860

5961
setHasOptionsMenu(true)
6062
}
@@ -68,6 +70,12 @@ class NotesFragment : Fragment(R.layout.fragment_notes), NotesAdapter.OnItemClic
6870
val pinFavoriteMenu = menu.findItem(R.id.action_pin_favorites)
6971
pinFavoriteMenu.isChecked = prefs.favoritePinnedStatus()
7072

73+
when (prefs.sortBy()) {
74+
SortBy.TITLE.colName -> updateMenu(menu.findItem(R.id.action_sort_by_title))
75+
SortBy.CREATED_AT.colName -> updateMenu(menu.findItem(R.id.action_sort_by_date_created))
76+
SortBy.LAST_UPDATED_AT.colName -> updateMenu(menu.findItem(R.id.action_sort_by_date_modified))
77+
}
78+
7179
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
7280
override fun onQueryTextSubmit(query: String?): Boolean {
7381
return true
@@ -92,17 +100,17 @@ class NotesFragment : Fragment(R.layout.fragment_notes), NotesAdapter.OnItemClic
92100
}
93101
R.id.action_sort_by_title -> {
94102
prefs.setSortBy(SortBy.TITLE.colName)
95-
viewModel.allNotes.observe(this, { notesAdapter.setData(it) })
103+
updateMenu(item)
96104
true
97105
}
98106
R.id.action_sort_by_date_created -> {
99107
prefs.setSortBy(SortBy.CREATED_AT.colName)
100-
viewModel.allNotes.observe(this, { notesAdapter.setData(it) })
108+
updateMenu(item)
101109
true
102110
}
103111
R.id.action_sort_by_date_modified -> {
104112
prefs.setSortBy(SortBy.LAST_UPDATED_AT.colName)
105-
viewModel.allNotes.observe(this, { notesAdapter.setData(it) })
113+
updateMenu(item)
106114
true
107115
}
108116
R.id.action_settings -> {
@@ -121,6 +129,11 @@ class NotesFragment : Fragment(R.layout.fragment_notes), NotesAdapter.OnItemClic
121129
}
122130
}
123131

132+
private fun updateMenu(item: MenuItem) {
133+
viewModel.allNotes.observe(this, { notesAdapter.setData(it) })
134+
item.isChecked = true
135+
}
136+
124137
private fun deleteAllDialog() {
125138

126139
if (!notesAdapter.NoteList.isNullOrEmpty()) {
@@ -176,15 +189,21 @@ class NotesFragment : Fragment(R.layout.fragment_notes), NotesAdapter.OnItemClic
176189
_binding = null
177190
}
178191

179-
companion object {
180-
val sortBy = listOf("title", "created_at", "last_updated_at", "favorite")
181-
}
182-
183192
override fun onResume() {
184193
super.onResume()
185194
viewModel.allNotes.observe(viewLifecycleOwner) { notesAdapter.setData(it) }
186195
}
187196

197+
private fun layoutStyle() {
198+
if (prefs.getViewStyle().equals("list")) {
199+
binding!!.rvNotes.layoutManager =
200+
LinearLayoutManager(context)
201+
} else {
202+
binding!!.rvNotes.layoutManager =
203+
StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
204+
}
205+
}
206+
188207
override fun onFavoriteClicked(markedFavorite: Boolean, id: Long) {
189208
sharedViewModel.markAsFavorite(markedFavorite, id)
190209
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import androidx.lifecycle.LiveData
77
import androidx.lifecycle.viewModelScope
88
import com.codexo.notes.data.Note
99
import com.codexo.notes.data.NoteDatabase
10-
import com.codexo.notes.data.PreferenceManager
11-
import kotlinx.coroutines.Dispatchers
10+
import com.codexo.notes.data.PrefsManager
1211
import kotlinx.coroutines.launch
1312

1413
class NotesViewModel constructor(application: Application) : AndroidViewModel(application) {
@@ -17,7 +16,7 @@ class NotesViewModel constructor(application: Application) : AndroidViewModel(ap
1716

1817
lateinit var searchQuery: String
1918

20-
private val prefs = PreferenceManager(application)
19+
private val prefs = PrefsManager(application)
2120

2221
val allNotes: LiveData<List<Note>>
2322
get() = getNotes()

app/src/main/java/com/codexo/notes/utils/Const.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.codexo.notes.utils
33
const val THEME_PREF_KEY = "themePref"
44
const val VIEW_PREF_KEY = "viewPref"
55

6+
67
enum class SortBy(val colName: String) {
78
ID("id"),
89
TITLE("title"),

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@
99
app:actionViewClass="androidx.appcompat.widget.SearchView"
1010
app:showAsAction="always|collapseActionView" />
1111

12-
<group android:checkableBehavior="single">
13-
14-
<item
15-
android:icon="@drawable/ic_sort"
16-
android:title="@string/sort"
17-
app:showAsAction="ifRoom">
18-
<menu>
19-
<group>
20-
<item
21-
android:id="@+id/action_pin_favorites"
22-
android:checkable="true"
23-
android:checked="true"
24-
android:title="@string/pin_favorites"
25-
app:showAsAction="never" />
26-
</group>
12+
<item
13+
android:icon="@drawable/ic_sort"
14+
android:title="@string/sort"
15+
app:showAsAction="ifRoom">
16+
<menu>
17+
<group>
18+
<item
19+
android:id="@+id/action_pin_favorites"
20+
android:checkable="true"
21+
android:checked="true"
22+
android:title="@string/pin_favorites"
23+
app:showAsAction="never" />
24+
</group>
25+
<group android:checkableBehavior="single"
26+
android:id="@+id/sort_menu">
2727
<item
28+
2829
android:id="@+id/action_sort_by_title"
2930
android:title="@string/title" />
3031
<item
@@ -33,10 +34,9 @@
3334
<item
3435
android:id="@+id/action_sort_by_date_modified"
3536
android:title="@string/date_modified" />
36-
</menu>
37-
38-
</item>
39-
</group>
37+
</group>
38+
</menu>
39+
</item>
4040

4141
<!-- <item-->
4242
<!-- android:id="@+id/action_show_archived"-->

app/src/main/res/values/arrays.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<resources>
2+
<!-- Application Theme -->
23
<string-array name="themeListArray">
34
<item>Light</item>
45
<item>Dark</item>
@@ -10,4 +11,15 @@
1011
<item>dark</item>
1112
<item>default</item>
1213
</string-array>
14+
15+
<!-- Recyclerview Layout Style -->
16+
<string-array name="viewListArray">
17+
<item>List</item>
18+
<item>Grid</item>
19+
</string-array>
20+
21+
<string-array name="viewEntryArray">
22+
<item>list</item>
23+
<item>grid</item>
24+
</string-array>
1325
</resources>

app/src/main/res/xml/root_preferences.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,19 @@
1414

1515
</PreferenceCategory>
1616

17+
<PreferenceCategory
18+
app:key="view_category"
19+
app:title="View">
20+
21+
<ListPreference
22+
app:key="viewPref"
23+
app:title="Layout Style"
24+
app:defaultValue="grid"
25+
app:dialogTitle="Select layout style"
26+
app:entries="@array/viewListArray"
27+
app:entryValues="@array/viewEntryArray"
28+
app:useSimpleSummaryProvider="true"/>
29+
30+
</PreferenceCategory>
31+
1732
</PreferenceScreen>

0 commit comments

Comments
 (0)