-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTMToastContents.xaml.cs
More file actions
229 lines (195 loc) · 7.88 KB
/
Copy pathTMToastContents.xaml.cs
File metadata and controls
229 lines (195 loc) · 7.88 KB
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
using CommunityToolkit.Maui.Behaviors;
using Trimble.Modus.Components.Constant;
using Trimble.Modus.Components.Enums;
using Trimble.Modus.Components.Helpers;
using Trimble.Modus.Components.Popup.Services;
namespace Trimble.Modus.Components.Controls.Toast;
public partial class TMToastContents : PopupPage
{
#region Private Properties
private const int DELAYTIME = 5000;
#endregion
#region Public Properties
public string Message { get; set; }
public string RightIconText { get; set; }
public double ToastWidthRequest { get; set; }
#endregion
public static readonly BindableProperty ToastBackgroundColorProperty =
BindableProperty.Create(nameof(ToastBackgroundColor), typeof(Color), typeof(TMToast), Colors.Black);
public static readonly BindableProperty TextColorProperty =
BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(TMToast), Colors.White);
public static readonly BindableProperty LeftIconTintColorProperty =
BindableProperty.Create(nameof(LeftIconTintColor), typeof(Color), typeof(TMToast), Colors.White,
propertyChanged: OnLeftIconTintColorChanged);
public static readonly BindableProperty RightIconTintColorProperty =
BindableProperty.Create(nameof(RightIconTintColor), typeof(Color), typeof(TMToast), Colors.White,
propertyChanged: OnRightIconTintColorChanged);
internal TMToastContents(string message, string actionButtonText, ToastTheme theme, Action action, bool isDismissable)
{
InitializeComponent();
SetTheme(theme.ToString());
SetDynamicResource(StyleProperty, theme.ToString());
PopupData(message, actionButtonText, action, isDismissable);
BindingContext = this;
CloseAfterDelay();
}
public Color ToastBackgroundColor
{
get { return (Color)GetValue(ToastBackgroundColorProperty); }
set { SetValue(ToastBackgroundColorProperty, value); }
}
public Color TextColor
{
get => (Color)GetValue(TextColorProperty);
set => SetValue(TextColorProperty, value);
}
public Color LeftIconTintColor
{
get => (Color)GetValue(LeftIconTintColorProperty);
set => SetValue(LeftIconTintColorProperty, value);
}
public Color RightIconTintColor
{
get => (Color)GetValue(RightIconTintColorProperty);
set => SetValue(RightIconTintColorProperty, value);
}
private static void OnLeftIconTintColorChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is TMToastContents tmToastContent && DeviceInfo.Platform != DevicePlatform.WinUI)
{
tmToastContent.UpdateIconColor();
}
}
private static void OnRightIconTintColorChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is TMToastContents tmToastContent && DeviceInfo.Platform != DevicePlatform.WinUI)
{
tmToastContent.UpdateIconColor();
}
}
private void UpdateIconColor()
{
if (DeviceInfo.Platform != DevicePlatform.WinUI)
{
leftIconImage.Behaviors.Clear();
if (LeftIconTintColor != null)
{
var behavior = new IconTintColorBehavior
{
TintColor = LeftIconTintColor
};
leftIconImage.Behaviors.Add(behavior);
}
closeButton.Behaviors.Clear();
if (RightIconTintColor != null)
{
var behavior = new IconTintColorBehavior
{
TintColor = RightIconTintColor
};
closeButton.Behaviors.Add(behavior);
}
}
}
#region Private Methods
private void SetTheme(String toastTheme)
{
ToastTheme theme = (ToastTheme)Enum.Parse(typeof(ToastTheme), toastTheme);
switch (theme)
{
case ToastTheme.Dark:
leftIconImage.Source = ImageSource.FromFile(ImageConstants.WhiteInfoIcon);
closeButton.Source = ImageSource.FromFile(ImageConstants.ToastWhiteCloseIcon);
break;
case ToastTheme.Primary:
closeButton.SetDynamicResource(ImageButton.SourceProperty, "ToastPrimaryCloseIcon");
leftIconImage.SetDynamicResource(Image.SourceProperty, "ToastPrimaryInfoIcon");
break;
case ToastTheme.Secondary:
closeButton.SetDynamicResource(ImageButton.SourceProperty, "ToastCloseIcon");
leftIconImage.SetDynamicResource(Image.SourceProperty, "ToastSecondaryInfoIcon");
break;
case ToastTheme.Danger:
closeButton.SetDynamicResource(ImageButton.SourceProperty, "ToastCloseIcon");
leftIconImage.SetDynamicResource(Image.SourceProperty, "ToastDangerIcon");
break;
case ToastTheme.Warning:
closeButton.SetDynamicResource(ImageButton.SourceProperty, "ToastCloseIcon");
leftIconImage.SetDynamicResource(Image.SourceProperty, "ToastWarningIcon");
break;
case ToastTheme.Success:
leftIconImage.SetDynamicResource(Image.SourceProperty, "ToastSuccessIcon");
closeButton.SetDynamicResource(ImageButton.SourceProperty, "ToastCloseIcon");
break;
default:
closeButton.SetDynamicResource(ImageButton.SourceProperty, "ToastCloseIcon");
leftIconImage.SetDynamicResource(Image.SourceProperty, "ToastDefaultInfoIcon");
break;
}
UpdateIconColor();
}
bool isPopupClosed = false;
private void CloseButtonClicked(object sender, EventArgs e)
{
PopupService.Instance.RemovePageAsync(this, true);
isPopupClosed = true;
}
public void CloseAfterDelay()
{
Task.Run(async () =>
{
await Task.Delay(DELAYTIME);
if (!isPopupClosed)
await PopupService.Instance.RemovePageAsync(this, true);
});
}
private void PopupData(string message, string actionButtonText, Action action, bool isDismissable)
{
RightIconText = actionButtonText;
actionButton.Text = RightIconText;
actionButton.TextColor = TextColor;
actionButton.BackgroundColor = ToastBackgroundColor;
if (string.IsNullOrEmpty(RightIconText))
{
closeButton.IsVisible = isDismissable;
actionButton.IsVisible = false;
if (isDismissable)
{
closeButton.Clicked += (sender, args) =>
{
action?.Invoke();
};
}
}
else
{
closeButton.IsVisible = false;
actionButton.IsVisible = true;
actionButton.Clicked += (sender, args) =>
{
action?.Invoke();
};
}
var idiom = DeviceInfo.Current.Idiom;
setWidth(idiom);
Message = message;
}
private void setWidth(DeviceIdiom idiom)
{
double minimumTabletWidth = 480;
double maximumTabletWidthPercentage = 0.7;
double deviceWidth = DeviceDisplay.MainDisplayInfo.Width;
if (idiom == DeviceIdiom.Phone)
{
toastLayout.Padding = new Thickness(16, 0, 16, 10);
}
else if (idiom == DeviceIdiom.Tablet)
{
toastLayout.Padding = new Thickness(0, 0, 0, 10);
toastLayout.MinimumWidthRequest = minimumTabletWidth;
toastLayout.MaximumWidthRequest = deviceWidth * maximumTabletWidthPercentage;
toastLayout.HorizontalOptions = LayoutOptions.CenterAndExpand;
}
}
#endregion
}