flauschtrud / broccoli

Broccoli is a free recipe app that lets you build your own personal recipe collection and helps you cook in a more eco-friendly way.
GNU General Public License v3.0
77 stars 8 forks source link

Chosen category is lost when the screen is rotated #135

Open flauschtrud opened 3 years ago

flauschtrud commented 3 years ago

(only applies to custom categories)

flauschtrud commented 3 years ago

Initial fix idea unfortunately breaks seasonal search:

private void setUpSpinner() {
    ArrayAdapter<Category> arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item);
    arrayAdapter.add(Category.ALL);
    arrayAdapter.add(Category.SEASONAL);
    arrayAdapter.add(Category.UNASSIGNED);
    arrayAdapter.add(Category.FAVORITES);
    viewModel.getCategories().observe(getViewLifecycleOwner(), categories -> categories.forEach(arrayAdapter::add));
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(arrayAdapter);

    spinner.post(() -> {
        spinner.setOnItemSelectedListener(this);

        Category selectedCategory = viewModel.getCategory() != null? viewModel.getCategory() : getPreferredCategory();
        int position = arrayAdapter.getPosition(selectedCategory);
        spinner.setSelection(position, false);
        viewModel.setFilterName(selectedCategory.getName());
    });

}