From 991a8067485cddd3c5ab8eee894563901e00e088 Mon Sep 17 00:00:00 2001 From: pokkst Date: Sat, 13 May 2023 02:28:47 -0500 Subject: [PATCH] Cleanup onboarding screen --- .../onboarding/OnboardingFragment.java | 72 +----- .../onboarding/OnboardingViewModel.java | 78 ++++++- .../main/res/layout/fragment_onboarding.xml | 219 +++++++++--------- 3 files changed, 204 insertions(+), 165 deletions(-) diff --git a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java index 91c239f..0c13407 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java @@ -1,5 +1,6 @@ package net.mynero.wallet.fragment.onboarding; +import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -85,6 +86,7 @@ public class OnboardingFragment extends Fragment implements NodeSelectionBottomS private TextView moreOptionsDropdownTextView; private SwitchCompat torSwitch; private ConstraintLayout proxySettingsLayout; + private ConstraintLayout advancedOptionsLayout; private ImageView moreOptionsChevronImageView; private CheckBox seedOffsetCheckbox; private Button selectNodeButton; @@ -113,6 +115,7 @@ public class OnboardingFragment extends Fragment implements NodeSelectionBottomS walletProxyAddressEditText = view.findViewById(R.id.wallet_proxy_address_edittext); walletProxyPortEditText = view.findViewById(R.id.wallet_proxy_port_edittext); seedOffsetCheckbox.setChecked(useOffset); + advancedOptionsLayout = view.findViewById(R.id.more_options_layout); bindListeners(); bindObservers(); @@ -122,14 +125,16 @@ public class OnboardingFragment extends Fragment implements NodeSelectionBottomS mViewModel.showMoreOptions.observe(getViewLifecycleOwner(), show -> { if (show) { moreOptionsChevronImageView.setImageResource(R.drawable.ic_keyboard_arrow_up); - walletSeedEditText.setVisibility(View.VISIBLE); - walletRestoreHeightEditText.setVisibility(View.VISIBLE); + advancedOptionsLayout.setVisibility(View.VISIBLE); } else { moreOptionsChevronImageView.setImageResource(R.drawable.ic_keyboard_arrow_down); - walletSeedEditText.setVisibility(View.GONE); - walletRestoreHeightEditText.setVisibility(View.GONE); + advancedOptionsLayout.setVisibility(View.GONE); } }); + + mViewModel.enableCreateButton.observe(getViewLifecycleOwner(), enable -> { + createWalletButton.setEnabled(enable); + }); } private void bindListeners() { @@ -230,47 +235,9 @@ public class OnboardingFragment extends Fragment implements NodeSelectionBottomS } private void createOrImportWallet(String walletPassword, String confirmedPassword, String walletSeed, String restoreHeightText) { - String offset = useOffset ? walletPassword : ""; - MainActivity mainActivity = (MainActivity) getActivity(); - if (mainActivity != null) { - if (!walletPassword.isEmpty()) { - if(!walletPassword.equals(confirmedPassword)) { - mainActivity.runOnUiThread(() -> Toast.makeText(mainActivity, getString(R.string.invalid_confirmed_password), Toast.LENGTH_SHORT).show()); - return; - } - PrefService.getInstance().edit().putBoolean(Constants.PREF_USES_PASSWORD, true).apply(); - } - long restoreHeight = getNewRestoreHeight(); - File walletFile = new File(mainActivity.getApplicationInfo().dataDir, Constants.WALLET_NAME); - Wallet wallet = null; - if(!offset.isEmpty()) { - PrefService.getInstance().edit().putBoolean(Constants.PREF_USES_OFFSET, true).apply(); - } - - if (walletSeed.isEmpty()) { - Wallet tmpWallet = createTempWallet(mainActivity.getApplicationInfo().dataDir); //we do this to get seed, then recover wallet so we can use seed offset - wallet = WalletManager.getInstance().recoveryWallet(walletFile, walletPassword, tmpWallet.getSeed(""), offset, restoreHeight); - } else { - if (!checkMnemonic(walletSeed)) { - mainActivity.runOnUiThread(() -> Toast.makeText(mainActivity, getString(R.string.invalid_mnemonic_code), Toast.LENGTH_SHORT).show()); - return; - } - if (!restoreHeightText.isEmpty()) { - restoreHeight = Long.parseLong(restoreHeightText); - } - wallet = WalletManager.getInstance().recoveryWallet(walletFile, walletPassword, walletSeed, offset, restoreHeight); - } - Wallet.Status walletStatus = wallet.getStatus(); - wallet.close(); - boolean ok = walletStatus.isOk(); - walletFile.delete(); // cache is broken for some reason when recovering wallets. delete the file here. this happens in monerujo too. - - if (ok) { - mainActivity.init(walletFile, walletPassword); - mainActivity.runOnUiThread(mainActivity::onBackPressed); - } else { - mainActivity.runOnUiThread(() -> Toast.makeText(mainActivity, getString(R.string.create_wallet_failed, walletStatus.getErrorString()), Toast.LENGTH_SHORT).show()); - } + Activity activity = getActivity(); + if(activity != null) { + mViewModel.createOrImportWallet(activity, walletPassword, confirmedPassword, walletSeed, restoreHeightText, useOffset); } } @@ -294,21 +261,6 @@ public class OnboardingFragment extends Fragment implements NodeSelectionBottomS } } - private long getNewRestoreHeight() { - Calendar restoreDate = Calendar.getInstance(); - restoreDate.add(Calendar.DAY_OF_MONTH, 0); - return RestoreHeight.getInstance().getHeight(restoreDate.getTime()); - } - - private Wallet createTempWallet(String dir) { - File tmpWalletFile = new File(dir, Constants.WALLET_NAME + "_tmp"); - return WalletManager.getInstance().createWallet(tmpWalletFile, "", Constants.MNEMONIC_LANGUAGE, 0); - } - - private boolean checkMnemonic(String seed) { - return (seed.split("\\s").length == 25); - } - @Override public void onNodeSelected() { Node node = PrefService.getInstance().getNode(); diff --git a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.java b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.java index 218775d..aabd86f 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.java +++ b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.java @@ -1,20 +1,30 @@ package net.mynero.wallet.fragment.onboarding; +import android.app.Activity; import android.util.Patterns; +import android.widget.Toast; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; +import net.mynero.wallet.MainActivity; import net.mynero.wallet.MoneroApplication; -import net.mynero.wallet.data.DefaultNodes; +import net.mynero.wallet.R; +import net.mynero.wallet.model.Wallet; import net.mynero.wallet.model.WalletManager; import net.mynero.wallet.service.PrefService; import net.mynero.wallet.util.Constants; +import net.mynero.wallet.util.RestoreHeight; + +import java.io.File; +import java.util.Calendar; public class OnboardingViewModel extends ViewModel { private final MutableLiveData _showMoreOptions = new MutableLiveData<>(false); public LiveData showMoreOptions = _showMoreOptions; + private final MutableLiveData _enableCreateButton = new MutableLiveData<>(true); + public LiveData enableCreateButton = _enableCreateButton; private String proxyAddress = ""; private String proxyPort = ""; @@ -51,4 +61,70 @@ public class OnboardingViewModel extends ViewModel { public void setProxyPort(String port) { this.proxyPort = port; } + + public void createOrImportWallet(Activity mainActivity, String walletPassword, String confirmedPassword, String walletSeed, String restoreHeightText, boolean useOffset) { + MoneroApplication application = (MoneroApplication)mainActivity.getApplication(); + application.getExecutor().execute(() -> { + mainActivity.runOnUiThread(() -> { + _enableCreateButton.setValue(false); + }); + String offset = useOffset ? walletPassword : ""; + if (!walletPassword.isEmpty()) { + if(!walletPassword.equals(confirmedPassword)) { + mainActivity.runOnUiThread(() -> Toast.makeText(mainActivity, application.getString(R.string.invalid_confirmed_password), Toast.LENGTH_SHORT).show()); + return; + } + PrefService.getInstance().edit().putBoolean(Constants.PREF_USES_PASSWORD, true).apply(); + } + long restoreHeight = getNewRestoreHeight(); + File walletFile = new File(mainActivity.getApplicationInfo().dataDir, Constants.WALLET_NAME); + Wallet wallet = null; + if(!offset.isEmpty()) { + PrefService.getInstance().edit().putBoolean(Constants.PREF_USES_OFFSET, true).apply(); + } + + if (walletSeed.isEmpty()) { + Wallet tmpWallet = createTempWallet(mainActivity.getApplicationInfo().dataDir); //we do this to get seed, then recover wallet so we can use seed offset + wallet = WalletManager.getInstance().recoveryWallet(walletFile, walletPassword, tmpWallet.getSeed(""), offset, restoreHeight); + } else { + if (!checkMnemonic(walletSeed)) { + mainActivity.runOnUiThread(() -> Toast.makeText(mainActivity, application.getString(R.string.invalid_mnemonic_code), Toast.LENGTH_SHORT).show()); + return; + } + if (!restoreHeightText.isEmpty()) { + restoreHeight = Long.parseLong(restoreHeightText); + } + wallet = WalletManager.getInstance().recoveryWallet(walletFile, walletPassword, walletSeed, offset, restoreHeight); + } + Wallet.Status walletStatus = wallet.getStatus(); + wallet.close(); + boolean ok = walletStatus.isOk(); + walletFile.delete(); // cache is broken for some reason when recovering wallets. delete the file here. this happens in monerujo too. + + if (ok) { + ((MainActivity)mainActivity).init(walletFile, walletPassword); + mainActivity.runOnUiThread(mainActivity::onBackPressed); + } else { + mainActivity.runOnUiThread(() -> { + _enableCreateButton.setValue(true); + Toast.makeText(mainActivity, application.getString(R.string.create_wallet_failed, walletStatus.getErrorString()), Toast.LENGTH_SHORT).show(); + }); + } + }); + } + + private long getNewRestoreHeight() { + Calendar restoreDate = Calendar.getInstance(); + restoreDate.add(Calendar.DAY_OF_MONTH, 0); + return RestoreHeight.getInstance().getHeight(restoreDate.getTime()); + } + + private Wallet createTempWallet(String dir) { + File tmpWalletFile = new File(dir, Constants.WALLET_NAME + "_tmp"); + return WalletManager.getInstance().createWallet(tmpWalletFile, "", Constants.MNEMONIC_LANGUAGE, 0); + } + + private boolean checkMnemonic(String seed) { + return (seed.split("\\s").length == 25); + } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_onboarding.xml b/app/src/main/res/layout/fragment_onboarding.xml index a5f69f4..c4fa189 100644 --- a/app/src/main/res/layout/fragment_onboarding.xml +++ b/app/src/main/res/layout/fragment_onboarding.xml @@ -17,32 +17,15 @@ android:id="@+id/create_wallet_textview" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginBottom="32dp" + android:layout_marginBottom="16dp" android:text="@string/create_wallet" android:textSize="32sp" android:textStyle="bold" - app:layout_constraintBottom_toTopOf="@id/select_node_button" + app:layout_constraintBottom_toTopOf="@id/wallet_password_edittext" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> -