Search in sources :

Example 1 with ChooseProductAdapter

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, () -> {
    });
}
Also used : BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) MasterPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) DefaultItemAnimator(androidx.recyclerview.widget.DefaultItemAnimator) Timer(java.util.Timer) TimerTask(java.util.TimerTask) ChooseProductViewModel(xyz.zedler.patrick.grocy.viewmodel.ChooseProductViewModel) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) ChooseProductAdapter(xyz.zedler.patrick.grocy.adapter.ChooseProductAdapter) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar)

Aggregations

Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 DefaultItemAnimator (androidx.recyclerview.widget.DefaultItemAnimator)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 Snackbar (com.google.android.material.snackbar.Snackbar)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 ChooseProductAdapter (xyz.zedler.patrick.grocy.adapter.ChooseProductAdapter)1 MasterPlaceholderAdapter (xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter)1 BottomSheetEvent (xyz.zedler.patrick.grocy.model.BottomSheetEvent)1 SnackbarMessage (xyz.zedler.patrick.grocy.model.SnackbarMessage)1 ClickUtil (xyz.zedler.patrick.grocy.util.ClickUtil)1 ChooseProductViewModel (xyz.zedler.patrick.grocy.viewmodel.ChooseProductViewModel)1