2using Microsoft.Maui.Controls;
6 internal sealed
class ViewSwitcherViewCache
8 private readonly Dictionary<int, View> indexCache =
new Dictionary<int, View>();
9 private readonly Dictionary<string, View> stateCache =
new Dictionary<string, View>();
11 public bool IsEnabled {
get;
set; }
13 public bool TryGetByIndex(
int index, out View? view)
21 return this.indexCache.TryGetValue(index, out view);
24 public bool TryGetByStateKey(
string stateKey, out View? view)
32 if (
string.IsNullOrWhiteSpace(stateKey))
38 return this.stateCache.TryGetValue(stateKey, out view);
41 public void StoreByIndex(
int index, View view)
46 this.indexCache[index] = view;
49 public void StoreByStateKey(
string stateKey, View view)
54 if (
string.IsNullOrWhiteSpace(stateKey))
57 this.stateCache[stateKey] = view;
60 public void Remove(View view)
65 List<int> indexesToRemove =
new List<int>();
66 foreach (KeyValuePair<int, View> pair
in this.indexCache)
68 if (ReferenceEquals(pair.Value, view))
69 indexesToRemove.Add(pair.Key);
72 foreach (
int index
in indexesToRemove)
74 this.indexCache.Remove(index);
77 List<string> statesToRemove =
new List<string>();
78 foreach (KeyValuePair<string, View> pair
in this.stateCache)
80 if (ReferenceEquals(pair.Value, view))
81 statesToRemove.Add(pair.Key);
84 foreach (
string key
in statesToRemove)
86 this.stateCache.Remove(key);
92 this.indexCache.Clear();
93 this.stateCache.Clear();