-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaches.cs
130 lines (116 loc) · 2.96 KB
/
Caches.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
namespace ZCache
{
using System.Collections.Generic;
using System;
using System.Windows;
using System.Linq;
using System.ComponentModel;
/// <summary>
/// Container for all cacheable items.
/// </summary>
public class Caches
{
#region Public Properties
/// <summary>
/// Dictionary of cached entities.
/// </summary
public Dictionary<string, ZCache.Interfaces.IZCacheable> CacheList { get; set; }
#endregion
#region Private Properties
/// <summary>
/// Load window.
/// Reports cache status.
/// </summary>
private ZCache.Windows.LoadWindow loadWindow;
#endregion
#region Status Checks
/// <summary>
/// Returns true if all caches have been loaded.
/// </summary>
public bool IsLoaded { get { return this.CacheList.All(f => f.Value.IsLoaded); } }
/// <summary>
/// Returns true if all caches have been completed.
/// </summary>
public bool IsComplete { get { return this.CacheList.All(f => f.Value.IsCompleted); } }
#endregion
#region Constructors
/// <summary>
/// Default constructor.
/// </summary>
public Caches()
{
this.CacheList = new Dictionary<string, Interfaces.IZCacheable>();
}
#endregion
#region Public Methods
/// <summary>
/// Adds an IZCacheable object to cacheable list.
/// </summary>
/// <param name="cacheableObject"></param>
public void AddToCache(Interfaces.IZCacheable cacheableObject)
{
try
{
// Reduntant. I know...
if ( cacheableObject.IsCacheable )
{
this.CacheList.Add(
cacheableObject.ResourceName,
cacheableObject);
}
}
catch ( Exception ex ) { new Windows.ErrorWindow(ex).Show(); }
}
public ZCache.Interfaces.IZCacheable GetCache(string cacheName)
{
// This might be slow. Improve with a foreach or something.
if ( this.CacheList.Keys.Contains(cacheName) )
return this.CacheList[cacheName];
else
return null;
}
/// <summary>
/// Attempts to load caches in cacheable list.
/// </summary>
public void LoadCaches()
{
if ( !this.IsLoaded )
{
if ( this.CacheList.Count > 0 )
{
this.loadWindow = new Windows.LoadWindow();
this.loadWindow.IsLoading = true;
this.loadWindow.labelResults.Content = string.Empty;
this.loadWindow.Show();
// Open pop-up window
foreach ( var resource in this.CacheList.Values )
{
// B.A.L.D.
resource.ResourceLoaded += (sender, e) =>
{
this.loadWindow.UpdateStatus(
(ZCache.Interfaces.IZCacheable)sender,
e.Success ? "Loaded" : "Failed to load");
if ( this.IsComplete )
{
this.loadWindow.IsLoading = false;
this.loadWindow.CompleteLoad(this.IsComplete);
}
};
this.loadWindow.UpdateStatus(resource, "Loading");
resource.LoadCache();
}
}
else
{
new Windows.ErrorWindow(
"Cache list is empty.",
"Please add entities to the cache list.").Show();
}
}
}
#endregion
#region Private Methods
#endregion
}
}