Search in sources :

Example 1 with POSITION

use of xyz.zedler.patrick.grocy.util.Constants.FAB.POSITION in project grocy-android by patzly.

the class TasksFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    viewModel = new ViewModelProvider(this).get(TasksViewModel.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();
    // 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 MasterPlaceholderAdapter());
    if (savedInstanceState == null) {
        binding.recycler.scrollToPosition(0);
        viewModel.resetSearch();
    }
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), state -> {
        if (!state) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getFilteredTasksLive().observe(getViewLifecycleOwner(), items -> {
        if (items == null) {
            return;
        }
        if (items.isEmpty()) {
            InfoFullscreen info;
            if (viewModel.isSearchActive()) {
                info = new InfoFullscreen(InfoFullscreen.INFO_NO_SEARCH_RESULTS);
            } else {
                info = new InfoFullscreen(InfoFullscreen.INFO_EMPTY_TASKS);
            }
            viewModel.getInfoFullscreenLive().setValue(info);
        } else {
            viewModel.getInfoFullscreenLive().setValue(null);
        }
        if (binding.recycler.getAdapter() instanceof TasksItemAdapter) {
            ((TasksItemAdapter) binding.recycler.getAdapter()).updateData(items, viewModel.getSortMode());
        } else {
            binding.recycler.setAdapter(new TasksItemAdapter(requireContext(), items, this, viewModel.getSortMode()));
        }
    });
    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.swipe.setEnabled(!swipeStarted)) {

            @Override
            public void instantiateUnderlayButton(RecyclerView.ViewHolder viewHolder, List<UnderlayButton> underlayButtons) {
                int position = viewHolder.getAdapterPosition();
                ArrayList<Task> displayedItems = viewModel.getFilteredTasksLive().getValue();
                if (displayedItems == null || position < 0 || position >= displayedItems.size()) {
                    return;
                }
                Task task = displayedItems.get(position);
                underlayButtons.add(new UnderlayButton(R.drawable.ic_round_done, pos -> {
                    if (pos >= displayedItems.size()) {
                        return;
                    }
                    swipeBehavior.recoverLatestSwipedItem();
                    viewModel.performAction(ACTION.UNDO, displayedItems.get(pos));
                }));
            }
        };
    }
    swipeBehavior.attachToRecyclerView(binding.recycler);
    hideDisabledFeatures();
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(savedInstanceState == null);
}
Also used : TasksItemAdapter(xyz.zedler.patrick.grocy.adapter.TasksItemAdapter) Bundle(android.os.Bundle) MasterPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter) NonNull(androidx.annotation.NonNull) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) MainActivity(xyz.zedler.patrick.grocy.activity.MainActivity) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Event(xyz.zedler.patrick.grocy.model.Event) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) R(xyz.zedler.patrick.grocy.R) ActivityInfo(android.content.pm.ActivityInfo) ViewUtil(xyz.zedler.patrick.grocy.util.ViewUtil) View(android.view.View) Animation(android.view.animation.Animation) RecyclerView(androidx.recyclerview.widget.RecyclerView) ACTION(xyz.zedler.patrick.grocy.util.Constants.ACTION) ViewModelProvider(androidx.lifecycle.ViewModelProvider) LayoutInflater(android.view.LayoutInflater) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) Constants(xyz.zedler.patrick.grocy.util.Constants) POSITION(xyz.zedler.patrick.grocy.util.Constants.FAB.POSITION) ViewGroup(android.view.ViewGroup) FragmentTasksBinding(xyz.zedler.patrick.grocy.databinding.FragmentTasksBinding) Task(xyz.zedler.patrick.grocy.model.Task) List(java.util.List) Nullable(androidx.annotation.Nullable) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) TasksViewModel(xyz.zedler.patrick.grocy.viewmodel.TasksViewModel) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Snackbar(com.google.android.material.snackbar.Snackbar) Task(xyz.zedler.patrick.grocy.model.Task) AppBarBehavior(xyz.zedler.patrick.grocy.behavior.AppBarBehavior) MasterPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter) ArrayList(java.util.ArrayList) SwipeBehavior(xyz.zedler.patrick.grocy.behavior.SwipeBehavior) TasksViewModel(xyz.zedler.patrick.grocy.viewmodel.TasksViewModel) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) TasksItemAdapter(xyz.zedler.patrick.grocy.adapter.TasksItemAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

ActivityInfo (android.content.pm.ActivityInfo)1 Bundle (android.os.Bundle)1 LayoutInflater (android.view.LayoutInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 Animation (android.view.animation.Animation)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 Snackbar (com.google.android.material.snackbar.Snackbar)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 R (xyz.zedler.patrick.grocy.R)1 MainActivity (xyz.zedler.patrick.grocy.activity.MainActivity)1 MasterPlaceholderAdapter (xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter)1 TasksItemAdapter (xyz.zedler.patrick.grocy.adapter.TasksItemAdapter)1 AppBarBehavior (xyz.zedler.patrick.grocy.behavior.AppBarBehavior)1