1
- using Xamarin . Plugin . Calendar . Models ;
1
+ using SampleApp . Model ;
2
2
using System ;
3
3
using System . Collections . Generic ;
4
+ using System . Collections . ObjectModel ;
4
5
using System . Globalization ;
5
- using System . Threading . Tasks ;
6
6
using System . Linq ;
7
- using System . Collections . ObjectModel ;
7
+ using System . Threading . Tasks ;
8
8
using System . Windows . Input ;
9
9
using Xamarin . Forms ;
10
- using SampleApp . Model ;
10
+ using Xamarin . Plugin . Calendar . Enums ;
11
+ using Xamarin . Plugin . Calendar . Models ;
11
12
12
13
namespace SampleApp . ViewModels
13
14
{
14
15
public class AdvancedPageViewModel : BasePageViewModel
15
16
{
16
17
public ICommand DayTappedCommand => new Command < DateTime > ( async ( date ) => await DayTapped ( date ) ) ;
17
- public ICommand SwipeLeftCommand => new Command ( ( ) => { MonthYear = MonthYear . AddMonths ( 2 ) ; } ) ;
18
- public ICommand SwipeRightCommand => new Command ( ( ) => { MonthYear = MonthYear . AddMonths ( - 2 ) ; } ) ;
19
- public ICommand SwipeUpCommand => new Command ( ( ) => { MonthYear = DateTime . Today ; } ) ;
18
+ public ICommand SwipeLeftCommand => new Command ( ( ) => ChangeShownUnit ( 1 ) ) ;
19
+ public ICommand SwipeRightCommand => new Command ( ( ) => ChangeShownUnit ( - 1 ) ) ;
20
+ public ICommand SwipeUpCommand => new Command ( ( ) => { ShownDate = DateTime . Today ; } ) ;
20
21
21
22
public ICommand EventSelectedCommand => new Command ( async ( item ) => await ExecuteEventSelectedCommand ( item ) ) ;
22
23
23
24
public AdvancedPageViewModel ( ) : base ( )
24
25
{
26
+ Device . BeginInvokeOnMainThread ( async ( ) => await App . Current . MainPage . DisplayAlert ( "Info" , "Loading events with delay, and changeing current view." , "Ok" ) ) ;
27
+
25
28
Culture = CultureInfo . CreateSpecificCulture ( "en-GB" ) ;
26
29
// testing all kinds of adding events
27
30
// when initializing collection
@@ -46,36 +49,34 @@ public AdvancedPageViewModel() : base()
46
49
// with indexer
47
50
Events [ DateTime . Now ] = new List < AdvancedEventModel > ( GenerateEvents ( 2 , "Boring" ) ) ;
48
51
49
- MonthYear = MonthYear . AddMonths ( 1 ) ;
50
-
51
- Task . Delay ( 5000 ) . ContinueWith ( _ =>
52
- {
53
- // indexer - update later
54
- Events [ DateTime . Now ] = new ObservableCollection < AdvancedEventModel > ( GenerateEvents ( 10 , "Cool" ) ) ;
55
-
56
- // add later
57
- Events . Add ( DateTime . Now . AddDays ( 3 ) , new List < AdvancedEventModel > ( GenerateEvents ( 5 , "Cool" ) ) ) ;
52
+ ShownDate = ShownDate . AddMonths ( 1 ) ;
58
53
59
- // indexer later
60
- Events [ DateTime . Now . AddDays ( 10 ) ] = new List < AdvancedEventModel > ( GenerateEvents ( 10 , "Boring" ) ) ;
54
+ Task . Delay ( 5000 ) . ContinueWith ( _ =>
55
+ {
56
+ // indexer - update later
57
+ Events [ DateTime . Now ] = new ObservableCollection < AdvancedEventModel > ( GenerateEvents ( 10 , "Cool" ) ) ;
61
58
62
- // add later
63
- Events . Add ( DateTime . Now . AddDays ( 15 ) , new List < AdvancedEventModel > ( GenerateEvents ( 10 , "Cool" ) ) ) ;
59
+ // add later
60
+ Events . Add ( DateTime . Now . AddDays ( 3 ) , new List < AdvancedEventModel > ( GenerateEvents ( 5 , "Cool" ) ) ) ;
64
61
62
+ // indexer later
63
+ Events [ DateTime . Now . AddDays ( 10 ) ] = new List < AdvancedEventModel > ( GenerateEvents ( 10 , "Boring" ) ) ;
65
64
66
- Task . Delay ( 3000 ) . ContinueWith ( t =>
67
- {
68
- MonthYear = MonthYear . AddMonths ( - 2 ) ;
65
+ // add later
66
+ Events . Add ( DateTime . Now . AddDays ( 15 ) , new List < AdvancedEventModel > ( GenerateEvents ( 10 , "Cool" ) ) ) ;
69
67
70
- // get observable collection later
71
- var todayEvents = Events [ DateTime . Now ] as ObservableCollection < AdvancedEventModel > ;
68
+ Task . Delay ( 3000 ) . ContinueWith ( t =>
69
+ {
70
+ ShownDate = ShownDate . AddMonths ( - 2 ) ;
72
71
73
- // insert/add items to observable collection
74
- todayEvents . Insert ( 0 , new AdvancedEventModel { Name = "Cool event insert" , Description = "This is Cool event's description!" , Starting = new DateTime ( ) } ) ;
75
- todayEvents . Add ( new AdvancedEventModel { Name = "Cool event add" , Description = "This is Cool event's description!" , Starting = new DateTime ( ) } ) ;
72
+ // get observable collection later
73
+ var todayEvents = Events [ DateTime . Now ] as ObservableCollection < AdvancedEventModel > ;
76
74
77
- } , TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
78
- } , TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
75
+ // insert/add items to observable collection
76
+ todayEvents . Insert ( 0 , new AdvancedEventModel { Name = "Cool event insert" , Description = "This is Cool event's description!" , Starting = new DateTime ( ) } ) ;
77
+ todayEvents . Add ( new AdvancedEventModel { Name = "Cool event add" , Description = "This is Cool event's description!" , Starting = new DateTime ( ) } ) ;
78
+ } , TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
79
+ } , TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
79
80
80
81
SelectedDate = DateTime . Today . AddDays ( 10 ) ;
81
82
}
@@ -92,22 +93,32 @@ private IEnumerable<AdvancedEventModel> GenerateEvents(int count, string name)
92
93
93
94
public EventCollection Events { get ; }
94
95
95
- private DateTime _monthYear = DateTime . Today ;
96
- public DateTime MonthYear
96
+ private DateTime _shownDate = DateTime . Today ;
97
+
98
+ public DateTime ShownDate
99
+ {
100
+ get => _shownDate ;
101
+ set => SetProperty ( ref _shownDate , value ) ;
102
+ }
103
+
104
+ private WeekLayout _calendarLayout = WeekLayout . Month ;
105
+
106
+ public WeekLayout CalendarLayout
97
107
{
98
- get => _monthYear ;
99
- set => SetProperty ( ref _monthYear , value ) ;
108
+ get => _calendarLayout ;
109
+ set => SetProperty ( ref _calendarLayout , value ) ;
100
110
}
101
111
102
112
private DateTime ? _selectedDate = DateTime . Today ;
113
+
103
114
public DateTime ? SelectedDate
104
115
{
105
116
get => _selectedDate ;
106
117
set => SetProperty ( ref _selectedDate , value ) ;
107
118
}
108
119
109
-
110
120
private CultureInfo _culture = CultureInfo . InvariantCulture ;
121
+
111
122
public CultureInfo Culture
112
123
{
113
124
get => _culture ;
@@ -129,5 +140,31 @@ private async Task ExecuteEventSelectedCommand(object item)
129
140
await App . Current . MainPage . DisplayAlert ( title , message , "Ok" ) ;
130
141
}
131
142
}
143
+
144
+ private void ChangeShownUnit ( int amountToAdd )
145
+ {
146
+ switch ( CalendarLayout )
147
+ {
148
+ case WeekLayout . Week :
149
+ case WeekLayout . TwoWeek :
150
+ ChangeShownWeek ( amountToAdd ) ;
151
+ break ;
152
+
153
+ case WeekLayout . Month :
154
+ default :
155
+ ChangeShownMonth ( amountToAdd ) ;
156
+ break ;
157
+ }
158
+ }
159
+
160
+ private void ChangeShownMonth ( int monthsToAdd )
161
+ {
162
+ ShownDate . AddMonths ( monthsToAdd ) ;
163
+ }
164
+
165
+ private void ChangeShownWeek ( int weeksToAdd )
166
+ {
167
+ ShownDate . AddDays ( weeksToAdd * 7 ) ;
168
+ }
132
169
}
133
170
}
0 commit comments