use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class WalletActivity method onCreate.
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.application = getWalletApplication();
this.config = application.getConfiguration();
walletActivityViewModel = new ViewModelProvider(this).get(AbstractWalletActivityViewModel.class);
viewModel = new ViewModelProvider(this).get(WalletActivityViewModel.class);
setContentView(R.layout.wallet_content);
contentView = findViewById(android.R.id.content);
exchangeRatesFragment = findViewById(R.id.wallet_main_twopanes_exchange_rates);
levitateView = contentView.findViewWithTag("levitate");
// Make view tagged with 'levitate' scroll away and quickly return.
if (levitateView != null) {
final CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(levitateView.getLayoutParams().width, levitateView.getLayoutParams().height);
layoutParams.setBehavior(new QuickReturnBehavior());
levitateView.setLayoutParams(layoutParams);
levitateView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
final int height = bottom - top;
final View targetList = findViewById(R.id.wallet_transactions_list);
targetList.setPadding(targetList.getPaddingLeft(), height, targetList.getPaddingRight(), targetList.getPaddingBottom());
final View targetEmpty = findViewById(R.id.wallet_transactions_empty);
targetEmpty.setPadding(targetEmpty.getPaddingLeft(), height, targetEmpty.getPaddingRight(), targetEmpty.getPaddingBottom());
});
}
OnFirstPreDraw.listen(contentView, viewModel);
enterAnimation = buildEnterAnimation(contentView);
viewModel.walletEncrypted.observe(this, isEncrypted -> invalidateOptionsMenu());
viewModel.walletLegacyFallback.observe(this, isLegacyFallback -> invalidateOptionsMenu());
viewModel.showHelpDialog.observe(this, new Event.Observer<Integer>() {
@Override
protected void onEvent(final Integer messageResId) {
HelpDialogFragment.page(getSupportFragmentManager(), messageResId);
}
});
viewModel.showBackupWalletDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
BackupWalletActivity.start(WalletActivity.this);
}
});
viewModel.showRestoreWalletDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
RestoreWalletDialogFragment.showPick(getSupportFragmentManager());
}
});
viewModel.showEncryptKeysDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
EncryptKeysDialogFragment.show(getSupportFragmentManager());
}
});
viewModel.showReportIssueDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
ReportIssueDialogFragment.show(getSupportFragmentManager(), R.string.report_issue_dialog_title_issue, R.string.report_issue_dialog_message_issue, Constants.REPORT_SUBJECT_ISSUE, null);
}
});
viewModel.showReportCrashDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
ReportIssueDialogFragment.show(getSupportFragmentManager(), R.string.report_issue_dialog_title_crash, R.string.report_issue_dialog_message_crash, Constants.REPORT_SUBJECT_CRASH, null);
}
});
viewModel.enterAnimation.observe(this, state -> {
if (state == WalletActivityViewModel.EnterAnimationState.WAITING) {
// API level 26: enterAnimation.setCurrentPlayTime(0);
for (final Animator animation : enterAnimation.getChildAnimations()) ((ValueAnimator) animation).setCurrentPlayTime(0);
} else if (state == WalletActivityViewModel.EnterAnimationState.ANIMATING) {
reportFullyDrawn();
enterAnimation.start();
enterAnimation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(final Animator animation) {
viewModel.animationFinished();
}
});
} else if (state == WalletActivityViewModel.EnterAnimationState.FINISHED) {
getWindow().getDecorView().setBackground(null);
}
});
if (savedInstanceState == null)
viewModel.animateWhenLoadingFinished();
else
viewModel.animationFinished();
if (savedInstanceState == null && CrashReporter.hasSavedCrashTrace())
viewModel.showReportCrashDialog.setValue(Event.simple());
config.touchLastUsed();
handleIntent(getIntent());
final FragmentManager fragmentManager = getSupportFragmentManager();
MaybeMaintenanceFragment.add(fragmentManager);
AlertDialogsFragment.add(fragmentManager);
}
use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class SendCoinsFragment method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fragmentManager = getChildFragmentManager();
setHasOptionsMenu(true);
walletActivityViewModel = new ViewModelProvider(activity).get(AbstractWalletActivityViewModel.class);
walletActivityViewModel.wallet.observe(this, wallet -> updateView());
viewModel = new ViewModelProvider(this).get(SendCoinsViewModel.class);
viewModel.addressBook.observe(this, addressBook -> updateView());
if (config.isEnableExchangeRates()) {
viewModel.exchangeRate.observe(this, exchangeRate -> {
final SendCoinsViewModel.State state = viewModel.state;
if (state == null || state.compareTo(SendCoinsViewModel.State.INPUT) <= 0)
amountCalculatorLink.setExchangeRate(exchangeRate != null ? exchangeRate.exchangeRate() : null);
});
}
viewModel.dynamicFees.observe(this, dynamicFees -> {
updateView();
handler.post(dryrunRunnable);
});
application.blockchainState.observe(this, blockchainState -> updateView());
viewModel.balance.observe(this, coin -> activity.invalidateOptionsMenu());
viewModel.progress.observe(this, new ProgressDialogFragment.Observer(fragmentManager));
viewModel.sentTransaction.observe(this, transaction -> {
if (viewModel.state == SendCoinsViewModel.State.SENDING) {
final TransactionConfidence confidence = transaction.getConfidence();
final ConfidenceType confidenceType = confidence.getConfidenceType();
final int numBroadcastPeers = confidence.numBroadcastPeers();
if (confidenceType == ConfidenceType.DEAD)
setState(SendCoinsViewModel.State.FAILED);
else if (numBroadcastPeers > 1 || confidenceType == ConfidenceType.BUILDING)
setState(SendCoinsViewModel.State.SENT);
}
updateView();
});
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
backgroundThread.start();
backgroundHandler = new Handler(backgroundThread.getLooper());
if (savedInstanceState == null) {
final Intent intent = activity.getIntent();
final String action = intent.getAction();
final Uri intentUri = intent.getData();
final String scheme = intentUri != null ? intentUri.getScheme() : null;
final String mimeType = intent.getType();
if ((Intent.ACTION_VIEW.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) && intentUri != null && "bitcoin".equals(scheme)) {
initStateFromBitcoinUri(intentUri);
} else if ((NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
final NdefMessage ndefMessage = (NdefMessage) intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
final byte[] ndefMessagePayload = Nfc.extractMimePayload(PaymentProtocol.MIMETYPE_PAYMENTREQUEST, ndefMessage);
initStateFromPaymentRequest(mimeType, ndefMessagePayload);
} else if ((Intent.ACTION_VIEW.equals(action)) && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
final byte[] paymentRequest = BitcoinIntegration.paymentRequestFromIntent(intent);
if (intentUri != null)
initStateFromIntentUri(mimeType, intentUri);
else if (paymentRequest != null)
initStateFromPaymentRequest(mimeType, paymentRequest);
else
throw new IllegalArgumentException();
} else if (intent.hasExtra(SendCoinsActivity.INTENT_EXTRA_PAYMENT_INTENT)) {
initStateFromIntentExtras(intent.getExtras());
} else {
updateStateFrom(PaymentIntent.blank());
}
}
}
use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class SweepWalletFragment method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fragmentManager = getChildFragmentManager();
setHasOptionsMenu(true);
if (!Constants.ENABLE_SWEEP_WALLET)
throw new IllegalStateException("ENABLE_SWEEP_WALLET is disabled");
walletActivityViewModel = new ViewModelProvider(activity).get(AbstractWalletActivityViewModel.class);
walletActivityViewModel.wallet.observe(this, wallet -> updateView());
viewModel = new ViewModelProvider(this).get(SweepWalletViewModel.class);
viewModel.getDynamicFees().observe(this, dynamicFees -> updateView());
viewModel.progress.observe(this, new ProgressDialogFragment.Observer(fragmentManager));
viewModel.privateKeyToSweep.observe(this, privateKeyToSweep -> updateView());
viewModel.walletToSweep.observe(this, walletToSweep -> {
if (walletToSweep != null) {
balanceView.setVisibility(View.VISIBLE);
final MonetaryFormat btcFormat = config.getFormat();
final MonetarySpannable balanceSpannable = new MonetarySpannable(btcFormat, walletToSweep.getBalance(BalanceType.ESTIMATED));
balanceSpannable.applyMarkup(null, null);
final SpannableStringBuilder balance = new SpannableStringBuilder(balanceSpannable);
balance.insert(0, ": ");
balance.insert(0, getString(R.string.sweep_wallet_fragment_balance));
balanceView.setText(balance);
} else {
balanceView.setVisibility(View.GONE);
}
updateView();
});
viewModel.sentTransaction.observe(this, transaction -> {
if (viewModel.state == SweepWalletViewModel.State.SENDING) {
final TransactionConfidence confidence = transaction.getConfidence();
final ConfidenceType confidenceType = confidence.getConfidenceType();
final int numBroadcastPeers = confidence.numBroadcastPeers();
if (confidenceType == ConfidenceType.DEAD)
setState(SweepWalletViewModel.State.FAILED);
else if (numBroadcastPeers > 1 || confidenceType == ConfidenceType.BUILDING)
setState(SweepWalletViewModel.State.SENT);
}
updateView();
});
viewModel.showDialog.observe(this, new DialogEvent.Observer(activity));
viewModel.showDialogWithRetryRequestBalance.observe(this, new DialogEvent.Observer(activity) {
@Override
protected void onBuildButtons(final DialogBuilder dialog) {
dialog.setPositiveButton(R.string.button_retry, (d, which) -> requestWalletBalance());
dialog.setNegativeButton(R.string.button_dismiss, null);
}
});
backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
backgroundThread.start();
backgroundHandler = new Handler(backgroundThread.getLooper());
if (savedInstanceState == null) {
final Intent intent = activity.getIntent();
if (intent.hasExtra(SweepWalletActivity.INTENT_EXTRA_KEY)) {
final PrefixedChecksummedBytes privateKeyToSweep = (PrefixedChecksummedBytes) intent.getSerializableExtra(SweepWalletActivity.INTENT_EXTRA_KEY);
viewModel.privateKeyToSweep.setValue(privateKeyToSweep);
// delay until fragment is resumed
handler.post(maybeDecodeKeyRunnable);
}
}
}
use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class ScanActivity method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
viewModel = new ViewModelProvider(this).get(ScanViewModel.class);
viewModel.showPermissionWarnDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
WarnDialogFragment.show(getSupportFragmentManager(), R.string.scan_camera_permission_dialog_title, getString(R.string.scan_camera_permission_dialog_message));
}
});
viewModel.showProblemWarnDialog.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
WarnDialogFragment.show(getSupportFragmentManager(), R.string.scan_camera_problem_dialog_title, getString(R.string.scan_camera_problem_dialog_message));
}
});
viewModel.maybeStartSceneTransition.observe(this, new Event.Observer<Void>() {
@Override
protected void onEvent(final Void v) {
if (sceneTransition != null) {
contentView.setAlpha(1);
sceneTransition.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
getWindow().setBackgroundDrawable(new ColorDrawable(getColor(android.R.color.black)));
}
});
sceneTransition.start();
sceneTransition = null;
}
}
});
// Stick to the orientation the activity was started with. We cannot declare this in the
// AndroidManifest.xml, because it's not allowed in combination with the windowIsTranslucent=true
// theme attribute.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
// Draw under navigation and status bars.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.scan_activity);
contentView = findViewById(android.R.id.content);
scannerView = findViewById(R.id.scan_activity_mask);
previewView = findViewById(R.id.scan_activity_preview);
previewView.setSurfaceTextureListener(this);
cameraThread = new HandlerThread("cameraThread", Process.THREAD_PRIORITY_BACKGROUND);
cameraThread.start();
cameraHandler = new Handler(cameraThread.getLooper());
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
log.info("missing {}, requesting", Manifest.permission.CAMERA);
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA }, 0);
}
if (savedInstanceState == null) {
final Intent intent = getIntent();
final int x = intent.getIntExtra(INTENT_EXTRA_SCENE_TRANSITION_X, -1);
final int y = intent.getIntExtra(INTENT_EXTRA_SCENE_TRANSITION_Y, -1);
if (x != -1 || y != -1) {
// Using alpha rather than visibility because 'invisible' will cause the surface view to never
// start up, so the animation will never start.
contentView.setAlpha(0);
getWindow().setBackgroundDrawable(new ColorDrawable(getColor(android.R.color.transparent)));
OnFirstPreDraw.listen(contentView, () -> {
float finalRadius = (float) (Math.max(contentView.getWidth(), contentView.getHeight()));
final int duration = getResources().getInteger(android.R.integer.config_mediumAnimTime);
sceneTransition = ViewAnimationUtils.createCircularReveal(contentView, x, y, 0, finalRadius);
sceneTransition.setDuration(duration);
sceneTransition.setInterpolator(new AccelerateInterpolator());
// (subclasses of) ValueAnimator.
return false;
});
}
}
}
use of androidx.lifecycle.ViewModelProvider in project bitcoin-wallet by bitcoin-wallet.
the class PeerListFragment method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityViewModel = new ViewModelProvider(activity).get(NetworkMonitorViewModel.class);
activityViewModel.selectedItem.observe(this, item -> {
if (item instanceof HostAndPort) {
final HostAndPort peerHostAndPort = (HostAndPort) item;
adapter.setSelectedPeer(peerHostAndPort);
final int position = adapter.positionOf(peerHostAndPort);
if (position != RecyclerView.NO_POSITION)
recyclerView.smoothScrollToPosition(position);
} else {
adapter.setSelectedPeer(null);
}
});
viewModel = new ViewModelProvider(this).get(PeerListViewModel.class);
viewModel.peers.observe(this, peers -> {
viewGroup.setDisplayedChild((peers == null || peers.isEmpty()) ? 1 : 2);
maybeSubmitList();
if (peers != null)
for (final Peer peer : peers) viewModel.getHostnames().reverseLookup(peer.getAddress().getAddr());
});
viewModel.getHostnames().observe(this, hostnames -> maybeSubmitList());
adapter = new PeerListAdapter(activity, this);
}
Aggregations