-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingPageContent.xaml.cs
More file actions
267 lines (244 loc) · 8.75 KB
/
SettingPageContent.xaml.cs
File metadata and controls
267 lines (244 loc) · 8.75 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using CustomObjects;
using Services;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
namespace MyRSSReader
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class SettingPageContent : Page
{
public ObservableCollection<CustomFeed> Feeds = new ObservableCollection<CustomFeed>();
public static event EventHandler<bool> ChangeStateOfBackButton;
public static event EventHandler<bool> ChangeStateOfOpenButton;
private static bool s_isFirstThemeSettingUIUpdate;
public SettingPageContent()
{
InitializeComponent();
this.Loaded += SetUpPage;
removeButton.Click += RemoveFeedsAsync;
addButton.Click += LauchAddFeedDialogAsync;
acceptButton.Click += SaveFeedsAsync;
radioButtons.SelectionChanged += SaveAppThemeSettingAsync;
}
private void SetUpPage(object sender, RoutedEventArgs e)
{
ChangeStateOfBackButton(this, true);
ChangeStateOfOpenButton(this, false);
s_isFirstThemeSettingUIUpdate = true;
UpdateThemeSettingRadioButtonsAsync();
UpdateFeedsAsync();
GC.Collect(1);
}
private async void SaveAppThemeSettingAsync(object sender, SelectionChangedEventArgs e)
{
if (s_isFirstThemeSettingUIUpdate)
{
s_isFirstThemeSettingUIUpdate = false;
}
else if (radioButtons.SelectedItem != null)
{
ElementTheme selectedTheme = ElementTheme.Default;
switch (radioButtons.SelectedItem.ToString())
{
case "Light":
selectedTheme = ElementTheme.Light;
break;
case "Dark":
selectedTheme = ElementTheme.Dark;
break;
case "Use system setting":
selectedTheme = ElementTheme.Default;
break;
}
try
{
Theme.SaveAppThemeSetting(selectedTheme);
}
catch (Exception)
{
await new ContentDialog
{
Title = "Error",
Content = "An error occured while saving app theme",
CloseButtonText = "Ok"
}.ShowAsync();
}
if (await new ContentDialog
{
Title = "Success",
Content = "App theme has been successfully changed, please restart to see the changes",
PrimaryButtonText = "Restart",
CloseButtonText = "Cancel"
}.ShowAsync() == ContentDialogResult.Primary)
{
await CoreApplication.RequestRestartAsync("");
}
}
}
private async void UpdateThemeSettingRadioButtonsAsync()
{
try
{
switch (Theme.GetAppThemeSetting())
{
case ElementTheme.Light:
radioButtons.SelectedIndex = 0;
break;
case ElementTheme.Dark:
radioButtons.SelectedIndex = 1;
break;
case ElementTheme.Default:
radioButtons.SelectedIndex = 2;
break;
}
}
catch (Exception)
{
await new ContentDialog
{
Title = "Error",
Content = "An error occured while getting app theme",
CloseButtonText = "Ok"
}.ShowAsync();
}
}
private async void UpdateFeedsAsync()
{
try
{
foreach (CustomFeed feed in await Task.Run(() => { return Services.Feeds.GetSavedFeeds(); }))
{
if (feed != null)
{
Feeds.Insert(0, feed);
}
}
}
catch (Exception)
{
await new ContentDialog
{
Title = "Error",
Content = "An error occured while getting saved feeds",
CloseButtonText = "Ok"
}.ShowAsync();
}
}
private async void RemoveFeedsAsync(object sender, RoutedEventArgs e)
{
try
{
var removeList = listView.SelectedItems.ToList();
if (removeList.Count != 0)
{
foreach (CustomFeed feed in removeList)
{
Feeds.Remove(feed);
}
}
}
catch (Exception)
{
await new ContentDialog
{
Title = "Error",
Content = "An error occured while removing feeds",
CloseButtonText = "Ok"
}.ShowAsync();
}
}
private async void SaveFeedsAsync(object sender, RoutedEventArgs e)
{
try
{
await Task.Run(() =>
{
Services.Feeds.SaveFeeds(Feeds.ToList());
});
}
catch (Exception)
{
await new ContentDialog
{
Title = "Error",
Content = "An error occured while saving changes",
CloseButtonText = "Ok"
}.ShowAsync();
}
}
//A way to get the result from the text box inside the dialog content... idk what I'm doing
private AddFeedDialogContent dialogContent;
private async void LauchAddFeedDialogAsync(object sender, object e)
{
dialogContent = new AddFeedDialogContent();
var addFeedDialog = new ContentDialog
{
Title = "Add new feed source",
PrimaryButtonText = "Ok",
CloseButtonText = "Cancel",
DefaultButton = ContentDialogButton.Primary,
Content = dialogContent
};
addFeedDialog.Closed += CheckFeedUrlAsync;
await addFeedDialog.ShowAsync();
}
private async void CheckFeedUrlAsync(ContentDialog sender, ContentDialogClosedEventArgs args)
{
if (args.Result == ContentDialogResult.Primary)
{
CustomFeed newFeed = new CustomFeed();
bool newFeedIsValid = true;
try
{
string feedUrl = dialogContent.GetFeedUrl();
await Task.Run(async () =>
{
newFeed = await Services.Feeds.GetFeedAsync(feedUrl);
});
dialogContent = null;
}
catch (Exception)
{
newFeedIsValid = false;
}
if (Feeds.Where(x => x.Link == newFeed.Link).Count() == 0 && newFeedIsValid)
{
Feeds.Add(newFeed);
await new ContentDialog
{
Title = "Success",
Content = "The feed has been successfully added",
CloseButtonText = "Ok"
}.ShowAsync();
}
else if (Feeds.Where(x => x.Link == newFeed.Link).Count() != 0 && newFeedIsValid)
{
await new ContentDialog
{
Title = "Error",
Content = "The feed has been already added",
CloseButtonText = "Ok"
}.ShowAsync();
}
else
{
await new ContentDialog
{
Title = "Error",
Content = "An error occured while adding the feed, please check the URL again",
CloseButtonText = "Ok"
}.ShowAsync();
}
}
GC.Collect(1);
}
}
}