Skip to content

Instantly share code, notes, and snippets.

@prin53
Created September 20, 2020 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prin53/efc51b6463074902353a0133bead0648 to your computer and use it in GitHub Desktop.
Save prin53/efc51b6463074902353a0133bead0648 to your computer and use it in GitHub Desktop.
public abstract class MvxAlertViewController<TViewModel> : MvxViewController<TViewModel>, IMvxAlertViewController
where TViewModel : class, IMvxViewModel
{
private const string ContentViewControllerKey = "contentViewController";
private MvxIosViewPresenter _viewPresenter;
private UIAlertController _alertController;
public override string Title
{
get => _alertController.Title;
set => _alertController.Title = value;
}
public string Message
{
get => _alertController.Message;
set => _alertController.Message = value;
}
protected UITextField AddTextField()
{
if (_alertController.PreferredStyle != UIAlertControllerStyle.Alert)
{
return null;
}
var textField = default(UITextField);
_alertController.AddTextField(view => textField = view);
return textField;
}
protected UIAlertAction AddAction(ICommand command, string title, UIAlertActionStyle style, bool isPreferred)
{
var alertAction = UIAlertAction.Create(title, style, async _ =>
{
command.SafeExecute();
await _viewPresenter.CloseModalViewController(_alertController, new MvxModalPresentationAttribute())
.ConfigureAwait(false);
});
_alertController.AddAction(alertAction);
if (isPreferred)
{
_alertController.PreferredAction = alertAction;
}
return alertAction;
}
public UIAlertController Wrap(MvxAlertPresentationAttribute attribute, MvxIosViewPresenter viewPresenter)
{
_viewPresenter = viewPresenter;
_alertController = UIAlertController.Create(
attribute.Title,
attribute.Message,
attribute.PreferredStyle
);
_alertController.SetValueForKey(this, new NSString(ContentViewControllerKey));
var preferredContentSize = new CGSize(1, 1);
PreferredContentSize = preferredContentSize;
_alertController.PreferredContentSize = preferredContentSize;
return _alertController;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment