use of androidx.lifecycle.ViewModelProvider in project collect by opendatakit.
the class FormEntryActivity method setupViewModels.
private void setupViewModels() {
backgroundLocationViewModel = ViewModelProviders.of(this, new BackgroundLocationViewModel.Factory(permissionsProvider, settingsProvider.getUnprotectedSettings())).get(BackgroundLocationViewModel.class);
backgroundAudioViewModel = new ViewModelProvider(this, backgroundAudioViewModelFactory).get(BackgroundAudioViewModel.class);
backgroundAudioViewModel.isPermissionRequired().observe(this, isPermissionRequired -> {
if (isPermissionRequired) {
showIfNotShowing(BackgroundAudioPermissionDialogFragment.class, getSupportFragmentManager());
}
});
identityPromptViewModel = ViewModelProviders.of(this).get(IdentityPromptViewModel.class);
identityPromptViewModel.requiresIdentityToContinue().observe(this, requiresIdentity -> {
if (requiresIdentity) {
showIfNotShowing(IdentifyUserPromptDialogFragment.class, getSupportFragmentManager());
}
});
identityPromptViewModel.isFormEntryCancelled().observe(this, isFormEntryCancelled -> {
if (isFormEntryCancelled) {
finish();
}
});
formEntryViewModel = ViewModelProviders.of(this, formEntryViewModelFactory).get(FormEntryViewModel.class);
formEntryViewModel.getError().observe(this, error -> {
if (error instanceof FormEntryViewModel.NonFatal) {
createErrorDialog(((FormEntryViewModel.NonFatal) error).getMessage(), false);
formEntryViewModel.errorDisplayed();
}
});
formSaveViewModel = new ViewModelProvider(this, formSaveViewModelFactoryFactory.create(this, null)).get(FormSaveViewModel.class);
formSaveViewModel.getSaveResult().observe(this, this::handleSaveResult);
formSaveViewModel.isSavingAnswerFile().observe(this, isSavingAnswerFile -> {
if (isSavingAnswerFile) {
DialogFragmentUtils.showIfNotShowing(SaveAnswerFileProgressDialogFragment.class, getSupportFragmentManager());
} else {
DialogFragmentUtils.dismissDialog(SaveAnswerFileProgressDialogFragment.class, getSupportFragmentManager());
}
});
formSaveViewModel.getAnswerFileError().observe(this, file -> {
if (file != null) {
DialogFragmentUtils.showIfNotShowing(SaveAnswerFileErrorDialogFragment.class, getSupportFragmentManager());
}
});
internalRecordingRequester = new InternalRecordingRequester(this, audioRecorder, permissionsProvider, formEntryViewModel);
waitingForDataRegistry = new FormControllerWaitingForDataRegistry();
externalAppRecordingRequester = new ExternalAppRecordingRequester(this, intentLauncher, waitingForDataRegistry, permissionsProvider, formEntryViewModel);
RecordingHandler recordingHandler = new RecordingHandler(formSaveViewModel, this, audioRecorder, new AMRAppender(), new M4AAppender());
audioRecorder.getCurrentSession().observe(this, session -> {
if (session != null && session.getFile() != null) {
recordingHandler.handle(getFormController(), session, success -> {
if (success) {
onScreenRefresh();
formSaveViewModel.resumeSave();
} else {
String path = session.getFile().getAbsolutePath();
String message = getString(R.string.answer_file_copy_failed_message, path);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
});
}
});
}
use of androidx.lifecycle.ViewModelProvider in project collect by opendatakit.
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 opendatakit.
the class FormMapActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DaggerUtils.getComponent(this).inject(this);
setContentView(R.layout.form_map_activity);
Form form = loadForm();
selectionMapViewModel = new ViewModelProvider(this).get(SelectionMapViewModel.class);
selectionMapViewModel.setMapTitle(form.getDisplayName());
FormNavigator formNavigator = new FormNavigator(currentProjectProvider, settingsProvider, instancesRepositoryProvider::get);
getSupportFragmentManager().setFragmentResultListener(SelectionMapFragment.REQUEST_SELECT_ITEM, this, (requestKey, result) -> {
if (result.containsKey(SelectionMapFragment.RESULT_SELECTED_ITEM)) {
long instanceId = result.getLong(SelectionMapFragment.RESULT_SELECTED_ITEM);
formNavigator.editInstance(this, instanceId);
} else if (result.containsKey(SelectionMapFragment.RESULT_CREATE_NEW_ITEM)) {
formNavigator.newInstance(this, form.getDbId());
}
});
}
use of androidx.lifecycle.ViewModelProvider in project collect by opendatakit.
the class FormMapActivity method loadForm.
@Nullable
private Form loadForm() {
Form form = formsRepositoryProvider.get().get(getIntent().getLongExtra(EXTRA_FORM_ID, -1));
FormMapViewModelFactory viewModelFactory = new FormMapViewModelFactory(form, instancesRepositoryProvider.get());
formMapViewModel = new ViewModelProvider(this, viewModelFactory).get(FormMapViewModel.class);
return form;
}
use of androidx.lifecycle.ViewModelProvider in project collect by opendatakit.
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()));
}
Aggregations