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));
}
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);
}
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);
}
});
}
};
}
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);
}
});
}
};
}
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);
}
Aggregations