Search in sources :

Example 1 with ShoppingPlaceholderAdapter

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

the class ShoppingModeFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    viewModel = new ViewModelProvider(this).get(ShoppingModeViewModel.class);
    viewModel.setOfflineLive(!activity.isOnline());
    binding.setViewModel(viewModel);
    binding.setActivity(activity);
    binding.setFragment(this);
    binding.setLifecycleOwner(getViewLifecycleOwner());
    infoFullscreenHelper = new InfoFullscreenHelper(binding.frame);
    clickUtil = new ClickUtil();
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
    debug = PrefsUtil.isDebuggingEnabled(sharedPrefs);
    handler = new Handler();
    if (savedInstanceState == null) {
        binding.recycler.scrollTo(0, 0);
    }
    binding.recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    binding.recycler.setAdapter(new ShoppingPlaceholderAdapter());
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), state -> {
        if (!state) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getSelectedShoppingListIdLive().observe(getViewLifecycleOwner(), this::changeAppBarTitle);
    viewModel.getFilteredGroupedListItemsLive().observe(getViewLifecycleOwner(), items -> {
        if (items == null) {
            return;
        }
        if (items.isEmpty()) {
            InfoFullscreen info = new InfoFullscreen(InfoFullscreen.INFO_EMPTY_SHOPPING_LIST);
            viewModel.getInfoFullscreenLive().setValue(info);
        } else {
            viewModel.getInfoFullscreenLive().setValue(null);
        }
        if (binding.recycler.getAdapter() instanceof ShoppingModeItemAdapter) {
            ((ShoppingModeItemAdapter) binding.recycler.getAdapter()).updateData(items, viewModel.getProductHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getShoppingListItemAmountsHashMap(), viewModel.getMissingProductIds());
        } else {
            binding.recycler.setAdapter(new ShoppingModeItemAdapter(activity, (LinearLayoutManager) binding.recycler.getLayoutManager(), items, viewModel.getProductHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getShoppingListItemAmountsHashMap(), viewModel.getMissingProductIds(), this, sharedPrefs.getBoolean(SHOPPING_MODE.USE_SMALLER_FONT, SETTINGS_DEFAULT.SHOPPING_MODE.USE_SMALLER_FONT), sharedPrefs.getBoolean(SHOPPING_MODE.SHOW_PRODUCT_DESCRIPTION, SETTINGS_DEFAULT.SHOPPING_MODE.SHOW_PRODUCT_DESCRIPTION)));
            binding.recycler.scheduleLayoutAnimation();
        }
    });
    viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
        if (event.getType() == Event.SNACKBAR_MESSAGE) {
            activity.showSnackbar(((SnackbarMessage) event).getSnackbar(activity, activity.binding.frameMainContainer));
        }
    });
    hideDisabledFeatures();
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(ShoppingModeFragmentArgs.fromBundle(requireArguments()).getAnimateStart() && savedInstanceState == null);
}
Also used : ShoppingPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) ShoppingModeViewModel(xyz.zedler.patrick.grocy.viewmodel.ShoppingModeViewModel) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) Handler(android.os.Handler) ShoppingModeItemAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingModeItemAdapter) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 2 with ShoppingPlaceholderAdapter

use of xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter 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)

Example 3 with ShoppingPlaceholderAdapter

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

