-
Notifications
You must be signed in to change notification settings - Fork 175
/
PageModelCoreMethods.cs
291 lines (241 loc) · 12.2 KB
/
PageModelCoreMethods.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
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Linq;
namespace FreshMvvm
{
public class PageModelCoreMethods : IPageModelCoreMethods
{
Page _currentPage;
FreshBasePageModel _currentPageModel;
public PageModelCoreMethods (Page currentPage, FreshBasePageModel pageModel)
{
_currentPage = currentPage;
_currentPageModel = pageModel;
}
public async Task DisplayAlert (string title, string message, string cancel)
{
if (_currentPage != null)
await _currentPage.DisplayAlert (title, message, cancel);
}
public async Task<string> DisplayActionSheet (string title, string cancel, string destruction, params string[] buttons)
{
if (_currentPage != null)
return await _currentPage.DisplayActionSheet (title, cancel, destruction, buttons);
return null;
}
public async Task<bool> DisplayAlert (string title, string message, string accept, string cancel)
{
if (_currentPage != null)
return await _currentPage.DisplayAlert (title, message, accept, cancel);
return false;
}
public async Task PushPageModel<T>(Action<T> setPageModel, bool modal = false, bool animate = true) where T : FreshBasePageModel
{
T pageModel = FreshIOC.Container.Resolve<T>();
setPageModel?.Invoke(pageModel);
await PushPageModel(pageModel, null, modal, animate);
}
public async Task PushPageModel<T>(object data, bool modal = false, bool animate = true) where T : FreshBasePageModel
{
T pageModel = FreshIOC.Container.Resolve<T>();
await PushPageModel(pageModel, data, modal, animate);
}
public async Task PushPageModel<T, TPage> (object data, bool modal = false, bool animate = true) where T : FreshBasePageModel where TPage : Page
{
T pageModel = FreshIOC.Container.Resolve<T> ();
TPage page = FreshIOC.Container.Resolve<TPage>();
FreshPageModelResolver.BindingPageModel(data, page, pageModel);
await PushPageModelWithPage(page, pageModel, data, modal, animate);
}
public Task PushPageModel(Type pageModelType, bool animate = true)
{
return PushPageModel(pageModelType, null, animate);
}
public Task PushPageModel(Type pageModelType, object data, bool modal = false, bool animate = true)
{
var pageModel = FreshIOC.Container.Resolve(pageModelType) as FreshBasePageModel;
return PushPageModel(pageModel, data, modal, animate);
}
async Task PushPageModel(FreshBasePageModel pageModel, object data, bool modal = false, bool animate = true)
{
var page = FreshPageModelResolver.ResolvePageModel(data, pageModel);
await PushPageModelWithPage(page, pageModel, data, modal, animate);
}
async Task PushPageModelWithPage(Page page, FreshBasePageModel pageModel, object data, bool modal = false, bool animate = true)
{
pageModel.PreviousPageModel = _currentPageModel; //This is the previous page model because it's push to a new one, and this is current
pageModel.CurrentNavigationServiceName = _currentPageModel.CurrentNavigationServiceName;
if (string.IsNullOrWhiteSpace(pageModel.PreviousNavigationServiceName))
pageModel.PreviousNavigationServiceName = _currentPageModel.PreviousNavigationServiceName;
if (page is FreshMasterDetailNavigationContainer)
{
await this.PushNewNavigationServiceModal ((FreshMasterDetailNavigationContainer)page, pageModel, animate);
}
else if (page is FreshTabbedNavigationContainer)
{
await this.PushNewNavigationServiceModal ((FreshTabbedNavigationContainer)page, pageModel, animate);
}
else
{
IFreshNavigationService rootNavigation = FreshIOC.Container.Resolve<IFreshNavigationService> (_currentPageModel.CurrentNavigationServiceName);
await rootNavigation.PushPage (page, pageModel, modal, animate);
}
}
public async Task PopPageModel (bool modal = false, bool animate = true)
{
string navServiceName = _currentPageModel.CurrentNavigationServiceName;
if (_currentPageModel.IsModalFirstChild)
{
await PopModalNavigationService(animate);
}
else
{
if (modal)
this._currentPageModel.RaisePageWasPopped();
IFreshNavigationService rootNavigation = FreshIOC.Container.Resolve<IFreshNavigationService> (navServiceName);
await rootNavigation.PopPage (modal, animate);
}
}
public async Task PopToRoot(bool animate)
{
IFreshNavigationService rootNavigation = FreshIOC.Container.Resolve<IFreshNavigationService> (_currentPageModel.CurrentNavigationServiceName);
await rootNavigation.PopToRoot (animate);
}
public async Task PopPageModel (object data, bool modal = false, bool animate = true)
{
if (_currentPageModel != null && _currentPageModel.PreviousPageModel != null && data != null) {
_currentPageModel.PreviousPageModel.ReverseInit (data);
}
await PopPageModel (modal, animate);
}
public Task PushPageModel<T> (bool animate = true) where T : FreshBasePageModel
{
return PushPageModel<T> (null, false, animate);
}
public Task PushPageModel<T, TPage> (bool animate = true) where T : FreshBasePageModel where TPage : Page
{
return PushPageModel<T, TPage> (null, animate);
}
public Task PushNewNavigationServiceModal (FreshTabbedNavigationContainer tabbedNavigationContainer, FreshBasePageModel basePageModel = null, bool animate = true)
{
var models = tabbedNavigationContainer.TabbedPages.Select (o => o.GetModel ()).ToList();
if (basePageModel != null)
models.Add (basePageModel);
return PushNewNavigationServiceModal (tabbedNavigationContainer, models.ToArray (), animate);
}
public Task PushNewNavigationServiceModal (FreshMasterDetailNavigationContainer masterDetailContainer, FreshBasePageModel basePageModel = null, bool animate = true)
{
var models = masterDetailContainer.Pages.Select (o =>
{
if (o.Value is NavigationPage)
return ((NavigationPage)o.Value).CurrentPage.GetModel ();
else
return o.Value.GetModel();
}).ToList();
if (basePageModel != null)
models.Add (basePageModel);
return PushNewNavigationServiceModal (masterDetailContainer, models.ToArray(), animate);
}
public Task PushNewNavigationServiceModal (IFreshNavigationService newNavigationService, FreshBasePageModel basePageModels, bool animate = true)
{
return PushNewNavigationServiceModal (newNavigationService, new FreshBasePageModel[] { basePageModels }, animate);
}
public async Task PushNewNavigationServiceModal (IFreshNavigationService newNavigationService, FreshBasePageModel[] basePageModels, bool animate = true)
{
var navPage = newNavigationService as Page;
if (navPage == null)
throw new Exception ("Navigation service is not Page");
foreach (var pageModel in basePageModels) {
pageModel.CurrentNavigationServiceName = newNavigationService.NavigationServiceName;
pageModel.PreviousNavigationServiceName = _currentPageModel.CurrentNavigationServiceName;
pageModel.IsModalFirstChild = true;
}
IFreshNavigationService rootNavigation = FreshIOC.Container.Resolve<IFreshNavigationService> (_currentPageModel.CurrentNavigationServiceName);
await rootNavigation.PushPage (navPage, null, true, animate);
}
public void SwitchOutRootNavigation (string navigationServiceName)
{
IFreshNavigationService rootNavigation = FreshIOC.Container.Resolve<IFreshNavigationService> (navigationServiceName);
if (!(rootNavigation is Page))
throw new Exception("Navigation service is not a page");
Xamarin.Forms.Application.Current.MainPage = rootNavigation as Page;
}
public async Task PopModalNavigationService(bool animate = true)
{
var currentNavigationService = FreshIOC.Container.Resolve<IFreshNavigationService> (_currentPageModel.CurrentNavigationServiceName);
currentNavigationService.NotifyChildrenPageWasPopped();
FreshIOC.Container.Unregister<IFreshNavigationService>(_currentPageModel.CurrentNavigationServiceName);
var navServiceName = _currentPageModel.PreviousNavigationServiceName;
IFreshNavigationService rootNavigation = FreshIOC.Container.Resolve<IFreshNavigationService> (navServiceName);
await rootNavigation.PopPage (animate);
}
/// <summary>
/// This method switches the selected main page, TabbedPage the selected tab or if MasterDetail, works with custom pages also
/// </summary>
public Task<FreshBasePageModel> SwitchSelectedRootPageModel<T>() where T : FreshBasePageModel
{
var currentNavigationService = FreshIOC.Container.Resolve<IFreshNavigationService> (_currentPageModel.CurrentNavigationServiceName);
return currentNavigationService.SwitchSelectedRootPageModel<T>();
}
/// <summary>
/// This method is used when you want to switch the selected page,
/// </summary>
public Task<FreshBasePageModel> SwitchSelectedTab<T>() where T : FreshBasePageModel
{
return SwitchSelectedRootPageModel<T>();
}
/// <summary>
/// This method is used when you want to switch the selected page,
/// </summary>
public Task<FreshBasePageModel> SwitchSelectedMaster<T>() where T : FreshBasePageModel
{
return SwitchSelectedRootPageModel<T>();
}
public async Task<string> PushPageModelWithNewNavigation<T> (object data, bool animate = true) where T : FreshBasePageModel
{
var page = FreshPageModelResolver.ResolvePageModel<T>(data);
var navigationName = Guid.NewGuid().ToString();
var naviationContainer = new FreshNavigationContainer(page, navigationName);
await PushNewNavigationServiceModal(naviationContainer, page.GetModel(), animate);
return navigationName;
}
public void BatchBegin()
{
_currentPage.BatchBegin ();
}
public void BatchCommit()
{
_currentPage.BatchCommit ();
}
/// <summary>
/// Removes current page/pagemodel from navigation
/// </summary>
public void RemoveFromNavigation ()
{
this._currentPageModel.RaisePageWasPopped ();
this._currentPage.Navigation.RemovePage (_currentPage);
}
/// <summary>
/// Removes specific page/pagemodel from navigation
/// </summary>
public void RemoveFromNavigation<TPageModel> (bool removeAll = false) where TPageModel : FreshBasePageModel =>
RemoveFromNavigation (typeof(TPageModel), removeAll);
/// <summary>
/// Removes specific page/pagemodel from navigation
/// </summary>
public void RemoveFromNavigation (Type type, bool removeAll = false)
{
foreach (var page in this._currentPage.Navigation.NavigationStack.Reverse().ToList())
{
if (page.BindingContext?.GetType() == type)
{
page.GetModel()?.RaisePageWasPopped ();
this._currentPage.Navigation.RemovePage (page);
if (!removeAll)
break;
}
}
}
}
}