Permalink
Browse files

Follow MVVM pattern

  • Loading branch information...
lontivero committed Nov 5, 2018
1 parent 5ab3d14 commit 3211c4e61ef019c494b87e236282904171b33c95
@@ -2,20 +2,22 @@
using Avalonia.Controls;
using Avalonia.Xaml.Interactivity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using WalletWasabi.Gui.Tabs.WalletManager;
namespace WalletWasabi.Gui.Behaviors
{
internal class MnemonicSuggestionBehavior : Behavior<TextBox>
{
private CompositeDisposable _disposables;
private static readonly AvaloniaProperty<string[]> SuggestionItemsProperty =
AvaloniaProperty.Register<MnemonicSuggestionBehavior, string[]>(nameof(SuggestionItems), defaultBindingMode: Avalonia.Data.BindingMode.TwoWay);
private static readonly AvaloniaProperty<IEnumerable<MnemonicViewModel>> SuggestionItemsProperty =
AvaloniaProperty.Register<MnemonicSuggestionBehavior, IEnumerable<MnemonicViewModel>>(nameof(SuggestionItems), defaultBindingMode: Avalonia.Data.BindingMode.TwoWay);
public string[] SuggestionItems
public IEnumerable<MnemonicViewModel> SuggestionItems
{
get => GetValue(SuggestionItemsProperty);
set => SetValue(SuggestionItemsProperty, value);
@@ -51,9 +53,9 @@ private void HandleAutoUpdate()
var enteredWordList = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var lastWorld = enteredWordList.LastOrDefault();
var suggestions = SuggestionItems;
if(suggestions.Length == 1)
if(suggestions.Count() == 1)
{
textBox.Text = text.Substring(0, text.Length - lastWorld.Length) + suggestions[0] + " ";
suggestions.First().OnSelected();
}
}
}
@@ -0,0 +1,24 @@
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
namespace WalletWasabi.Gui.Tabs.WalletManager
{
internal class MnemonicViewModel
{
public string Word { get; }
public Action<string> OnSelection { get; }
public MnemonicViewModel(string word, Action<string> onSeleted)
{
Word = word;
OnSelection = onSeleted;
}
public void OnSelected()
{
OnSelection(Word);
}
}
}
@@ -25,7 +25,7 @@
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Focusable="False" Content="{Binding}" Command="{Binding $parent[ItemsControl].DataContext.OnAddWord}" CommandParameter="{Binding}" />
<Button Focusable="False" Content="{Binding Word}" Command="{Binding OnSelected}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
@@ -1,11 +1,15 @@
using AvalonStudio.Extensibility;
using Avalonia.Threading;
using AvalonStudio.Extensibility;
using AvalonStudio.Shell;
using NBitcoin;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using WalletWasabi.Gui.ViewModels;
using WalletWasabi.Helpers;
using WalletWasabi.KeyManagement;
@@ -20,7 +24,7 @@ internal class RecoverWalletViewModel : CategoryViewModel
private string _walletName;
private bool _termsAccepted;
private string _validationMessage;
private string[] _suggestions;
private ObservableCollection<MnemonicViewModel> _suggestions;
public RecoverWalletViewModel(WalletManagerViewModel owner) : base("Recover Wallet")
{
@@ -77,6 +81,7 @@ public RecoverWalletViewModel(WalletManagerViewModel owner) : base("Recover Wall
}
}
});
_suggestions = new ObservableCollection<MnemonicViewModel>();
}
public string Password
@@ -91,7 +96,7 @@ public string MnemonicWords
set { this.RaiseAndSetIfChanged(ref _mnemonicWords, value); }
}
public string[] Suggestions
public ObservableCollection<MnemonicViewModel> Suggestions
{
get { return _suggestions; }
set { this.RaiseAndSetIfChanged(ref _suggestions, value); }
@@ -153,21 +158,26 @@ private void UpdateSuggestions(string words)
if (lastWorld.Length < 1)
{
Suggestions = new string[0];
_suggestions.Clear();
return;
}
var suggestedWords = EnglishWords.Where(w => w.StartsWith(lastWorld));
Suggestions = suggestedWords.ToArray();
_suggestions.Clear();
foreach(var suggestion in suggestedWords)
{
_suggestions.Add(new MnemonicViewModel(suggestion, OnAddWord));
}
}
public void OnAddWord(string word)
{
string[] words = MnemonicWords.Split(' ', StringSplitOptions.RemoveEmptyEntries);
words[words.Length-1] = word;
Suggestions = new string[0];
MnemonicWords = string.Join(' ', words) + " ";
Task.Delay(50).ContinueWith(x=>
Dispatcher.UIThread.InvokeAsync(()=>_suggestions.Clear()));
}
private static IEnumerable<string> EnglishWords = Wordlist.English.GetWords();

0 comments on commit 3211c4e

Please sign in to comment.