@@ -6,21 +6,24 @@ import android.view.Menu
66import android.view.MenuInflater
77import android.view.MenuItem
88import android.view.View
9+ import android.widget.Toast
910import androidx.appcompat.widget.SearchView
1011import androidx.core.view.isVisible
1112import androidx.fragment.app.Fragment
1213import androidx.fragment.app.viewModels
1314import androidx.navigation.fragment.findNavController
15+ import androidx.recyclerview.widget.LinearLayoutManager
1416import androidx.recyclerview.widget.StaggeredGridLayoutManager
1517import com.codexo.notes.R
1618import com.codexo.notes.adapters.NotesAdapter
1719import com.codexo.notes.data.Note
18- import com.codexo.notes.data.PreferenceManager
20+ import com.codexo.notes.data.PrefsManager
1921import com.codexo.notes.databinding.FragmentNotesBinding
2022import com.codexo.notes.ui.SharedViewModel
2123import com.codexo.notes.utils.SortBy
2224import com.google.android.material.dialog.MaterialAlertDialogBuilder
2325import com.google.android.material.snackbar.Snackbar
26+ import kotlinx.android.synthetic.main.fragment_notes.*
2427
2528class 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 }
0 commit comments