Search in sources :

Example 1 with Task

use of xyz.zedler.patrick.grocy.model.Task in project grocy-android by patzly.

the class TasksItemAdapter method onBindViewHolder.

@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int positionDoNotUse) {
    int position = viewHolder.getAdapterPosition();
    Task task = tasks.get(position);
    TaskViewHolder holder = (TaskViewHolder) viewHolder;
    // NAME
    holder.binding.title.setText(task.getName());
    // CONTAINER
    holder.binding.linearContainer.setOnClickListener(view -> listener.onItemRowClicked(task));
}
Also used : Task(xyz.zedler.patrick.grocy.model.Task) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint)

Example 2 with Task

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

Example 3 with Task

use of xyz.zedler.patrick.grocy.model.Task in project grocy-android by patzly.

the class DownloadHelper method getTasks.

public QueueItem getTasks(OnTasksResponseListener onResponseListener, OnErrorListener onErrorListener) {
    return new QueueItem() {

        @Override
        public void perform(@Nullable OnStringResponseListener responseListener, @Nullable OnErrorListener errorListener, @Nullable String uuid) {
            get(grocyApi.getObjects(GrocyApi.ENTITY.TASKS), uuid, response -> {
                Type type = new TypeToken<List<Task>>() {
                }.getType();
                ArrayList<Task> tasks = new Gson().fromJson(response, type);
                if (debug) {
                    Log.i(tag, "download Tasks: " + tasks);
                }
                if (onResponseListener != null) {
                    onResponseListener.onResponse(tasks);
                }
                if (responseListener != null) {
                    responseListener.onResponse(response);
                }
            }, error -> {
                if (onErrorListener != null) {
                    onErrorListener.onError(error);
                }
                if (errorListener != null) {
                    errorListener.onError(error);
                }
            });
        }
    };
}
Also used : Type(java.lang.reflect.Type) Task(xyz.zedler.patrick.grocy.model.Task) Gson(com.google.gson.Gson) ArrayList(java.util.ArrayList) ShoppingList(xyz.zedler.patrick.grocy.model.ShoppingList) List(java.util.List) Nullable(androidx.annotation.Nullable)

Example 4 with Task

use of xyz.zedler.patrick.grocy.model.Task in project grocy-android by patzly.

the class DownloadHelper method getObjects.

public QueueItem getObjects(OnObjectsResponseListener onResponseListener, OnErrorListener onErrorListener, String grocyEntity) {
    return new QueueItem() {

        @Override
        public void perform(@Nullable OnStringResponseListener responseListener, @Nullable OnErrorListener errorListener, @Nullable String uuid) {
            get(grocyApi.getObjects(grocyEntity), uuid, response -> {
                Type type;
                // noinspection IfCanBeSwitch
                if (grocyEntity.equals(GrocyApi.ENTITY.QUANTITY_UNITS)) {
                    // Don't change to switch-case!
                    type = new TypeToken<List<QuantityUnit>>() {
                    }.getType();
                } else if (grocyEntity.equals(GrocyApi.ENTITY.LOCATIONS)) {
                    type = new TypeToken<List<Location>>() {
                    }.getType();
                } else if (grocyEntity.equals(GrocyApi.ENTITY.PRODUCT_GROUPS)) {
                    type = new TypeToken<List<ProductGroup>>() {
                    }.getType();
                } else if (grocyEntity.equals(GrocyApi.ENTITY.STORES)) {
                    type = new TypeToken<List<Store>>() {
                    }.getType();
                } else if (grocyEntity.equals(GrocyApi.ENTITY.TASKS)) {
                    type = new TypeToken<List<Task>>() {
                    }.getType();
                } else if (grocyEntity.equals(GrocyApi.ENTITY.TASK_CATEGORIES)) {
                    type = new TypeToken<List<TaskCategory>>() {
                    }.getType();
                } else {
                    type = new TypeToken<List<Product>>() {
                    }.getType();
                }
                ArrayList<Object> objects = new Gson().fromJson(response, type);
                if (debug) {
                    Log.i(tag, "download Objects: " + objects);
                }
                if (onResponseListener != null) {
                    onResponseListener.onResponse(objects);
                }
                if (responseListener != null) {
                    responseListener.onResponse(response);
                }
            }, error -> {
                if (onErrorListener != null) {
                    onErrorListener.onError(error);
                }
                if (errorListener != null) {
                    errorListener.onError(error);
                }
            });
        }
    };
}
Also used : ProductGroup(xyz.zedler.patrick.grocy.model.ProductGroup) Task(xyz.zedler.patrick.grocy.model.Task) Gson(com.google.gson.Gson) Type(java.lang.reflect.Type) TypeToken(com.google.gson.reflect.TypeToken) ArrayList(java.util.ArrayList) ShoppingList(xyz.zedler.patrick.grocy.model.ShoppingList) List(java.util.List) JSONObject(org.json.JSONObject) Nullable(androidx.annotation.Nullable) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit)

Example 5 with Task

use of xyz.zedler.patrick.grocy.model.Task in project grocy-android by patzly.

the class TasksViewModel method updateFilteredTasks.

public void updateFilteredTasks() {
    ArrayList<Task> filteredTasks = new ArrayList<>();
    for (Task task : this.tasks) {
        boolean searchContainsItem = true;
        if (searchInput != null && !searchInput.isEmpty()) {
            searchContainsItem = task.getName().toLowerCase().contains(searchInput);
        }
        if (!searchContainsItem) {
            continue;
        }
        filteredTasks.add(task);
    }
    switch(sortMode) {
        case SORT_NAME:
            SortUtil.sortTasksByName(getApplication(), filteredTasks, sortAscending);
            break;
    }
    filteredTasksLive.setValue(filteredTasks);
}
Also used : Task(xyz.zedler.patrick.grocy.model.Task) ArrayList(java.util.ArrayList)

Aggregations

Task (xyz.zedler.patrick.grocy.model.Task)6 ArrayList (java.util.ArrayList)4 Nullable (androidx.annotation.Nullable)3 List (java.util.List)3 Gson (com.google.gson.Gson)2 Type (java.lang.reflect.Type)2 ShoppingList (xyz.zedler.patrick.grocy.model.ShoppingList)2 SuppressLint (android.annotation.SuppressLint)1 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 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