1
+ using Notes . Models ;
2
+ using System ;
3
+ using System . Collections . Generic ;
4
+ using System . IO ;
5
+ using System . Linq ;
6
+ using System . Text ;
7
+ using System . Threading . Tasks ;
8
+
9
+ using Xamarin . Forms ;
10
+ using Xamarin . Forms . Xaml ;
11
+ using Xamarin . Forms . DualScreen ;
12
+
13
+ namespace Notes
14
+ {
15
+ [ XamlCompilation ( XamlCompilationOptions . Compile ) ]
16
+ public partial class MainPage : ContentPage
17
+ {
18
+ public static MainPage Current { get ; private set ; }
19
+ public MainPage ( )
20
+ {
21
+ InitializeComponent ( ) ;
22
+ twoPaneView . LayoutChanged += TwoPaneView_LayoutChanged ;
23
+ Current = this ;
24
+ }
25
+
26
+
27
+ public bool DeviceIsSpanned => DualScreenInfo . Current . SpanMode != TwoPaneViewMode . SinglePane ;
28
+
29
+ private void TwoPaneView_LayoutChanged ( object sender , EventArgs e )
30
+ {
31
+ if ( DeviceIsSpanned )
32
+ {
33
+ twoPaneView . WideModeConfiguration = TwoPaneViewWideModeConfiguration . LeftRight ;
34
+ twoPaneView . TallModeConfiguration = TwoPaneViewTallModeConfiguration . TopBottom ;
35
+ }
36
+ else
37
+ { // single screen!
38
+ twoPaneView . WideModeConfiguration = TwoPaneViewWideModeConfiguration . SinglePane ;
39
+ twoPaneView . TallModeConfiguration = TwoPaneViewTallModeConfiguration . SinglePane ;
40
+ }
41
+
42
+ }
43
+
44
+ void OnNoteAddedClicked ( object sender , EventArgs e )
45
+ {
46
+ twoPaneView . Pane2 . BindingContext = new Note ( ) ;
47
+ twoPaneView . PanePriority = TwoPaneViewPriority . Pane2 ;
48
+ }
49
+ protected override void OnAppearing ( )
50
+ {
51
+ base . OnAppearing ( ) ;
52
+
53
+ RefreshData ( ) ;
54
+ }
55
+
56
+ public void RefreshData ( )
57
+ {
58
+ var notes = new List < Note > ( ) ;
59
+
60
+ var files = Directory . EnumerateFiles ( App . FolderPath , "*.notes.txt" ) ;
61
+ foreach ( var filename in files )
62
+ {
63
+ notes . Add ( new Note
64
+ {
65
+ Filename = filename ,
66
+ Text = File . ReadAllText ( filename ) ,
67
+ Date = File . GetCreationTime ( filename )
68
+ } ) ;
69
+ }
70
+
71
+ BindingContext = notes
72
+ . OrderBy ( d => d . Date )
73
+ . ToList ( ) ;
74
+
75
+ twoPaneView . PanePriority = TwoPaneViewPriority . Pane1 ;
76
+ }
77
+ public void OnListViewItemSelected ( Note note )
78
+ {
79
+ if ( note != null )
80
+ {
81
+ twoPaneView . Pane2 . BindingContext = note ;
82
+ twoPaneView . PanePriority = TwoPaneViewPriority . Pane2 ;
83
+ }
84
+ }
85
+ protected override bool OnBackButtonPressed ( )
86
+ {
87
+ if ( ! DeviceIsSpanned )
88
+ { // single-screen
89
+ if ( twoPaneView . PanePriority == TwoPaneViewPriority . Pane2 )
90
+ { //showing detail, back goes to master (list)
91
+ twoPaneView . PanePriority = TwoPaneViewPriority . Pane1 ;
92
+ return true ;
93
+ }
94
+ }
95
+ return base . OnBackButtonPressed ( ) ;
96
+ }
97
+
98
+
99
+ }
100
+ }
0 commit comments