public async Task ExecuteActionAsync(Func <CancellationToken, Task> action, IView mainView = null, Action cancellationAction = null) { if (action == null) { throw new ArgumentNullException(nameof(action)); } using var cts = new CancellationTokenSource(); void OnCancel() { cts.Cancel(); _loadingView.Hide(); } try { _loadingView.OperationCancelled += OnCancel; var activityTask = action(cts.Token); var notifyDelayTask = Task.Delay(_options.Value.NotifyLoadingAfterMilliseconds); var winner = await Task.WhenAny(activityTask, notifyDelayTask); if (winner == notifyDelayTask) { if (mainView != null) { mainView.Enabled = false; } _loadingView.Show(mainView); } await activityTask; } catch (TaskCanceledException) { cancellationAction?.Invoke(); } catch (Exception e) { _messageBoxPresenter.Show(mainView, $"Unexpected error occured: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); await Task.Delay(1, cts.Token); cancellationAction?.Invoke(); } finally { if (mainView != null) { mainView.Enabled = true; } _loadingView.OperationCancelled -= OnCancel; _loadingView.Hide(); } }
IEnumerator LoadNextScene(string sceneName) { if (mLoadingView != null) { yield return(StartCoroutine(mLoadingView.Show(mModel.GetLoadingTips()))); } AsyncOperation op = SceneManager.LoadSceneAsync(sceneName); op.allowSceneActivation = false; while (op.progress < 0.9f) { yield return(new WaitForEndOfFrame()); } // Allow the activation of the scene again. op.allowSceneActivation = true; while (!op.isDone) { yield return(new WaitForEndOfFrame()); } if (mSceneController != null) { yield return(StartCoroutine(mSceneController.Init())); } if (mLoadingView != null) { yield return(StartCoroutine(mLoadingView.Hide())); } }
private void HideLoadingView() { loadingView?.Hide(); }