void IAdvertisement.Show(Action onComplete, Action onSkip, Action onFailure) { if (IsReady) { IsReady = false; onComplete(); } else { Promoter.RaiseExceptionDetected(new InvalidOperationException($"Advertisement with \"{Id}\" is not ready!")); onFailure(); } }
void IAdvertisement.Show(Action onComplete, Action onSkip, Action onFailure) { if (!IsReady) { Promoter.RaiseExceptionDetected(new InvalidOperationException($"Advertisement with \"{Id}\" is not ready!")); onFailure(); return; } var showOptions = new ShowOptions() { resultCallback = OnResult }; if (string.IsNullOrWhiteSpace(PlacementId)) { Advertisement.Show(showOptions); } else { Advertisement.Show(PlacementId, showOptions); } void OnResult(ShowResult result) { switch (result) { case ShowResult.Failed: Promoter.RaiseExceptionDetected(new OperationCanceledException($"{nameof(result)}: {result}")); onFailure(); break; case ShowResult.Skipped: Promoter.RaiseExceptionDetected(new SkipException()); onSkip(); break; case ShowResult.Finished: onComplete(); break; } } }
async void IAdvertisement.Prepare(Action onComplete, Action onFailure) { var configuration = Promoter.Configuration; var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.CancelAfter(configuration.Timeout ?? 10000); try { var token = cancellationTokenSource.Token; while (!IsReady) { await Task.Delay(10); token.ThrowIfCancellationRequested(); } onComplete(); } catch (TaskCanceledException) { Promoter.RaiseExceptionDetected(new TimeoutException()); onFailure(); } catch (Exception exception) { Promoter.RaiseExceptionDetected(exception); onFailure(); } finally { cancellationTokenSource.Dispose(); } }