| | 1 | | using DCLServices.CameraReelService; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace DCLFeatures.CameraReel.Section |
| | 7 | | { |
| | 8 | | public class CameraReelModel |
| | 9 | | { |
| | 10 | | public delegate void StorageUpdatedHandler(int totalScreenshots, int maxScreenshots); |
| 0 | 11 | | private readonly LinkedList<CameraReelResponse> reels = new (); |
| | 12 | |
|
| 0 | 13 | | public int LoadedScreenshotCount => reels.Count; |
| 0 | 14 | | public int TotalScreenshotsInStorage { get; private set; } |
| 0 | 15 | | public int MaxScreenshotsInStorage { get; private set; } |
| | 16 | |
|
| | 17 | | public event Action<CameraReelResponse> ScreenshotRemoved; |
| | 18 | | public event Action<bool, CameraReelResponse> ScreenshotAdded; |
| | 19 | | public event StorageUpdatedHandler StorageUpdated; |
| | 20 | |
|
| | 21 | | public void SetStorageStatus(int totalScreenshots, int maxScreenshots) |
| | 22 | | { |
| 0 | 23 | | TotalScreenshotsInStorage = totalScreenshots; |
| 0 | 24 | | MaxScreenshotsInStorage = maxScreenshots; |
| 0 | 25 | | StorageUpdated?.Invoke(totalScreenshots, maxScreenshots); |
| 0 | 26 | | } |
| | 27 | |
|
| | 28 | | public void AddScreenshotAsFirst(CameraReelResponse screenshot, CameraReelStorageStatus storage) |
| | 29 | | { |
| 0 | 30 | | AddScreenshotAsFirst(screenshot); |
| 0 | 31 | | SetStorageStatus(storage.CurrentScreenshots, storage.MaxScreenshots); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | private void AddScreenshotAsFirst(CameraReelResponse screenshot) |
| | 35 | | { |
| 0 | 36 | | CameraReelResponse existingScreenshot = reels.FirstOrDefault(s => s.id == screenshot.id); |
| | 37 | |
|
| 0 | 38 | | if (existingScreenshot != null) |
| 0 | 39 | | RemoveScreenshot(existingScreenshot); |
| | 40 | |
|
| 0 | 41 | | reels.AddFirst(screenshot); |
| 0 | 42 | | ScreenshotAdded?.Invoke(true, screenshot); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | public void AddScreenshotAsLast(CameraReelResponse screenshot) |
| | 46 | | { |
| 0 | 47 | | CameraReelResponse existingScreenshot = reels.FirstOrDefault(s => s.id == screenshot.id); |
| | 48 | |
|
| 0 | 49 | | if (existingScreenshot != null) |
| 0 | 50 | | RemoveScreenshot(existingScreenshot); |
| | 51 | |
|
| 0 | 52 | | reels.AddLast(screenshot); |
| 0 | 53 | | ScreenshotAdded?.Invoke(false, screenshot); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | public void RemoveScreenshot(CameraReelResponse current) |
| | 57 | | { |
| 0 | 58 | | LinkedListNode<CameraReelResponse> nodeToRemove = reels.Find(current); |
| | 59 | |
|
| 0 | 60 | | if (nodeToRemove != null) |
| 0 | 61 | | reels.Remove(nodeToRemove); |
| | 62 | |
|
| 0 | 63 | | ScreenshotRemoved?.Invoke(current); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | public CameraReelResponse GetNextScreenshot(CameraReelResponse current) => |
| 0 | 67 | | reels.Find(current)?.Next?.Value; |
| | 68 | |
|
| | 69 | | public CameraReelResponse GetPreviousScreenshot(CameraReelResponse current) => |
| 0 | 70 | | reels.Find(current)?.Previous?.Value; |
| | 71 | | } |
| | 72 | | } |