|
| 1 | +using VijayAnand.MauiToolkit.Services; |
| 2 | + |
| 3 | +namespace PopupDialogs.ViewModels |
| 4 | +{ |
| 5 | + public partial class MainViewModel : BaseViewModel |
| 6 | + { |
| 7 | + private readonly IDialogService _genericDialog; |
| 8 | + private readonly IMauiDialogService _mauiDialog; |
| 9 | + |
| 10 | + public MainViewModel(IDialogService genericDialog, IMauiDialogService mauiDialog) |
| 11 | + => (_genericDialog, _mauiDialog) = (genericDialog, mauiDialog); |
| 12 | + |
| 13 | + [RelayCommand] |
| 14 | + private async Task GenericDialog(string arg) |
| 15 | + { |
| 16 | + // DI approach |
| 17 | + // Switch betwen native/custom implementations in the MauiProgram.cs |
| 18 | + // The order in which dependencies are registered determines the instance injected |
| 19 | + // The latest one wins |
| 20 | + |
| 21 | + switch (arg) |
| 22 | + { |
| 23 | + case "Alert1": |
| 24 | + await _genericDialog.DisplayAlertAsync("Greeting", "Hello from .NET MAUI!!!", "OK"); |
| 25 | + break; |
| 26 | + case "Alert2": |
| 27 | + var response = await _genericDialog.DisplayAlertAsync("Confirm", "Would you like to proceed?", "Yes", "No"); |
| 28 | + var message = response is true ? "Glad that you opted in." : "Sorry to miss you, hope to see you soon."; |
| 29 | + await _genericDialog.DisplayAlertAsync("Response", message, "OK"); |
| 30 | + break; |
| 31 | + case "Prompt": |
| 32 | + var result = await _genericDialog.DisplayPromptAsync("Subscribe", "Enter your email to send notifications.", maxLength: 100, inputType: InputType.Email); |
| 33 | + |
| 34 | + if (!string.IsNullOrWhiteSpace(result)) |
| 35 | + { |
| 36 | + await _genericDialog.DisplayAlertAsync("Response", result, "OK"); |
| 37 | + } |
| 38 | + break; |
| 39 | + case "ActionSheet": |
| 40 | + var action = await _genericDialog.DisplayActionSheetAsync("Unsaved Changes ...", "What would you like to do?", "Cancel", "Discard", "Save", "Save", "Upload", "Share"); |
| 41 | + await _genericDialog.DisplayAlertAsync("Response", action, "OK"); |
| 42 | + break; |
| 43 | + default: |
| 44 | + // Static approach for custom implementation |
| 45 | + // Static approach for native dialogs soon in an upcoming preview |
| 46 | + await PopupDialog.Instance.DisplayAlertAsync("Static Invocation", "Welcome to .NET MAUI!!!", "OK"); |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + [RelayCommand] |
| 52 | + private async Task MauiDialog(string arg) |
| 53 | + { |
| 54 | + // DI approach |
| 55 | + // Switch betwen native/custom implementations in the MauiProgram.cs |
| 56 | + // The order in which dependencies are registered determines the instance injected |
| 57 | + // The latest one wins |
| 58 | + |
| 59 | + var rightToLeft = FlowDirection.RightToLeft; |
| 60 | + |
| 61 | + switch (arg) |
| 62 | + { |
| 63 | + case "Alert1": |
| 64 | + await _mauiDialog.DisplayAlertAsync("Greeting", "Hello from .NET MAUI!!!", "OK", rightToLeft); |
| 65 | + break; |
| 66 | + case "Alert2": |
| 67 | + var response = await _mauiDialog.DisplayAlertAsync("Confirm", "Would you like to proceed?", "Yes", "No", rightToLeft); |
| 68 | + var message = response is true ? "Glad that you opted in." : "Sorry to miss you, hope to see you soon."; |
| 69 | + await _mauiDialog.DisplayAlertAsync("Response", message, "OK", rightToLeft); |
| 70 | + break; |
| 71 | + case "Prompt": |
| 72 | + var result = await _mauiDialog.DisplayPromptAsync("Lucky Draw", "Enter your age to participate.", maxLength: 3, inputType: InputType.Numeric, predicate: ValidateUserEntry); |
| 73 | + |
| 74 | + if (!string.IsNullOrWhiteSpace(result)) |
| 75 | + { |
| 76 | + await _mauiDialog.DisplayAlertAsync("Response", result, "OK"); |
| 77 | + } |
| 78 | + break; |
| 79 | + case "ActionSheet": |
| 80 | + var action = await _mauiDialog.DisplayActionSheetAsync("Unsaved Changes ..."/*, "What would you like to do?"*/, "Cancel", "Discard", rightToLeft, "Save", "Upload", "Share"); |
| 81 | + await _mauiDialog.DisplayAlertAsync("Response", action, "OK", rightToLeft); |
| 82 | + break; |
| 83 | + default: |
| 84 | + // Static approach for custom implementation |
| 85 | + // Static approach for native dialogs soon in an upcoming preview |
| 86 | + await MauiPopupDialog.Instance.DisplayAlertAsync("Static Invocation", "Welcome to .NET MAUI!!!", "OK", rightToLeft); |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private (bool, string) ValidateUserEntry(string value) |
| 92 | + { |
| 93 | + if (string.IsNullOrWhiteSpace(value)) |
| 94 | + { |
| 95 | + return (false, "Age is required."); |
| 96 | + } |
| 97 | + |
| 98 | + if (int.TryParse(value, out int age)) |
| 99 | + { |
| 100 | + if (age < 0 || age > 120) |
| 101 | + { |
| 102 | + return (false, "Enter a valid age."); |
| 103 | + } |
| 104 | + else if (age >= 18) |
| 105 | + { |
| 106 | + return (true, string.Empty); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + return (false, "You should be atleast 18 years old to participate."); |
| 111 | + } |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + return (false, "Enter a valid age."); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments