-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathViewControllerBase.cs
140 lines (116 loc) · 3.68 KB
/
ViewControllerBase.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
// Developed by Softeq Development Corporation
// http://www.softeq.com
using System;
using System.Collections.Generic;
using Softeq.XToolkit.Bindings;
using Softeq.XToolkit.Bindings.Abstract;
using Softeq.XToolkit.Bindings.Extensions;
using Softeq.XToolkit.WhiteLabel.Mvvm;
using UIKit;
namespace Softeq.XToolkit.WhiteLabel.iOS
{
/// <summary>
/// Based on <see cref="T:UIKit.UIViewController"/>, used for creating ViewControllers.
/// </summary>
public abstract class ViewControllerBase : UIViewController
{
protected ViewControllerBase()
{
}
protected internal ViewControllerBase(IntPtr handle)
: base(handle)
{
}
public List<IViewControllerComponent> ControllerComponents { get; } = new List<IViewControllerComponent>();
}
/// <summary>
/// Generic class based on <see cref="T:UIKit.UIViewController"/>, used for creating ViewControllers.
/// </summary>
/// <typeparam name="TViewModel">Type of ViewModel.</typeparam>
public abstract class ViewControllerBase<TViewModel> : ViewControllerBase, IBindable
where TViewModel : IViewModelBase
{
protected ViewControllerBase()
{
InitViewController();
}
protected internal ViewControllerBase(IntPtr handle)
: base(handle)
{
InitViewController();
}
public List<Binding> Bindings { get; } = new List<Binding>();
public object DataContext { get; private set; } = default!;
public TViewModel ViewModel => (TViewModel) DataContext;
void IBindable.SetDataContext(object dataContext)
{
DataContext = dataContext;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
ViewModel.OnInitialize();
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
NotifyViewModelAboutAppearing();
AttachBindings();
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
if (IsViewLoaded)
{
ViewWillReallyDisappear(animated);
}
}
protected virtual void ViewWillReallyDisappear(bool animated)
{
DetachBindings();
NotifyViewModelAboutDisappearing();
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
CloseDialogIfNeeded();
}
protected virtual void NotifyViewModelAboutAppearing()
{
ViewModel.OnAppearing();
}
protected virtual void NotifyViewModelAboutDisappearing()
{
ViewModel.OnDisappearing();
}
protected virtual void DoAttachBindings()
{
}
protected virtual void DoDetachBindings()
{
}
private void AttachBindings()
{
DoAttachBindings();
}
private void DetachBindings()
{
BindableExtensions.DetachBindings(this);
DoDetachBindings();
}
private void InitViewController()
{
ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen;
}
private void CloseDialogIfNeeded()
{
if (ViewModel is DialogViewModelBase dialogViewModel)
{
if (IsBeingDismissed || IsMovingFromParentViewController)
{
dialogViewModel.DialogComponent.CloseCommand.Execute(null);
}
}
}
}
}