public ReceiveTabViewModel(WalletViewModel walletViewModel)
            : base("Receive", walletViewModel)
        {
            _addresses = new ObservableCollection <AddressViewModel>();
            Label      = "";

            InitializeAddresses();

            GenerateCommand = ReactiveCommand.Create(() =>
            {
                var label = new SmartLabel(Label);
                Label     = label.ToString();
                if (label.IsEmpty)
                {
                    LabelRequiredNotificationVisible = true;
                    LabelRequiredNotificationOpacity = 1;

                    Dispatcher.UIThread.PostLogException(async() =>
                    {
                        await Task.Delay(TimeSpan.FromSeconds(4));
                        LabelRequiredNotificationOpacity = 0;
                    });

                    return;
                }

                Dispatcher.UIThread.PostLogException(() =>
                {
                    HdPubKey newKey = Global.WalletService.GetReceiveKey(new SmartLabel(Label), Addresses.Select(x => x.Model).Take(7));                                     // Never touch the first 7 keys.

                    AddressViewModel found = Addresses.FirstOrDefault(x => x.Model == newKey);
                    if (found != default)
                    {
                        Addresses.Remove(found);
                    }

                    var newAddress = new AddressViewModel(newKey, Global);

                    Addresses.Insert(0, newAddress);

                    SelectedAddress = newAddress;

                    Label = "";
                });
            });

            this.WhenAnyValue(x => x.Label).Subscribe(UpdateSuggestions);

            this.WhenAnyValue(x => x.SelectedAddress).Subscribe(async address =>
            {
                if (Global.UiConfig?.Autocopy is false || address is null)
                {
                    return;
                }

                await address.TryCopyToClipboardAsync();
            });

            var isCoinListItemSelected = this.WhenAnyValue(x => x.SelectedAddress).Select(coin => coin != null);

            CopyAddress = ReactiveCommand.CreateFromTask(async() =>
            {
                if (SelectedAddress is null)
                {
                    return;
                }

                await SelectedAddress.TryCopyToClipboardAsync();
            },
                                                         isCoinListItemSelected);

            CopyLabel = ReactiveCommand.CreateFromTask(async() =>
            {
                try
                {
                    await Application.Current.Clipboard.SetTextAsync(SelectedAddress.Label ?? string.Empty);
                }
                catch (Exception)
                { }
            },
                                                       isCoinListItemSelected);

            ToggleQrCode = ReactiveCommand.Create(() =>
            {
                try
                {
                    SelectedAddress.IsExpanded = !SelectedAddress.IsExpanded;
                }
                catch (Exception)
                { }
            },
                                                  isCoinListItemSelected);

#pragma warning disable IDE0053 // Use expression body for lambda expressions
            ChangeLabelCommand = ReactiveCommand.Create(() => { SelectedAddress.InEditMode = true; });
#pragma warning restore IDE0053 // Use expression body for lambda expressions

            DisplayAddressOnHwCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                var client    = new HwiClient(Global.Network);
                using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
                try
                {
                    await client.DisplayAddressAsync(KeyManager.MasterFingerprint.Value, SelectedAddress.Model.FullKeyPath, cts.Token);
                }
                catch (HwiException)
                {
                    await PinPadViewModel.UnlockAsync(Global);
                    await client.DisplayAddressAsync(KeyManager.MasterFingerprint.Value, SelectedAddress.Model.FullKeyPath, cts.Token);
                }
            });

            _suggestions = new ObservableCollection <SuggestionViewModel>();
        }
Exemple #2
0
        public ReceiveTabViewModel(WalletViewModel walletViewModel)
            : base("Receive", walletViewModel)
        {
            _addresses = new ObservableCollection <AddressViewModel>();
            Label      = "";

            InitializeAddresses();

            GenerateCommand = ReactiveCommand.Create(() =>
            {
                Label = Label.Trim(',', ' ').Trim();
                if (string.IsNullOrWhiteSpace(Label))
                {
                    LabelRequiredNotificationVisible = true;
                    LabelRequiredNotificationOpacity = 1;

                    Dispatcher.UIThread.PostLogException(async() =>
                    {
                        await Task.Delay(TimeSpan.FromSeconds(4));
                        LabelRequiredNotificationOpacity = 0;
                    });

                    return;
                }

                Dispatcher.UIThread.PostLogException(() =>
                {
                    var label       = Label;
                    HdPubKey newKey = Global.WalletService.GetReceiveKey(label, Addresses.Select(x => x.Model).Take(7));                     // Never touch the first 7 keys.

                    AddressViewModel found = Addresses.FirstOrDefault(x => x.Model == newKey);
                    if (found != default)
                    {
                        Addresses.Remove(found);
                    }

                    var newAddress = new AddressViewModel(newKey, Global);

                    Addresses.Insert(0, newAddress);

                    SelectedAddress = newAddress;

                    Label = "";
                });
            });

            this.WhenAnyValue(x => x.Label).Subscribe(UpdateSuggestions);

            this.WhenAnyValue(x => x.SelectedAddress).Subscribe(async address =>
            {
                if (Global.UiConfig?.Autocopy is false || address is null)
                {
                    return;
                }

                await address.TryCopyToClipboardAsync();
            });

            var isCoinListItemSelected = this.WhenAnyValue(x => x.SelectedAddress).Select(coin => coin != null);

            CopyAddress = ReactiveCommand.CreateFromTask(async() =>
            {
                if (SelectedAddress is null)
                {
                    return;
                }

                await SelectedAddress.TryCopyToClipboardAsync();
            }, isCoinListItemSelected);

            CopyLabel = ReactiveCommand.CreateFromTask(async() =>
            {
                try
                {
                    await Application.Current.Clipboard.SetTextAsync(SelectedAddress.Label ?? string.Empty);
                }
                catch (Exception)
                { }
            }, isCoinListItemSelected);

            ShowQrCode = ReactiveCommand.Create(() =>
            {
                try
                {
                    SelectedAddress.IsExpanded = true;
                }
                catch (Exception)
                { }
            }, isCoinListItemSelected);

            ChangeLabelCommand = ReactiveCommand.Create(() =>
            {
                SelectedAddress.InEditMode = true;
            });

            _suggestions = new ObservableCollection <SuggestionViewModel>();
        }