|
1 | 1 | using UnityEngine; |
| 2 | +using System; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.Threading.Tasks; |
| 5 | +using System.Collections.Concurrent; |
| 6 | +using System.Linq; |
4 | 7 |
|
5 | 8 | namespace Extensions.Unity.ImageLoader |
6 | 9 | { |
7 | 10 | public static partial class ImageLoader |
8 | 11 | { |
9 | | - private static Dictionary<string, Future<Sprite>> loadingInProcess = new Dictionary<string, Future<Sprite>>(); |
| 12 | + private static ConcurrentDictionary<string, Future<Sprite>> loadingInProcess = new ConcurrentDictionary<string, Future<Sprite>>(); |
10 | 13 | private static void AddLoading(Future<Sprite> future) |
11 | 14 | { |
12 | | - loadingInProcess.Add(future.Url, future); |
| 15 | + if (!loadingInProcess.TryAdd(future.Url, future)) |
| 16 | + throw new Exception($"[ImageLoader] AddLoading: {future.Url} already loading"); |
| 17 | + |
13 | 18 | if (settings.debugLevel <= DebugLevel.Log) |
14 | 19 | Debug.Log($"[ImageLoader] AddLoading: {future.Url}, total {loadingInProcess.Count} loading tasks"); |
15 | 20 | } |
16 | | - private static void RemoveLoading(Future<Sprite> future) |
| 21 | + private static void RemoveLoading(Future<Sprite> future) => RemoveLoading(future.Url); |
| 22 | + private static void RemoveLoading(string url) |
17 | 23 | { |
18 | | - if (loadingInProcess.Remove(future.Url)) |
| 24 | + if (loadingInProcess.TryRemove(url, out var future)) |
19 | 25 | { |
20 | 26 | if (settings.debugLevel <= DebugLevel.Log) |
21 | | - Debug.Log($"[ImageLoader] RemoveLoading: {future.Url}, left {loadingInProcess.Count} loading tasks"); |
| 27 | + Debug.Log($"[ImageLoader] RemoveLoading: {url}, left {loadingInProcess.Count} loading tasks"); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + if (settings.debugLevel <= DebugLevel.Warning) |
| 32 | + Debug.LogWarning($"[ImageLoader] RemoveLoading: {url} not found in loading tasks"); |
22 | 33 | } |
23 | 34 | } |
24 | 35 |
|
@@ -48,17 +59,29 @@ public static void Init() |
48 | 59 | /// Return all current loading Futures |
49 | 60 | /// </summary> |
50 | 61 | /// <returns>Returns read only list of all current loading Futures</returns> |
51 | | - public static IReadOnlyCollection<Future<Sprite>> GetLoadingFutures() => loadingInProcess.Values; |
| 62 | + public static IReadOnlyCollection<Future<Sprite>> GetLoadingFutures() => loadingInProcess.Values.ToArray(); |
52 | 63 |
|
53 | 64 | /// <summary> |
54 | 65 | /// Clear cache from Memory and Disk layers for all urls |
55 | 66 | /// </summary> |
| 67 | + /// <returns>Returns task of the disk cache clearing process</returns> |
56 | 68 | public static Task ClearCache() |
57 | 69 | { |
58 | 70 | ClearMemoryCache(); |
59 | 71 | return ClearDiskCache(); |
60 | 72 | } |
61 | 73 |
|
| 74 | + /// <summary> |
| 75 | + /// Clear cache from Memory and Disk layers for all urls |
| 76 | + /// </summary> |
| 77 | + /// <param name="url">URL to the picture, web or local</param> |
| 78 | + /// <returns>Returns task of the disk cache clearing process</returns> |
| 79 | + public static Task ClearCache(string url) |
| 80 | + { |
| 81 | + ClearMemoryCache(url); |
| 82 | + return ClearDiskCache(url); |
| 83 | + } |
| 84 | + |
62 | 85 | /// <summary> |
63 | 86 | /// Checks cache at Memory and at Disk by the url |
64 | 87 | /// </summary> |
|
0 commit comments