use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class WalletAddressFragment method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fragmentManager = getChildFragmentManager();
activityViewModel = new ViewModelProvider(activity).get(WalletActivityViewModel.class);
viewModel = new ViewModelProvider(this).get(WalletAddressViewModel.class);
viewModel.qrCode.observe(this, qrCode -> {
final BitmapDrawable qrDrawable = new BitmapDrawable(getResources(), qrCode);
qrDrawable.setFilterBitmap(false);
currentAddressQrView.setImageDrawable(qrDrawable);
currentAddressQrCardView.setOnClickListener(v -> viewModel.showWalletAddressDialog.setValue(Event.simple()));
});
viewModel.bitcoinUri.observe(this, bitcoinUri -> {
final NfcAdapter nfcAdapter = WalletAddressFragment.this.nfcAdapter;
if (nfcAdapter != null)
nfcAdapter.setNdefPushMessage(createNdefMessage(bitcoinUri.toString()), activity);
activityViewModel.addressLoadingFinished();
});
viewModel.showWalletAddressDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
final Address address = viewModel.currentAddress.getValue();
WalletAddressDialogFragment.show(fragmentManager);
log.info("Current address enlarged: {}", address);
}
});
}
use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class RestoreWalletDialogFragment method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
log.info("opening dialog {}", getClass().getName());
viewModel = new ViewModelProvider(this).get(RestoreWalletViewModel.class);
viewModel.showSuccessDialog.observe(this, new Event.Observer<Boolean>() {
@Override
protected void onEvent(final Boolean showEncryptedMessage) {
SuccessDialogFragment.showDialog(fragmentManager, showEncryptedMessage);
}
});
viewModel.showFailureDialog.observe(this, new Event.Observer<String>() {
@Override
protected void onEvent(final String message) {
FailureDialogFragment.showDialog(fragmentManager, message, viewModel.backupUri.getValue());
}
});
viewModel.backupUri.observe(this, uri -> {
final String backupProvider = WalletUtils.uriToProvider(uri);
log.info("picked '{}'{}", uri, backupProvider != null ? " (" + backupProvider + ")" : "");
final Cursor cursor = contentResolver.query(uri, null, null, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst())
viewModel.displayName.setValue(cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)));
} finally {
cursor.close();
}
}
});
viewModel.displayName.observe(this, name -> messageView.setText(name));
final Bundle args = getArguments();
if (args != null) {
viewModel.backupUri.setValue((Uri) args.getParcelable(KEY_BACKUP_URI));
} else {
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
try {
startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT);
} catch (final ActivityNotFoundException x) {
log.warn("Cannot open document selector: {}", intent);
new Toast(activity).longToast(R.string.toast_start_storage_provider_selector_failed);
}
}
}
use of androidx.lifecycle.ViewModelProvider in project J2ME-Loader by nikita36078.
the class MainActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (FileUtils.isExternalStorageLegacy()) {
permissionsLauncher.launch(STORAGE_PERMISSIONS);
}
appListModel = new ViewModelProvider(this).get(AppListModel.class);
if (savedInstanceState == null) {
Intent intent = getIntent();
Uri uri = null;
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
uri = intent.getData();
}
AppsListFragment fragment = AppsListFragment.newInstance(uri);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
}
preferences = PreferenceManager.getDefaultSharedPreferences(this);
if (!preferences.contains(PREF_TOOLBAR)) {
boolean enable = !ViewConfiguration.get(this).hasPermanentMenuKey();
preferences.edit().putBoolean(PREF_TOOLBAR, enable).apply();
}
boolean warningShown = preferences.getBoolean(PREF_STORAGE_WARNING_SHOWN, false);
if (!FileUtils.isExternalStorageLegacy() && !warningShown) {
showScopedStorageDialog();
preferences.edit().putBoolean(PREF_STORAGE_WARNING_SHOWN, true).apply();
}
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
use of androidx.lifecycle.ViewModelProvider in project J2ME-Loader by nikita36078.
the class AppsListFragment method onCreate.
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = requireArguments();
appUri = args.getParcelable(KEY_APP_URI);
args.remove(KEY_APP_URI);
preferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
AppListModel appListModel = new ViewModelProvider(requireActivity()).get(AppListModel.class);
appRepository = appListModel.getAppRepository();
appRepository.observeErrors(this, this::alertDbError);
appRepository.observeApps(this, this::onDbUpdated);
}
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();
}
});
}
});
}
Aggregations