Skip to content

Commit

Permalink
Fix bug where the saved preferences is out of sync with the UI. Note:…
Browse files Browse the repository at this point in the history
… HomepageViewModel is starting to become a God Class... Perhaps there is a better way to design this.
  • Loading branch information
meiron03 committed Jan 7, 2024
1 parent 7f8df0e commit ea2dfa7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class DiningSettingsFragment(dataModel: HomepageDataModel) : Fragment() {
private var _binding : FragmentDiningPreferencesBinding? = null
private val binding get() = _binding!!

private var savedNewPrefs = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
Expand Down Expand Up @@ -83,25 +85,36 @@ class DiningSettingsFragment(dataModel: HomepageDataModel) : Fragment() {
.subscribe({ diningHalls ->
mActivity.runOnUiThread {
halls = diningHalls
val adapter = DiningSettingsAdapter(diningHalls)
binding.diningHallRv.adapter = adapter
binding.diningHallRv.adapter = DiningSettingsAdapter(diningHalls)
}
}, {
Log.e("DiningSettings", "error fetching dining halls")
})
}

override fun onDestroyView() {
super.onDestroyView()
super.onDestroyView()
if (!savedNewPrefs) restoreOriginal()
mActivity.toolbar.visibility = View.GONE
_binding = null
}

private fun restoreOriginal() {
val sp = PreferenceManager.getDefaultSharedPreferences(mActivity)
val originalPreferences = dataModel.getDiningHallPrefs()

val editor = sp.edit()
for (hall in halls) {
editor.putBoolean(hall.name, originalPreferences.contains(hall.id))
editor.apply()
}
}

private fun saveDiningPreferences() {
savedNewPrefs = true
val sp = PreferenceManager.getDefaultSharedPreferences(mActivity)
val favoriteDiningHalls = ArrayList<Int>()


for (hall in halls) {
if (sp.getBoolean(hall.name, false)) {
favoriteDiningHalls.add(hall.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ class HomeFragment : Fragment() {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
homepageViewModel.updateState.collect { updateState ->
Log.i("Fuck", "fuck")
updateState.positions.firstOrNull()?.let { pos ->
if (binding.homeCellsRv.adapter != null) {
mActivity.runOnUiThread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ interface HomepageDataModel {
fun notifyPostBlurLoaded()
fun notifyNewsBlurLoaded()
fun updateDining(venues: List<Int>)
fun getDiningHallPrefs() : List<Int>
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,13 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
addCell(DiningCell(venues), DINING_POS)
updatePosition(DINING_POS)
}

override fun getDiningHallPrefs() : List<Int> {
// if empty, return an empty list
val diningCell = homepageCells[DINING_POS]
if (diningCell.type != "dining") {
return emptyList()
}
return (diningCell as DiningCell).venues
}
}

0 comments on commit ea2dfa7

Please sign in to comment.