Skip to content

Commit

Permalink
Make allergens choice persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Jan 26, 2024
1 parent 68cc0c0 commit 4356a3c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -43,13 +45,15 @@
*/
class MenuPagerAdapter extends FragmentStateAdapter {
private static final int LEGEND = -63;
private boolean showAllergens = false;

private boolean showAllergens;

private List<RestoMenu> data = Collections.emptyList();

public MenuPagerAdapter(@NonNull Fragment fragment) {
super(fragment);
showAllergens = PreferenceManager.getDefaultSharedPreferences(fragment.requireContext())
.getBoolean(RestoPreferenceFragment.PREF_SHOW_ALLERGENS, false);
}

@SuppressLint("NotifyDataSetChanged")
Expand Down Expand Up @@ -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;
}
Expand Down
68 changes: 43 additions & 25 deletions app/src/main/java/be/ugent/zeus/hydra/resto/menu/RestoFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/res/menu/menu_resto.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_history"
android:icon="@drawable/ic_history"
android:orderInCategory="50"
android:title="@string/resto_history"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_refresh"
android:icon="@drawable/ic_refresh"
android:orderInCategory="60"
android:title="@string/action_refresh"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_history"
android:icon="@drawable/ic_history"
android:orderInCategory="50"
android:title="@string/resto_history"
app:showAsAction="never" />
<item
android:id="@+id/resto_show_website"
Expand All @@ -49,6 +49,5 @@
android:orderInCategory="120"
android:title="@string/resto_show_allergens"
android:checkable="true"
android:checked="false"
app:showAsAction="never" />
</menu>

0 comments on commit 4356a3c

Please sign in to comment.