use of xyz.zedler.patrick.grocy.util.ClickUtil 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);
}
use of xyz.zedler.patrick.grocy.util.ClickUtil 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.util.ClickUtil in project grocy-android by patzly.
the class ChooseProductFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
activity = (MainActivity) requireActivity();
clickUtil = new ClickUtil();
String barcode = ChooseProductFragmentArgs.fromBundle(requireArguments()).getBarcode();
boolean forbidCreateProduct = ChooseProductFragmentArgs.fromBundle(requireArguments()).getForbidCreateProduct();
Object newProductId = getFromThisDestinationNow(ARGUMENT.PRODUCT_ID);
if (newProductId != null) {
// if user created a new product and navigates back to this fragment this is the new productId
setForPreviousDestination(Constants.ARGUMENT.PRODUCT_ID, newProductId);
setForPreviousDestination(ARGUMENT.BARCODE, barcode);
activity.navigateUp();
return;
}
viewModel = new ViewModelProvider(this, new ChooseProductViewModel.ChooseProductViewModelFactory(activity.getApplication(), barcode, forbidCreateProduct)).get(ChooseProductViewModel.class);
viewModel.setOfflineLive(!activity.isOnline());
binding.setFragment(this);
binding.setActivity(activity);
binding.setViewModel(viewModel);
binding.setLifecycleOwner(getViewLifecycleOwner());
binding.setClickUtil(clickUtil);
viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), state -> {
binding.swipe.setRefreshing(state);
if (!state) {
viewModel.setCurrentQueueLoading(null);
}
});
binding.swipe.setOnRefreshListener(() -> viewModel.downloadDataForceUpdate());
binding.swipe.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(activity, R.color.surface));
binding.swipe.setColorSchemeColors(ContextCompat.getColor(activity, R.color.secondary));
viewModel.getDisplayedItemsLive().observe(getViewLifecycleOwner(), products -> {
if (products == null) {
return;
}
if (binding.recycler.getAdapter() instanceof ChooseProductAdapter) {
((ChooseProductAdapter) binding.recycler.getAdapter()).updateData(products);
} else {
binding.recycler.setAdapter(new ChooseProductAdapter(products, this));
binding.recycler.scheduleLayoutAnimation();
}
});
viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
if (event.getType() == Event.SNACKBAR_MESSAGE) {
SnackbarMessage msg = (SnackbarMessage) event;
Snackbar snackbar = msg.getSnackbar(activity, activity.binding.frameMainContainer);
activity.showSnackbar(snackbar);
} else if (event.getType() == Event.BOTTOM_SHEET) {
BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
} else if (event.getType() == Event.FOCUS_INVALID_VIEWS) {
activity.showKeyboard(binding.editTextProduct);
}
});
// INITIALIZE VIEWS
binding.back.setOnClickListener(v -> activity.onBackPressed());
binding.recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
binding.recycler.setItemAnimator(new DefaultItemAnimator());
binding.recycler.setAdapter(new MasterPlaceholderAdapter());
binding.editTextProduct.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
private Timer timer = new Timer();
@Override
public void afterTextChanged(final Editable s) {
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
activity.runOnUiThread(() -> viewModel.displayItems());
}
}, 500);
}
});
if (savedInstanceState == null) {
viewModel.loadFromDatabase(true);
}
// UPDATE UI
activity.getScrollBehavior().setUpScroll(binding.scroll);
activity.getScrollBehavior().setHideOnScroll(true);
activity.updateBottomAppBar(POSITION.GONE, R.menu.menu_empty, () -> {
});
}
use of xyz.zedler.patrick.grocy.util.ClickUtil in project grocy-android by patzly.
the class LoginRequestFragment method onViewCreated.
@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
activity = (MainActivity) requireActivity();
LoginRequestFragmentArgs args = LoginRequestFragmentArgs.fromBundle(requireArguments());
viewModel = new ViewModelProvider(this, new LoginRequestViewModel.LoginViewModelFactory(activity.getApplication(), args)).get(LoginRequestViewModel.class);
binding.setViewModel(viewModel);
binding.setActivity(activity);
binding.setFragment(this);
binding.setClickUtil(new ClickUtil());
binding.setLifecycleOwner(getViewLifecycleOwner());
viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
if (event.getType() == Event.SNACKBAR_MESSAGE) {
activity.showSnackbar(((SnackbarMessage) event).getSnackbar(activity, activity.binding.frameMainContainer));
} else if (event.getType() == Event.LOGIN_SUCCESS) {
activity.updateGrocyApi();
navigateToStartDestination();
} else if (event.getType() == Event.BOTTOM_SHEET) {
BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
}
});
}
use of xyz.zedler.patrick.grocy.util.ClickUtil in project grocy-android by patzly.
the class SettingsCatServerFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
activity = (MainActivity) requireActivity();
viewModel = new ViewModelProvider(this).get(SettingsViewModel.class);
binding.setActivity(activity);
binding.setFragment(this);
binding.setViewModel(viewModel);
binding.setSharedPrefs(PreferenceManager.getDefaultSharedPreferences(activity));
binding.setClickUtil(new ClickUtil());
binding.setLifecycleOwner(getViewLifecycleOwner());
viewModel.getEventHandler().observe(getViewLifecycleOwner(), event -> {
if (event.getType() == Event.SNACKBAR_MESSAGE) {
activity.showSnackbar(((SnackbarMessage) event).getSnackbar(activity, activity.binding.frameMainContainer));
} else if (event.getType() == Event.BOTTOM_SHEET) {
BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
}
});
if (activity.binding.bottomAppBar.getVisibility() == View.VISIBLE) {
activity.getScrollBehavior().setUpScroll(binding.scroll);
activity.getScrollBehavior().setHideOnScroll(true);
activity.updateBottomAppBar(Constants.FAB.POSITION.GONE, R.menu.menu_empty, false, () -> {
});
activity.binding.fabMain.hide();
}
setForPreviousDestination(Constants.ARGUMENT.ANIMATED, false);
}
Aggregations