/
MainPage.xaml.cs
287 lines (259 loc) · 9.32 KB
/
MainPage.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using Fotoideen.Helper;
using Fotoideen.Model;
using Fotoideen.Resources;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Newtonsoft.Json;
namespace Fotoideen
{
public partial class MainPage : PhoneApplicationPage
{
private const string VisibilityGroupName = "VisibilityStates";
private const string OpenVisibilityStateName = "Open";
private const string ClosedVisibilityStateName = "Closed";
private const string StateKey_Value = "DateTimePickerPageBase_State_Value";
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (Content.SelectedItem == mainPan)
{
}
else if (Content.SelectedItem == panWords)
{
Content.DefaultItem = Content.Items[Content.Items.IndexOf(mainPan)];
e.Cancel = true;
}
else if (Content.SelectedItem == panSettings)
{
}
base.OnBackKeyPress(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
State["index"] = Content.SelectedIndex;
base.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (State.ContainsKey("index"))
{
Content.DefaultItem = Content.Items[int.Parse(State["index"].ToString())];
}
base.OnNavigatedTo(e);
}
private void Check()
{
}
// Constructor
public MainPage()
{
InitializeComponent();
DataContext = App.Model;
if (App.Model == null || App.Model.Items == null || App.Model.Items.Count == 0)
{
if (Content.Items.Contains(panWords))
{
Content.Items.Remove(panWords);
}
}
}
private void SetRandomAmount()
{
var amount = App.RandomAmount;
if (amount == 3)
{
dpRandom.SelectedIndex = 0;
}
else if (amount == 5)
{
dpRandom.SelectedIndex = 1;
}
else if (amount == 10)
{
dpRandom.SelectedIndex = 2;
}
else if (amount == 15)
{
dpRandom.SelectedIndex = 3;
}
else if (amount == 20)
{
dpRandom.SelectedIndex = 4;
}
else
{
dpRandom.SelectedIndex = 0;
}
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
if (App.Model.Items != null && App.Model.Items.Count > 1)
{
lbIdeas.SelectedIndex = 1;
}
dpRandom.SelectionChanged += new SelectionChangedEventHandler(dpRandom_SelectionChanged);
SetRandomAmount();
}
private void StartUpdate()
{
ShowPogressIndicator();
var proxy = new Proxy();
proxy.ProxyEvent += new EventHandler(ProxyFinishedEvent);
proxy.LoadData();
}
void ProxyFinishedEvent(object sender, EventArgs e)
{
var args = e as ProxyEventArgs;
HandleProxy(args);
}
private void HandleProxy(ProxyEventArgs args)
{
if (!Deployment.Current.Dispatcher.CheckAccess())
{
Deployment.Current.Dispatcher.BeginInvoke(() => HandleProxy(args));
return;
}
HidePogressIndicator();
if (args.Code == HttpStatusCode.OK)
{
PushDataToModel(args.Response);
}
else if (args.Code == HttpStatusCode.Unused)
{
MessageBox.Show(Resource.message_Exception, Resource.hint, MessageBoxButton.OK);
}
else
{
MessageBox.Show(Resource.message_WebExeption, Resource.hint, MessageBoxButton.OK);
}
}
private void PushDataToModel(string normalizedJsonData)
{
var json = JsonConvert.DeserializeObject(normalizedJsonData) as Newtonsoft.Json.Linq.JObject;
var table = json["table"];
var rows = table.SelectToken("rows");
int i = 0;
if (App.Model.Items == null) App.Model.Items = new ObservableCollection<ColumnModel>();
App.Model.Items.Clear();
foreach (var row in rows.Children())
{
if (i == 0)
{
var c = row.SelectToken("c");
foreach (var column in c)
{
App.Model.AddItem(column.Value<string>("v"));
App.Model.UpdateModel();
}
}
else
{
var c = row.SelectToken("c");
int y = 0;
string name = string.Empty;
foreach (var column in c)
{
if (y == 0) // name
{
name = column.Value<string>("v");
}
else //belongs to
{
if (column.Value<string>("v") == "x")
{
App.Model.Items[y].AddItem(name);
}
}
y++;
}
}
i++;
}
if (App.Model != null && App.Model.Items != null && App.Model.Items.Count > 0)
{
if (!Content.Items.Contains(panWords))
{
Content.Items.Insert(1, panWords);
}
panWords.DataContext = App.Model.Items[1];
}
//if (App.Model.Items != null && App.Model.Items.Count > 1)
//{
// lbIdeas.SelectedIndex = 1;
//}
}
private void lbWords_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listbox = sender as ListBox;
if (listbox.SelectedIndex == -1) return;
listbox.SelectedIndex = -1;
}
private void lbIdeas_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
if (listBox.SelectedIndex == -1) return;
var item = listBox.SelectedItem as ColumnModel;
item.Update();
panWords.DataContext = item;
listBox.SelectedIndex = -1;
}
private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
StartUpdate();
}
private bool _isVisible = false;
//private ProgressIndicator _indicator;
private void ShowPogressIndicator()
//private void ShowPogressIndicator(ProgressTypes type = ProgressTypes.WaitCursor, bool showLabel = true)
{
//if (!Deployment.Current.Dispatcher.CheckAccess())
//{
// Deployment.Current.Dispatcher.BeginInvoke(() => ShowPogressIndicator(type, showLabel));
// return;
//}
//if (_isVisible) return;
//if (_indicator == null) _indicator = new ProgressIndicator();
//_indicator.ProgressType = type;
//_indicator.ShowLabel = showLabel;
//_indicator.Show();
//_isVisible = true;
}
private void HidePogressIndicator()
{
//if (!Deployment.Current.Dispatcher.CheckAccess())
//{
// Deployment.Current.Dispatcher.BeginInvoke(HidePogressIndicator);
// return;
//}
//if (!_isVisible) return;
//if (_indicator == null) return;
//_indicator.Hide();
//_isVisible = false;
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
App.Model.Reset();
Content.DefaultItem = Content.Items[Content.Items.IndexOf(mainPan)];
if (Content.Items.Contains(panWords))
{
Content.Items.Remove(panWords);
}
}
private void dpRandom_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = sender as ListPicker;
var amount = ((ListPickerItem)item.SelectedItem).Tag.ToString();
App.RandomAmount = int.Parse(amount);
}
private void btnSheet_Click(object sender, RoutedEventArgs e)
{
var task = new WebBrowserTask();
task.Uri = new Uri(Resource.linkSheet, UriKind.Absolute);
task.Show();
}
}
}