-
Notifications
You must be signed in to change notification settings - Fork 0
/
InBoxDetailsViewController.cs
275 lines (216 loc) · 7.15 KB
/
InBoxDetailsViewController.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using Foundation;
using System;
using UIKit;
using AccengageSDK;
using System.Collections.Generic;
using CoreGraphics;
namespace Xamarin_SDK_Sample.iOS
{
public partial class InBoxDetailsViewController : UIViewController
{
public AccengageInboxMessage message;
public AccengageInboxMessageContent content;
public InBoxDetailsViewController (IntPtr handle) : base (handle)
{
}
// View life cycle
public override void ViewDidLoad()
{
base.ViewDidLoad();
NavigationController.NavigationBar.Translucent = false;
// Setup tool bar with custom buttons
setupToolBar();
Subject.Text = message.Title;
Sender.Text = "Expéditeur : " + message.From;
// Category
Category.Text = (message.Category != null) ? message.Category : "";
Category.BackgroundColor = InBoxTools.colorForCategory(message.Category);
ReceiptDate.Text = "Reçu : " + InBoxTools.labelTextForDate(message.Date);
Details.Text = message.Text;
switch (content.Type)
{
case AccengageInboxMessageContentType.Text:
Webview.Hidden = true;
TextView.Text = content.Body;
break;
case AccengageInboxMessageContentType.Web:
TextView.Hidden = true;
Loader.StartAnimating();
Webview.Alpha = 0;
Webview.ScrollView.Bounces = false;
Webview.Delegate = new WebviewDelegate(Webview, Loader);
Webview.LoadRequest(new NSUrlRequest(new NSUrl(content.Body)));
break;
}
if (message.IconUrl.Length > 0)
{
NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl(message.IconUrl));
NSUrlConnection.SendAsynchronousRequest(request, NSOperationQueue.MainQueue, (response, data, error) =>
{
if (error == null)
IconMsg.Image = UIImage.LoadFromData(data);
});
}
else
IconMsg.Hidden = true;
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.DidChangeStatusBarOrientationNotification, deviceOrientationDidChangeNotification);
updateForDeviceOrientation();
}
// UI management
void setupToolBar()
{
UIBarButtonItem space = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null, null);
UIBarButtonItem markAsRead = createBarButton((message.Read) ? "Mark as unread" : "Mark as read", UIBarButtonItemStyle.Plain, markAsUnreadAction);
UIBarButtonItem archive = createBarButton((message.Archived) ? "Restore" : "Delete", UIBarButtonItemStyle.Plain, archiveAction);
List<UIBarButtonItem> buttons;
if (content.Buttons.Count == 0)
buttons = new List<UIBarButtonItem> { space, markAsRead, space, archive, space };
else
buttons = new List<UIBarButtonItem> { space };
int i = 0;
foreach (AccengageInboxButton button in content.Buttons)
{
UIBarButtonItem aButton = createBarButton(button.Title, UIBarButtonItemStyle.Plain, interact);
aButton.Tag = i;
buttons.Add(aButton);
buttons.Add(space);
i++;
}
ToolBar.SetItems(buttons.ToArray(), false);
}
void updateDetailsViewFrame()
{
CGRect detailViewFrame = DetailView.Frame;
if (!TransitionButton.Selected)
detailViewFrame.Height = 155;
else
detailViewFrame.Height = TransitionButton.Frame.Height;
DetailView.Frame = detailViewFrame;
}
void updateToolbarFrame()
{
CGRect toolBarFrame = ToolBar.Frame;
toolBarFrame.Y = View.Bounds.GetMaxY();
if (!TransitionButton.Selected)
toolBarFrame.Y -= toolBarFrame.Height;
ToolBar.Frame = toolBarFrame;
}
void updateRichContentViewFrame()
{
CGRect richContentViewFrame = RichContentView.Frame;
CGRect toolBarFrame = ToolBar.Frame;
CGRect detailViewFrame = DetailView.Frame;
richContentViewFrame.Y = detailViewFrame.Height + detailViewFrame.Y;
richContentViewFrame.Height = toolBarFrame.Y - richContentViewFrame.Y;
RichContentView.Frame = richContentViewFrame;
}
void updateDetailsAlpha()
{
foreach (UIView view in DetailCollection)
view.Alpha = Convert.ToInt32(!TransitionButton.Selected);
}
void updateSubjectFrame()
{
CGRect subjectFrame = Subject.Frame;
if (!TransitionButton.Selected)
{
subjectFrame.X = Sender.Frame.X;
subjectFrame.Y = IconMsg.Frame.Y;
subjectFrame.Height = Sender.Frame.Height;
}
else
{
subjectFrame.X = IconMsg.Frame.X;
subjectFrame.Y = TransitionButton.Frame.Y;
subjectFrame.Height = TransitionButton.Frame.Height;
}
Subject.Frame = subjectFrame;
}
void updateForDeviceOrientation()
{
UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
if (orientation == UIInterfaceOrientation.Portrait)
showDetails(true);
else if (orientation == UIInterfaceOrientation.LandscapeLeft || orientation == UIInterfaceOrientation.LandscapeRight)
showDetails(false);
}
// Tools
UIBarButtonItem createBarButton(string title, UIBarButtonItemStyle style, EventHandler action)
{
return new UIBarButtonItem(title, style, action);
}
// Webview delegate
class WebviewDelegate : UIWebViewDelegate
{
UIWebView webview;
UIActivityIndicatorView loader;
public WebviewDelegate(UIWebView arg1, UIActivityIndicatorView arg2)
{
webview = arg1;
loader = arg2;
}
public override void LoadingFinished(UIWebView webView)
{
stopLoadingAnimation();
}
public override void LoadFailed(UIWebView webView, NSError error)
{
stopLoadingAnimation();
}
void stopLoadingAnimation()
{
if (loader.IsAnimating)
webview.Alpha = 1;
loader.StopAnimating();
}
}
// Actions
void markAsUnreadAction(object sender, EventArgs e)
{
if (message.Read)
message.Read = false;
else
message.Read = true;
setupToolBar();
}
void archiveAction(object sender, EventArgs e)
{
if (message.Archived)
message.Archived = false;
else
message.Archived = true;
setupToolBar();
}
void interact(object sender, EventArgs e)
{
UIBarButtonItem button = sender as UIBarButtonItem;
var tag = (int) button.Tag;
content.Buttons[tag].Interact();
}
partial void TransitionButton_TouchUpInside(UIButton sender)
{
showDetails(TransitionButton.Selected);
}
void showDetails(bool show)
{
if (show != TransitionButton.Selected)
return;
TransitionButton.Selected = !show;
UIView.Animate(0.2, () =>
{
updateDetailsViewFrame();
updateDetailsAlpha();
updateToolbarFrame();
updateRichContentViewFrame();
});
}
// Orientation change notification
void deviceOrientationDidChangeNotification(NSNotification notification)
{
UIInterfaceOrientation newOrientation = UIApplication.SharedApplication.StatusBarOrientation;
UIInterfaceOrientation oldOrientation = (UIInterfaceOrientation) Int32.Parse(notification.UserInfo[UIApplication.StatusBarOrientationUserInfoKey].ToString());
if ((newOrientation == UIInterfaceOrientation.Portrait) != (oldOrientation == UIInterfaceOrientation.LandscapeLeft || oldOrientation == UIInterfaceOrientation.LandscapeRight))
updateForDeviceOrientation();
}
}
}