use of xyz.zedler.patrick.grocy.adapter.ChooseProductAdapter 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, () -> {
});
}
Aggregations