Skip to content

Commit

Permalink
More code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Oct 27, 2023
1 parent 44ecba1 commit f806d3f
Show file tree
Hide file tree
Showing 42 changed files with 187 additions and 265 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/be/ugent/zeus/hydra/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@
* <p>
* The logic for handling the navigation drawer is not immediately obvious to those who do not work with it regularly.
* Proceed with caution.
*
* <p>
* <h1>Navigation</h1>
* <p>
* One of the main responsibilities of this activity is managing navigation between the drawer, the fragments and the
* fragments between each other. The navigation is built in two main components: navigation forward and navigating
* backwards. Each component itself is not that difficult. Together the provide an intuitive navigation.
*
* <p>
* <h2>Forward navigation</h2>
* <p>
* When the user navigates to a new fragment in this activity, the back stack (of the {@link #getSupportFragmentManager()}
Expand Down Expand Up @@ -112,7 +112,7 @@
* <p>
* The third and last scenario is the easiest: nothing should be done when the activity is first started or recreated.
* The fragments should not be added to the back stack.
*
* <p>
* <h2>Backwards navigation</h2>
* <p>
* The logic above makes the backwards navigation quite simple, and can be summarized as:
Expand Down Expand Up @@ -149,7 +149,7 @@
* After this method call, the fragment can behave as if the arguments were directly set on the fragment itself.
* <p>
* This function will only be called when creating a fragment, not when popping from the back stack.
*
* <p>
* <h1>Common views and removal</h1>
* <p>
* The activity provides some common views:
Expand All @@ -172,7 +172,7 @@
* <p>
* The reason fragments cannot fully rely on the default lifecycle methods, such as {@link Fragment#onStop()}, is that
* the fragment is not always removed immediately by the activity when it is hidden (this as to do with performance).
*
* <p>
* <h1>Arguments</h1>
* <p>
* The activity has one public argument: which child fragment to load. The preferred way of using it is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import androidx.annotation.IdRes;
import androidx.annotation.MenuRes;
import androidx.annotation.NonNull;
import androidx.core.view.MenuProvider;
import androidx.fragment.app.Fragment;

import java.util.function.Function;

import be.ugent.zeus.hydra.R;
import be.ugent.zeus.hydra.common.ui.BaseActivity;
import be.ugent.zeus.hydra.common.ui.RefreshViewModel;
import org.jetbrains.annotations.NotNull;

/**
* @author Niko Strijbol
Expand All @@ -53,4 +64,32 @@ public static Bundle requireArguments(Fragment fragment) {
return arguments;
}
}

// TODO: this is an experimental abstraction, since there isn't a lot of benefit vs just doing it
public static void registerMenuProvider(@NonNull Fragment fragment, @MenuRes int menuRes, @IdRes int[] icons, Function<MenuItem, Boolean> onSelected) {
BaseActivity<?> activity = requireBaseActivity(fragment);

activity.addMenuProvider(new MenuProvider() {
@Override
public void onCreateMenu(@NonNull @NotNull Menu menu, @NonNull @NotNull MenuInflater menuInflater) {
menuInflater.inflate(menuRes, menu);
activity.tintToolbarIcons(menu, icons);
}

@Override
public boolean onMenuItemSelected(@NonNull @NotNull MenuItem menuItem) {
return onSelected.apply(menuItem);
}
}, fragment.getViewLifecycleOwner());
}

public static void registerRefreshMenu(@NonNull Fragment fragment, @NonNull RefreshViewModel viewModel) {
registerMenuProvider(fragment, R.menu.menu_resto, new int[]{R.id.action_refresh}, menuItem -> {
if (menuItem.getItemId() == R.id.action_refresh) {
viewModel.onRefresh();
return true;
}
return false;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.os.BundleCompat;
import androidx.fragment.app.Fragment;

import be.ugent.zeus.hydra.common.reporting.BaseEvents;
Expand Down Expand Up @@ -65,7 +66,7 @@ public static SingleDayFragment newInstance(RestoMenu menu, boolean showAllergen
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
data = requireArguments().getParcelable(ARG_DATA_MENU);
data = BundleCompat.getParcelable(requireArguments(), ARG_DATA_MENU, RestoMenu.class);
}

@Override
Expand Down
70 changes: 29 additions & 41 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 @@ -66,6 +66,7 @@
import com.google.android.material.textfield.MaterialAutoCompleteTextView;
import com.google.android.material.textfield.TextInputLayout;

import static be.ugent.zeus.hydra.common.utils.FragmentUtils.registerMenuProvider;
import static be.ugent.zeus.hydra.common.utils.FragmentUtils.requireBaseActivity;

/**
Expand Down Expand Up @@ -107,12 +108,6 @@ public class RestoFragment extends Fragment implements
@Nullable
private LocalDate startDate;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -139,6 +134,32 @@ 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;
}
return false;
});

requireBaseActivity(this).requireToolbar().setDisplayShowTitleEnabled(false);
exposedDropdown = requireActivity().findViewById(R.id.exposed_dropdown);
exposedDropdown.setEnabled(false);
Expand Down Expand Up @@ -196,7 +217,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
private void receiveResto(@NonNull List<RestoChoice> restos) {
SelectedResto selectedResto = new SelectedResto(requireContext());
selectedResto.setData(restos);

// Set the things.
List<SelectedResto.Wrapper> wrappers = selectedResto.getAsWrappers();
ArrayAdapter<SelectedResto.Wrapper> items = new ArrayAdapter<>(requireBaseActivity(this).requireToolbar().getThemedContext(), R.layout.x_simple_spinner_dropdown_item);
Expand Down Expand Up @@ -249,39 +270,6 @@ private void receiveData(@NonNull List<RestoMenu> data) {
}
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.menu_resto, menu);
requireBaseActivity(this).tintToolbarIcons(menu, R.id.action_history);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.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) {
item.setChecked(!item.isChecked());
pageAdapter.setShowAllergens(item.isChecked());
return true;
}
return super.onOptionsItemSelected(item);
}

public void onRestoSelected(@NonNull AdapterView<?> parent, View view, int position, long id) {
// Get the item we selected.
SelectedResto.Wrapper wrapper = (SelectedResto.Wrapper) parent.getItemAtPosition(position);
Expand Down Expand Up @@ -362,7 +350,7 @@ public void onDestroyView() {

private void hideExternalViews() {
bottomNavigation.setVisibility(View.GONE);
bottomNavigation.setOnNavigationItemSelectedListener(null);
bottomNavigation.setOnItemSelectedListener(null);
exposedDropdownWrapper.setVisibility(View.GONE);
requireBaseActivity(this).requireToolbar().setDisplayShowTitleEnabled(true);
exposedDropdownProgress.setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
Expand All @@ -40,7 +42,7 @@
import be.ugent.zeus.hydra.common.utils.ColourUtils;
import com.google.android.material.snackbar.Snackbar;

import static be.ugent.zeus.hydra.common.utils.FragmentUtils.requireBaseActivity;
import static be.ugent.zeus.hydra.common.utils.FragmentUtils.registerRefreshMenu;

/**
* Activity that shows a list of regular sandwiches.
Expand All @@ -54,27 +56,6 @@ public class EcologicalFragment extends Fragment {
private final EcologicalAdapter adapter = new EcologicalAdapter();
private EcologicalViewModel viewModel;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.menu_refresh, menu);
requireBaseActivity(this).tintToolbarIcons(menu, R.id.action_refresh);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.action_refresh) {
viewModel.onRefresh();
return true;
}
return super.onOptionsItemSelected(item);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -99,6 +80,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
viewModel.getData().observe(getViewLifecycleOwner(), new AdapterObserver<>(adapter));
viewModel.getRefreshing().observe(getViewLifecycleOwner(), swipeRefreshLayout::setRefreshing);
swipeRefreshLayout.setOnRefreshListener(viewModel);

registerRefreshMenu(this, viewModel);
}

private void onError(Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;

import java.time.LocalDate;
import java.util.List;
Expand Down Expand Up @@ -105,6 +106,7 @@ public int hashCode() {
return Objects.hash(name, ingredients, start, end, vegan);
}

@NonNull
@Override
public String toString() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
Expand All @@ -40,7 +42,7 @@
import be.ugent.zeus.hydra.common.utils.ColourUtils;
import com.google.android.material.snackbar.Snackbar;

import static be.ugent.zeus.hydra.common.utils.FragmentUtils.requireBaseActivity;
import static be.ugent.zeus.hydra.common.utils.FragmentUtils.registerRefreshMenu;

/**
* Activity that shows a list of regular sandwiches.
Expand All @@ -54,12 +56,6 @@ public class RegularFragment extends Fragment {
private final RegularAdapter adapter = new RegularAdapter();
private RegularViewModel viewModel;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -84,6 +80,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
viewModel.getData().observe(getViewLifecycleOwner(), new AdapterObserver<>(adapter));
viewModel.getRefreshing().observe(getViewLifecycleOwner(), swipeRefreshLayout::setRefreshing);
swipeRefreshLayout.setOnRefreshListener(viewModel);

registerRefreshMenu(this, viewModel);
}

private void onError(Throwable throwable) {
Expand All @@ -92,19 +90,4 @@ private void onError(Throwable throwable) {
.setAction(getString(R.string.action_again), v -> viewModel.onRefresh())
.show();
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.menu_refresh, menu);
requireBaseActivity(this).tintToolbarIcons(menu, R.id.action_refresh);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.action_refresh) {
viewModel.onRefresh();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Loading

0 comments on commit f806d3f

Please sign in to comment.