-
Notifications
You must be signed in to change notification settings - Fork 0
/
InBoxViewController.cs
257 lines (218 loc) · 6.39 KB
/
InBoxViewController.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using Foundation;
using System;
using UIKit;
using AccengageSDK;
using CoreGraphics;
namespace Xamarin_SDK_Sample.iOS
{
public partial class InBoxViewController : UIViewController
{
AccengageInbox inbox;
UIRefreshControl refreshControl;
public InBoxViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
NSNotificationCenter notficationCenter = NSNotificationCenter.DefaultCenter;
// Register for inbox notication
notficationCenter.AddObserver(AccengageIOS.Constants.BMA4SInBoxDataChanged, inboxChanged);
// Register for cell notification
notficationCenter.AddObserver(new NSString("RefreshInbox"), inboxRefresh);
// Set Title
// Ex : Inbox (3)
if (inbox != null)
{
if (inbox.UnreadMessageCount == 0)
Title = "Inbox";
else
Title = "Inbox (" + inbox.UnreadMessageCount.ToString() + ")";
}
// Initialize the refresh controll
refreshControl = new UIRefreshControl();
refreshControl.BackgroundColor = UIColor.FromWhiteAlpha(0, 0.1f);
refreshControl.AddTarget((sender, e) => { reloadData(); }, UIControlEvent.ValueChanged);
InBoxTableView.AddSubview(refreshControl);
InvokeOnMainThread(delegate
{
refreshControl.BeginRefreshing();
refreshControl.EndRefreshing();
});
// Add Edit Button
UIBarButtonItem rigthButton = new UIBarButtonItem("edit", UIBarButtonItemStyle.Plain, startEditing);
if (NavigationItem != null)
{
NavigationItem.RightBarButtonItem = rigthButton;
}
InBoxTableView.Delegate = new InBoxTableViewDelegate(this);
reloadData();
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
// Desable edition
activeEditionState(false);
View.AddSubview(ToolBar);
}
void startEditing(object sender, EventArgs e)
{
activeEditionState(!InBoxTableView.Editing);
}
partial void MarkAsReadButton_Activated(UIBarButtonItem sender)
{
if (InBoxTableView.IndexPathsForSelectedRows != null)
{
foreach (NSIndexPath indexPath in InBoxTableView.IndexPathsForSelectedRows)
{
inbox.ObtainMessageAtIndex(indexPath.Row, (message, requestedIndex) =>
{
if (message.Read)
message.Read = false;
else
message.Read = true;
});
}
}
}
partial void ArchiveMessageButton_Activated(UIBarButtonItem sender)
{
if (InBoxTableView.IndexPathsForSelectedRows != null)
{
foreach (NSIndexPath indexPath in InBoxTableView.IndexPathsForSelectedRows)
{
inbox.ObtainMessageAtIndex(indexPath.Row, (message, requestedIndex) =>
{
if (message.Archived)
message.Archived = false;
else
message.Archived = true;
});
}
}
}
void reloadData()
{
Accengage.GetInbox((arg1) => {
inbox = arg1;
updateUIandData();
});
}
// UI management
void updateUIandData()
{
// update table content
InBoxTableView.DataSource = new InBoxTableViewDataSource(inbox);
InBoxTableView.ReloadData();
//title updating
if (inbox != null)
{
if (inbox.UnreadMessageCount == 0)
Title = "Inbox";
else
Title = "Inbox (" + inbox.UnreadMessageCount.ToString() + ")";
}
// End the refreshing
if (refreshControl != null)
{
string title = "Last update: " + DateTime.Now.ToString("MMM d, hh:mm");
NSAttributedString attributedTitle = new NSAttributedString(title, null, UIColor.DarkGray);
refreshControl.AttributedTitle = attributedTitle;
refreshControl.EndRefreshing();
}
}
void updateToolBarPosition()
{
CGRect frame = ToolBar.Frame;
nfloat y = (float) View.Bounds.GetMaxY();
UIEdgeInsets inset = InBoxTableView.ContentInset;
if (InBoxTableView.Editing)
{
y -= ToolBar.Bounds.Size.Height;
inset.Bottom += ToolBar.Bounds.Size.Height;
}
frame.Y = y;
UIView.Animate(0.3, () => {
ToolBar.Frame = frame;
InBoxTableView.ContentInset = inset;
});
}
void activeEditionState(bool actived)
{
InBoxTableView.SetEditing(actived, true);
if (InBoxTableView.Editing)
{
NavigationItem.RightBarButtonItem.Title = "Done";
NavigationItem.RightBarButtonItem.Style = UIBarButtonItemStyle.Done;
}
else
{
NavigationItem.RightBarButtonItem.Title = "Edit";
NavigationItem.RightBarButtonItem.Style = UIBarButtonItemStyle.Plain;
}
updateToolBarPosition();
updateUIandData();
}
void inboxChanged(NSNotification obj)
{
updateUIandData();
}
void inboxRefresh(NSNotification obj)
{
InBoxTableView.ReloadData();
}
class InBoxTableViewDataSource : UITableViewDataSource
{
AccengageInbox inbox;
public InBoxTableViewDataSource(AccengageInbox value)
{
inbox = value;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
InBoxViewCell cell = (InBoxViewCell)tableView.DequeueReusableCell("InBoxViewCell", indexPath);
cell.index = indexPath.Row;
cell.setLoading();
inbox.ObtainMessageAtIndex(indexPath.Row, (message, requestedIndex) =>
{
if (cell.index == (int) requestedIndex)
cell.setMessage(message);
});
cell.Accessory = UITableViewCellAccessory.None;
return cell;
}
public override nint RowsInSection(UITableView tableView, nint section)
{
return (nint)((inbox != null) ? inbox.Size : 0);
}
}
class InBoxTableViewDelegate : UITableViewDelegate
{
InBoxViewController InBoxViewController;
public InBoxTableViewDelegate(InBoxViewController value)
{
InBoxViewController = value;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
if (!tableView.Editing)
{
tableView.DeselectRow(indexPath, false);
InBoxViewController.inbox.ObtainMessageAtIndex(indexPath.Row, (message, requestedIndex) =>
{
message.InteractWithDisplayHandler((content) =>
{
InBoxDetailsViewController controller = (InBoxDetailsViewController) InBoxViewController.Storyboard.InstantiateViewController("InBoxDetailsViewController");
if (controller != null)
{
controller.message = message;
controller.content = content;
InBoxViewController.NavigationController.PushViewController(controller, true);
}
});
});
}
}
}
}
}