From 7652506534e6fac81995a84a03073ead2d0056bc Mon Sep 17 00:00:00 2001 From: tomashrib Date: Sat, 22 Apr 2023 22:13:44 +0200 Subject: [PATCH 1/2] feat: change if statement to detect false and use ViewModel from separate file --- .../zephyruswallet/data/ZephyrusViewModel.kt | 29 +++++++++++++++++++ .../zephyruswallet/ui/wallet/HomeScreen.kt | 10 ++++--- .../zephyruswallet/ui/wallet/SendScreen.kt | 1 + 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/xyz/tomashrib/zephyruswallet/data/ZephyrusViewModel.kt diff --git a/app/src/main/java/xyz/tomashrib/zephyruswallet/data/ZephyrusViewModel.kt b/app/src/main/java/xyz/tomashrib/zephyruswallet/data/ZephyrusViewModel.kt new file mode 100644 index 0000000..7292b3b --- /dev/null +++ b/app/src/main/java/xyz/tomashrib/zephyruswallet/data/ZephyrusViewModel.kt @@ -0,0 +1,29 @@ +package xyz.tomashrib.zephyruswallet.data + +import android.content.Context +import androidx.compose.runtime.mutableStateOf +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.Dispatchers + +class ZephyrusViewModel : ViewModel() { + // Define your mutable states and other properties here + + val hasSynced = mutableStateOf(false) + +// // Define your update functions and other ViewModel related functions here +// fun updateBalance() { +// viewModelScope.launch(Dispatchers.IO) { +// // Update balance logic +// } +// } +// +// fun updatePrice(context: Context) { +// viewModelScope.launch(Dispatchers.IO) { +// // Update price logic +// } +// } + + // Other ViewModel related functions +} diff --git a/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/HomeScreen.kt b/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/HomeScreen.kt index 6be703e..d179345 100644 --- a/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/HomeScreen.kt +++ b/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/HomeScreen.kt @@ -49,6 +49,7 @@ import xyz.tomashrib.zephyruswallet.ui.theme.sourceSansSemiBold import xyz.tomashrib.zephyruswallet.tools.formatSats import xyz.tomashrib.zephyruswallet.tools.timestampToString import java.util.concurrent.CountDownLatch +import xyz.tomashrib.zephyruswallet.data.ZephyrusViewModel // viewmodel handles the data across screen refreshes internal class WalletViewModel() : ViewModel() { @@ -109,7 +110,8 @@ internal class WalletViewModel() : ViewModel() { internal fun HomeScreen( navController: NavController, context: Context, - walletViewModel: WalletViewModel = viewModel() + walletViewModel: WalletViewModel = viewModel(), + zephyrusViewModel: ZephyrusViewModel = viewModel() ) { //complete list of all transaction associated with current wallet // val allTransactions: List = Wallet.getTransactions() @@ -131,14 +133,14 @@ internal fun HomeScreen( Log.i(TAG, "Creating new blockchain") Wallet.createBlockchain() } - val hasSynced = rememberSaveable { mutableStateOf(false) } - if (!hasSynced.value) { + if(!zephyrusViewModel.hasSynced.value) { walletViewModel.updateBalance() walletViewModel.updatePrice(context) Toast.makeText(context, "Wallet is syncing...", Toast.LENGTH_SHORT).show() - hasSynced.value = true + zephyrusViewModel.hasSynced.value = true } + // walletViewModel.updateBalance() // Toast.makeText(context, "Wallet is syncing...", Toast.LENGTH_SHORT).show() diff --git a/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/SendScreen.kt b/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/SendScreen.kt index 70ca27e..9d64d31 100644 --- a/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/SendScreen.kt +++ b/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/SendScreen.kt @@ -63,6 +63,7 @@ import okhttp3.OkHttpClient import org.json.JSONObject import java.io.IOException import java.util.concurrent.CountDownLatch +import xyz.tomashrib.zephyruswallet.data.ZephyrusViewModel internal class SendScreenViewModel() : ViewModel(){ private var _feeRates: MutableLiveData> = MutableLiveData(arrayOf()) From ddf288af9257305d78e352c5b2887353feb5fb0c Mon Sep 17 00:00:00 2001 From: tomashrib Date: Sat, 22 Apr 2023 22:43:07 +0200 Subject: [PATCH 2/2] feat: dont route to HomeScreen after tx broadcast --- .idea/misc.xml | 1 - .../tomashrib/zephyruswallet/ui/wallet/HomeScreen.kt | 12 ++++++------ .../tomashrib/zephyruswallet/ui/wallet/SendScreen.kt | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ad17cb..8978d23 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/HomeScreen.kt b/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/HomeScreen.kt index d179345..2cd6135 100644 --- a/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/HomeScreen.kt +++ b/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/HomeScreen.kt @@ -134,12 +134,6 @@ internal fun HomeScreen( Wallet.createBlockchain() } - if(!zephyrusViewModel.hasSynced.value) { - walletViewModel.updateBalance() - walletViewModel.updatePrice(context) - Toast.makeText(context, "Wallet is syncing...", Toast.LENGTH_SHORT).show() - zephyrusViewModel.hasSynced.value = true - } // walletViewModel.updateBalance() // Toast.makeText(context, "Wallet is syncing...", Toast.LENGTH_SHORT).show() @@ -362,6 +356,12 @@ internal fun HomeScreen( } //constraint end + if(!zephyrusViewModel.hasSynced.value) { + walletViewModel.updateBalance() + walletViewModel.updatePrice(context) + Toast.makeText(context, "Wallet is syncing...", Toast.LENGTH_SHORT).show() + zephyrusViewModel.hasSynced.value = true + } } //function that checks if the internet connectivity is available diff --git a/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/SendScreen.kt b/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/SendScreen.kt index 9d64d31..2bfdc8e 100644 --- a/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/SendScreen.kt +++ b/app/src/main/java/xyz/tomashrib/zephyruswallet/ui/wallet/SendScreen.kt @@ -605,7 +605,7 @@ private fun broadcastTransaction(recipientAddress: String, amount: ULong, feeRat Toast.makeText(context, "Transaction was broadcasted!", Toast.LENGTH_SHORT).show() //because its successfull, it goes back to HomeScreen - navController.navigate(Screen.HomeScreen.route) +// navController.navigate(Screen.HomeScreen.route) } catch (e: Throwable) { Log.i(TAG, "Broadcast error: ${e.message}")