-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
RefreshView.cs
160 lines (130 loc) · 5.47 KB
/
RefreshView.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
#nullable disable
using System;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Maui.Graphics;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/RefreshView.xml" path="Type[@FullName='Microsoft.Maui.Controls.RefreshView']/Docs/*" />
[ContentProperty(nameof(Content))]
public partial class RefreshView : ContentView, IElementConfiguration<RefreshView>, IRefreshView
{
readonly Lazy<PlatformConfigurationRegistry<RefreshView>> _platformConfigurationRegistry;
public event EventHandler Refreshing;
/// <include file="../../docs/Microsoft.Maui.Controls/RefreshView.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public RefreshView()
{
IsClippedToBounds = true;
#pragma warning disable CS0618 // Type or member is obsolete
VerticalOptions = HorizontalOptions = LayoutOptions.FillAndExpand;
#pragma warning restore CS0618 // Type or member is obsolete
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<RefreshView>>(() => new PlatformConfigurationRegistry<RefreshView>(this));
}
/// <summary>Bindable property for <see cref="IsRefreshing"/>.</summary>
public static readonly BindableProperty IsRefreshingProperty =
BindableProperty.Create(nameof(IsRefreshing), typeof(bool), typeof(RefreshView), false, BindingMode.TwoWay, coerceValue: OnIsRefreshingPropertyCoerced, propertyChanged: OnIsRefreshingPropertyChanged);
static void OnIsRefreshingPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
bool value = (bool)newValue;
if (!value)
return;
var refreshView = (RefreshView)bindable;
refreshView.Refreshing?.Invoke(bindable, EventArgs.Empty);
refreshView.Command?.Execute(refreshView.CommandParameter);
}
static object OnIsRefreshingPropertyCoerced(BindableObject bindable, object value)
{
RefreshView view = (RefreshView)bindable;
bool newValue = (bool)value;
// IsRefreshing can always be toggled to false
if (!newValue)
return value;
if (!view.IsEnabled)
return false;
if (view.Command == null)
return value;
return value;
}
/// <include file="../../docs/Microsoft.Maui.Controls/RefreshView.xml" path="//Member[@MemberName='IsRefreshing']/Docs/*" />
public bool IsRefreshing
{
get { return (bool)GetValue(IsRefreshingProperty); }
set { SetValue(IsRefreshingProperty, value); }
}
/// <summary>Bindable property for <see cref="Command"/>.</summary>
public static readonly BindableProperty CommandProperty =
BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(RefreshView), propertyChanged: OnCommandChanged);
static void OnCommandChanged(BindableObject bindable, object oldValue, object newValue)
{
RefreshView refreshView = (RefreshView)bindable;
if (oldValue is ICommand oldCommand)
oldCommand.CanExecuteChanged -= refreshView.RefreshCommandCanExecuteChanged;
if (newValue is ICommand newCommand)
newCommand.CanExecuteChanged += refreshView.RefreshCommandCanExecuteChanged;
refreshView.RefreshCommandCanExecuteChanged(bindable, EventArgs.Empty);
}
/// <include file="../../docs/Microsoft.Maui.Controls/RefreshView.xml" path="//Member[@MemberName='Command']/Docs/*" />
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
/// <summary>Bindable property for <see cref="CommandParameter"/>.</summary>
public static readonly BindableProperty CommandParameterProperty =
BindableProperty.Create(nameof(CommandParameter),
typeof(object),
typeof(RefreshView),
null,
propertyChanged: (bindable, oldvalue, newvalue) => ((RefreshView)(bindable)).RefreshCommandCanExecuteChanged(((RefreshView)(bindable)).Command, EventArgs.Empty));
/// <include file="../../docs/Microsoft.Maui.Controls/RefreshView.xml" path="//Member[@MemberName='CommandParameter']/Docs/*" />
public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
void RefreshCommandCanExecuteChanged(object sender, EventArgs eventArgs)
{
if (IsRefreshing)
return;
if (Command != null)
{
SetValue(IsEnabledProperty, Command.CanExecute(CommandParameter));
}
else
{
SetValue(IsEnabledProperty, true);
}
}
/// <summary>Bindable property for <see cref="RefreshColor"/>.</summary>
public static readonly BindableProperty RefreshColorProperty =
BindableProperty.Create(nameof(RefreshColor), typeof(Color), typeof(RefreshView), null);
/// <include file="../../docs/Microsoft.Maui.Controls/RefreshView.xml" path="//Member[@MemberName='RefreshColor']/Docs/*" />
public Color RefreshColor
{
get { return (Color)GetValue(RefreshColorProperty); }
set { SetValue(RefreshColorProperty, value); }
}
/// <inheritdoc/>
public IPlatformElementConfiguration<T, RefreshView> On<T>() where T : IConfigPlatform
{
return _platformConfigurationRegistry.Value.On<T>();
}
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (IsEnabledProperty.PropertyName == propertyName &&
!IsEnabled &&
IsRefreshing)
{
IsRefreshing = false;
}
}
Paint IRefreshView.RefreshColor => RefreshColor?.AsPaint();
IView IRefreshView.Content => base.Content;
bool IRefreshView.IsRefreshing
{
get => IsRefreshing;
set { SetValue(IsRefreshingProperty, value, SetterSpecificity.FromHandler); }
}
}
}