the class ShoppingListFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    viewModel = new ViewModelProvider(this).get(ShoppingListViewModel.class);
    viewModel.setOfflineLive(!activity.isOnline());
    binding.setViewModel(viewModel);
    binding.setActivity(activity);
    binding.setFragment(this);
    binding.setLifecycleOwner(getViewLifecycleOwner());
    infoFullscreenHelper = new InfoFullscreenHelper(binding.frame);
    clickUtil = new ClickUtil();
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
    pluralUtil = new PluralUtil(activity);
    // APP BAR BEHAVIOR
    appBarBehavior = new AppBarBehavior(activity, binding.appBarDefault, binding.appBarSearch, savedInstanceState);
    binding.recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    binding.recycler.setAdapter(new ShoppingPlaceholderAdapter());
    if (savedInstanceState == null) {
        binding.recycler.scrollToPosition(0);
        viewModel.resetSearch();
    }
    Object forcedSelectedId = getFromThisDestinationNow(Constants.ARGUMENT.SELECTED_ID);
    if (forcedSelectedId != null) {
        viewModel.selectShoppingList((Integer) forcedSelectedId);
        removeForThisDestination(Constants.ARGUMENT.SELECTED_ID);
    }
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), state -> {
        if (!state) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getSelectedShoppingListIdLive().observe(getViewLifecycleOwner(), this::changeAppBarTitle);
    viewModel.getFilteredShoppingListItemsLive().observe(getViewLifecycleOwner(), items -> {
        if (items == null)
            return;
        if (binding.recycler.getAdapter() instanceof ShoppingListItemAdapter) {
            ((ShoppingListItemAdapter) binding.recycler.getAdapter()).updateData(requireContext(), items, viewModel.getProductHashMap(), viewModel.getProductNamesHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getProductGroupHashMap(), viewModel.getStoreHashMap(), viewModel.getShoppingListItemAmountsHashMap(), viewModel.getMissingProductIds(), viewModel.getShoppingListNotes(), viewModel.getGroupingMode());
        } else {
            binding.recycler.setAdapter(new ShoppingListItemAdapter(requireContext(), items, viewModel.getProductHashMap(), viewModel.getProductNamesHashMap(), viewModel.getQuantityUnitHashMap(), viewModel.getProductGroupHashMap(), viewModel.getStoreHashMap(), viewModel.getShoppingListItemAmountsHashMap(), viewModel.getMissingProductIds(), this, viewModel.getShoppingListNotes(), viewModel.getGroupingMode()));
            binding.recycler.scheduleLayoutAnimation();
        }
    });
    viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
        if (event.getType() == Event.SNACKBAR_MESSAGE) {
            activity.showSnackbar(((SnackbarMessage) event).getSnackbar(activity, activity.binding.frameMainContainer));
        }
    });
    if (swipeBehavior == null) {
        swipeBehavior = new SwipeBehavior(activity, swipeStarted -> binding.swipeShoppingList.setEnabled(!swipeStarted)) {

            @Override
            public void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons) {
                if (viewHolder.getItemViewType() != GroupedListItem.TYPE_ENTRY)
                    return;
                if (!(binding.recycler.getAdapter() instanceof ShoppingListItemAdapter))
                    return;
                int position = viewHolder.getAdapterPosition();
                ArrayList<GroupedListItem> groupedListItems = ((ShoppingListItemAdapter) binding.recycler.getAdapter()).getGroupedListItems();
                if (groupedListItems == null || position < 0 || position >= groupedListItems.size()) {
                    return;
                }
                GroupedListItem item = groupedListItems.get(position);
                if (!(item instanceof ShoppingListItem)) {
                    return;
                }
                ShoppingListItem shoppingListItem = (ShoppingListItem) item;
                underlayButtons.add(new SwipeBehavior.UnderlayButton(R.drawable.ic_round_done, pos -> {
                    if (position >= groupedListItems.size()) {
                        return;
                    }
                    viewModel.toggleDoneStatus(shoppingListItem);
                }));
            }
        };
    }
    swipeBehavior.attachToRecyclerView(binding.recycler);
    hideDisabledFeatures();
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(ShoppingListFragmentArgs.fromBundle(requireArguments()).getAnimateStart() && savedInstanceState == null);
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) Bundle(android.os.Bundle) Spanned(android.text.Spanned) NonNull(androidx.annotation.NonNull) ShoppingListHelper(xyz.zedler.patrick.grocy.helper.ShoppingListHelper) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) NumUtil(xyz.zedler.patrick.grocy.util.NumUtil) MainActivity(xyz.zedler.patrick.grocy.activity.MainActivity) ShoppingListItemBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.ShoppingListItemBottomSheet) HashMap(java.util.HashMap) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Event(xyz.zedler.patrick.grocy.model.Event) ARGUMENT(xyz.zedler.patrick.grocy.util.Constants.ARGUMENT) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) R(xyz.zedler.patrick.grocy.R) ViewUtil(xyz.zedler.patrick.grocy.util.ViewUtil) View(android.view.View) SortUtil(xyz.zedler.patrick.grocy.util.SortUtil) RecyclerView(androidx.recyclerview.widget.RecyclerView) FragmentShoppingListBinding(xyz.zedler.patrick.grocy.databinding.FragmentShoppingListBinding) ShoppingListViewModel(xyz.zedler.patrick.grocy.viewmodel.ShoppingListViewModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) LayoutInflater(android.view.LayoutInflater) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) TextEditBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.TextEditBottomSheet) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) Constants(xyz.zedler.patrick.grocy.util.Constants) ShoppingList(xyz.zedler.patrick.grocy.model.ShoppingList) ViewGroup(android.view.ViewGroup) ShoppingListsBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.ShoppingListsBottomSheet) List(java.util.List) Nullable(androidx.annotation.Nullable) SharedPreferences(android.content.SharedPreferences) Product(xyz.zedler.patrick.grocy.model.Product) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) PreferenceManager(androidx.preference.PreferenceManager) ShoppingListItemAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingListItemAdapter) ShoppingPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter) ShoppingListClearBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.ShoppingListClearBottomSheet) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Snackbar(com.google.android.material.snackbar.Snackbar) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) GroupedListItem(xyz.zedler.patrick.grocy.model.GroupedListItem) PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) ShoppingPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter) ShoppingListItemAdapter(xyz.zedler.patrick.grocy.adapter.ShoppingListItemAdapter) ArrayList(java.util.ArrayList) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) ShoppingListItem(xyz.zedler.patrick.grocy.model.ShoppingListItem) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ShoppingListViewModel(xyz.zedler.patrick.grocy.viewmodel.ShoppingListViewModel) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) RecyclerView(androidx.recyclerview.widget.RecyclerView) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)3 ShoppingPlaceholderAdapter (xyz.zedler.patrick.grocy.adapter.ShoppingPlaceholderAdapter)3 SharedPreferences (android.content.SharedPreferences)2 View (android.view.View)2 ViewModelProvider (androidx.lifecycle.ViewModelProvider)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 InfoFullscreenHelper (xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper)2 ClickUtil (xyz.zedler.patrick.grocy.util.ClickUtil)2 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Spanned (android.text.Spanned)1 LayoutInflater (android.view.LayoutInflater)1 MenuItem (android.view.MenuItem)1 ViewGroup (android.view.ViewGroup)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 MutableLiveData (androidx.lifecycle.MutableLiveData)1 PreferenceManager (androidx.preference.PreferenceManager)1