| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using DCLFeatures.CameraReel.Gallery; |
| | 5 | | using DCLFeatures.CameraReel.ScreenshotViewer; |
| | 6 | | using DCLServices.CameraReelService; |
| | 7 | | using System; |
| | 8 | | using System.Threading; |
| | 9 | |
|
| | 10 | | namespace DCLFeatures.CameraReel.Section |
| | 11 | | { |
| | 12 | | public class CameraReelSectionController : IDisposable |
| | 13 | | { |
| | 14 | | private const int LIMIT = 30; |
| | 15 | |
|
| | 16 | | private readonly CameraReelModel cameraReelModel; |
| | 17 | | private readonly ICameraReelSectionView sectionView; |
| | 18 | | private readonly ICameraReelGalleryView galleryView; |
| | 19 | | private readonly ICameraReelGalleryStorageView galleryStorageView; |
| | 20 | | private readonly DataStore dataStore; |
| | 21 | | private readonly ICameraReelStorageService storageService; |
| | 22 | | private readonly Func<ScreenshotViewerController> screenshotViewerControllerFactory; |
| | 23 | | private readonly ICameraReelAnalyticsService analytics; |
| | 24 | |
|
| | 25 | | private ScreenshotViewerController screenshotViewerController; |
| | 26 | | private bool isUpdating; |
| | 27 | | private int offset; |
| | 28 | | private CancellationTokenSource fetchScreenshotsCancellationToken; |
| | 29 | |
|
| 0 | 30 | | public CameraReelSectionController(ICameraReelSectionView sectionView, |
| | 31 | | ICameraReelGalleryView galleryView, |
| | 32 | | ICameraReelGalleryStorageView galleryStorageView, |
| | 33 | | DataStore dataStore, |
| | 34 | | ICameraReelStorageService storageService, |
| | 35 | | CameraReelModel cameraReelModel, |
| | 36 | | Func<ScreenshotViewerController> screenshotViewerControllerFactory, |
| | 37 | | ICameraReelAnalyticsService analytics) |
| | 38 | | { |
| 0 | 39 | | this.sectionView = sectionView; |
| 0 | 40 | | this.galleryStorageView = galleryStorageView; |
| 0 | 41 | | this.dataStore = dataStore; |
| 0 | 42 | | this.storageService = storageService; |
| 0 | 43 | | this.screenshotViewerControllerFactory = screenshotViewerControllerFactory; |
| 0 | 44 | | this.analytics = analytics; |
| 0 | 45 | | this.galleryView = galleryView; |
| 0 | 46 | | this.cameraReelModel = cameraReelModel; |
| | 47 | |
|
| 0 | 48 | | cameraReelModel.ScreenshotRemoved += OnScreenshotRemoved; |
| 0 | 49 | | cameraReelModel.ScreenshotAdded += OnScreenshotAdded; |
| 0 | 50 | | cameraReelModel.StorageUpdated += OnStorageUpdated; |
| | 51 | |
|
| 0 | 52 | | dataStore.HUDs.cameraReelSectionVisible.OnChange += SwitchGalleryVisibility; |
| | 53 | |
|
| 0 | 54 | | galleryView.ShowMoreButtonClicked += OnShowMoreClicked; |
| 0 | 55 | | galleryView.ScreenshotThumbnailClicked += ShowScreenshotWithMetadata; |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public void Dispose() |
| | 59 | | { |
| 0 | 60 | | cameraReelModel.ScreenshotRemoved -= OnScreenshotRemoved; |
| 0 | 61 | | cameraReelModel.ScreenshotAdded -= OnScreenshotAdded; |
| 0 | 62 | | cameraReelModel.StorageUpdated -= OnStorageUpdated; |
| | 63 | |
|
| 0 | 64 | | dataStore.HUDs.cameraReelSectionVisible.OnChange -= SwitchGalleryVisibility; |
| 0 | 65 | | galleryView.ShowMoreButtonClicked -= OnShowMoreClicked; |
| 0 | 66 | | galleryView.ScreenshotThumbnailClicked -= ShowScreenshotWithMetadata; |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | private void SwitchGalleryVisibility(bool isVisible, bool _) |
| | 70 | | { |
| 0 | 71 | | sectionView.SwitchVisibility(isVisible); |
| 0 | 72 | | UpdateEmptyStateVisibility(); |
| | 73 | |
|
| 0 | 74 | | if (!isUpdating && isVisible) |
| 0 | 75 | | FetchScreenshots(0); |
| | 76 | |
|
| 0 | 77 | | string source = dataStore.HUDs.cameraReelOpenSource.Get(); |
| | 78 | |
|
| 0 | 79 | | if (isVisible && !string.IsNullOrEmpty(source)) |
| 0 | 80 | | analytics.OpenCameraReel(source); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private void OnScreenshotAdded(bool isFirst, CameraReelResponse screenshot) |
| | 84 | | { |
| 0 | 85 | | galleryView.AddScreenshotThumbnail(screenshot, isFirst); |
| 0 | 86 | | UpdateEmptyStateVisibility(); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private void OnScreenshotRemoved(CameraReelResponse screenshot) |
| | 90 | | { |
| 0 | 91 | | galleryView.DeleteScreenshotThumbnail(screenshot); |
| 0 | 92 | | UpdateEmptyStateVisibility(); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | private void ShowScreenshotWithMetadata(CameraReelResponse reelResponse) |
| | 96 | | { |
| 0 | 97 | | screenshotViewerController ??= screenshotViewerControllerFactory.Invoke(); |
| 0 | 98 | | screenshotViewerController.Show(reelResponse); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | private void FetchScreenshots(int offset) |
| | 102 | | { |
| | 103 | | async UniTaskVoid FetchScreenshotsAndUpdateUiAsync(int offset, CancellationToken cancellationToken) |
| | 104 | | { |
| 0 | 105 | | this.offset = offset; |
| 0 | 106 | | isUpdating = true; |
| | 107 | |
|
| 0 | 108 | | CameraReelResponses screenshots = await storageService.GetScreenshotGallery( |
| | 109 | | dataStore.player.ownPlayer.Get().id, LIMIT, offset, cancellationToken); |
| | 110 | |
|
| 0 | 111 | | isUpdating = false; |
| 0 | 112 | | this.offset += LIMIT; |
| | 113 | |
|
| 0 | 114 | | foreach (CameraReelResponse reel in screenshots.images) |
| 0 | 115 | | cameraReelModel.AddScreenshotAsLast(reel); |
| | 116 | |
|
| 0 | 117 | | cameraReelModel.SetStorageStatus(screenshots.currentImages, screenshots.maxImages); |
| 0 | 118 | | sectionView.HideLoading(); |
| 0 | 119 | | galleryView.SwitchVisibility(true); |
| 0 | 120 | | } |
| | 121 | |
|
| 0 | 122 | | fetchScreenshotsCancellationToken = fetchScreenshotsCancellationToken.SafeRestart(); |
| 0 | 123 | | FetchScreenshotsAndUpdateUiAsync(offset, fetchScreenshotsCancellationToken.Token).Forget(); |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | private void UpdateEmptyStateVisibility() => |
| 0 | 127 | | galleryView.SwitchEmptyStateVisibility(cameraReelModel.LoadedScreenshotCount == 0); |
| | 128 | |
|
| | 129 | | private void UpdateShowMoreVisibility() => |
| 0 | 130 | | galleryView.SwitchShowMoreVisibility(cameraReelModel.TotalScreenshotsInStorage > cameraReelModel.LoadedScree |
| | 131 | |
|
| | 132 | | private void OnShowMoreClicked() => |
| 0 | 133 | | FetchScreenshots(offset); |
| | 134 | |
|
| | 135 | | private void OnStorageUpdated(int totalScreenshots, int maxScreenshots) |
| | 136 | | { |
| 0 | 137 | | galleryStorageView.UpdateStorageBar(totalScreenshots, maxScreenshots); |
| 0 | 138 | | UpdateShowMoreVisibility(); |
| 0 | 139 | | } |
| | 140 | | } |
| | 141 | | } |