Ejemplo n.º 1
0
		private async void PresentInternal(IPresentable presentable, IPresentable presentableParent, Transform transform)
		{
			var zIndex = GetZIndex(presentable);

			try
			{
				// 1) Execute the middleware chain. Order is important here.
				if (_presentDelegates != null)
				{
					foreach (var middleware in _presentDelegates)
					{
						await middleware(this, presentable);
					}
				}

				// 2) Load the controller view.
				var view = await _viewFactory.CreateViewAsync(presentable.PrefabPath, presentable.Layer, zIndex, presentable.PresentOptions, transform);

				if (view is null)
				{
					throw new PresentException(presentable, "View is null.");
				}

				// 3) Create the controller (or dispose view if the controller is dismissed at this point).
				if (presentable.IsDismissed)
				{
					view.Dispose();
					throw new OperationCanceledException();
				}
				else
				{
					presentable.CreateController(view);
				}

				_controllerMap.Add(presentable.Controller, presentable);

				// 4) Dismiss the specified controllers if requested.
				if ((presentable.PresentOptions & PresentOptions.DismissAll) != 0)
				{
					foreach (var p in _presentables)
					{
						if (p != presentable)
						{
							p.DismissCancel();
						}
					}
				}
				else if ((presentable.PresentOptions & PresentOptions.DismissCurrent) != 0)
				{
					presentableParent?.DismissCancel();
				}

				// 5) Dismiss controllers of the same type if requested (for singleton controllers only).
				if ((presentable.PresentOptions & PresentOptions.Singleton) != 0)
				{
					foreach (var p in _presentables)
					{
						if (p != presentable && p.ControllerType == presentable.ControllerType)
						{
							p.DismissCancel();
						}
					}
				}
			}
			catch (Exception e)
			{
				presentable.Dismiss(e);
			}
		}