Skip to content

Commit

Permalink
Merge pull request #49 from tohrxyz/fix/sync-after-broadcast-tx
Browse files Browse the repository at this point in the history
fix: sync after broadcast tx
  • Loading branch information
tohrxyz authored Apr 22, 2023
2 parents c73e4a2 + ddf288a commit 484bd7c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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<TransactionDetails> = Wallet.getTransactions()
Expand All @@ -131,14 +133,8 @@ internal fun HomeScreen(
Log.i(TAG, "Creating new blockchain")
Wallet.createBlockchain()
}
val hasSynced = rememberSaveable { mutableStateOf(false) }

if (!hasSynced.value) {
walletViewModel.updateBalance()
walletViewModel.updatePrice(context)
Toast.makeText(context, "Wallet is syncing...", Toast.LENGTH_SHORT).show()
hasSynced.value = true
}

// walletViewModel.updateBalance()
// Toast.makeText(context, "Wallet is syncing...", Toast.LENGTH_SHORT).show()

Expand Down Expand Up @@ -360,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<ULong>> = MutableLiveData(arrayOf())
Expand Down Expand Up @@ -604,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}")

Expand Down

0 comments on commit 484bd7c

Please sign in to comment.