-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordDefinitionPage.xaml.cs
124 lines (105 loc) · 3.83 KB
/
WordDefinitionPage.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Collections;
using System.ComponentModel.Design;
namespace LearningApp;
public partial class WordDefinitionPage : ContentPage
{
private string? _currentWordText;
private readonly DictionaryService _dictionaryService = new DictionaryService();
private readonly Action<string>? _onWordRemoved;
private readonly DatabaseService _databaseService;
public WordDefinitionPage(string definition, Action<string> onWordRemoved = null)
{
InitializeComponent();
var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "words.db3"); ;
_databaseService = new DatabaseService(dbPath);
_onWordRemoved = onWordRemoved;
SearchDefinition(definition);
}
private async void SearchDefinition(string word)
{
string query = word;
if (!string.IsNullOrEmpty(query))
{
var wordData = await _dictionaryService.SearchWordAsync(query);
if (wordData != null)
{
_currentWordText = wordData.Word;
DisplayWordDetails(wordData);
}
else
{
await DisplayAlert("Error", "Word not found or request failed.", "OK");
}
}
}
private async void OnCloseButtonClicked(object sender, EventArgs e)
{
await Navigation.PopModalAsync();
}
private void DisplayWordDetails(Words wordData)
{
ContentArea.Children.Clear();
ContentArea.Children.Add(new Label
{
Text = wordData.Word,
FontAttributes = FontAttributes.Bold,
FontSize = 24,
HorizontalOptions = LayoutOptions.Center
});
if (wordData.Phonetics != null && wordData.Phonetics.Count > 0)
{
ContentArea.Children.Add(new Label
{
Text = "Phonetics:",
FontAttributes = FontAttributes.Bold,
FontSize = 18,
Margin = new Thickness(0, 10, 0, 5)
});
foreach (var phonetic in wordData.Phonetics)
{
ContentArea.Children.Add(new Label
{
Text = phonetic.Text,
FontSize = 16
});
}
}
if (wordData.Meanings != null && wordData.Meanings.Count > 0)
{
foreach (var meaning in wordData.Meanings)
{
ContentArea.Children.Add(new Label
{
Text = char.ToUpper(meaning.PartOfSpeech[0]) + meaning.PartOfSpeech.Substring(1) + ":",
FontAttributes = FontAttributes.Bold,
FontSize = 18,
Margin = new Thickness(0, 10, 0, 5)
});
if (meaning.Definitions != null && meaning.Definitions.Count > 0)
{
ContentArea.Children.Add(new Label
{
Text = meaning.Definitions[0].DefinitionText,
FontSize = 16,
Margin = new Thickness(10, 0, 0, 5)
});
}
}
}
var addWordButton = new Button
{
Text = "Remove Word",
Margin = new Thickness(0, 20, 0, 0),
HorizontalOptions = LayoutOptions.Center
};
addWordButton.Clicked += OnRemovedWordButtonClicked;
ContentArea.Children.Add(addWordButton);
}
private async void OnRemovedWordButtonClicked(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(_currentWordText)) return;
await _databaseService.RemoveWordAsync(_currentWordText);
_onWordRemoved?.Invoke(_currentWordText);
await DisplayAlert("Remove Word", "The word has been removed successfully!", "OK");
}
}