Last active
February 14, 2020 08:28
-
-
Save shanempope/eab23c06b0e28bb9dcaf to your computer and use it in GitHub Desktop.
xamarin forms dialog service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
namespace MyCompany.Mobile.Services | |
{ | |
public class DialogService : IDialogService | |
{ | |
private readonly IPlatformDialogService _platformDialogService; | |
public DialogService() | |
{ | |
_platformDialogService = DependencyService.Get<IPlatformDialogService>(); | |
} | |
public void CloseAllDialogs() | |
{ | |
_platformDialogService.CloseAllDialogs(); | |
} | |
public async Task ShowError(string title, string message, string buttonText, Action afterHideCallback) | |
{ | |
await Task.Run(() => _platformDialogService.ShowAlert(title, message, buttonText, afterHideCallback)); | |
} | |
public async Task ShowError(string title, Exception error, string buttonText, Action afterHideCallback) | |
{ | |
await Task.Run(() => _platformDialogService.ShowAlert(title, error.Message, buttonText, afterHideCallback)); | |
} | |
public async Task ShowMessage(string title, string message) | |
{ | |
await Task.Run(() => _platformDialogService.ShowAlert(title, message, "OK", null)); | |
} | |
public async Task ShowMessage(string title, string message, string buttonText, Action afterHideCallback) | |
{ | |
var result = await Task.Run(() => _platformDialogService.ShowAlert(title, message, buttonText, afterHideCallback)); | |
} | |
public async Task<bool> ShowMessage(string title, string message, string buttonConfirmText, string buttonCancelText, | |
Action<bool> afterHideCallback) | |
{ | |
var result = await Task.Run(() => | |
_platformDialogService.ShowAlertConfirm(title, message, buttonConfirmText, buttonCancelText, afterHideCallback)); | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
namespace MyCompany.Mobile.Contracts | |
{ | |
public interface IDialogService | |
{ | |
void CloseAllDialogs(); | |
Task ShowError(string title, string message, string buttonText, Action afterHideCallback); | |
Task ShowError(string title, Exception error, string buttonText, Action afterHideCallback); | |
Task ShowMessage(string title, string message); | |
Task ShowMessage(string title, string message, string buttonText, Action afterHideCallback); | |
Task<bool> ShowMessage(string title, string message, string buttonConfirmText, string buttonCancelText, Action<bool> afterHideCallback); | |
} | |
public interface IPlatformDialogService | |
{ | |
void CloseAllDialogs(); | |
Task<bool> ShowAlert(string title, string content, string okButton, Action callback); | |
Task<bool> ShowAlertConfirm(string title, string content, string confirmButton, string cancelButton, | |
Action<bool> callback); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Android.App; | |
using Android.Content; | |
using Android.Widget; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.Android; | |
[assembly: Dependency(typeof (PlatformDialogService))] | |
namespace MyCompany.Mobile.Android.Services | |
{ | |
public class PlatformDialogService : IPlatformDialogService | |
{ | |
List<AlertDialog> _openDialogs = new List<AlertDialog>(); | |
public void CloseAllDialogs() | |
{ | |
foreach (var dialog in _openDialogs) | |
{ | |
dialog.Dismiss(); | |
} | |
_openDialogs.Clear(); | |
} | |
public async Task<bool> ShowAlert(string title, string content, string okButton, Action callback) | |
{ | |
return await Task.Run(() => Alert(title, content, okButton, callback)); | |
} | |
public async Task<bool> ShowAlertConfirm(string title, string content, string confirmButton, string cancelButton, | |
Action<bool> callback) | |
{ | |
return await Task.Run(() => AlertConfirm(title, content, confirmButton, cancelButton, callback)); | |
} | |
private bool Alert(string title, string content, string okButton, Action callback) | |
{ | |
var alert = new AlertDialog.Builder(Forms.Context); | |
alert.SetTitle(title); | |
alert.SetMessage(content); | |
alert.SetNegativeButton(okButton, (sender, e) => | |
{ | |
if (!Equals(callback, null)) | |
{ | |
callback(); | |
} | |
_openDialogs.Remove((AlertDialog)sender); | |
}); | |
Device.BeginInvokeOnMainThread(() => | |
{ | |
var dialog = alert.Show(); | |
_openDialogs.Add(dialog); | |
dialog.SetCanceledOnTouchOutside(false); | |
dialog.SetCancelable(false); | |
}); | |
return true; | |
} | |
private bool AlertConfirm(string title, string content, string confirmButton, string cancelButton, | |
Action<bool> callback) | |
{ | |
var alert = new AlertDialog.Builder(Forms.Context); | |
alert.SetTitle(title); | |
alert.SetMessage(content); | |
alert.SetPositiveButton(confirmButton, (sender, e) => | |
{ | |
callback(true); | |
_openDialogs.Remove((AlertDialog)sender); | |
}); | |
alert.SetNegativeButton(cancelButton, (sender, e) => | |
{ | |
callback(false); | |
_openDialogs.Remove((AlertDialog)sender); | |
}); | |
Device.BeginInvokeOnMainThread(() => | |
{ | |
var dialog = alert.Show(); | |
_openDialogs.Add(dialog); | |
dialog.SetCanceledOnTouchOutside(false); | |
dialog.SetCancelable(false); | |
}); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using UIKit; | |
using System.Collections.Generic; | |
[assembly: Dependency(typeof (PlatformDialogService))] | |
namespace MyCompany.Mobile.iOS.Services | |
{ | |
public class PlatformDialogService : IPlatformDialogService | |
{ | |
readonly List<UIAlertView> _openDialogs = new List<UIAlertView>(); | |
public void CloseAllDialogs() | |
{ | |
foreach (var dialog in _openDialogs) | |
{ | |
dialog.DismissWithClickedButtonIndex(-1, false); // Cancel and don't do ANYTHING | |
} | |
_openDialogs.Clear(); | |
} | |
public async Task<bool> ShowAlert(string title, string content, string okButton, Action callback) | |
{ | |
return await Task.Run(() => Alert(title, content, okButton, callback)); | |
} | |
public async Task<bool> ShowAlertConfirm(string title, string content, string confirmButton, string cancelButton, Action<bool> callback) | |
{ | |
return await Task.Run(() => AlertConfirm(title, content, confirmButton, cancelButton, callback)); | |
} | |
private bool Alert(string title, string content, string okButton, Action callback) | |
{ | |
UIApplication.SharedApplication.InvokeOnMainThread(() => | |
{ | |
var alert = new UIAlertView(title, content, null, okButton); | |
_openDialogs.Add(alert); | |
alert.Clicked += (sender, buttonArgs) => | |
{ | |
if (!Equals(callback, null)) | |
{ | |
callback(); | |
} | |
_openDialogs.Remove(alert); | |
}; | |
alert.Show(); | |
}); | |
return true; | |
} | |
private bool AlertConfirm(string title, string content, string confirmButton, string cancelButton, Action<bool> callback) | |
{ | |
UIApplication.SharedApplication.InvokeOnMainThread(() => | |
{ | |
var alert = new UIAlertView(title, content, null, confirmButton, cancelButton); | |
_openDialogs.Add(alert); | |
alert.Clicked += (sender, buttonArgs) => | |
{ | |
callback(buttonArgs.ButtonIndex == 0); | |
_openDialogs.Remove(alert); | |
}; | |
alert.Show(); | |
}); | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dialog Service and the interfaces are in the PCL solution.
The android file is in android solution, and the iOS file is in iOS solution