use of androidx.lifecycle.ViewModelProvider in project collect by getodk.
the class AndroidShortcutsActivity method onCreate.
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
DaggerUtils.getComponent(this).inject(this);
BlankFormsListViewModel blankFormsListViewModel = new ViewModelProvider(this, blankFormsListViewModelFactory).get(BlankFormsListViewModel.class);
List<BlankForm> forms = blankFormsListViewModel.getForms();
showFormListDialog(forms);
}
use of androidx.lifecycle.ViewModelProvider in project collect by getodk.
the class SaveAnswerFileErrorDialogFragment method onAttach.
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
DaggerUtils.getComponent(context).inject(this);
ViewModelProvider viewModelProvider = new ViewModelProvider(requireActivity(), formSaveViewModelFactoryFactory.create(requireActivity(), null));
formSaveViewModel = viewModelProvider.get(FormSaveViewModel.class);
}
use of androidx.lifecycle.ViewModelProvider in project collect by getodk.
the class FixedDatePickerDialog method onAttach.
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
themeUtils = new ThemeUtils(context);
if (context instanceof CustomDatePickerDialog.DateChangeListener) {
dateChangeListener = (CustomDatePickerDialog.DateChangeListener) context;
}
viewModel = new ViewModelProvider(this).get(DateTimeViewModel.class);
viewModel.setDialogTheme(getArguments().getInt(DateTimeWidgetUtils.DIALOG_THEME));
viewModel.setLocalDateTime((LocalDateTime) getArguments().getSerializable(DateTimeWidgetUtils.DATE));
viewModel.setDatePickerDetails((DatePickerDetails) getArguments().getSerializable(DateTimeWidgetUtils.DATE_PICKER_DETAILS));
viewModel.getSelectedDate().observe(this, localDateTime -> {
if (localDateTime != null) {
dateChangeListener.onDateChanged(localDateTime);
}
});
}
use of androidx.lifecycle.ViewModelProvider in project collect by getodk.
the class BaseSelectListWidget method setUpSearchBox.
private void setUpSearchBox() {
ComponentActivity activity = (ComponentActivity) getContext();
SearchQueryViewModel searchQueryViewModel = new ViewModelProvider(activity).get(SearchQueryViewModel.class);
binding.choicesSearchBox.setVisibility(View.VISIBLE);
binding.choicesSearchBox.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getAnswerFontSize());
binding.choicesSearchBox.addTextChangedListener(new TextWatcher() {
private String oldText = "";
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().equals(oldText)) {
recyclerViewAdapter.getFilter().filter(s.toString());
searchQueryViewModel.setQuery(getFormEntryPrompt().getIndex().toString(), s.toString());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
oldText = s.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
binding.choicesSearchBox.setText(searchQueryViewModel.getQuery(getFormEntryPrompt().getIndex().toString()));
}
use of androidx.lifecycle.ViewModelProvider in project grocy-android by patzly.
the class InventoryFragment method onViewCreated.
@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
activity = (MainActivity) requireActivity();
InventoryFragmentArgs args = InventoryFragmentArgs.fromBundle(requireArguments());
viewModel = new ViewModelProvider(this, new InventoryViewModelFactory(activity.getApplication(), args)).get(InventoryViewModel.class);
binding.setActivity(activity);
binding.setViewModel(viewModel);
binding.setFragment(this);
binding.setFormData(viewModel.getFormData());
binding.setLifecycleOwner(getViewLifecycleOwner());
infoFullscreenHelper = new InfoFullscreenHelper(binding.container);
// INITIALIZE VIEWS
viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isDownloading -> binding.swipe.setRefreshing(isDownloading));
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.TRANSACTION_SUCCESS) {
assert getArguments() != null;
if (InventoryFragmentArgs.fromBundle(getArguments()).getCloseWhenFinished()) {
activity.navigateUp();
} else {
clearFormAndFocusProductInput();
}
} 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) {
focusNextInvalidView();
} else if (event.getType() == Event.QUICK_MODE_ENABLED) {
focusProductInputIfNecessary();
} else if (event.getType() == Event.QUICK_MODE_DISABLED) {
clearInputFocus();
} else if (event.getType() == Event.CONTINUE_SCANNING) {
embeddedFragmentScanner.startScannerIfVisible();
} else if (event.getType() == Event.CHOOSE_PRODUCT) {
String barcode = event.getBundle().getString(ARGUMENT.BARCODE);
navigate(InventoryFragmentDirections.actionInventoryFragmentToChooseProductFragment(barcode));
}
});
Integer productIdSavedSate = (Integer) getFromThisDestinationNow(Constants.ARGUMENT.PRODUCT_ID);
if (productIdSavedSate != null) {
removeForThisDestination(Constants.ARGUMENT.PRODUCT_ID);
viewModel.setQueueEmptyAction(() -> viewModel.setProduct(productIdSavedSate));
} else if (NumUtil.isStringInt(args.getProductId())) {
int productId = Integer.parseInt(args.getProductId());
setArguments(new InventoryFragmentArgs.Builder(args).setProductId(null).build().toBundle());
viewModel.setQueueEmptyAction(() -> viewModel.setProduct(productId));
}
String barcode = (String) getFromThisDestinationNow(ARGUMENT.BARCODE);
if (barcode != null) {
removeForThisDestination(Constants.ARGUMENT.BARCODE);
viewModel.addBarcodeToExistingProduct(barcode);
}
embeddedFragmentScanner.setScannerVisibilityLive(viewModel.getFormData().getScannerVisibilityLive());
// following lines are necessary because no observers are set in Views
viewModel.getFormData().getQuantityUnitStockLive().observe(getViewLifecycleOwner(), i -> {
});
if (savedInstanceState == null) {
viewModel.loadFromDatabase(true);
}
focusProductInputIfNecessary();
setHasOptionsMenu(true);
updateUI(args.getAnimateStart() && savedInstanceState == null);
}
Aggregations