/// <summary> /// Wait for the dialog to complete and get the result. /// </summary> /// <param name="timeout"></param> /// <returns></returns> /// <remarks> /// Only one thing can wait for the dialog result. Erm will that be a problem? /// </remarks> public PendingDialogResult WaitForDialog(TimeSpan?timeout = null) { Logger.Trace("WaitForDialog called"); signal.Reset(); result = PendingDialogResult.None; Interlocked.Increment(ref waiting); Interlocked.Increment(ref pending); try { if (timeout != null) { signal.WaitOne((TimeSpan)timeout); } else { signal.WaitOne(); } } finally { Interlocked.Exchange(ref waiting, 0); } Logger.Trace($"WaitForDialog complete! Result: {result}"); return(result); }
/// <summary> /// Show the pending dialog in the given context. When the dialog is complete, it will signal /// anyone waiting on this pending dialog /// </summary> /// <param name="context"></param> public void ShowDialog(Context context) { Logger.Trace("ShowDialog called"); Interlocked.Exchange(ref pending, 0); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.SetTitle(Title).SetMessage(Message); builder.SetPositiveButton(PositiveText, (d, i) => { result = PendingDialogResult.Positive; if (PositiveAction != null) { PositiveAction.Invoke(context); } signal.Set(); }); if (!String.IsNullOrWhiteSpace(NegativeText) || NegativeAction != null) { builder.SetNegativeButton(NegativeText, (d, i) => { result = PendingDialogResult.Negative; if (NegativeAction != null) { NegativeAction.Invoke(context); } signal.Set(); }); } builder.SetCancelable(Cancellable); builder.Create().Show(); }
/// <summary> /// Fully reset the dialog and free any waiting threads /// </summary> public void Reset() { waiting = 0; pending = 0; result = PendingDialogResult.None; signal.Set(); //Cancel any pending stuff ID = 0; Title = ""; Message = ""; PositiveText = ""; NegativeText = ""; PositiveAction = null; NegativeAction = null; Cancellable = true; }