This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathRootElement.cs
More file actions
194 lines (162 loc) · 5.04 KB
/
RootElement.cs
File metadata and controls
194 lines (162 loc) · 5.04 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using Foundation;
using System.Collections.Generic;
using UIKit;
using System.Collections.ObjectModel;
using System.Linq;
namespace CodeHub.iOS.DialogElements
{
public class RootElement : IEnumerable<Section>
{
private readonly List<Section> _sections = new List<Section>();
private readonly WeakReference<UITableView> _tableView;
public UITableView TableView
{
get { return _tableView.Get(); }
}
public IReadOnlyList<Section> Sections
{
get { return new ReadOnlyCollection<Section>(_sections); }
}
public RootElement(UITableView tableView)
{
_tableView = new WeakReference<UITableView>(tableView);
}
public int Count
{
get { return _sections.Count; }
}
public Section this [int idx]
{
get { return _sections[idx]; }
}
internal int IndexOf (Section target)
{
int idx = 0;
foreach (Section s in _sections){
if (s == target)
return idx;
idx++;
}
return -1;
}
public void Add (Section section)
{
if (section == null)
return;
_sections.Add (section);
section.Root = this;
_tableView.Get()?.InsertSections (MakeIndexSet (_sections.Count-1, 1), UITableViewRowAnimation.None);
}
public void Add (IEnumerable<Section> sections)
{
foreach (var s in sections)
Add (s);
}
public void Add(params Section[] sections)
{
Add((IEnumerable<Section>)sections);
}
NSIndexSet MakeIndexSet (int start, int count)
{
NSRange range;
range.Location = start;
range.Length = count;
return NSIndexSet.FromNSRange (range);
}
public void Insert (int idx, UITableViewRowAnimation anim, params Section [] newSections)
{
if (idx < 0 || idx > _sections.Count)
return;
if (newSections == null)
return;
_tableView.Get()?.BeginUpdates ();
int pos = idx;
foreach (var s in newSections){
s.Root = this;
_sections.Insert (pos++, s);
}
_tableView.Get()?.InsertSections (MakeIndexSet (idx, newSections.Length), anim);
_tableView.Get()?.EndUpdates ();
}
public void Insert (int idx, Section section)
{
Insert (idx, UITableViewRowAnimation.None, section);
}
public void RemoveAt (int idx, UITableViewRowAnimation anim)
{
if (idx < 0 || idx >= _sections.Count)
return;
_sections.RemoveAt (idx);
_tableView.Get()?.DeleteSections (NSIndexSet.FromIndex (idx), anim);
}
public void Remove (Section s)
{
if (s == null)
return;
int idx = _sections.IndexOf (s);
if (idx == -1)
return;
RemoveAt (idx, UITableViewRowAnimation.Fade);
}
public void Remove (Section s, UITableViewRowAnimation anim)
{
if (s == null)
return;
int idx = _sections.IndexOf (s);
if (idx == -1)
return;
RemoveAt (idx, anim);
}
public void Clear ()
{
foreach (var s in _sections)
s.Root = null;
_sections.Clear();
_tableView.Get()?.ReloadData ();
}
public void Reset(IEnumerable<Section> sections)
{
foreach (var s in _sections)
s.Root = null;
_sections.Clear();
foreach (var s in sections)
{
s.Root = this;
_sections.Add(s);
}
_tableView.Get()?.ReloadData();
}
public void Reset(params Section[] sections)
{
Reset((IEnumerable<Section>)sections);
}
public void Reload (Element element)
{
Reload(new [] { element });
}
public void Reload (Element[] elements)
{
var paths = (elements ?? Enumerable.Empty<Element>())
.Where(x => x.Section != null && x.Section.Root != null)
.Select(x => x.IndexPath);
try
{
_tableView.Get()?.BeginUpdates();
_tableView.Get()?.ReloadRows(paths.ToArray(), UITableViewRowAnimation.None);
}
finally
{
_tableView.Get()?.EndUpdates();
}
}
public IEnumerator<Section> GetEnumerator()
{
return _sections.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}