-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.xaml.cs
81 lines (66 loc) · 2.47 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace Wacton.Desu.ExampleWpf
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;
using Wacton.Desu.ExampleWpf.Properties;
using Wacton.Desu.Kanji;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private readonly List<IKanjiEntry> kanjiEntries;
private IKanjiEntry currentEntry;
private readonly Random random = new Random();
public MainWindow()
{
kanjiEntries = KanjiDictionary.ParseEntries().ToList();
UpdateKanji();
InitializeComponent();
}
public string Text => currentEntry.Literal;
public string Meanings => GetEnglishMeanings();
public ObservableCollection<Geometry> Strokes { get; set; }
private void OnNextButtonClick(object sender, RoutedEventArgs e) => UpdateKanji();
private void UpdateKanji()
{
var hasStrokePaths = false;
while (!hasStrokePaths)
{
currentEntry = kanjiEntries[GetRandomIndex()];
hasStrokePaths = currentEntry.StrokePaths.Any();
}
Strokes = new ObservableCollection<Geometry>();
foreach (var strokePath in currentEntry.StrokePaths)
{
Strokes.Add(Geometry.Parse(strokePath));
}
OnPropertyChanged(nameof(Text));
OnPropertyChanged(nameof(Meanings));
OnPropertyChanged(nameof(Strokes));
}
private int GetRandomIndex()
{
return random.Next(0, kanjiEntries.Count - 1);
}
private string GetEnglishMeanings()
{
var englishMeanings = currentEntry.Meanings
.Where(meaning => meaning.Language.Equals(Enums.Language.English))
.Select(meaning => meaning.Term);
return string.Join(" · ", englishMeanings);
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}