summaryrefslogtreecommitdiff
path: root/FireBase/Offline/OfflineCacheAdapter.cs
blob: 3153d1b5fd5dec8c510705dea1dd4df23783df5a (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Firebase.Database.Offline
{
    internal class OfflineCacheAdapter<TKey, T> : IDictionary<string, T>, IDictionary
    {
        private readonly IDictionary<string, OfflineEntry> database;

        public OfflineCacheAdapter(IDictionary<string, OfflineEntry> database)
        {
            this.database = database;
        }

        public void CopyTo(Array array, int index)
        {
            throw new NotImplementedException();
        }

        public bool IsSynchronized { get; }

        public object SyncRoot { get; }

        object IDictionary.this[object key]
        {
            get => database[key.ToString()].Deserialize<T>();

            set
            {
                var keyString = key.ToString();
                if (database.ContainsKey(keyString))
                    database[keyString] = new OfflineEntry(keyString, value, database[keyString].Priority,
                        database[keyString].SyncOptions);
                else
                    database[keyString] = new OfflineEntry(keyString, value, 1, SyncOptions.None);
            }
        }

        ICollection IDictionary.Values { get; }

        ICollection IDictionary.Keys { get; }

        public bool Contains(object key)
        {
            return ContainsKey(key.ToString());
        }

        IDictionaryEnumerator IDictionary.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public void Remove(object key)
        {
            Remove(key.ToString());
        }

        public bool IsFixedSize => false;

        public void Add(object key, object value)
        {
            Add(key.ToString(), (T) value);
        }

        public int Count => database.Count;

        public bool IsReadOnly => database.IsReadOnly;

        public ICollection<string> Keys => database.Keys;

        public ICollection<T> Values => database.Values.Select(o => o.Deserialize<T>()).ToList();

        public T this[string key]
        {
            get => database[key].Deserialize<T>();

            set
            {
                if (database.ContainsKey(key))
                    database[key] = new OfflineEntry(key, value, database[key].Priority, database[key].SyncOptions);
                else
                    database[key] = new OfflineEntry(key, value, 1, SyncOptions.None);
            }
        }

        public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
        {
            return database.Select(d => new KeyValuePair<string, T>(d.Key, d.Value.Deserialize<T>())).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(KeyValuePair<string, T> item)
        {
            Add(item.Key, item.Value);
        }

        public void Clear()
        {
            database.Clear();
        }

        public bool Contains(KeyValuePair<string, T> item)
        {
            return ContainsKey(item.Key);
        }

        public void CopyTo(KeyValuePair<string, T>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public bool Remove(KeyValuePair<string, T> item)
        {
            return database.Remove(item.Key);
        }

        public void Add(string key, T value)
        {
            database.Add(key, new OfflineEntry(key, value, 1, SyncOptions.None));
        }

        public bool ContainsKey(string key)
        {
            return database.ContainsKey(key);
        }

        public bool Remove(string key)
        {
            return database.Remove(key);
        }

        public bool TryGetValue(string key, out T value)
        {
            OfflineEntry val;

            if (database.TryGetValue(key, out val))
            {
                value = val.Deserialize<T>();
                return true;
            }

            value = default(T);
            return false;
        }
    }
}