-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainPage.xaml.cs
167 lines (153 loc) · 6.22 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
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;
using Windows.UI.Xaml.Media.Imaging;
using System.Diagnostics;
using Windows.Storage;
using Windows.Storage.Pickers;
// 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 MainPage : Page
{
public Deck currentDeck = null;
private Point initialPt;
private bool enableManipulation=false;
public MainPage()
{
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)
{
//e.Parameter should contain deck info, either StorageFile or Deck object
if (e.Parameter != null)
{
Dictionary<String, Object> p = e.Parameter as Dictionary<String, Object>;
if (p.ContainsKey("DeckFile"))
{
loadData(p["DeckFile"] as StorageFile);
}
else if (p.ContainsKey("Deck"))
{
this.currentDeck = p["Deck"] as Deck;
this.currentDeck.shuffle();
this.DataContext = this.currentDeck;
CardStackPanel.DataContext = this.currentDeck.getNextCard();
this.updateVisibleCard();
}
}
}
private async void loadData(StorageFile file) {
//File Picker, feed to processor, wait and show progress
DeckPackageProcessor dpp = new DeckPackageProcessor();
progring.IsEnabled = true;
progring.IsActive = true;
await dpp.readPackageAsync(file);
//Once we're done, kill progress, set up controls, and update the display
progring.IsEnabled = false;
progring.IsActive = false;
NextCardBtn.Visibility = Visibility.Visible;
PrevCardBtn.Visibility = Visibility.Visible;
this.currentDeck = dpp.deck;
this.currentDeck.shuffle();
this.DataContext = this.currentDeck;
CardStackPanel.DataContext = this.currentDeck.getNextCard();
this.updateVisibleCard();
}
private void NextCardBtn_Click(object sender, RoutedEventArgs e)
{
CardStackPanel.DataContext = this.currentDeck.getNextCard();
this.updateVisibleCard();
}
private void updateVisibleCard()
{
SideBLabel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
ProgressTextBlock.Text = "Card " + this.currentDeck.getPosition() + " of " + this.currentDeck.getLength();
if (this.currentDeck.isNextCard()) { this.NextCardBtn.IsEnabled=true; }
else { this.NextCardBtn.IsEnabled=false; }
if (this.currentDeck.isPreviousCard()) { this.PrevCardBtn.IsEnabled = true; }
else { this.PrevCardBtn.IsEnabled = false; }
}
private void PrevCardBtn_Click(object sender, RoutedEventArgs e)
{
CardStackPanel.DataContext = this.currentDeck.getPreviousCard();
this.updateVisibleCard();
}
private void FlipCardBtn_Click(object sender, RoutedEventArgs e)
{
SideBLabel.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
private void Card_ManipulationDelta (object sender, ManipulationDeltaRoutedEventArgs e)
{
int swipeThreshold = 250;
if ((e.IsInertial) && (this.enableManipulation))
{
Point curr = e.Position;
if ((curr.X-this.initialPt.X > swipeThreshold) && (this.currentDeck.isPreviousCard()))
{
this.enableManipulation = false;
CardStackPanel.DataContext = this.currentDeck.getPreviousCard();
this.updateVisibleCard();
}
if ((this.initialPt.X - curr.X > swipeThreshold)&&(this.currentDeck.isNextCard()))
{
this.enableManipulation = false;
CardStackPanel.DataContext = this.currentDeck.getNextCard();
this.updateVisibleCard();
}
}
}
private void Card_ManipulationStarted (object sender, ManipulationStartedRoutedEventArgs e)
{
if (e.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
{
this.initialPt = e.Position;
this.enableManipulation = true;
}
else
{
this.enableManipulation = false;
}
}
private void Card_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
this.enableManipulation = false;
}
private void Card_Tapped(object sender, TappedRoutedEventArgs e)
{
if (SideBLabel.Visibility == Windows.UI.Xaml.Visibility.Visible)
{
SideBLabel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
else
{
SideBLabel.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
}
private void BackBtn_Tapped(object sender, TappedRoutedEventArgs e)
{
//Using Navigate rather than GoBack so we can retain the current deck in memory
Dictionary<String, Object> p = new Dictionary<string, Object>();
p.Add("Deck",this.currentDeck);
this.Frame.Navigate(typeof(DeckSummaryPage), p);
}
}
}