/
MvxNotifyPropertyChanged.cs
113 lines (97 loc) · 3.77 KB
/
MvxNotifyPropertyChanged.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
// MvxNotifyPropertyChanged.cs
// (c) Copyright Cirrious Ltd. http://www.cirrious.com
// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
//
// Project Lead - Stuart Lodge, @slodge, me@slodge.com
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using Cirrious.CrossCore.Core;
namespace Cirrious.MvvmCross.ViewModels
{
public abstract class MvxNotifyPropertyChanged
: MvxMainThreadDispatchingObject
, IMvxNotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _shouldAlwaysRaiseInpcOnUserInterfaceThread;
public bool ShouldAlwaysRaiseInpcOnUserInterfaceThread()
{
return _shouldAlwaysRaiseInpcOnUserInterfaceThread;
}
public void ShouldAlwaysRaiseInpcOnUserInterfaceThread(bool value)
{
_shouldAlwaysRaiseInpcOnUserInterfaceThread = value;
}
protected MvxNotifyPropertyChanged()
{
var alwaysOnUIThread = (MvxSingletonCache.Instance == null)
? true
: MvxSingletonCache.Instance.Settings.AlwaysRaiseInpcOnUserInterfaceThread;
ShouldAlwaysRaiseInpcOnUserInterfaceThread(alwaysOnUIThread);
}
public void RaisePropertyChanged<T>(Expression<Func<T>> property)
{
var name = this.GetPropertyNameFromExpression(property);
RaisePropertyChanged(name);
}
public void RaisePropertyChanged([CallerMemberName] string whichProperty = "")
{
var changedArgs = new PropertyChangedEventArgs(whichProperty);
RaisePropertyChanged(changedArgs);
}
public virtual void RaiseAllPropertiesChanged()
{
var changedArgs = new PropertyChangedEventArgs(string.Empty);
RaisePropertyChanged(changedArgs);
}
public virtual void RaisePropertyChanged(PropertyChangedEventArgs changedArgs)
{
// check for interception before broadcasting change
if (InterceptRaisePropertyChanged(changedArgs)
== MvxInpcInterceptionResult.DoNotRaisePropertyChanged)
return;
var raiseAction = new Action(() =>
{
var handler = PropertyChanged;
if (handler != null)
handler(this, changedArgs);
});
if (ShouldAlwaysRaiseInpcOnUserInterfaceThread())
{
// check for subscription before potentially causing a cross-threaded call
if (PropertyChanged == null)
return;
InvokeOnMainThread(raiseAction);
}
else
{
raiseAction();
}
}
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Equals(storage, value))
{
return false;
}
storage = value;
RaisePropertyChanged(propertyName);
return true;
}
protected virtual MvxInpcInterceptionResult InterceptRaisePropertyChanged(PropertyChangedEventArgs changedArgs)
{
if (MvxSingletonCache.Instance != null)
{
var interceptor = MvxSingletonCache.Instance.InpcInterceptor;
if (interceptor != null)
{
return interceptor.Intercept(this, changedArgs);
}
}
return MvxInpcInterceptionResult.NotIntercepted;
}
}
}