/
MvxModalSupportTouchViewPresenter.cs
98 lines (83 loc) · 3.33 KB
/
MvxModalSupportTouchViewPresenter.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
#region Copyright
// <copyright file="MvxModalSupportTouchViewPresenter.cs" company="Cirrious">
// (c) Copyright Cirrious. http://www.cirrious.com
// This source is subject to the Microsoft Public License (Ms-PL)
// Please see license.txt on http://opensource.org/licenses/ms-pl.html
// All other rights reserved.
// </copyright>
//
// Project Lead - Stuart Lodge, Cirrious. http://www.cirrious.com
#endregion
using Cirrious.MvvmCross.Exceptions;
using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.Platform.Diagnostics;
using Cirrious.MvvmCross.Interfaces.ViewModels;
using Cirrious.MvvmCross.Platform.Diagnostics;
using Cirrious.MvvmCross.Touch.Interfaces;
using Cirrious.MvvmCross.Views;
using MonoTouch.UIKit;
namespace Cirrious.MvvmCross.Touch.Views.Presenters
{
public class MvxModalSupportTouchViewPresenter : MvxTouchViewPresenter
{
private UIViewController _currentModalViewController;
public MvxModalSupportTouchViewPresenter(UIApplicationDelegate applicationDelegate, UIWindow window)
: base (applicationDelegate, window)
{
}
public override void Show (IMvxTouchView view)
{
if (view is IMvxModalTouchView)
{
if (_currentModalViewController != null)
throw new MvxException("Only one modal view controller at a time supported");
_currentModalViewController = view as UIViewController;
PresentModalViewController(view as UIViewController, true);
return;
}
base.Show(view);
}
public override void NativeModalViewControllerDisappearedOnItsOwn()
{
if (_currentModalViewController != null)
{
MvxTrace.Trace(MvxTraceLevel.Error, "How did a modal disappear when we didn't have one showing?");
return;
}
// clear our local reference to avoid back confusion
_currentModalViewController = null;
}
public override void CloseModalViewController()
{
if (_currentModalViewController != null)
{
_currentModalViewController.DismissModalViewControllerAnimated(true);
_currentModalViewController = null;
return;
}
base.CloseModalViewController();
}
public override void Close(IMvxViewModel toClose)
{
if (_currentModalViewController != null)
{
var touchView = _currentModalViewController as IMvxTouchView;
if (touchView == null)
{
MvxTrace.Trace(MvxTraceLevel.Error, "Unable to close view - modal is showing but not an IMvxTouchView");
return;
}
var viewModel = touchView.ReflectionGetViewModel();
if (viewModel != toClose)
{
MvxTrace.Trace(MvxTraceLevel.Error, "Unable to close view - modal is showing but is not the requested viewmodel");
return;
}
_currentModalViewController.DismissModalViewControllerAnimated(true);
_currentModalViewController = null;
return;
}
base.Close(toClose);
}
}
}