From 4356a3c6dc0715ffbd5f812adbf07e4f9460672f Mon Sep 17 00:00:00 2001 From: Niko Strijbol Date: Fri, 26 Jan 2024 23:58:18 +0100 Subject: [PATCH] Make allergens choice persistent --- .../hydra/resto/RestoPreferenceFragment.java | 2 + .../zeus/hydra/resto/SingleDayFragment.java | 1 - .../hydra/resto/menu/MenuPagerAdapter.java | 10 ++- .../zeus/hydra/resto/menu/RestoFragment.java | 68 ++++++++++++------- app/src/main/res/menu/menu_resto.xml | 13 ++-- 5 files changed, 58 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/be/ugent/zeus/hydra/resto/RestoPreferenceFragment.java b/app/src/main/java/be/ugent/zeus/hydra/resto/RestoPreferenceFragment.java index da0be8dc0..f7f8e16b0 100644 --- a/app/src/main/java/be/ugent/zeus/hydra/resto/RestoPreferenceFragment.java +++ b/app/src/main/java/be/ugent/zeus/hydra/resto/RestoPreferenceFragment.java @@ -61,6 +61,8 @@ public class RestoPreferenceFragment extends PreferenceFragment { public static final String DEFAULT_CLOSING_TIME = "21:00"; public static final String PREF_RESTO_CLOSING_HOUR = "pref_resto_closing_hour"; + public static final String PREF_SHOW_ALLERGENS = "key_show_allergens"; + public static String getDefaultRestoEndpoint(Context context) { return context.getString(R.string.value_resto_default_endpoint); } diff --git a/app/src/main/java/be/ugent/zeus/hydra/resto/SingleDayFragment.java b/app/src/main/java/be/ugent/zeus/hydra/resto/SingleDayFragment.java index 8b85c42c1..9282a8da3 100644 --- a/app/src/main/java/be/ugent/zeus/hydra/resto/SingleDayFragment.java +++ b/app/src/main/java/be/ugent/zeus/hydra/resto/SingleDayFragment.java @@ -26,7 +26,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.os.BundleCompat; diff --git a/app/src/main/java/be/ugent/zeus/hydra/resto/menu/MenuPagerAdapter.java b/app/src/main/java/be/ugent/zeus/hydra/resto/menu/MenuPagerAdapter.java index e0b8d8dab..2cb47482d 100644 --- a/app/src/main/java/be/ugent/zeus/hydra/resto/menu/MenuPagerAdapter.java +++ b/app/src/main/java/be/ugent/zeus/hydra/resto/menu/MenuPagerAdapter.java @@ -26,6 +26,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.preference.PreferenceManager; import androidx.viewpager2.adapter.FragmentStateAdapter; import java.time.LocalDate; @@ -34,6 +35,7 @@ import java.util.Objects; import be.ugent.zeus.hydra.resto.RestoMenu; +import be.ugent.zeus.hydra.resto.RestoPreferenceFragment; import be.ugent.zeus.hydra.resto.SingleDayFragment; /** @@ -43,13 +45,15 @@ */ class MenuPagerAdapter extends FragmentStateAdapter { private static final int LEGEND = -63; - - private boolean showAllergens = false; + + private boolean showAllergens; private List data = Collections.emptyList(); public MenuPagerAdapter(@NonNull Fragment fragment) { super(fragment); + showAllergens = PreferenceManager.getDefaultSharedPreferences(fragment.requireContext()) + .getBoolean(RestoPreferenceFragment.PREF_SHOW_ALLERGENS, false); } @SuppressLint("NotifyDataSetChanged") @@ -108,7 +112,7 @@ public boolean containsItem(long itemId) { return true; } - for (RestoMenu menu: this.data) { + for (RestoMenu menu : this.data) { if (Objects.hash(menu, showAllergens) == itemId) { return true; } diff --git a/app/src/main/java/be/ugent/zeus/hydra/resto/menu/RestoFragment.java b/app/src/main/java/be/ugent/zeus/hydra/resto/menu/RestoFragment.java index 8804975cc..3c912b4e2 100644 --- a/app/src/main/java/be/ugent/zeus/hydra/resto/menu/RestoFragment.java +++ b/app/src/main/java/be/ugent/zeus/hydra/resto/menu/RestoFragment.java @@ -30,6 +30,7 @@ import android.widget.*; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.view.MenuProvider; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.preference.PreferenceManager; @@ -64,8 +65,8 @@ import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayoutMediator; import com.google.android.material.textfield.TextInputLayout; +import org.jetbrains.annotations.NotNull; -import static be.ugent.zeus.hydra.common.utils.FragmentUtils.registerMenuProvider; import static be.ugent.zeus.hydra.common.utils.FragmentUtils.requireBaseActivity; /** @@ -133,31 +134,48 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat super.onViewCreated(view, savedInstanceState); Log.d(TAG, "receiveResto: on view created"); - registerMenuProvider(this, R.menu.menu_resto, new int[]{R.id.action_history}, menuItem -> { - int itemId = menuItem.getItemId(); - if (itemId == R.id.action_refresh) { - Toast toast = Toast.makeText(requireContext(), R.string.resto_extra_refresh_started, Toast.LENGTH_SHORT); - toast.show(); - metaViewModel.onRefresh(); - menuViewModel.onRefresh(); - return true; - } else if (itemId == R.id.resto_show_website) { - NetworkUtils.maybeLaunchBrowser(requireContext(), URL); - return true; - } else if (itemId == R.id.action_history) { - startActivity(new Intent(requireContext(), HistoryActivity.class)); - return true; - } else if (itemId == R.id.resto_order_online) { - String url = ORDER_URL + requireContext().getString(R.string.value_info_endpoint); - NetworkUtils.maybeLaunchBrowser(requireContext(), url); - return true; - } else if (itemId == R.id.resto_show_allergens) { - menuItem.setChecked(!menuItem.isChecked()); - pageAdapter.setShowAllergens(menuItem.isChecked()); - return true; + var manager = PreferenceManager.getDefaultSharedPreferences(requireContext()); + var activity = requireBaseActivity(this); + activity.addMenuProvider(new MenuProvider() { + @Override + public void onCreateMenu(@NonNull @NotNull Menu menu, @NonNull @NotNull MenuInflater menuInflater) { + menuInflater.inflate(R.menu.menu_resto, menu); + activity.tintToolbarIcons(menu, R.id.action_history); + var showAllergens = manager.getBoolean(RestoPreferenceFragment.PREF_SHOW_ALLERGENS, false); + menu.findItem(R.id.resto_show_allergens).setChecked(showAllergens); } - return false; - }); + + @Override + public boolean onMenuItemSelected(@NonNull @NotNull MenuItem menuItem) { + int itemId = menuItem.getItemId(); + if (itemId == R.id.action_refresh) { + Toast toast = Toast.makeText(requireContext(), R.string.resto_extra_refresh_started, Toast.LENGTH_SHORT); + toast.show(); + metaViewModel.onRefresh(); + menuViewModel.onRefresh(); + return true; + } else if (itemId == R.id.resto_show_website) { + NetworkUtils.maybeLaunchBrowser(requireContext(), URL); + return true; + } else if (itemId == R.id.action_history) { + startActivity(new Intent(requireContext(), HistoryActivity.class)); + return true; + } else if (itemId == R.id.resto_order_online) { + String url = ORDER_URL + requireContext().getString(R.string.value_info_endpoint); + NetworkUtils.maybeLaunchBrowser(requireContext(), url); + return true; + } else if (itemId == R.id.resto_show_allergens) { + menuItem.setChecked(!menuItem.isChecked()); + var newValue = menuItem.isChecked(); + manager.edit() + .putBoolean(RestoPreferenceFragment.PREF_SHOW_ALLERGENS, newValue) + .apply(); + pageAdapter.setShowAllergens(newValue); + return true; + } + return false; + } + }, getViewLifecycleOwner()); requireBaseActivity(this).requireToolbar().setDisplayShowTitleEnabled(false); exposedDropdown = requireActivity().findViewById(R.id.exposed_dropdown); diff --git a/app/src/main/res/menu/menu_resto.xml b/app/src/main/res/menu/menu_resto.xml index 528ca504e..5f2d7126f 100644 --- a/app/src/main/res/menu/menu_resto.xml +++ b/app/src/main/res/menu/menu_resto.xml @@ -22,17 +22,17 @@ - +