Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ViewSwitcherViewCache.cs
2using Microsoft.Maui.Controls;
3
5{
6 internal sealed class ViewSwitcherViewCache
7 {
8 private readonly Dictionary<int, View> indexCache = new Dictionary<int, View>();
9 private readonly Dictionary<string, View> stateCache = new Dictionary<string, View>();
10
11 public bool IsEnabled { get; set; }
12
13 public bool TryGetByIndex(int index, out View? view)
14 {
15 if (!this.IsEnabled)
16 {
17 view = null;
18 return false;
19 }
20
21 return this.indexCache.TryGetValue(index, out view);
22 }
23
24 public bool TryGetByStateKey(string stateKey, out View? view)
25 {
26 if (!this.IsEnabled)
27 {
28 view = null;
29 return false;
30 }
31
32 if (string.IsNullOrWhiteSpace(stateKey))
33 {
34 view = null;
35 return false;
36 }
37
38 return this.stateCache.TryGetValue(stateKey, out view);
39 }
40
41 public void StoreByIndex(int index, View view)
42 {
43 if (!this.IsEnabled)
44 return;
45
46 this.indexCache[index] = view;
47 }
48
49 public void StoreByStateKey(string stateKey, View view)
50 {
51 if (!this.IsEnabled)
52 return;
53
54 if (string.IsNullOrWhiteSpace(stateKey))
55 return;
56
57 this.stateCache[stateKey] = view;
58 }
59
60 public void Remove(View view)
61 {
62 if (!this.IsEnabled)
63 return;
64
65 List<int> indexesToRemove = new List<int>();
66 foreach (KeyValuePair<int, View> pair in this.indexCache)
67 {
68 if (ReferenceEquals(pair.Value, view))
69 indexesToRemove.Add(pair.Key);
70 }
71
72 foreach (int index in indexesToRemove)
73 {
74 this.indexCache.Remove(index);
75 }
76
77 List<string> statesToRemove = new List<string>();
78 foreach (KeyValuePair<string, View> pair in this.stateCache)
79 {
80 if (ReferenceEquals(pair.Value, view))
81 statesToRemove.Add(pair.Key);
82 }
83
84 foreach (string key in statesToRemove)
85 {
86 this.stateCache.Remove(key);
87 }
88 }
89
90 public void Clear()
91 {
92 this.indexCache.Clear();
93 this.stateCache.Clear();
94 }
95 }
96}