-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeckSummaryPage.xaml.cs
200 lines (184 loc) · 7.05 KB
/
DeckSummaryPage.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace QuizCards
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class DeckSummaryPage : Page
{
private Deck currentDeck;
private String frontSide = "A";
public DeckSummaryPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
CardsGridView.ItemTemplate = SideATemplate;
if (e.Parameter != null)
{
Dictionary<String, Object> p = e.Parameter as Dictionary<String, Object>;
if (p.ContainsKey("DeckFile"))
{
loadDeckFromFile(p["DeckFile"] as StorageFile);
}
else if (p.ContainsKey("Deck"))
{
this.currentDeck = p["Deck"] as Deck;
this.DataContext = this.currentDeck;
}
if (p.ContainsKey("Card"))
{
CardsGridView.SelectedItem = p["Card"] as Card;
CardsGridView.Loaded += scrollToSelected;
}
}
else
{
currentDeck = new Deck();
this.currentDeck.title = "My Deck Name";
this.currentDeck.sideAName = "Side A";
this.currentDeck.sideBName = "Side B";
this.DataContext = this.currentDeck;
}
}
private void scrollToSelected(object sender, RoutedEventArgs args)
{
CardsGridView.ScrollIntoView(CardsGridView.SelectedItem,ScrollIntoViewAlignment.Leading);
}
private void BackBtn_Tapped(object sender, RoutedEventArgs e)
{
//autosave deck
if (this.currentDeck.filename == null)
{
this.currentDeck.filename = this.currentDeck.title + ".qcd";
}
SaveDeckFromFileName();
//Viewing a deck->Quizzing->View blows up backstack, so use Navigate instead.
this.Frame.Navigate(typeof(LandingPage));
}
private async void loadDeckFromFile(StorageFile file)
{
//File Picker, feed to processor, wait and show progress
DeckPackageProcessor dpp = new DeckPackageProcessor();
progressRing.IsEnabled = true;
progressRing.IsActive = true;
if (await dpp.readPackageAsync(file))
{
//Once we're done, kill progress, set up controls, and update the display
progressRing.IsEnabled = false;
progressRing.IsActive = false;
this.currentDeck = dpp.deck;
this.DataContext = this.currentDeck;
}
else
{
Windows.UI.Popups.MessageDialog errorDialog = new Windows.UI.Popups.MessageDialog("There was a problem loading the file you selected. Make sure you've selected a valid QuizCards deck.", "Uh oh! I couldn't load that file.");
await errorDialog.ShowAsync();
Frame.Navigate(typeof(LandingPage));
}
//CardsGridView.DataContext = this.currentDeck;
}
private void GridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SummaryAppBar.IsOpen = true;
if (CardsGridView.SelectedItem != null)
{
EditCardBtn.IsEnabled = true;
}
else
{
EditCardBtn.IsEnabled = false;
}
}
private void StartQuizBtn_Tapped(object sender, TappedRoutedEventArgs e)
{
Dictionary<String, Object> p = new Dictionary<String, Object>();
p.Add("Deck", this.currentDeck);
this.Frame.Navigate(typeof(MainPage), p);
}
private void FlipCardbtn_Tapped(object sender, TappedRoutedEventArgs e)
{
if (this.frontSide == "B")
{
CardsGridView.ItemTemplate = SideATemplate;
this.frontSide = "A";
}
else
{
CardsGridView.ItemTemplate = SideBTemplate;
this.frontSide = "B";
}
}
private void EditDeckBtn_Tapped(object sender, TappedRoutedEventArgs e)
{
Dictionary<String, Object> p = new Dictionary<String, Object>();
p.Add("Deck", this.currentDeck);
this.Frame.Navigate(typeof(DeckEditPage), p);
}
private void AddCardBtn_Tapped(object sender, TappedRoutedEventArgs e)
{
Dictionary<String, Object> p = new Dictionary<String, Object>();
p.Add("Deck", this.currentDeck);
this.Frame.Navigate(typeof(CardEditPage), p);
}
private void EditCardBtn_Tapped(object sender, TappedRoutedEventArgs e)
{
Dictionary<String, Object> p = new Dictionary<String, Object>();
p.Add("Deck", this.currentDeck);
p.Add("Card", CardsGridView.SelectedItem as Card);
this.Frame.Navigate(typeof(CardEditPage), p);
}
private void SaveDeckBtn_Tapped(object sender, TappedRoutedEventArgs e)
{
PickSaveFile();
}
private async void SaveDeckFromFileName() {
var folder = ApplicationData.Current.LocalFolder;
StorageFile outfile = await folder.CreateFileAsync(this.currentDeck.filename);
await SaveDeck(outfile);
this.currentDeck.disposeOfBitmaps();
}
private async Task<bool> SaveDeck(StorageFile file)
{
if (file != null)
{
DeckPackageProcessor dpp = new DeckPackageProcessor();
return await dpp.writePackageAsync(file, this.currentDeck);
}
else
{
return false;
}
}
private async void PickSaveFile()
{
FileSavePicker picker = new FileSavePicker();
picker.FileTypeChoices.Add("QuizCard Deck", new string[] { ".qcd" });
StorageFile file = await picker.PickSaveFileAsync();
SaveDeck(file);
}
}
}