|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Splat; |
| 5 | + |
| 6 | + |
| 7 | +namespace Acr.UserDialogs |
| 8 | +{ |
| 9 | + |
| 10 | + public abstract class AbstractUserDialogs : IUserDialogs |
| 11 | + { |
| 12 | + |
| 13 | + public abstract IDisposable Alert(AlertConfig config); |
| 14 | + public abstract IDisposable ActionSheet(ActionSheetConfig config); |
| 15 | + public abstract IDisposable Confirm(ConfirmConfig config); |
| 16 | + public abstract IDisposable DatePrompt(DatePromptConfig config); |
| 17 | + public abstract IDisposable TimePrompt(TimePromptConfig config); |
| 18 | + public abstract IDisposable Login(LoginConfig config); |
| 19 | + public abstract IDisposable Prompt(PromptConfig config); |
| 20 | + public abstract void ShowImage(IBitmap image, string message, int timeoutMillis); |
| 21 | + public abstract void ShowError(string message, int timeoutMillis); |
| 22 | + public abstract void ShowSuccess(string message, int timeoutMillis); |
| 23 | + public abstract void Toast(ToastConfig config); |
| 24 | + protected abstract IProgressDialog CreateDialogInstance(); |
| 25 | + |
| 26 | + |
| 27 | + public virtual Task<string> ActionSheetAsync(string title, string cancel, string destructive, CancellationToken? cancelToken = null, params string[] buttons) |
| 28 | + { |
| 29 | + var tcs = new TaskCompletionSource<string>(); |
| 30 | + var cfg = new ActionSheetConfig(); |
| 31 | + if (title != null) |
| 32 | + cfg.Title = title; |
| 33 | + |
| 34 | + if (cancel != null) |
| 35 | + cfg.SetCancel(cancel, () => tcs.TrySetResult(cancel)); |
| 36 | + |
| 37 | + if (destructive != null) |
| 38 | + cfg.SetDestructive(destructive, () => tcs.TrySetResult(destructive)); |
| 39 | + |
| 40 | + foreach (var btn in buttons) |
| 41 | + cfg.Add(btn, () => tcs.TrySetResult(btn)); |
| 42 | + |
| 43 | + var disp = this.ActionSheet(cfg); |
| 44 | + cancelToken?.Register(disp.Dispose); |
| 45 | + |
| 46 | + return tcs.Task; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + public virtual IDisposable Alert(string message, string title, string okText) |
| 51 | + { |
| 52 | + return this.Alert(new AlertConfig |
| 53 | + { |
| 54 | + Message = message, |
| 55 | + Title = title, |
| 56 | + OkText = okText ?? AlertConfig.DefaultOkText |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + IProgressDialog loading; |
| 62 | + public virtual void ShowLoading(string title, MaskType? maskType) |
| 63 | + { |
| 64 | + if (this.loading == null) |
| 65 | + this.loading = this.Loading(title, null, null, true, maskType); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + public virtual void HideLoading() |
| 70 | + { |
| 71 | + this.loading?.Dispose(); |
| 72 | + this.loading = null; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public virtual IProgressDialog Loading(string title, Action onCancel, string cancelText, bool show, MaskType? maskType) |
| 77 | + { |
| 78 | + return this.Progress(new ProgressDialogConfig |
| 79 | + { |
| 80 | + Title = title ?? ProgressDialogConfig.DefaultTitle, |
| 81 | + AutoShow = show, |
| 82 | + CancelText = cancelText ?? ProgressDialogConfig.DefaultCancelText, |
| 83 | + MaskType = maskType ?? ProgressDialogConfig.DefaultMaskType, |
| 84 | + IsDeterministic = false, |
| 85 | + OnCancel = onCancel |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + public virtual IProgressDialog Progress(string title, Action onCancel, string cancelText, bool show, MaskType? maskType) |
| 91 | + { |
| 92 | + return this.Progress(new ProgressDialogConfig |
| 93 | + { |
| 94 | + Title = title ?? ProgressDialogConfig.DefaultTitle, |
| 95 | + AutoShow = show, |
| 96 | + CancelText = cancelText ?? ProgressDialogConfig.DefaultCancelText, |
| 97 | + MaskType = maskType ?? ProgressDialogConfig.DefaultMaskType, |
| 98 | + IsDeterministic = true, |
| 99 | + OnCancel = onCancel |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + public virtual IProgressDialog Progress(ProgressDialogConfig config) |
| 105 | + { |
| 106 | + var dlg = this.CreateDialogInstance(); |
| 107 | + dlg.Title = config.Title; |
| 108 | + dlg.IsDeterministic = config.IsDeterministic; |
| 109 | + dlg.MaskType = config.MaskType; |
| 110 | + |
| 111 | + if (config.OnCancel != null) |
| 112 | + dlg.SetCancel(config.OnCancel, config.CancelText); |
| 113 | + |
| 114 | + if (config.AutoShow) |
| 115 | + dlg.Show(); |
| 116 | + |
| 117 | + return dlg; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + public virtual Task AlertAsync(AlertConfig config, CancellationToken? cancelToken = null) |
| 122 | + { |
| 123 | + var tcs = new TaskCompletionSource<object>(); |
| 124 | + config.OnOk = () => tcs.TrySetResult(null); |
| 125 | + |
| 126 | + var disp = this.Alert(config); |
| 127 | + cancelToken?.Register(() => |
| 128 | + { |
| 129 | + disp.Dispose(); |
| 130 | + tcs.TrySetCanceled(); |
| 131 | + }); |
| 132 | + |
| 133 | + return tcs.Task; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + public virtual Task AlertAsync(string message, string title, string okText, CancellationToken? cancelToken = null) |
| 138 | + { |
| 139 | + return this.AlertAsync(new AlertConfig |
| 140 | + { |
| 141 | + Message = message, |
| 142 | + Title = title, |
| 143 | + OkText = okText ?? AlertConfig.DefaultOkText |
| 144 | + }, cancelToken); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + public virtual Task<bool> ConfirmAsync(ConfirmConfig config, CancellationToken? cancelToken = null) |
| 149 | + { |
| 150 | + var tcs = new TaskCompletionSource<bool>(); |
| 151 | + config.OnConfirm = x => tcs.TrySetResult(x); |
| 152 | + |
| 153 | + var disp = this.Confirm(config); |
| 154 | + cancelToken?.Register(() => |
| 155 | + { |
| 156 | + disp.Dispose(); |
| 157 | + tcs.TrySetCanceled(); |
| 158 | + }); |
| 159 | + return tcs.Task; |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + public virtual Task<bool> ConfirmAsync(string message, string title, string okText, string cancelText, CancellationToken? cancelToken = null) |
| 164 | + { |
| 165 | + return this.ConfirmAsync(new ConfirmConfig |
| 166 | + { |
| 167 | + Message = message, |
| 168 | + Title = title, |
| 169 | + CancelText = cancelText ?? ConfirmConfig.DefaultCancelText, |
| 170 | + OkText = okText ?? ConfirmConfig.DefaultOkText |
| 171 | + }, cancelToken); |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + public virtual Task<DatePromptResult> DatePromptAsync(DatePromptConfig config, CancellationToken? cancelToken = null) |
| 176 | + { |
| 177 | + var tcs = new TaskCompletionSource<DatePromptResult>(); |
| 178 | + config.OnResult = x => tcs.TrySetResult(x); |
| 179 | + |
| 180 | + var disp = this.DatePrompt(config); |
| 181 | + cancelToken?.Register(() => |
| 182 | + { |
| 183 | + disp.Dispose(); |
| 184 | + tcs.TrySetCanceled(); |
| 185 | + }); |
| 186 | + |
| 187 | + return tcs.Task; |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + public virtual Task<DatePromptResult> DatePromptAsync(string title, CancellationToken? cancelToken = null) |
| 192 | + { |
| 193 | + return this.DatePromptAsync( |
| 194 | + new DatePromptConfig { Title = title }, |
| 195 | + cancelToken |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + |
| 200 | + public virtual Task<TimePromptResult> TimePromptAsync(TimePromptConfig config, CancellationToken? cancelToken = null) |
| 201 | + { |
| 202 | + var tcs = new TaskCompletionSource<TimePromptResult>(); |
| 203 | + config.OnResult = x => tcs.TrySetResult(x); |
| 204 | + |
| 205 | + var disp = this.TimePrompt(config); |
| 206 | + cancelToken?.Register(() => |
| 207 | + { |
| 208 | + disp.Dispose(); |
| 209 | + tcs.TrySetCanceled(); |
| 210 | + });; |
| 211 | + |
| 212 | + return tcs.Task; |
| 213 | + } |
| 214 | + |
| 215 | + |
| 216 | + public virtual Task<TimePromptResult> TimePromptAsync(string title, CancellationToken? cancelToken = null) |
| 217 | + { |
| 218 | + return this.TimePromptAsync( |
| 219 | + new TimePromptConfig { Title = title }, |
| 220 | + cancelToken |
| 221 | + ); |
| 222 | + } |
| 223 | + |
| 224 | + |
| 225 | + public virtual Task<LoginResult> LoginAsync(LoginConfig config, CancellationToken? cancelToken = null) |
| 226 | + { |
| 227 | + var tcs = new TaskCompletionSource<LoginResult>(); |
| 228 | + config.OnResult = x => tcs.TrySetResult(x); |
| 229 | + |
| 230 | + var disp = this.Login(config); |
| 231 | + cancelToken?.Register(() => |
| 232 | + { |
| 233 | + disp.Dispose(); |
| 234 | + tcs.TrySetCanceled(); |
| 235 | + }); |
| 236 | + |
| 237 | + return tcs.Task; |
| 238 | + } |
| 239 | + |
| 240 | + |
| 241 | + public virtual Task<LoginResult> LoginAsync(string title, string message, CancellationToken? cancelToken = null) |
| 242 | + { |
| 243 | + return this.LoginAsync(new LoginConfig |
| 244 | + { |
| 245 | + Title = title ?? LoginConfig.DefaultTitle, |
| 246 | + Message = message |
| 247 | + }, cancelToken); |
| 248 | + } |
| 249 | + |
| 250 | + |
| 251 | + public virtual Task<PromptResult> PromptAsync(PromptConfig config, CancellationToken? cancelToken = null) |
| 252 | + { |
| 253 | + var tcs = new TaskCompletionSource<PromptResult>(); |
| 254 | + config.OnResult = x => tcs.TrySetResult(x); |
| 255 | + |
| 256 | + var disp = this.Prompt(config); |
| 257 | + cancelToken?.Register(() => |
| 258 | + { |
| 259 | + disp.Dispose(); |
| 260 | + tcs.TrySetCanceled(); |
| 261 | + }); |
| 262 | + |
| 263 | + return tcs.Task; |
| 264 | + } |
| 265 | + |
| 266 | + |
| 267 | + public virtual Task<PromptResult> PromptAsync(string message, string title, string okText, string cancelText, string placeholder, InputType inputType, CancellationToken? cancelToken = null) |
| 268 | + { |
| 269 | + return this.PromptAsync(new PromptConfig |
| 270 | + { |
| 271 | + Message = message, |
| 272 | + Title = title, |
| 273 | + CancelText = cancelText ?? PromptConfig.DefaultCancelText, |
| 274 | + OkText = okText ?? PromptConfig.DefaultOkText, |
| 275 | + Placeholder = placeholder, |
| 276 | + InputType = inputType |
| 277 | + }, cancelToken); |
| 278 | + } |
| 279 | + |
| 280 | + |
| 281 | + public virtual void InfoToast(string title, string description, int timeoutMillis) |
| 282 | + { |
| 283 | + this.Toast(ToastEvent.Info, title, description, timeoutMillis); |
| 284 | + } |
| 285 | + |
| 286 | + |
| 287 | + public virtual void SuccessToast(string title, string description, int timeoutMillis) |
| 288 | + { |
| 289 | + this.Toast(ToastEvent.Success, title, description, timeoutMillis); |
| 290 | + } |
| 291 | + |
| 292 | + |
| 293 | + public virtual void WarnToast(string title, string description, int timeoutMillis) |
| 294 | + { |
| 295 | + this.Toast(ToastEvent.Warn, title, description, timeoutMillis); |
| 296 | + } |
| 297 | + |
| 298 | + |
| 299 | + public virtual void ErrorToast(string title, string description, int timeoutMillis) |
| 300 | + { |
| 301 | + this.Toast(ToastEvent.Error, title, description, timeoutMillis); |
| 302 | + } |
| 303 | + |
| 304 | + |
| 305 | + public virtual void Toast(ToastEvent toastEvent, string title, string description, int timeoutMillis) |
| 306 | + { |
| 307 | + this.Toast(new ToastConfig(toastEvent, title) |
| 308 | + { |
| 309 | + Description = description, |
| 310 | + Duration = TimeSpan.FromMilliseconds(timeoutMillis) |
| 311 | + }); |
| 312 | + } |
| 313 | + } |
| 314 | +} |
0 commit comments