diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/DiningSettingsFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/DiningSettingsFragment.kt index 997af0e0..ea2e884e 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/DiningSettingsFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/DiningSettingsFragment.kt @@ -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) @@ -83,8 +85,7 @@ 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") @@ -92,16 +93,28 @@ class DiningSettingsFragment(dataModel: HomepageDataModel) : Fragment() { } 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() - for (hall in halls) { if (sp.getBoolean(hall.name, false)) { favoriteDiningHalls.add(hall.id) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt index eee8d266..6addc832 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt @@ -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 { diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageDataModel.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageDataModel.kt index 603fdbdb..49c01eca 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageDataModel.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageDataModel.kt @@ -6,4 +6,5 @@ interface HomepageDataModel { fun notifyPostBlurLoaded() fun notifyNewsBlurLoaded() fun updateDining(venues: List) + fun getDiningHallPrefs() : List } diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt index 381b13fd..75290391 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt @@ -347,4 +347,13 @@ class HomepageViewModel : HomepageDataModel, ViewModel() { addCell(DiningCell(venues), DINING_POS) updatePosition(DINING_POS) } + + override fun getDiningHallPrefs() : List { + // if empty, return an empty list + val diningCell = homepageCells[DINING_POS] + if (diningCell.type != "dining") { + return emptyList() + } + return (diningCell as DiningCell).venues + } }