From a023260bf2041304ffcb6901f8a5ae4bd9fd8592 Mon Sep 17 00:00:00 2001 From: pokkst Date: Thu, 8 Sep 2022 00:02:35 -0500 Subject: [PATCH] Add loading bar --- .../com/m2049r/xmrwallet/MainActivity.java | 5 +++ .../xmrwallet/fragment/home/HomeFragment.java | 20 ++++++++++ .../onboarding/OnboardingFragment.java | 2 +- .../xmrwallet/service/BlockchainService.java | 40 +++++++++++++++++++ .../drawable/sync_progress_bar_drawable.xml | 20 ++++++++++ app/src/main/res/layout/fragment_home.xml | 9 +++++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/m2049r/xmrwallet/service/BlockchainService.java create mode 100644 app/src/main/res/drawable/sync_progress_bar_drawable.xml diff --git a/app/src/main/java/com/m2049r/xmrwallet/MainActivity.java b/app/src/main/java/com/m2049r/xmrwallet/MainActivity.java index e325c3f..9799fcf 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/MainActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/MainActivity.java @@ -15,6 +15,7 @@ import com.m2049r.xmrwallet.model.Wallet; import com.m2049r.xmrwallet.model.WalletManager; import com.m2049r.xmrwallet.service.AddressService; import com.m2049r.xmrwallet.service.BalanceService; +import com.m2049r.xmrwallet.service.BlockchainService; import com.m2049r.xmrwallet.service.HistoryService; import com.m2049r.xmrwallet.service.MoneroHandlerThread; import com.m2049r.xmrwallet.service.PrefService; @@ -30,6 +31,7 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre private BalanceService balanceService = null; private AddressService addressService = null; private HistoryService historyService = null; + private BlockchainService blockchainService = null; @Override protected void onCreate(Bundle savedInstanceState) { @@ -44,6 +46,7 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre init(walletFile, ""); } else { PasswordBottomSheetDialog passwordDialog = new PasswordBottomSheetDialog(); + passwordDialog.setCancelable(false); passwordDialog.listener = this; passwordDialog.show(getSupportFragmentManager(), null); } @@ -74,6 +77,7 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre this.balanceService = new BalanceService(this, thread); this.addressService = new AddressService(this, thread); this.historyService = new HistoryService(this, thread); + this.blockchainService = new BlockchainService(this, thread); thread.start(); } @@ -82,6 +86,7 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre this.historyService.refreshHistory(); this.balanceService.refreshBalance(); this.addressService.refreshAddress(); + this.blockchainService.refreshBlockchain(); } @Override diff --git a/app/src/main/java/com/m2049r/xmrwallet/fragment/home/HomeFragment.java b/app/src/main/java/com/m2049r/xmrwallet/fragment/home/HomeFragment.java index bd12d91..92c7950 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/fragment/home/HomeFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/fragment/home/HomeFragment.java @@ -8,6 +8,7 @@ import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; +import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -30,8 +31,10 @@ import com.m2049r.xmrwallet.fragment.dialog.ReceiveBottomSheetDialog; import com.m2049r.xmrwallet.fragment.dialog.SendBottomSheetDialog; import com.m2049r.xmrwallet.model.TransactionInfo; import com.m2049r.xmrwallet.model.Wallet; +import com.m2049r.xmrwallet.model.WalletManager; import com.m2049r.xmrwallet.service.AddressService; import com.m2049r.xmrwallet.service.BalanceService; +import com.m2049r.xmrwallet.service.BlockchainService; import com.m2049r.xmrwallet.service.HistoryService; import com.m2049r.xmrwallet.service.PrefService; import com.m2049r.xmrwallet.service.TxService; @@ -90,6 +93,7 @@ public class HomeFragment extends Fragment implements TransactionInfoAdapter.TxI BalanceService balanceService = BalanceService.getInstance(); HistoryService historyService = HistoryService.getInstance(); + BlockchainService blockchainService = BlockchainService.getInstance(); if(balanceService != null) { balanceService.balance.observe(getViewLifecycleOwner(), balance -> { @@ -106,6 +110,22 @@ public class HomeFragment extends Fragment implements TransactionInfoAdapter.TxI }); } + ProgressBar progressBar = view.findViewById(R.id.sync_progress_bar); + if(blockchainService != null) { + blockchainService.height.observe(getViewLifecycleOwner(), height -> { + long daemonHeight = WalletManager.getInstance().getWallet().getDaemonBlockChainHeight(); + int syncPct = (int)blockchainService.getSyncPercentage(); + progressBar.setIndeterminate(height < 1 || daemonHeight <= 0); + if(height > 1 && daemonHeight > 1) { + progressBar.setProgress(syncPct); + + if(WalletManager.getInstance().getWallet().isSynchronized()) { + progressBar.setVisibility(View.INVISIBLE); + } + } + }); + } + TransactionInfoAdapter adapter = new TransactionInfoAdapter(this); txHistoryRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); txHistoryRecyclerView.setAdapter(adapter); diff --git a/app/src/main/java/com/m2049r/xmrwallet/fragment/onboarding/OnboardingFragment.java b/app/src/main/java/com/m2049r/xmrwallet/fragment/onboarding/OnboardingFragment.java index da67f07..0153d53 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/fragment/onboarding/OnboardingFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/fragment/onboarding/OnboardingFragment.java @@ -50,7 +50,7 @@ public class OnboardingFragment extends Fragment { File walletFile = new File(getActivity().getApplicationInfo().dataDir, Constants.WALLET_NAME); Wallet wallet = null; if(walletSeed.isEmpty()) { - wallet = WalletManager.getInstance().createWallet(walletFile, walletPassword, Constants.MNEMONIC_LANGUAGE, -1); + wallet = WalletManager.getInstance().createWallet(walletFile, walletPassword, Constants.MNEMONIC_LANGUAGE, 1); } else { if(!checkMnemonic(walletSeed)) { Toast.makeText(getContext(), getString(R.string.invalid_mnemonic_code), Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/BlockchainService.java b/app/src/main/java/com/m2049r/xmrwallet/service/BlockchainService.java new file mode 100644 index 0000000..487e07a --- /dev/null +++ b/app/src/main/java/com/m2049r/xmrwallet/service/BlockchainService.java @@ -0,0 +1,40 @@ +package com.m2049r.xmrwallet.service; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; + +import com.m2049r.xmrwallet.MainActivity; +import com.m2049r.xmrwallet.model.WalletManager; + +public class BlockchainService extends ServiceBase { + public static BlockchainService instance = null; + + public static BlockchainService getInstance() { + return instance; + } + + private final MutableLiveData _currentHeight = new MutableLiveData<>(0L); + public LiveData height = _currentHeight; + + public BlockchainService(MainActivity mainActivity, MoneroHandlerThread thread) { + super(mainActivity, thread); + instance = this; + } + + public void refreshBlockchain() { + _currentHeight.postValue(getCurrentHeight()); + } + + public long getCurrentHeight() { + return WalletManager.getInstance().getWallet().getBlockChainHeight(); + } + + public long getDaemonHeight() { + return WalletManager.getInstance().getWallet().getDaemonBlockChainHeight(); + } + + public double getSyncPercentage() { + double percentRaw = (double)getCurrentHeight()/(double)getDaemonHeight(); + return percentRaw * 100d; + } +} diff --git a/app/src/main/res/drawable/sync_progress_bar_drawable.xml b/app/src/main/res/drawable/sync_progress_bar_drawable.xml new file mode 100644 index 0000000..0e51519 --- /dev/null +++ b/app/src/main/res/drawable/sync_progress_bar_drawable.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index ff9a0c5..dc21dc1 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -6,6 +6,15 @@ android:layout_height="match_parent" tools:context=".fragment.home.HomeFragment"> + +