-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
1491 lines (1294 loc) · 53.7 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Core;
using Windows.Devices.Input;
using Windows.Media.SpeechSynthesis;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Microsoft.Toolkit.Uwp.Notifications; // Notifications library
using Windows.UI.Notifications;
using Windows.UI.Xaml.Input;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace BibleBrowserUWP
{
enum CurrentView { Chapter, Search }
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
#region Constants
const int MINMARGIN = 10;
const int MAXTEXTWIDTH = 400;
const int VERSECOLUMN = 30;
const int MIDDLCOLUMN = 30;
#endregion
#region Member Variables
// The media object for controlling and playing audio.
MediaElement m_mediaElement = new MediaElement();
// The object for controlling the speech synthesis engine (voice).
SpeechSynthesizer m_synth = new SpeechSynthesizer();
CurrentView m_currentView = CurrentView.Chapter;
bool m_isPlaybackStarted = false;
bool m_areTabsLoaded = false;
bool m_isAppNewlyOpened = true;
bool m_areDropdownsDisplayed = false;
private BibleReference m_previousReference = new BibleReference(BibleVersion.DefaultVersion, null);
ObservableCollection<SearchResult> m_SearchResults = new ObservableCollection<SearchResult>();
SpeechSynthesisStream m_speechStream;
#endregion
#region Properties
TrulyObservableCollection<BrowserTab> Tabs { get => BrowserTab.Tabs; }
ObservableCollection<SearchResult> SearchResults { get => m_SearchResults; }
// Gets all available Bibles, minus the one already selected, if there is one.
ObservableCollection<BibleVersion> Bibles {
get {
if (BrowserTab.Selected == null || BrowserTab.Selected.Reference == null)
return BibleLoader.Bibles;
else
return BrowserTab.Selected.OtherVersions;
}
}
List<Verse> Verses {
get {
if (BrowserTab.Selected == null || BrowserTab.Selected.Reference == null)
return null;
else
return BrowserTab.Selected.Reference.Verses;
}
}
/// <summary>
/// The maximum width the chapter column can take up, in which verses fit.
/// </summary>
public double ChapterWidth {
get; private set;
}
/// <summary>
/// Determine whether the user has a keyboard, or is using touchscreen only (for tablets).
/// </summary>
public static bool IsKeyboardAttached {
get {
KeyboardCapabilities keyboardCapabilities = new KeyboardCapabilities();
if(keyboardCapabilities.KeyboardPresent == 0)
{
Debug.WriteLine("Keyboard not found.");
return false;
}
else
{
Debug.WriteLine("Keyboard present.");
return true;
}
}
}
#endregion
#region Page Initialization and Events
public MainPage()
{
// Load previous tabs when the app opens
Application.Current.LeavingBackground += new LeavingBackgroundEventHandler(App_LeavingBackground);
// Create the UI
this.InitializeComponent();
// Set theme for window root.
FrameworkElement root = (FrameworkElement)Window.Current.Content;
root.RequestedTheme = AppSettings.Theme;
SetThemeToggle(AppSettings.Theme);
SetNotificationToggle(AppSettings.ReadingNotifications);
SetNotificationTime(AppSettings.NotifyTime);
StyleTitleBar();
cbDefaultVersion.SelectedItem = BibleVersion.DefaultVersion;
// Ensure the text remains within the window size
this.SizeChanged += MainPage_SizeChanged;
// Prevent clicks from being eaten by textbox
asbSearch.AddHandler(TappedEvent, new TappedEventHandler(asbSearch_Tapped), true);
// Save tabs when the app closes
Application.Current.Suspending += new SuspendingEventHandler(App_Suspending);
}
private void SetNotificationTime(TimeSpan notifyTime)
{
Debug.WriteLine("Notification time found as " + notifyTime);
//tpNotificationTime.Time = notifyTime;
}
/// <summary>
/// Set the theme toggle to the correct position (off for the default theme, and on for the non-default).
/// </summary>
private void SetThemeToggle(ElementTheme theme)
{
if (theme == AppSettings.DEFAULTTHEME)
tglAppTheme.IsOn = false;
else
tglAppTheme.IsOn = true;
}
/// <summary>
/// Set the Bible reading notification toggle.
/// </summary>
private void SetNotificationToggle(bool notificationsAllowed)
{
if(notificationsAllowed)
{
//tglNotifications.IsOn = true;
//tpNotificationTime.IsEnabled = true;
}
else
{
//tglNotifications.IsOn = false;
//tpNotificationTime.IsEnabled = false;
}
}
/// <summary>
/// Fires when the window size is changed by dragging, snapping, or pixel density.
/// </summary>
private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
{
SetWidth();
}
/// <summary>
/// Set the width of the title bar and reading main content area.
/// </summary>
private void SetWidth()
{
double pageWidth = ((Frame)Window.Current.Content).ActualWidth;
double contentWidth;
// Do the same for the compare view
if (pageWidth < (2 * MINMARGIN) + (2 * MAXTEXTWIDTH))
{
contentWidth = pageWidth - (2 * MINMARGIN) - VERSECOLUMN - MIDDLCOLUMN;
ChapterWidth = contentWidth;
gvCompareVerses.Width = contentWidth;
lvSearchResults.Width = contentWidth;
}
else
{
contentWidth = (MAXTEXTWIDTH * 2) - VERSECOLUMN - MIDDLCOLUMN;
ChapterWidth = contentWidth;
gvCompareVerses.Width = contentWidth;
lvSearchResults.Width = contentWidth;
}
// Set the maximum width of the tab area
CoreApplicationViewTitleBar titleBar = CoreApplication.GetCurrentView().TitleBar;
spTabArea.MaxWidth = pageWidth - (titleBar.SystemOverlayLeftInset + titleBar.SystemOverlayRightInset);
}
/// <summary>
/// Fires when the app is opened, and when the app gets re-selected.
/// </summary>
async void App_LeavingBackground(Object sender, LeavingBackgroundEventArgs e)
{
Debug.WriteLine("App leaving background!");
if (m_areTabsLoaded == false)
{
await BrowserTab.LoadSavedTabs();
m_areTabsLoaded = true;
// Open the tab that was active before the app was last closed
lvTabs.SelectedItem = BrowserTab.Selected;
}
SetWidth();
LoseFocus(asbSearch);
}
/// <summary>
/// Fires whenever the user switches to another app, the desktop, or the Start screen
/// Save the currently open tabs to an XML file.
/// </summary>
async void App_Suspending(Object sender, SuspendingEventArgs e)
{
SuspendingDeferral defer = e.SuspendingOperation.GetDeferral(); // Wait while we asynchronously create the xml document
await BrowserTab.SaveOpenTabs();
Debug.WriteLine("The app is suspending!");
defer.Complete();
}
private void CoreTitleBar_IsVisibleChanged(CoreApplicationViewTitleBar sender, object args)
{
if (sender.IsVisible)
grdTitleBar.Visibility = Visibility.Visible;
else
grdTitleBar.Visibility = Visibility.Collapsed;
}
private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
UpdateTitleBarLayout(sender);
}
#endregion
#region Methods
/// <summary>
/// Go to the previous reference in the current tab's history.
/// </summary>
private void GoToPreviousReference()
{
if (BrowserTab.Selected.Previous != null)
{
BibleReference reference = BrowserTab.Selected.Reference;
reference.VerticalScrollOffset = svPageScroller.VerticalOffset;
BrowserTab.Selected.AddToHistory(ref reference, BrowserTab.NavigationMode.Previous);
if(reference.IsSearch)
{
Debug.WriteLine("```````````````````````````````````");
Debug.WriteLine("Is search: " + BrowserTab.Selected.Reference.IsSearch);
Debug.WriteLine("Search itm: " + BrowserTab.Selected.Reference.Search);
Debug.WriteLine("Raw query: " + BrowserTab.Selected.Reference.Search.RawQuery);
if (string.IsNullOrWhiteSpace(BrowserTab.Selected.Reference.Search.RawQuery))
throw new Exception("String null or white space");
m_SearchResults.Clear();
// Search was already done
if (reference.Search.IsComplete && reference.Search.SearchProgressInfo != null)
{
ReportSearchProgress(reference.Search.SearchProgressInfo);
SetCurrentView(CurrentView.Search);
}
else
{
ProcessRawUserSearchQuery(BrowserTab.Selected.Reference.Search.RawQuery, BrowserTab.Selected.Reference);
}
}
else
{
PrintChapter(reference);
svPageScroller.ChangeView(null, reference.VerticalScrollOffset, null);
}
}
}
/// <summary>
/// Go to the next reference in the current tab's history.
/// </summary>
private void GoToNextReference()
{
if (BrowserTab.Selected.Next != null)
{
BibleReference reference = BrowserTab.Selected.Reference;
reference.VerticalScrollOffset = svPageScroller.VerticalOffset;
BrowserTab.Selected.AddToHistory(ref reference, BrowserTab.NavigationMode.Next);
if(BrowserTab.Selected.Reference.IsSearch)
{
Debug.WriteLine("-------------------------------------");
Debug.WriteLine("Is search: " + BrowserTab.Selected.Reference.IsSearch);
Debug.WriteLine("Search itm: " + BrowserTab.Selected.Reference.Search);
Debug.WriteLine("Raw query: " + BrowserTab.Selected.Reference.Search.RawQuery);
if (string.IsNullOrWhiteSpace(BrowserTab.Selected.Reference.Search.RawQuery))
throw new Exception("String null or white space");
m_SearchResults.Clear();
if(reference.Search.IsComplete)
{
ReportSearchProgress(reference.Search.SearchProgressInfo);
SetCurrentView(CurrentView.Search);
}
else
{
ProcessRawUserSearchQuery(BrowserTab.Selected.Reference.Search.RawQuery, BrowserTab.Selected.Reference);
}
}
else
{
PrintChapter(reference);
svPageScroller.ChangeView(null, reference.VerticalScrollOffset, null);
}
}
}
/// <summary>
/// Display the chapter previous to the current one.
/// </summary>
private void PreviousChapter()
{
BibleReference oldReference = BrowserTab.Selected.Reference;
if(oldReference != null) // Not a new tab
{
int chapter = oldReference.Chapter;
BibleBook book = oldReference.Book;
chapter--;
if (chapter < 1)
{
int bookIndex = (int)book - 1; // Go to the previous book
if(bookIndex < 0) // First book wraps to last
{
bookIndex = Enum.GetNames(typeof(BibleBook)).Length - 1;
}
book = (BibleBook)bookIndex;
chapter = int.MaxValue; // Because this gets clamped later
}
BibleReference newReference = new BibleReference(oldReference.Version, oldReference.ComparisonVersion, book, chapter);
BrowserTab.Selected.AddToHistory(ref newReference, BrowserTab.NavigationMode.Add);
svPageScroller.ChangeView(null, 0, null, true);
}
}
/// <summary>
/// Display the chapter next to the current one.
/// </summary>
private void NextChapter()
{
BibleReference oldReference = BrowserTab.Selected.Reference;
if (oldReference != null) // Not a new tab
{
int chapter = oldReference.Chapter;
BibleBook book = oldReference.Book;
chapter++;
if (chapter > oldReference.Chapters.Count)
{
int bookIndex = (int)book + 1; // Go to the next book
if(bookIndex > Enum.GetNames(typeof(BibleBook)).Length - 1) // Last book loops back to first
{
bookIndex = 0;
}
book = (BibleBook)bookIndex;
chapter = 1;
}
BibleReference newReference = new BibleReference(oldReference.Version, oldReference.ComparisonVersion, book, chapter);
BrowserTab.Selected.AddToHistory(ref newReference, BrowserTab.NavigationMode.Add);
svPageScroller.ChangeView(null, 0, null, true);
}
}
/// <summary>
/// Fill version, book, and chapter search box dropdowns with the value of the current reference and make them visible.
/// If they are hidden, the search box gets filled with the reference text.
/// </summary>
private void ShowSearchDropdowns(bool show)
{
// Show the search box dropdowns
if (show)
{
if (BrowserTab.Selected != null)
{
BibleReference reference = BrowserTab.Selected.Reference;
if (reference == null)
{
gvBooks.ItemsSource = m_previousReference.Version.BookNames;
lvChapters.ItemsSource = m_previousReference.Chapters;
asbSearch.Text = string.Empty;
asbSearch.PlaceholderText = string.Empty;
ddbVersion.Visibility = Visibility.Visible;
ddbBook.Visibility = Visibility.Visible;
ddbChapter.Visibility = Visibility.Collapsed;
m_areDropdownsDisplayed = true;
}
else
{
// Fill dropdowns with content
gvBooks.ItemsSource = reference.Version.BookNames;
lvChapters.ItemsSource = reference.Chapters;
asbSearch.Text = string.Empty;
asbSearch.PlaceholderText = string.Empty;
if (reference.ComparisonVersion == null)
{
btnRemoveCompareView.Visibility = Visibility.Collapsed;
ddbVersion.Content = reference.Version;
}
else
{
btnRemoveCompareView.Visibility = Visibility.Visible;
ddbVersion.Content = reference.Version + ":" + reference.ComparisonVersion;
}
ddbBook.Content = reference.BookName;
ddbChapter.Content = reference.Chapter;
ddbVersion.Visibility = Visibility.Visible;
ddbBook.Visibility = Visibility.Visible;
ddbChapter.Visibility = Visibility.Visible;
m_areDropdownsDisplayed = true;
}
}
}
// Hide search dropdowns
else
{
if (BrowserTab.Selected != null)
{
BibleReference reference = BrowserTab.Selected.Reference;
if (reference == null)
{
asbSearch.PlaceholderText = "Search or enter reference";
}
else if (reference.IsSearch)
{
asbSearch.Text = reference.Search.RawQuery;
}
else if (reference.ComparisonVersion == null)
{
asbSearch.Text = reference.Version + ": " + reference.ToString();
}
else {
asbSearch.Text = reference.Version + ":" + reference.ComparisonVersion + " " + reference.ToString();
}
}
ddbVersion.Visibility = Visibility.Collapsed;
ddbBook.Visibility = Visibility.Collapsed;
ddbChapter.Visibility = Visibility.Collapsed;
m_areDropdownsDisplayed = false;
}
}
/// <summary>
/// Get the main text and begin reading it asynchronously.
/// </summary>
async private void ReadChapterText()
{
if(m_isPlaybackStarted)
{
m_mediaElement.Play();
}
else
{
BrowserTab tab = BrowserTab.Selected;
// Detect the voice for the language
try
{
m_synth.Voice = SpeechSynthesizer.AllVoices.Where(p => p.Language.Contains(tab.LanguageCode)).First();
// Generate the audio stream from plain text.
m_speechStream = await m_synth.SynthesizeTextToStreamAsync(tab.Reference.GetChapterPlainText());
// Send the stream to the media object
m_mediaElement.SetSource(m_speechStream, m_speechStream.ContentType);
m_mediaElement.Play();
m_isPlaybackStarted = true;
}
// The computer doesn't have the language
catch (InvalidOperationException)
{
// Show an error message
var messageDialog = new MessageDialog("Please install the language pack for this language (" + new CultureInfo(tab.LanguageCode) + ").");
messageDialog.Commands.Add(new UICommand("Close"));
messageDialog.DefaultCommandIndex = 0;
messageDialog.CancelCommandIndex = 0;
await messageDialog.ShowAsync();
}
}
}
/// <summary>
/// Hide the default title bar to create a custom look instead.
/// </summary>
private void StyleTitleBar()
{
// Hide default title bar
CoreApplicationViewTitleBar titleBar = CoreApplication.GetCurrentView().TitleBar;
ApplicationViewTitleBar appBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.ExtendViewIntoTitleBar = true;
appBar.ButtonBackgroundColor = Colors.Transparent;
appBar.ButtonForegroundColor = Colors.White;
appBar.ButtonInactiveBackgroundColor = Colors.Transparent;
UpdateTitleBarLayout(titleBar);
// Set XAML element as a draggable region.
Window.Current.SetTitleBar(grdTitleBar);
// Register a handler for when the size of the overlaid caption control changes.
// For example, when the app moves to a screen with a different DPI.
titleBar.LayoutMetricsChanged += CoreTitleBar_LayoutMetricsChanged;
// Register a handler for when the title bar visibility changes.
// For example, when the title bar is invoked in full screen mode.
titleBar.IsVisibleChanged += CoreTitleBar_IsVisibleChanged;
}
/// <summary>
/// Get the size of the caption controls area and back button
/// (returned in logical pixels), and move content around as necessary.
/// </summary>
/// <param name="coreTitleBar"></param>
private void UpdateTitleBarLayout(CoreApplicationViewTitleBar coreTitleBar)
{
LeftPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayLeftInset);
RightPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayRightInset);
cdLeftPadding.Width = new GridLength(coreTitleBar.SystemOverlayLeftInset);
cdRightPadding.Width = new GridLength(coreTitleBar.SystemOverlayRightInset);
// Update title bar control size as needed to account for system size changes.
grdTitleBar.Height = coreTitleBar.Height;
}
/// <summary>
/// Based on the current tab, decide whether the Previous, Next, and Play commands should be clickable.
/// </summary>
private void ActivateButtons()
{
// New tab: no button should be clickable
if (BrowserTab.Selected.Reference == null)
{
btnPrevious.IsEnabled = false;
btnNext.IsEnabled = false;
//btnPlay.IsEnabled = false;
}
// There is text already displayed: check whether there is history
else
{
//btnPlay.IsEnabled = true;
// There is a history of references
if (BrowserTab.Selected.History.Count >= 2)
{
if (BrowserTab.Selected.Next == null)
btnNext.IsEnabled = false;
else
btnNext.IsEnabled = true;
if (BrowserTab.Selected.Previous == null)
btnPrevious.IsEnabled = false;
else
btnPrevious.IsEnabled = true;
}
// There is no history
else
{
btnPrevious.IsEnabled = false;
btnNext.IsEnabled = false;
}
}
}
/// <summary>
/// Print a chapter of the Bible to the app page according to the reference sent.
/// Stop a search if it is in progress.
/// </summary>
/// <param name="reference">The chapter to print. If null, this will simply erase page contents.</param>
private void PrintChapter(BibleReference reference)
{
SetCurrentView(CurrentView.Chapter);
BindReadingVoices(reference);
lvSearchResults.ItemsSource = null;
if(reference.IsSearch && reference.Search.Cancellation != null)
reference.Search.Cancellation.Dispose();
// New tab, leave blank
if (BrowserTab.Selected.Reference == null)
{
gvCompareVerses.ItemsSource = null;
}
// Single version
else if (reference.ComparisonVersion == null)
{
gvCompareVerses.ItemsSource = null;
gvCompareVerses.ItemsSource = reference.Verses;
}
// With comparison version
else
{
gvCompareVerses.ItemsSource = null;
gvCompareVerses.ItemsSource = reference.Verses;
}
}
private void BindReadingVoices(BibleReference reference)
{
try
{
string code = BrowserTab.Selected.LanguageCode;
IEnumerable<VoiceInformation> voices = SpeechSynthesizer.AllVoices.Where(voice => voice.Language.Contains(code));
List<string> voiceNames = new List<string>();
foreach (VoiceInformation voice in voices)
{
voiceNames.Add(voice.DisplayName);
}
cbSelectVoice.ItemsSource = voiceNames;
cbSelectVoice.SelectedItem = voiceNames.FirstOrDefault();
}
catch {
cbSelectVoice.Visibility = Visibility.Collapsed;
}
}
/// <summary>
/// Choose whether to show the chapter view or the search view.
/// All UI elements relating to one view will be shown, and the other will be hidden.
/// </summary>
/// <param name="view">The wanted view</param>
private void SetCurrentView(CurrentView view)
{
switch(view)
{
case CurrentView.Chapter:
Debug.WriteLine("View being set to chapter");
m_currentView = CurrentView.Chapter;
ShowChapter(true);
ShowSearch(false);
break;
case CurrentView.Search:
Debug.WriteLine("View being set to search");
m_currentView = CurrentView.Search;
ShowChapter(false);
ShowSearchDropdowns(false);
ShowSearch(true);
break;
default:
break;
}
}
/// <summary>
/// Show the chapter text. Does not print anything.
/// </summary>
private void ShowChapter(bool show)
{
if (show)
{
gvCompareVerses.Visibility = Visibility.Visible;
btnLeftPage.Visibility = Visibility.Visible;
btnRightPage.Visibility = Visibility.Visible;
}
else
{
gvCompareVerses.Visibility = Visibility.Collapsed;
btnLeftPage.Visibility = Visibility.Collapsed;
btnRightPage.Visibility = Visibility.Collapsed;
}
}
/// <summary>
/// Show the search results region and progress bar.
/// </summary>
private void ShowSearch(bool show)
{
if (show)
{
lvSearchResults.Visibility = Visibility.Visible;
progSearchProgress.Visibility = Visibility.Visible;
txtSearchStatus.Visibility = Visibility.Visible;
btnCancelSearch.Visibility = Visibility.Visible;
}
else
{
lvSearchResults.Visibility = Visibility.Collapsed;
progSearchProgress.Visibility = Visibility.Collapsed;
txtSearchStatus.Visibility = Visibility.Collapsed;
btnCancelSearch.Visibility = Visibility.Collapsed;
}
}
/// <summary>
/// TODO
/// </summary>
private async void PickNewBibleAsync() // TODO
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
picker.FileTypeFilter.Add(".xml");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// Save the file to the app directory
}
else
{
// Cancelled
}
}
/// <summary>
/// Add a version to compare to the original reference, and print the new layout.
/// </summary>
private static void AddCompareToVersion(BibleVersion compareVersion, BibleReference oldReference)
{
BibleReference newReference = new BibleReference(oldReference.Version, compareVersion, oldReference.Book, oldReference.Chapter, oldReference.Verse);
BrowserTab.Selected.AddToHistory(ref newReference, BrowserTab.NavigationMode.Add);
Debug.WriteLine("Compare version added as " + newReference.ComparisonVersion);
}
/// <summary>
/// Remove a version to compare to the original reference, and print the new layout.
/// </summary>
private static void RemoveCompareToVersion(BibleReference oldReference)
{
if (oldReference.ComparisonVersion != null)
{
BibleReference newReference = new BibleReference(oldReference.Version, null, oldReference.Book, oldReference.Chapter, oldReference.Verse);
BrowserTab.Selected.AddToHistory(ref newReference, BrowserTab.NavigationMode.Add);
Debug.WriteLine("Compare version removed.");
}
}
#endregion
#region Events
private async void Home_Click(object sender, RoutedEventArgs e)
{
await BrowserTab.SaveOpenTabs();
}
private void BtnPlay_Click(object sender, RoutedEventArgs e)
{
ReadChapterText();
//btnPlay.Visibility = Visibility.Collapsed;
//btnPause.Visibility = Visibility.Visible;
}
private void BtnPause_Click(object sender, RoutedEventArgs e)
{
m_mediaElement.Pause();
//btnPause.Visibility = Visibility.Collapsed;
//btnPlay.Visibility = Visibility.Visible;
}
/// <summary>
/// Go to the previous reference or search result in the current tab's history.
/// </summary>
private void BtnPrevious_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Fired!");
GoToPreviousReference();
}
/// <summary>
/// Go to the next reference or search result in the current tab's history.
/// </summary>
private void BtnNext_Click(object sender, RoutedEventArgs e)
{
GoToNextReference();
}
/// <summary>
/// Open a new tab.
/// </summary>
private void BtnNewTab_Click(object sender, RoutedEventArgs e)
{
if(BrowserTab.Selected != null && BrowserTab.Selected.Reference != null)
m_previousReference = BrowserTab.Selected.Reference;
Tabs.Add(new BrowserTab());
//btnCompare.IsEnabled = false;
lvTabs.SelectedIndex = Tabs.Count - 1;
asbSearch.Text = string.Empty;
ActivateButtons();
ShowSearchDropdowns(true);
//if(IsKeyboardAttached)
// asbSearch.Focus(FocusState.Programmatic); // TODO this seem to always execute
// regardless of being in tablet mode without keyboard
}
private void MfiAddBible_Click(object sender, RoutedEventArgs e)
{
PickNewBibleAsync();
}
/// <summary>
/// Close a tab.
/// </summary>
private async void BtnCloseTab_Click(object sender, RoutedEventArgs e)
{
// There is still another tab to show when one is removed
if (lvTabs.Items.Count >= 2)
{
Guid tabGuid = ((Guid)((Button)sender).Tag);
BrowserTab removeTab = Tabs.Single(p => p.Guid == tabGuid);
int removeIndex = Tabs.IndexOf(removeTab);
// The selected tab is being removed
if (removeIndex == lvTabs.SelectedIndex)
{
if (removeIndex == Tabs.Count - 1)
lvTabs.SelectedIndex = Tabs.Count - 2;
else if (removeIndex == 0)
lvTabs.SelectedIndex = 1;
else
lvTabs.SelectedIndex = removeIndex + 1;
}
Tabs.RemoveAt(removeIndex);
}
// There is no new tab to show; close the app
else
{
await BrowserTab.SaveOpenTabs();
CoreApplication.Exit();
}
ActivateButtons();
}
/// <summary>
/// A new tab was selected; track this in app memory.
/// Load the new page contents according to the reference of the newly selected tab.
/// </summary>
private void LvTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Some event caused no tab to be selected; we always want a tab selected, so override that
if(lvTabs.SelectedIndex == -1)
{
lvTabs.SelectedIndex = BrowserTab.SelectedIndex; // HACK prevent -1 selection
return;
}
BrowserTab.SelectedIndex = lvTabs.SelectedIndex;
BrowserTab selected = (BrowserTab)lvTabs.SelectedItem;
if (selected == null)
{
// Keep the previous selection
throw new Exception("No selected tab found");
}
// Stop audio playback when changing tabs
m_isPlaybackStarted = false;
m_mediaElement.Stop();
//btnPause.Visibility = Visibility.Collapsed;
//btnPlay.Visibility = Visibility.Visible;
// Only display the reference when there is still a tab to show; if not, we are closing the app anyway
if (lvTabs.Items.Count > 0)
{
BibleReference reference = BrowserTab.Selected.Reference;
if(reference == null)
{
//asbSearch.Focus(FocusState.Programmatic); // Focus autohides all dropdowns // TODO this causes problems on tablets
PrintChapter(null);
}
else
{
ShowSearchDropdowns(true);
PrintChapter(BrowserTab.Selected.Reference);
}
}
ActivateButtons();
}
/// <summary>
/// When a version is selected from the "compare to" list.
/// </summary>
private void LvCompareVersions_ItemClicked(object sender, ItemClickEventArgs e)
{
AddCompareToVersion((BibleVersion)e.ClickedItem, BrowserTab.Selected.Reference);
}
/// <summary>
/// Go to the version the user clicks and show the books flyout.
/// </summary>
private void GvVersions_ItemClick(object sender, ItemClickEventArgs e)
{
// Get the version the user clicked
BibleVersion version = (BibleVersion)e.ClickedItem;
// Go to the version in the present reference
BibleReference oldReference = BrowserTab.Selected.Reference;
BibleReference newReference;
if (version == oldReference.ComparisonVersion) // Flip versions when they would result in two of the same version
{
newReference = new BibleReference(version, oldReference.Version, oldReference.Book, oldReference.Chapter);
}
else {
newReference = new BibleReference(version, oldReference.ComparisonVersion, oldReference.Book, oldReference.Chapter);
}
BrowserTab.Selected.AddToHistory(ref newReference, BrowserTab.NavigationMode.Add);
flyVersion.Hide();
}
/// <summary>
/// Go to the book the user clicks and show the chapters flyout.
/// </summary>
private void GvBooks_ItemClick(object sender, ItemClickEventArgs e)
{
// Get the name of the book the user clicked
string book = (string)e.ClickedItem;
// Go to the book in the present reference
BibleVersion version;
BibleVersion comparisonVersion;
BibleReference reference;
if (BrowserTab.Selected.Reference != null) // This tab is already open
{
version = BrowserTab.Selected.Reference.Version;
comparisonVersion = BrowserTab.Selected.Reference.ComparisonVersion;
reference = new BibleReference(version, comparisonVersion, BibleReference.StringToBook(book, version));
}
// A new tab has a null reference, but the user may be seeing dropdowns relating to the previous reference;
// this is desirable because it gives him a default starting point for his new tab when using the touchscreen.
else
{
reference = m_previousReference;
}
BrowserTab.Selected.AddToHistory(ref reference, BrowserTab.NavigationMode.Add);
flyBook.Hide();
flyChapter.ShowAt(ddbChapter);
}
/// <summary>
/// Go to the chapter the user clicks and hide the flyout.
/// </summary>
private void GvChapters_ItemClick(object sender, ItemClickEventArgs e)
{