-
Notifications
You must be signed in to change notification settings - Fork 96
Refactoring / Bug fix #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
69c001a
3d927f8
1c44318
154fb57
4049acc
8b1d5f9
b36f9d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using UnityEngine; | ||
| using UnityScreenNavigator.Runtime.Foundation.Coroutine; | ||
|
|
||
| namespace UnityScreenNavigator.Runtime.Core.Modal | ||
| { | ||
| internal sealed class ModalLifecycleHandler | ||
| { | ||
| private readonly IModalBackdropHandler _backdropHandler; | ||
| private readonly IEnumerable<IModalContainerCallbackReceiver> _callbackReceivers; | ||
| private readonly RectTransform _containerTransform; | ||
|
|
||
| public ModalLifecycleHandler( | ||
| RectTransform containerTransform, | ||
| IEnumerable<IModalContainerCallbackReceiver> callbackReceivers, | ||
| IModalBackdropHandler backdropHandler | ||
| ) | ||
| { | ||
| _containerTransform = containerTransform; | ||
| _callbackReceivers = callbackReceivers; | ||
| _backdropHandler = backdropHandler; | ||
| } | ||
|
|
||
| public IEnumerator AfterLoad(ModalPushContext context) | ||
| { | ||
| yield return context.EnterModal.AfterLoad(_containerTransform); | ||
| } | ||
|
|
||
| public IEnumerator BeforePush(ModalPushContext context) | ||
| { | ||
| foreach (var receiver in _callbackReceivers) | ||
| receiver.BeforePush(context.EnterModal, context.ExitModal); | ||
|
|
||
| var handles = new List<AsyncProcessHandle>(); | ||
| if (context.ExitModal != null) | ||
| handles.Add(context.ExitModal.BeforeExit(true, context.EnterModal)); | ||
|
|
||
| handles.Add(context.EnterModal.BeforeEnter(true, context.ExitModal)); | ||
| foreach (var handle in handles) | ||
| while (!handle.IsTerminated) | ||
| yield return handle; | ||
| } | ||
|
|
||
| public IEnumerator Push(ModalPushContext context, bool playAnimation) | ||
| { | ||
| var handles = new List<AsyncProcessHandle> | ||
| { | ||
| _backdropHandler.BeforeModalEnter(context.EnterModal, context.EnterModalIndex, playAnimation) | ||
| }; | ||
|
|
||
| if (context.ExitModal != null) | ||
| handles.Add(context.ExitModal.Exit(true, playAnimation, context.EnterModal)); | ||
|
|
||
| handles.Add(context.EnterModal.Enter(true, playAnimation, context.ExitModal)); | ||
| foreach (var handle in handles) | ||
| while (!handle.IsTerminated) | ||
| yield return handle; | ||
|
|
||
| _backdropHandler.AfterModalEnter(context.EnterModal, context.EnterModalIndex, true); | ||
| } | ||
|
|
||
| public void AfterPush(ModalPushContext context) | ||
| { | ||
| if (context.ExitModal != null) | ||
| context.ExitModal.AfterExit(true, context.EnterModal); | ||
|
|
||
| context.EnterModal.AfterEnter(true, context.ExitModal); | ||
|
|
||
| foreach (var receiver in _callbackReceivers) | ||
| receiver.AfterPush(context.EnterModal, context.ExitModal); | ||
| } | ||
|
|
||
| public IEnumerator BeforePop(ModalPopContext context) | ||
| { | ||
| foreach (var receiver in _callbackReceivers) | ||
| receiver.BeforePop(context.EnterModal, context.FirstExitModal); | ||
|
|
||
| var handles = new List<AsyncProcessHandle>(); | ||
| foreach (var exitModal in context.ExitModals) | ||
| handles.Add(exitModal.BeforeExit(false, context.EnterModal)); | ||
|
|
||
| if (context.EnterModal != null) | ||
| handles.Add(context.EnterModal.BeforeEnter(false, context.FirstExitModal)); | ||
|
|
||
| foreach (var handle in handles) | ||
| while (!handle.IsTerminated) | ||
| yield return handle; | ||
| } | ||
|
|
||
| public IEnumerator Pop(ModalPopContext context, bool playAnimation) | ||
| { | ||
| var handles = new List<AsyncProcessHandle>(); | ||
|
|
||
| for (var i = 0; i < context.ExitModals.Count; i++) | ||
| { | ||
| var exitModal = context.ExitModals[i]; | ||
| var exitModalIndex = context.ExitModalIndices[i]; | ||
| var partner = i == context.ExitModals.Count - 1 ? context.EnterModal : context.ExitModals[i + 1]; | ||
|
|
||
| handles.Add(_backdropHandler.BeforeModalExit(exitModal, exitModalIndex, playAnimation)); | ||
| handles.Add(exitModal.Exit(false, playAnimation, partner)); | ||
| } | ||
|
|
||
| if (context.EnterModal != null) | ||
| handles.Add(context.EnterModal.Enter(false, playAnimation, context.FirstExitModal)); | ||
|
|
||
| foreach (var handle in handles) | ||
| while (!handle.IsTerminated) | ||
| yield return handle; | ||
|
|
||
| _backdropHandler.AfterModalExit(context.FirstExitModal, context.FirstExitModalIndex, playAnimation); | ||
| } | ||
|
|
||
| public void AfterPop(ModalPopContext context) | ||
| { | ||
| foreach (var exitModal in context.ExitModals) | ||
| exitModal.AfterExit(false, context.EnterModal); | ||
| if (context.EnterModal != null) | ||
| context.EnterModal.AfterEnter(false, context.FirstExitModal); | ||
|
|
||
| foreach (var receiver in _callbackReceivers) | ||
| receiver.AfterPop(context.EnterModal, context.FirstExitModal); | ||
| } | ||
|
|
||
| public IEnumerator AfterPopRoutine(ModalPopContext context) | ||
| { | ||
| var handles = context.ExitModals.Select(exitModal => exitModal.BeforeRelease()); | ||
|
|
||
| foreach (var handle in handles) | ||
| while (!handle.IsTerminated) | ||
| yield return handle; | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace UnityScreenNavigator.Runtime.Core.Modal | ||
| { | ||
| internal readonly struct ModalPopContext | ||
| { | ||
| public IReadOnlyList<string> ExitModalIds { get; } | ||
| public IReadOnlyList<Modal> ExitModals { get; } | ||
| public IReadOnlyList<int> ExitModalIndices { get; } | ||
|
|
||
| public string EnterModalId { get; } | ||
| public Modal EnterModal { get; } | ||
|
|
||
| public Modal FirstExitModal => ExitModals[0]; | ||
|
|
||
| public int FirstExitModalIndex => ExitModalIndices[0]; | ||
|
|
||
| private ModalPopContext( | ||
| List<string> exitModalIds, | ||
| List<Modal> exitModals, | ||
| List<int> exitModalIndices, | ||
| string enterModalId, | ||
| Modal enterModal | ||
| ) | ||
| { | ||
| ExitModalIds = exitModalIds; | ||
| ExitModals = exitModals; | ||
| ExitModalIndices = exitModalIndices; | ||
| EnterModalId = enterModalId; | ||
| EnterModal = enterModal; | ||
| } | ||
|
|
||
| public static ModalPopContext Create( | ||
| List<string> orderedModalIds, | ||
| Dictionary<string, Modal> modals, | ||
| int popCount | ||
| ) | ||
| { | ||
| var exitIds = new List<string>(); | ||
| var exitModals = new List<Modal>(); | ||
| var indices = new List<int>(); | ||
|
|
||
| for (var i = orderedModalIds.Count - 1; i >= orderedModalIds.Count - popCount; i--) | ||
| { | ||
| var id = orderedModalIds[i]; | ||
| exitIds.Add(id); | ||
| exitModals.Add(modals[id]); | ||
| indices.Add(i); | ||
| } | ||
|
|
||
| var enterIndex = orderedModalIds.Count - popCount - 1; | ||
| var enterId = enterIndex >= 0 ? orderedModalIds[enterIndex] : null; | ||
| var enterModal = enterId != null ? modals[enterId] : null; | ||
|
|
||
| return new ModalPopContext(exitIds, exitModals, indices, enterId, enterModal); | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace UnityScreenNavigator.Runtime.Core.Modal | ||
| { | ||
| internal readonly struct ModalPushContext | ||
| { | ||
| public string ModalId { get; } | ||
| public Modal EnterModal { get; } | ||
|
|
||
| public string ExitModalId { get; } | ||
| public Modal ExitModal { get; } | ||
|
|
||
| public int EnterModalIndex { get; } | ||
|
|
||
| private ModalPushContext( | ||
| string modalId, | ||
| Modal enterModal, | ||
| string exitModalId, | ||
| Modal exitModal, | ||
| int enterModalIndex | ||
| ) | ||
| { | ||
| ModalId = modalId; | ||
| EnterModal = enterModal; | ||
| ExitModalId = exitModalId; | ||
| ExitModal = exitModal; | ||
| EnterModalIndex = enterModalIndex; | ||
| } | ||
|
|
||
| public static ModalPushContext Create( | ||
| string modalId, | ||
| Modal enterModal, | ||
| List<string> orderedModalIds, | ||
| Dictionary<string, Modal> modals | ||
| ) | ||
| { | ||
| var hasExit = orderedModalIds.Count > 0; | ||
| var exitId = hasExit ? orderedModalIds[orderedModalIds.Count - 1] : null; | ||
| var exitModal = hasExit ? modals[exitId] : null; | ||
|
|
||
| var enterIndex = modals.Count; | ||
|
|
||
| return new ModalPushContext(modalId, enterModal, exitId, exitModal, enterIndex); | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider adding a comment explaining why waiting one frame (via WaitAndCallHomeLoadingPageShown) is necessary before invoking the HomeLoadingPageShown callback.