-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionaryPage.xaml.cs
139 lines (101 loc) · 3.01 KB
/
DictionaryPage.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Microsoft.Maui.Controls;
using System.Collections.Generic;
using System.Text;
namespace PracticaPalabrasMAUI;
public partial class DictionaryPage : ContentPage
{
private static string FileDir => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Language.LangCode);
private static string FilePath => Path.Combine(FileDir, "dictionary.txt");
private string text;
public DictionaryPage()
{
InitializeComponent();
BindingContext = this;
App.CurrentApp.langChanged += LoadText;
LoadText();
}
public string Text {
get => text;
set {
text = value;
OnPropertyChanged();
OnPropertyChanged(nameof(HasDuplicateds));
Save(TextToSave);
}
}
string TextToSave { get {
StringBuilder sb = new StringBuilder();
foreach (Word word in Clear(Parse(Text)))
{
sb.AppendLine(word.ToSaveString);
}
return sb.ToString();
} }
public bool HasDuplicateds {
get {
IEnumerable<Word> dirty = Parse(Text);
return Clear(dirty).Count != dirty.Count();
}
}
static IEnumerable<Word> AllWordsDirty => Parse(Load());
public static IList<Word> AllWords => Clear(AllWordsDirty);
protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);
Text=Load();
}
void LoadText(object sender=null, EventArgs e=null)
{
Text = Load();
}
static IEnumerable<Word> Parse(string text)
{
IEnumerable<Word> res;
if(text == null)
{
text = string.Empty;
}
if (text.Contains(Environment.NewLine[0]))
{
res = text.Split(Environment.NewLine[0]).Where(l => l.Trim().Length > 0).Select(p => Word.FromLine(p));
}
else
{
res = text.Length > 0 ? new Word[] { Word.FromLine(text) } : Array.Empty<Word>();
}
return res;
}
static IList<Word> Clear(IEnumerable<Word> words)
{
SortedList<string, Word> dic = new SortedList<string, Word>();
List<Word> ls = new List<Word>();
foreach (Word word in words)
{
if (!dic.ContainsKey(word.Content))
{
dic.Add(word.Content, word);
ls.Add(word);
}
}
return ConfigurationPage.Instance.SortDictionary? dic.Values : ls;
}
static void Save(string valor=null)
{
if (!Directory.Exists(FileDir))
{
Directory.CreateDirectory(FileDir);
}
File.WriteAllText(FilePath, valor);
}
static string Load()
{
if (File.Exists(FilePath))
{
return File.ReadAllText(FilePath);
}
else
{
return string.Empty; // o proporciona un valor predeterminado
}
}
}