Search in sources :

Example 71 with ViewModelProvider

use of androidx.lifecycle.ViewModelProvider in project grocy-android by patzly.

the class MasterProductCatBarcodesEditFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    MasterProductCatBarcodesEditFragmentArgs args = MasterProductCatBarcodesEditFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new MasterProductCatBarcodesEditViewModel.MasterProductCatBarcodesEditViewModelFactory(activity.getApplication(), args)).get(MasterProductCatBarcodesEditViewModel.class);
    binding.setActivity(activity);
    binding.setViewModel(viewModel);
    binding.setFormData(viewModel.getFormData());
    binding.setFragment(this);
    binding.setLifecycleOwner(getViewLifecycleOwner());
    viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
        if (event.getType() == Event.SNACKBAR_MESSAGE) {
            SnackbarMessage message = (SnackbarMessage) event;
            Snackbar snack = message.getSnackbar(activity, activity.binding.frameMainContainer);
            activity.showSnackbar(snack);
        } else if (event.getType() == Event.NAVIGATE_UP) {
            activity.navigateUp();
        } else if (event.getType() == Event.SET_SHOPPING_LIST_ID) {
            int id = event.getBundle().getInt(Constants.ARGUMENT.SELECTED_ID);
            setForDestination(R.id.shoppingListFragment, Constants.ARGUMENT.SELECTED_ID, id);
        } else if (event.getType() == Event.BOTTOM_SHEET) {
            BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
            activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
        }
    });
    infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isLoading -> {
        if (!isLoading) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    viewModel.getOfflineLive().observe(getViewLifecycleOwner(), offline -> {
        InfoFullscreen infoFullscreen = offline ? new InfoFullscreen(InfoFullscreen.ERROR_OFFLINE, () -> updateConnectivity(true)) : null;
        viewModel.getInfoFullscreenLive().setValue(infoFullscreen);
    });
    embeddedFragmentScanner.setScannerVisibilityLive(viewModel.getFormData().getScannerVisibilityLive());
    // necessary because else getValue() doesn't give current value (?)
    viewModel.getFormData().getQuantityUnitsLive().observe(getViewLifecycleOwner(), qUs -> {
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI();
}
Also used : SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) MasterProductCatBarcodesEditViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterProductCatBarcodesEditViewModel) BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 72 with ViewModelProvider

use of androidx.lifecycle.ViewModelProvider in project grocy-android by patzly.

the class MasterProductFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    MasterProductFragmentArgs args = MasterProductFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new MasterProductViewModel.MasterProductViewModelFactory(activity.getApplication(), args)).get(MasterProductViewModel.class);
    if (!viewModel.isActionEdit() && args.getProductName() != null) {
        // remove product name from arguments because it was filled
        // in the form during ViewModel creation
        setArguments(new MasterProductFragmentArgs.Builder(args).setProductName(null).setProductId(null).build().toBundle());
    }
    binding.setActivity(activity);
    binding.setFormData(viewModel.getFormData());
    binding.setViewModel(viewModel);
    binding.setFragment(this);
    binding.setLifecycleOwner(getViewLifecycleOwner());
    binding.categoryOptional.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatOptionalFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryLocation.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatLocationFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryDueDate.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatDueDateFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryAmount.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatAmountFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryQuantityUnit.setOnClickListener(v -> navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatQuantityUnitFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct())));
    binding.categoryBarcodes.setOnClickListener(v -> {
        if (!viewModel.isActionEdit()) {
            activity.showMessage(R.string.subtitle_product_not_on_server);
            return;
        }
        navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatBarcodesFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct()));
    });
    binding.categoryQuConversions.setOnClickListener(v -> {
        if (!viewModel.isActionEdit()) {
            activity.showMessage(R.string.subtitle_product_not_on_server);
            return;
        }
        navigate(MasterProductFragmentDirections.actionMasterProductFragmentToMasterProductCatConversionsFragment(viewModel.getAction()).setProduct(viewModel.getFilledProduct()));
    });
    Product product = (Product) getFromThisDestinationNow(Constants.ARGUMENT.PRODUCT);
    if (product != null) {
        viewModel.setCurrentProduct(product);
        removeForThisDestination(Constants.ARGUMENT.PRODUCT);
    }
    viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
        if (event.getType() == Event.SNACKBAR_MESSAGE) {
            SnackbarMessage message = (SnackbarMessage) event;
            Snackbar snack = message.getSnackbar(activity, activity.binding.frameMainContainer);
            activity.showSnackbar(snack);
        } else if (event.getType() == Event.NAVIGATE_UP) {
            activity.navigateUp();
        } else if (event.getType() == Event.SET_PRODUCT_ID) {
            int id = event.getBundle().getInt(Constants.ARGUMENT.PRODUCT_ID);
            setForPreviousDestination(Constants.ARGUMENT.PRODUCT_ID, id);
        } 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) {
            if (binding.editTextName.getText() == null || binding.editTextName.getText().length() == 0) {
                activity.showKeyboard(binding.editTextName);
            }
        }
    });
    infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isLoading -> {
        if (!isLoading) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    String action = (String) getFromThisDestinationNow(Constants.ARGUMENT.ACTION);
    if (action != null) {
        removeForThisDestination(Constants.ARGUMENT.ACTION);
        switch(action) {
            case ACTION.SAVE_CLOSE:
                new Handler().postDelayed(() -> viewModel.saveProduct(true), 500);
                break;
            case ACTION.SAVE_NOT_CLOSE:
                new Handler().postDelayed(() -> viewModel.saveProduct(false), 500);
                break;
            case ACTION.DELETE:
                new Handler().postDelayed(() -> viewModel.deleteProductSafely(), 500);
                break;
        }
    }
    viewModel.getIsOnlineLive().observe(getViewLifecycleOwner(), isOnline -> {
    // if(isOnline ) viewModel.downloadData();
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(savedInstanceState == null);
}
Also used : BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) Product(xyz.zedler.patrick.grocy.model.Product) Handler(android.os.Handler) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) MasterProductViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterProductViewModel) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 73 with ViewModelProvider

use of androidx.lifecycle.ViewModelProvider in project grocy-android by patzly.

the class SettingsCatBehaviorFragment 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();
    }
    updateShortcuts();
    setForPreviousDestination(Constants.ARGUMENT.ANIMATED, false);
}
Also used : BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) SettingsViewModel(xyz.zedler.patrick.grocy.viewmodel.SettingsViewModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 74 with ViewModelProvider

use of androidx.lifecycle.ViewModelProvider in project grocy-android by patzly.

the class SettingsCatNetworkFragment 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());
        }
    });
    viewModel.getTorEnabledLive().observe(getViewLifecycleOwner(), enabled -> {
        viewModel.setTorEnabled(enabled);
        assert viewModel.getProxyEnabledLive().getValue() != null;
        if (enabled && viewModel.getProxyEnabledLive().getValue()) {
            viewModel.getProxyEnabledLive().setValue(false);
        }
    });
    viewModel.getProxyEnabledLive().observe(getViewLifecycleOwner(), enabled -> {
        viewModel.setProxyEnabled(enabled);
        assert viewModel.getTorEnabledLive().getValue() != null;
        if (enabled && viewModel.getTorEnabledLive().getValue()) {
            viewModel.getTorEnabledLive().setValue(false);
        }
    });
    proxyHostLive = new MutableLiveData<>(viewModel.getProxyHost());
    proxyPortLive = new MutableLiveData<>(String.valueOf(viewModel.getProxyPort()));
    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);
    updateTimeoutValue();
}
Also used : BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) SettingsViewModel(xyz.zedler.patrick.grocy.viewmodel.SettingsViewModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 75 with ViewModelProvider

use of androidx.lifecycle.ViewModelProvider in project grocy-android by patzly.

the class SettingsCatStockFragment 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);
    viewModel.loadProductPresets();
}
Also used : BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) SettingsViewModel(xyz.zedler.patrick.grocy.viewmodel.SettingsViewModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

ViewModelProvider (androidx.lifecycle.ViewModelProvider)159 View (android.view.View)38 TextView (android.widget.TextView)28 BottomSheetEvent (xyz.zedler.patrick.grocy.model.BottomSheetEvent)25 Intent (android.content.Intent)24 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)24 InfoFullscreenHelper (xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper)23 Nullable (androidx.annotation.Nullable)21 Snackbar (com.google.android.material.snackbar.Snackbar)20 ClickUtil (xyz.zedler.patrick.grocy.util.ClickUtil)20 Bundle (android.os.Bundle)18 RecyclerView (androidx.recyclerview.widget.RecyclerView)18 Toolbar (androidx.appcompat.widget.Toolbar)16 SnackbarMessage (xyz.zedler.patrick.grocy.model.SnackbarMessage)16 Uri (android.net.Uri)10 ViewGroup (android.view.ViewGroup)10 NonNull (androidx.annotation.NonNull)10 Handler (android.os.Handler)9 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)8 IdpResponse (com.firebase.ui.auth.IdpResponse)8