private void OnSyncedWallet(Wallet wallet) { var accountBalances = wallet.CalculateBalances(2); // TODO: configurable confirmations var accountViewModels = wallet.EnumerateAccounts() .Zip(accountBalances, (a, bals) => new AccountViewModel(a.Item1, a.Item2, bals)) .ToList(); var txSet = wallet.RecentTransactions; var recentTx = txSet.UnminedTransactions .Select(x => new TransactionViewModel(wallet, x.Value, BlockIdentity.Unmined)) .Concat(txSet.MinedTransactions.ReverseList().SelectMany(b => b.Transactions.Select(tx => new TransactionViewModel(wallet, tx, b.Identity)))) .Take(10); var overviewViewModel = (OverviewViewModel)SingletonViewModelLocator.Resolve("Overview"); App.Current.Dispatcher.Invoke(() => { foreach (var vm in accountViewModels) { Accounts.Add(vm); } foreach (var tx in recentTx) { overviewViewModel.RecentTransactions.Add(tx); } }); TotalBalance = wallet.TotalBalance; TransactionCount = txSet.TransactionCount(); SyncedBlockHeight = wallet.ChainTip.Height; SelectedAccount = accountViewModels[0]; BlocksUntilTicketPriceRetarget = BlockChain.BlocksUntilTicketPriceRetarget(SyncedBlockHeight, wallet.ActiveChain); RaisePropertyChanged(nameof(TotalBalance)); RaisePropertyChanged(nameof(AccountNames)); overviewViewModel.AccountsCount = accountViewModels.Count(); var shell = (ShellViewModel)ViewModelLocator.ShellViewModel; shell.StartupWizardVisible = false; }
private void OnWalletChangesProcessed(object sender, Wallet.ChangesProcessedEventArgs e) { var wallet = (Wallet)sender; var currentHeight = e.NewChainTip?.Height ?? SyncedBlockHeight; // TODO: The OverviewViewModel should probably connect to this event. This could be // done after the wallet is synced. var overviewViewModel = ViewModelLocator.OverviewViewModel as OverviewViewModel; if (overviewViewModel != null) { var movedTxViewModels = overviewViewModel.RecentTransactions .Where(txvm => e.MovedTransactions.ContainsKey(txvm.TxHash)) .Select(txvm => Tuple.Create(txvm, e.MovedTransactions[txvm.TxHash])); var newTxViewModels = e.AddedTransactions.Select(tx => new TransactionViewModel(wallet, tx.Item1, tx.Item2)).ToList(); foreach (var movedTx in movedTxViewModels) { var txvm = movedTx.Item1; var location = movedTx.Item2; txvm.Location = location; txvm.Depth = BlockChain.Depth(currentHeight, location); } App.Current.Dispatcher.Invoke(() => { foreach (var txvm in newTxViewModels) { overviewViewModel.RecentTransactions.Insert(0, txvm); } while (overviewViewModel.RecentTransactions.Count > 10) { overviewViewModel.RecentTransactions.RemoveAt(10); } }); } // TODO: same.. in fact these tx viewmodels should be reused so changes don't need to be recalculated. // It would be a good idea for this synchronzier viewmodel to manage these and hand them out to other // viewmodels for sorting and organization. var transactionHistoryViewModel = ViewModelLocator.TransactionHistoryViewModel as TransactionHistoryViewModel; if (transactionHistoryViewModel != null) { foreach (var tx in transactionHistoryViewModel.Transactions) { var txvm = tx.Transaction; BlockIdentity newLocation; if (e.MovedTransactions.TryGetValue(txvm.TxHash, out newLocation)) { txvm.Location = newLocation; } txvm.Depth = BlockChain.Depth(currentHeight, txvm.Location); } transactionHistoryViewModel.AppendNewTransactions(wallet, e.AddedTransactions); } foreach (var modifiedAccount in e.ModifiedAccountProperties) { var accountNumber = checked ((int)modifiedAccount.Key.AccountNumber); var accountProperties = modifiedAccount.Value; if (accountNumber < Accounts.Count) { Accounts[accountNumber].AccountProperties = accountProperties; } } // TODO: this would be better if all new accounts were a field in the event message. var newAccounts = e.ModifiedAccountProperties. Where(kvp => kvp.Key.AccountNumber >= Accounts.Count && kvp.Key.AccountNumber != Wallet.ImportedAccountNumber). OrderBy(kvp => kvp.Key.AccountNumber); foreach (var modifiedAccount in newAccounts) { var accountNumber = checked ((int)modifiedAccount.Key.AccountNumber); var accountProperties = modifiedAccount.Value; // TODO: This is very inefficient because it recalculates balances of every account, for each new account. var accountBalance = wallet.CalculateBalances(2)[accountNumber]; var accountViewModel = new AccountViewModel(modifiedAccount.Key, accountProperties, accountBalance); App.Current.Dispatcher.Invoke(() => Accounts.Add(accountViewModel)); } if (e.NewChainTip != null) { SyncedBlockHeight = e.NewChainTip.Value.Height; var oldRetarget = BlocksUntilTicketPriceRetarget; BlocksUntilTicketPriceRetarget = BlockChain.BlocksUntilTicketPriceRetarget(SyncedBlockHeight, wallet.ActiveChain); if (BlocksUntilTicketPriceRetarget > oldRetarget) { Task.Run(async() => { var sdiff = await WalletRpcClient.TicketPriceAsync(); await App.Current.Dispatcher.InvokeAsync(() => TicketPrice = sdiff); }).ContinueWith(t => { if (t.Exception != null) { MessageBox.Show(t.Exception.InnerException.Message, "Error querying ticket price"); } }); } } if (e.AddedTransactions.Count != 0 || e.RemovedTransactions.Count != 0 || e.NewChainTip != null) { TotalBalance = wallet.TotalBalance; TransactionCount += e.AddedTransactions.Count - e.RemovedTransactions.Count; var balances = wallet.CalculateBalances(2); // TODO: don't hardcode confs for (var i = 0; i < balances.Length; i++) { Accounts[i].Balances = balances[i]; } } }