From dfd76d187f52ce26d1bea9d90c78649c0d4d331f Mon Sep 17 00:00:00 2001 From: Savya Bikram Shah Date: Mon, 6 Jul 2026 14:37:59 +0545 Subject: [PATCH] video and difficulty based sorting fixes --- .../Code/Features/AppBoot/Flow/AppBootFlow.cs | 95 +++++++++++++++---- .../DrawingCatalog/UI/DrawingCatalogButton.cs | 2 +- .../UI/DrawingCatalogPresenter.cs | 18 +++- .../DrawingCatalog/UI/DrawingCatalogView.cs | 12 ++- 4 files changed, 102 insertions(+), 25 deletions(-) diff --git a/Assets/Darkmatter/Code/Features/AppBoot/Flow/AppBootFlow.cs b/Assets/Darkmatter/Code/Features/AppBoot/Flow/AppBootFlow.cs index 0dec868..6c2ccea 100644 --- a/Assets/Darkmatter/Code/Features/AppBoot/Flow/AppBootFlow.cs +++ b/Assets/Darkmatter/Code/Features/AppBoot/Flow/AppBootFlow.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using Cysharp.Threading.Tasks; using Darkmatter.Core.Contracts.Features.Progression; @@ -14,6 +15,10 @@ namespace Darkmatter.Features.AppBoot.Flow { public class AppBootFlow : IAsyncStartable { + private const int IntroPrepareTimeoutMs = 3000; + private const float IntroMinimumFallbackSeconds = 6f; + private const float IntroFallbackPaddingSeconds = 2f; + private readonly AppBootSceneRefs _sceneRefs; private readonly ISceneService _sceneService; private readonly IEventBus _eventBus; @@ -42,30 +47,84 @@ namespace Darkmatter.Features.AppBoot.Flow Screen.sleepTimeout = SleepTimeout.NeverSleep; await _progression.LoadAsync(); - var tcs = new UniTaskCompletionSource(); var player = _sceneRefs.IntroVideoPlayer; - - void OnDone(VideoPlayer vp) - { - vp.loopPointReached -= OnDone; - tcs.TrySetResult(); - } - - player.loopPointReached += OnDone; - player.Play(); - _eventBus.Publish(new IntroStartedSignal()); + var introTask = PlayIntroAsync(player, cancellation); await _sceneService.LoadSceneAsync(nameof(GameScene.MainMenu), null, cancellation); - await tcs.Task.AttachExternalCancellation(cancellation); + await introTask.AttachExternalCancellation(cancellation); + + CleanupIntro(player); + if (_sceneRefs.IntroCanvas != null) + UnityEngine.Object.Destroy(_sceneRefs.IntroCanvas.gameObject); + _eventBus.Publish(new IntroCompletedSignal()); + } + + private async UniTask PlayIntroAsync(VideoPlayer player, CancellationToken cancellation) + { + if (player == null) + return; + + var prepared = new UniTaskCompletionSource(); + var completed = new UniTaskCompletionSource(); + + void OnPrepared(VideoPlayer vp) => prepared.TrySetResult(); + + void OnDone(VideoPlayer vp) => completed.TrySetResult(); + + void OnError(VideoPlayer vp, string message) + { + Debug.LogWarning($"[AppBootFlow] Intro video error: {message}"); + completed.TrySetResult(); + prepared.TrySetResult(); + } + + player.prepareCompleted += OnPrepared; + player.loopPointReached += OnDone; + player.errorReceived += OnError; + + try + { + if (!player.isPrepared) + { + player.Prepare(); + await UniTask.WhenAny( + prepared.Task, + UniTask.Delay(IntroPrepareTimeoutMs, cancellationToken: cancellation)); + } + + player.Play(); + _eventBus.Publish(new IntroStartedSignal()); + + await UniTask.WhenAny( + completed.Task, + UniTask.Delay(GetIntroFallbackTimeout(player), cancellationToken: cancellation)); + } + finally + { + player.prepareCompleted -= OnPrepared; + player.loopPointReached -= OnDone; + player.errorReceived -= OnError; + } + } + + private static TimeSpan GetIntroFallbackTimeout(VideoPlayer player) + { + var length = player != null && player.length > 0d + ? (float)player.length + : IntroMinimumFallbackSeconds; + + return TimeSpan.FromSeconds(Mathf.Max(IntroMinimumFallbackSeconds, length + IntroFallbackPaddingSeconds)); + } + + private static void CleanupIntro(VideoPlayer player) + { + if (player == null) + return; player.Stop(); var rt = player.targetTexture; if (rt != null) rt.Release(); - - if (_sceneRefs.IntroCanvas != null) - Object.Destroy(_sceneRefs.IntroCanvas.gameObject); - Object.Destroy(player.gameObject); - _eventBus.Publish(new IntroCompletedSignal()); + UnityEngine.Object.Destroy(player.gameObject); } } -} \ No newline at end of file +} diff --git a/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogButton.cs b/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogButton.cs index c2d7fa1..888e8e6 100644 --- a/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogButton.cs +++ b/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogButton.cs @@ -13,8 +13,8 @@ namespace Darkmatter.Features.DrawingCatalog { Id = id; thumbnail.sprite = thumbnailSprite; + button.onClick.RemoveAllListeners(); button.onClick.AddListener(onClick); } } } - diff --git a/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogPresenter.cs b/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogPresenter.cs index 569b4bf..8c4dd53 100644 --- a/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogPresenter.cs +++ b/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogPresenter.cs @@ -15,6 +15,7 @@ namespace Darkmatter.Features.DrawingCatalog private int _currentPage = 0; private int _totalPages = 1; + private const int RowCount = 2; private const int ItemPerPage = 8; //2 rows x 4 columns private readonly DrawingCatalogView _view; @@ -108,7 +109,7 @@ namespace Darkmatter.Features.DrawingCatalog private async UniTask RefreshAsync(CancellationToken ct) { - var ids = _controller.VisibleIds; + var ids = ToFixedRowGridOrder(_controller.VisibleIds); var vms = new List(ids.Count); _totalPages = (int)Math.Ceiling(ids.Count / (float)ItemPerPage); @@ -133,6 +134,19 @@ namespace Darkmatter.Features.DrawingCatalog _eventBus.Publish(new DrawingCatalogReadySignal()); } + private static List ToFixedRowGridOrder(IReadOnlyList ids) + { + var ordered = new List(ids.Count); + + for (var row = 0; row < RowCount; row++) + { + for (var index = row; index < ids.Count; index += RowCount) + ordered.Add(ids[index]); + } + + return ordered; + } + public void Dispose() { _view.OnItemClicked -= OnItemClicked; @@ -148,4 +162,4 @@ namespace Darkmatter.Features.DrawingCatalog _cts.Dispose(); } } -} \ No newline at end of file +} diff --git a/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogView.cs b/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogView.cs index b2cc3af..c475d11 100644 --- a/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogView.cs +++ b/Assets/Darkmatter/Code/Features/DrawingCatalog/UI/DrawingCatalogView.cs @@ -62,9 +62,6 @@ namespace Darkmatter.Features.DrawingCatalog while (_buttons.Count < items.Count) { var button = Instantiate(buttonPrefab, content); - var item = items[_buttons.Count]; - button.Initialize(item.Id, item.Thumbnail, - () => { OnItemClicked?.Invoke(item.Id); }); _buttons.Add(button); } @@ -75,6 +72,13 @@ namespace Darkmatter.Features.DrawingCatalog Destroy(last.gameObject); } + for (var i = 0; i < items.Count; i++) + { + var item = items[i]; + _buttons[i].Initialize(item.Id, item.Thumbnail, + () => { OnItemClicked?.Invoke(item.Id); }); + } + LayoutRebuilder.ForceRebuildLayoutImmediate(content); } @@ -178,4 +182,4 @@ namespace Darkmatter.Features.DrawingCatalog _suppressScrollEvents = false; } } -} \ No newline at end of file +}