Search in sources :

Example 1 with ShoppingListAdapter

use of xyz.zedler.patrick.grocy.adapter.ShoppingListAdapter in project grocy-android by patzly.

the class ShoppingListsBottomSheet method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_bottomsheet_list_selection, container, false);
    activity = (MainActivity) requireActivity();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
    boolean multipleListsFeature = sharedPrefs.getBoolean(Constants.PREF.FEATURE_MULTIPLE_SHOPPING_LISTS, true);
    MutableLiveData<Integer> selectedIdLive = activity.getCurrentFragment().getSelectedShoppingListIdLive();
    if (selectedIdLive == null) {
        dismiss();
        return view;
    }
    ShoppingListRepository repository = new ShoppingListRepository(activity.getApplication());
    TextView textViewTitle = view.findViewById(R.id.text_list_selection_title);
    textViewTitle.setText(activity.getString(R.string.property_shopping_lists));
    RecyclerView recyclerView = view.findViewById(R.id.recycler_list_selection);
    recyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(new ShoppingPlaceholderAdapter());
    repository.getShoppingListsLive().observe(getViewLifecycleOwner(), shoppingLists -> {
        if (shoppingLists == null) {
            return;
        }
        if (recyclerView.getAdapter() == null || !(recyclerView.getAdapter() instanceof ShoppingListAdapter)) {
            recyclerView.setAdapter(new ShoppingListAdapter(shoppingLists, selectedIdLive.getValue(), this, activity.getCurrentFragment() instanceof ShoppingListFragment && activity.isOnline()));
        } else {
            ((ShoppingListAdapter) recyclerView.getAdapter()).updateData(shoppingLists, selectedIdLive.getValue());
        }
    });
    selectedIdLive.observe(getViewLifecycleOwner(), selectedId -> {
        if (recyclerView.getAdapter() == null || !(recyclerView.getAdapter() instanceof ShoppingListAdapter)) {
            return;
        }
        ((ShoppingListAdapter) recyclerView.getAdapter()).updateSelectedId(selectedIdLive.getValue());
    });
    ActionButton buttonNew = view.findViewById(R.id.button_list_selection_new);
    if (activity.isOnline() && multipleListsFeature && activity.getCurrentFragment() instanceof ShoppingListFragment) {
        buttonNew.setVisibility(View.VISIBLE);
        buttonNew.setOnClickListener(v -> {
            dismiss();
            navigate(ShoppingListFragmentDirections.actionShoppingListFragmentToShoppingListEditFragment());
        });
    }
    progressConfirm = view.findViewById(R.id.progress_confirmation);
    return view;
}
Also used : SharedPreferences(android.content.SharedPreferences) ShoppingPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter) ShoppingListFragment(xyz.zedler.patrick.grocy.fragment.ShoppingListFragment) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) DefaultItemAnimator(androidx.recyclerview.widget.DefaultItemAnimator) ShoppingListAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingListAdapter) ActionButton(xyz.zedler.patrick.grocy.view.ActionButton) ShoppingListRepository(xyz.zedler.patrick.grocy.repository.ShoppingListRepository) TextView(android.widget.TextView) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 DefaultItemAnimator (androidx.recyclerview.widget.DefaultItemAnimator)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 ShoppingListAdapter (xyz.zedler.patrick.grocy.adapter.ShoppingListAdapter)1 ShoppingPlaceholderAdapter (xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter)1 ShoppingListFragment (xyz.zedler.patrick.grocy.fragment.ShoppingListFragment)1 ShoppingListRepository (xyz.zedler.patrick.grocy.repository.ShoppingListRepository)1 ActionButton (xyz.zedler.patrick.grocy.view.ActionButton)1