dismiss previous toast & cleanup

This commit is contained in:
Kyle Spearrin 2017-12-23 23:34:46 -05:00
parent c4c24ee240
commit ece35b96db
2 changed files with 35 additions and 7 deletions

View file

@ -12,7 +12,7 @@ namespace Bit.iOS.Core.Views
private NSLayoutConstraint _rightMarginConstraint; private NSLayoutConstraint _rightMarginConstraint;
private NSLayoutConstraint _bottomMarginConstraint; private NSLayoutConstraint _bottomMarginConstraint;
public Toast(string text) public Toast(string text)
: base(CoreGraphics.CGRect.FromLTRB(0, 0, 320, 38)) : base(CoreGraphics.CGRect.FromLTRB(0, 0, 320, 38))
{ {
TranslatesAutoresizingMaskIntoConstraints = false; TranslatesAutoresizingMaskIntoConstraints = false;
@ -47,9 +47,11 @@ namespace Bit.iOS.Core.Views
AddConstraints(hMessageConstraints); AddConstraints(hMessageConstraints);
AddConstraints(vMessageConstraints); AddConstraints(vMessageConstraints);
AddGestureRecognizer(new UITapGestureRecognizer(() => Dismiss())); AddGestureRecognizer(new UITapGestureRecognizer(() => Dismiss(false)));
} }
public bool Dismissed { get; set; }
public Action DismissCallback { get; set; }
public TimeSpan Duration { get; set; } = TimeSpan.FromSeconds(3); public TimeSpan Duration { get; set; } = TimeSpan.FromSeconds(3);
public UILabel MessageLabel { get; set; } public UILabel MessageLabel { get; set; }
public nfloat LeftMargin { get; set; } = 5; public nfloat LeftMargin { get; set; } = 5;
@ -104,17 +106,28 @@ namespace Bit.iOS.Core.Views
public void Dismiss(bool animated = true) public void Dismiss(bool animated = true)
{ {
if(Dismissed)
{
return;
}
Dismissed = true;
_dismissTimer?.Invalidate(); _dismissTimer?.Invalidate();
_dismissTimer = null; _dismissTimer = null;
if(!animated) if(!animated)
{ {
RemoveFromSuperview(); RemoveFromSuperview();
DismissCallback?.Invoke();
return; return;
} }
SetNeedsLayout(); SetNeedsLayout();
Animate(0.3f, 0, UIViewAnimationOptions.CurveEaseIn, () => { Alpha = 0; }, RemoveFromSuperview); Animate(0.3f, 0, UIViewAnimationOptions.CurveEaseIn, () => { Alpha = 0; }, () =>
{
RemoveFromSuperview();
DismissCallback?.Invoke();
});
} }
private void ShowWithAnimation() private void ShowWithAnimation()

View file

@ -21,6 +21,7 @@ namespace Bit.iOS.Services
private readonly IAppSettingsService _appSettingsService; private readonly IAppSettingsService _appSettingsService;
private readonly IDeviceInfoService _deviceInfoService; private readonly IDeviceInfoService _deviceInfoService;
private UIAlertController _progressAlert; private UIAlertController _progressAlert;
private Toast _toast;
public DeviceActionService( public DeviceActionService(
IAppSettingsService appSettingsService, IAppSettingsService appSettingsService,
@ -32,17 +33,27 @@ namespace Bit.iOS.Services
public void Toast(string text, bool longDuration = false) public void Toast(string text, bool longDuration = false)
{ {
var t = new Toast(text) if(_toast != null && !_toast.Dismissed)
{
_toast.Dismiss(false);
}
_toast = new Toast(text)
{ {
Duration = TimeSpan.FromSeconds(longDuration ? 5 : 3) Duration = TimeSpan.FromSeconds(longDuration ? 5 : 3)
}; };
if(TabBarVisible()) if(TabBarVisible())
{ {
t.BottomMargin = 55; _toast.BottomMargin = 55;
} }
t.Show(); _toast.Show();
_toast.DismissCallback = () =>
{
_toast?.Dispose();
_toast = null;
};
} }
public void CopyToClipboard(string text) public void CopyToClipboard(string text)
@ -277,6 +288,11 @@ namespace Bit.iOS.Services
public void ShowLoading(string text) public void ShowLoading(string text)
{ {
if(_progressAlert != null)
{
HideLoading();
}
var loadingIndicator = new UIActivityIndicatorView(new CGRect(10, 5, 50, 50)); var loadingIndicator = new UIActivityIndicatorView(new CGRect(10, 5, 50, 50));
loadingIndicator.HidesWhenStopped = true; loadingIndicator.HidesWhenStopped = true;
loadingIndicator.ActivityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray; loadingIndicator.ActivityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray;
@ -300,7 +316,6 @@ namespace Bit.iOS.Services
_progressAlert.DismissViewController(true, () => { }); _progressAlert.DismissViewController(true, () => { });
_progressAlert.Dispose(); _progressAlert.Dispose();
_progressAlert = null; _progressAlert = null;
} }
public Task LaunchAppAsync(string appName, Page page) public Task LaunchAppAsync(string appName, Page page)