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