| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class PlaceAndEventsCardsReloader : IDisposable |
| | 6 | | { |
| | 7 | | private readonly IPlacesAndEventsSubSectionComponentView view; |
| | 8 | | private readonly IPlacesAndEventsAPIRequester requester; |
| | 9 | | private readonly DataStore_ExploreV2 exploreV2Menu; |
| | 10 | |
|
| 51 | 11 | | internal bool firstLoading = true; |
| | 12 | | internal bool reloadSubSection; |
| | 13 | | private bool isWaitingAnimTransition; |
| | 14 | |
|
| | 15 | | internal float lastTimeAPIChecked; |
| | 16 | |
|
| 51 | 17 | | public PlaceAndEventsCardsReloader(IPlacesAndEventsSubSectionComponentView view, IPlacesAndEventsAPIRequester reques |
| | 18 | | { |
| 51 | 19 | | this.view = view; |
| 51 | 20 | | this.exploreV2Menu = exploreV2Menu; |
| 51 | 21 | | this.requester = requester; |
| 51 | 22 | | } |
| | 23 | |
|
| | 24 | | public void Initialize() |
| | 25 | | { |
| 0 | 26 | | exploreV2Menu.isOpen.OnChange += OnExploreV2Open; |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public void Dispose() |
| | 30 | | { |
| 51 | 31 | | exploreV2Menu.isOpen.OnChange -= OnExploreV2Open; |
| | 32 | |
|
| 51 | 33 | | if (isWaitingAnimTransition) |
| 1 | 34 | | exploreV2Menu.isInShowAnimationTransiton.OnChange -= OnAnimationTransitionFinished; |
| 51 | 35 | | } |
| | 36 | |
|
| | 37 | | internal void OnExploreV2Open(bool current, bool _) |
| | 38 | | { |
| 6 | 39 | | if (!current) |
| 3 | 40 | | reloadSubSection = true; |
| 6 | 41 | | } |
| | 42 | |
|
| | 43 | | public bool CanReload() |
| | 44 | | { |
| 6 | 45 | | if (!firstLoading) |
| 0 | 46 | | return reloadSubSection && !IsInCooldown(); |
| | 47 | |
|
| 6 | 48 | | reloadSubSection = true; |
| 6 | 49 | | lastTimeAPIChecked = Time.realtimeSinceStartup - PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK_API |
| | 50 | |
|
| 6 | 51 | | return true; |
| | 52 | |
|
| 0 | 53 | | bool IsInCooldown() => lastTimeAPIChecked < Time.realtimeSinceStartup - PlacesAndEventsSectionComponentControlle |
| | 54 | | } |
| | 55 | |
|
| | 56 | | public void RequestAll() |
| | 57 | | { |
| 6 | 58 | | view.RestartScrollViewPosition(); |
| 6 | 59 | | view.SetAllAsLoading(); |
| | 60 | |
|
| 6 | 61 | | reloadSubSection = false; |
| 6 | 62 | | lastTimeAPIChecked = Time.realtimeSinceStartup; |
| | 63 | |
|
| 6 | 64 | | if (!exploreV2Menu.isInShowAnimationTransiton.Get()) |
| 5 | 65 | | requester.RequestAllFromAPI(); |
| | 66 | | else |
| | 67 | | { |
| 1 | 68 | | exploreV2Menu.isInShowAnimationTransiton.OnChange += OnAnimationTransitionFinished; |
| 1 | 69 | | isWaitingAnimTransition = true; |
| | 70 | | } |
| 1 | 71 | | } |
| | 72 | |
|
| | 73 | | private void OnAnimationTransitionFinished(bool _, bool __) |
| | 74 | | { |
| 0 | 75 | | exploreV2Menu.isInShowAnimationTransiton.OnChange -= OnAnimationTransitionFinished; |
| 0 | 76 | | isWaitingAnimTransition = false; |
| | 77 | |
|
| 0 | 78 | | requester.RequestAllFromAPI(); |
| 0 | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public interface IPlacesAndEventsAPIRequester |
| | 83 | | { |
| | 84 | | void RequestAllFromAPI(); |
| | 85 | | } |