/
TermDetail.xaml.cs
executable file
·151 lines (136 loc) · 5.73 KB
/
TermDetail.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Glossary.Framework;
using Glossary.Items;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace Glossary
{
public partial class TermDetail : PhoneApplicationPage
{
//shortcut to get the instance of APP;
private App app = ((App)Application.Current);
private TermItem editingTermItem;
private DetailAction detailAction;
public TermDetail()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if(!NavigationContext.QueryString.ContainsKey("action"))
{//Should never happen
ToastPromptHelper.ShowToastPromptOnUIThreadAtEndOfQueue("Sorry, We have a technical issue, please try later.", 3000);
NavigationService.GoBack();
}
string action = NavigationContext.QueryString["action"];
if (action == "add")
{
PageTitle.Text = "add term";
detailAction = DetailAction.Add;
}
else
{
PageTitle.Text = "edit term";
detailAction = DetailAction.Edit;
if (!PhoneApplicationService.Current.State.ContainsKey("termItem"))
{
//Should never happen
ToastPromptHelper.ShowToastPromptOnUIThreadAtEndOfQueue(
"Sorry, We have a technical issue, please try later.", 3000);
NavigationService.GoBack();
}
editingTermItem = PhoneApplicationService.Current.State["termItem"] as TermItem;
TermTextBox.Text = editingTermItem.Term;
DescriptionTextBox.Text = editingTermItem.Description;
}
if(app.Activated)
{
if (PhoneApplicationService.Current.State.ContainsKey("editingTermItem"))
{
editingTermItem = PhoneApplicationService.Current.State["editingTermItem"] as TermItem;
PhoneApplicationService.Current.State.Remove("editingTermItem");
TermTextBox.Text = editingTermItem.Term;
DescriptionTextBox.Text = editingTermItem.Description;
}
if (PhoneApplicationService.Current.State.ContainsKey("detailAction"))
{
detailAction = (DetailAction)PhoneApplicationService.Current.State["detailAction"];
PhoneApplicationService.Current.State.Remove("detailAction");
}
app.Activated = false;
}
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
if (detailAction == DetailAction.Add)
{
editingTermItem = new TermItem();
}
editingTermItem.Term = TermTextBox.Text;
editingTermItem.Description = DescriptionTextBox.Text;
PhoneApplicationService.Current.State["editingTermItem"] = editingTermItem;
PhoneApplicationService.Current.State["detailAction"] = detailAction;
base.OnNavigatedFrom(e);
}
private void ApplicationBarIconButton_Cancel_Click(object sender, EventArgs e)
{
NavigationService.GoBack();
}
private void ApplicationBarIconButton_Save_Click(object sender, EventArgs e)
{
string term = TermTextBox.Text.Trim();
string description = DescriptionTextBox.Text.Trim();
if(term.Length == 0)
{
TermTextBox.Focus();
//ToastPromptHelper.ShowToastPrompt("Term shouldn't be empty.", 3000);
MessageBox.Show("Term shouldn't be empty.");
return;
}
if (description.Length == 0)
{
DescriptionTextBox.Focus();
//ToastPromptHelper.ShowToastPrompt("Description shouldn't be empty.", 3000);
MessageBox.Show("Description shouldn't be empty.");
return;
}
if (detailAction == DetailAction.Add)
{
if (app.Glossary.CheckNewTermName(term))
{
TermTextBox.SelectAll();
TermTextBox.Focus();
MessageBox.Show("Term name exists. Please input another term. Please notice Term name is case insensitive.");
return;
}
app.Glossary.Add(term, description);
NavigationService.GoBack();
}
else if (detailAction == DetailAction.Edit)
{
editingTermItem.Term = term;
editingTermItem.Description = description;
if (app.Glossary.CheckEditTermName(editingTermItem))
{
TermTextBox.SelectAll();
TermTextBox.Focus();
MessageBox.Show("Term name exists. Please input another term. Please notice Term name is case insensitive.");
return;
}
app.Glossary.Edit(editingTermItem);
NavigationService.GoBack();
}
}
}
}