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 pathMenuViewController.cs
More file actions
426 lines (357 loc) · 16.4 KB
/
MenuViewController.cs
File metadata and controls
426 lines (357 loc) · 16.4 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using CodeHub.Core.Services;
using CodeHub.Core.ViewModels.App;
using CodeHub.iOS.DialogElements;
using CodeHub.iOS.ViewControllers.Accounts;
using CodeHub.iOS.ViewControllers.Search;
using CodeHub.iOS.ViewControllers.Settings;
using CodeHub.iOS.Views;
using CoreGraphics;
using MvvmCross.Platform;
using Splat;
using UIKit;
namespace CodeHub.iOS.ViewControllers.Application
{
public class MenuViewController : ViewModelDrivenDialogViewController
{
private readonly ProfileButton _profileButton = new ProfileButton();
private readonly UILabel _title;
private MenuElement _notifications;
private Section _favoriteRepoSection;
public new MenuViewModel ViewModel
{
get { return (MenuViewModel) base.ViewModel; }
set { base.ViewModel = value; }
}
private static bool IsAccountEnterprise
=> Locator.Current.GetService<IApplicationService>().Account?.IsEnterprise ?? false;
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
public override string Title {
get {
return _title == null ? base.Title : " " + _title.Text;
}
set {
if (_title != null)
_title.Text = " " + value;
base.Title = value;
}
}
public MenuViewController()
: base(false, UITableViewStyle.Plain)
{
ViewModel = new MenuViewModel();
Appeared.Take(1).Subscribe(_ => PromptPushNotifications());
_title = new UILabel(new CGRect(0, 40, 320, 40));
_title.TextAlignment = UITextAlignment.Left;
_title.BackgroundColor = UIColor.Clear;
_title.Font = UIFont.SystemFontOfSize(16f);
_title.TextColor = UIColor.FromRGB(246, 246, 246);
NavigationItem.TitleView = _title;
OnActivation(d =>
{
d(_profileButton.GetClickedObservable().Subscribe(_ => ProfileButtonClicked()));
});
}
private static async Task PromptPushNotifications()
{
var appService = Mvx.Resolve<IApplicationService>();
if (IsAccountEnterprise)
return;
var featuresService = Mvx.Resolve<IFeaturesService>();
if (!featuresService.IsProEnabled)
return;
var alertDialogService = Mvx.Resolve<IAlertDialogService>();
var pushNotifications = Mvx.Resolve<IPushNotificationsService>();
if (appService.Account.IsPushNotificationsEnabled == null)
{
var result = await alertDialogService.PromptYesNo("Push Notifications", "Would you like to enable push notifications for this account?");
var accountsService = Mvx.Resolve<IAccountsService>();
appService.Account.IsPushNotificationsEnabled = result;
await accountsService.Save(appService.Account);
if (result)
{
pushNotifications.Register().ToBackground();
}
}
else if (appService.Account.IsPushNotificationsEnabled.Value)
{
pushNotifications.Register().ToBackground();
}
}
private void UpdateProfilePicture()
{
var size = new CGSize(32, 32);
if (UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeLeft ||
UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeRight)
{
size = new CGSize(24, 24);
}
_profileButton.Frame = new CGRect(new CGPoint(0, 4), size);
NavigationItem.LeftBarButtonItem = new UIBarButtonItem(_profileButton);
}
private void CreateMenuRoot()
{
var username = ViewModel.Account.Username;
Title = username;
ICollection<Section> sections = new LinkedList<Section>();
sections.Add(new Section
{
new MenuElement("Profile", () => ViewModel.GoToProfileCommand.Execute(null), Octicon.Person.ToImage()),
(_notifications = new MenuElement("Notifications", () => ViewModel.GoToNotificationsCommand.Execute(null), Octicon.Inbox.ToImage()) { NotificationNumber = ViewModel.Notifications }),
new MenuElement("News", () => ViewModel.GoToNewsCommand.Execute(null), Octicon.RadioTower.ToImage()),
new MenuElement("Issues", () => ViewModel.GoToMyIssuesCommand.Execute(null), Octicon.IssueOpened.ToImage())
});
Uri avatarUri;
Uri.TryCreate(ViewModel.Account.AvatarUrl, UriKind.Absolute, out avatarUri);
var eventsSection = new Section { HeaderView = new MenuSectionView("Events") };
eventsSection.Add(new MenuElement(username, () => ViewModel.GoToMyEvents.Execute(null), Octicon.Rss.ToImage(), avatarUri));
if (ViewModel.Organizations != null && ViewModel.Account.ShowOrganizationsInEvents)
{
foreach (var org in ViewModel.Organizations)
{
Uri.TryCreate(org.AvatarUrl, UriKind.Absolute, out avatarUri);
eventsSection.Add(new MenuElement(org.Login, () => ViewModel.GoToOrganizationEventsCommand.Execute(org.Login), Octicon.Rss.ToImage(), avatarUri));
}
}
sections.Add(eventsSection);
var repoSection = new Section() { HeaderView = new MenuSectionView("Repositories") };
repoSection.Add(new MenuElement("Owned", GoToOwnedRepositories, Octicon.Repo.ToImage()));
repoSection.Add(new MenuElement("Starred", GoToStarredRepositories, Octicon.Star.ToImage()));
repoSection.Add(new MenuElement("Trending", GoToTrendingRepositories, Octicon.Pulse.ToImage()));
repoSection.Add(new MenuElement("Search", GoToSearch, Octicon.Search.ToImage()));
sections.Add(repoSection);
if (ViewModel.PinnedRepositories.Any())
{
_favoriteRepoSection = new Section { HeaderView = new MenuSectionView("Favorite Repositories") };
foreach (var pinnedRepository in ViewModel.PinnedRepositories)
{
var element = new PinnedRepoElement(pinnedRepository);
element.Clicked.Subscribe(_ => GoToRepository(pinnedRepository.Owner, pinnedRepository.Name));
_favoriteRepoSection.Add(element);
}
sections.Add(_favoriteRepoSection);
}
else
{
_favoriteRepoSection = null;
}
var orgSection = new Section() { HeaderView = new MenuSectionView("Organizations") };
if (ViewModel.Organizations != null && ViewModel.Account.ExpandOrganizations)
{
foreach (var org in ViewModel.Organizations)
{
Uri.TryCreate(org.AvatarUrl, UriKind.Absolute, out avatarUri);
orgSection.Add(new MenuElement(org.Login, () => ViewModel.GoToOrganizationCommand.Execute(org.Login), Images.Avatar, avatarUri));
}
}
else
orgSection.Add(new MenuElement("Organizations", () => ViewModel.GoToOrganizationsCommand.Execute(null), Octicon.Organization.ToImage()));
//There should be atleast 1 thing...
if (orgSection.Elements.Count > 0)
sections.Add(orgSection);
var gistsSection = new Section() { HeaderView = new MenuSectionView("Gists") };
gistsSection.Add(new MenuElement("My Gists", GoToOwnedGists, Octicon.Gist.ToImage()));
gistsSection.Add(new MenuElement("Starred", GoToStarredGists, Octicon.Star.ToImage()));
gistsSection.Add(new MenuElement("Public", GoToPublicGists, Octicon.Globe.ToImage()));
sections.Add(gistsSection);
var infoSection = new Section() { HeaderView = new MenuSectionView("Info & Preferences") };
sections.Add(infoSection);
infoSection.Add(new MenuElement("Settings", GoToSettings, Octicon.Gear.ToImage()));
if (ViewModel.ShouldShowUpgrades)
infoSection.Add(new MenuElement("Upgrades", GoToUpgrades, Octicon.Lock.ToImage()));
infoSection.Add(new MenuElement("Feedback & Support", GoToSupport, Octicon.CommentDiscussion.ToImage()));
infoSection.Add(new MenuElement("Accounts", ProfileButtonClicked, Octicon.Person.ToImage()));
Root.Reset(sections);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
UpdateProfilePicture();
CreateMenuRoot();
// A user has been activated!
var appService = Mvx.Resolve<IApplicationService>();
appService.ActivationAction?.Invoke();
appService.ActivationAction = null;
#if DEBUG
GC.Collect();
GC.Collect();
GC.Collect();
#endif
}
private void GoToSearch()
{
var vc = new ExploreViewController();
NavigationController?.PushViewController(vc, true);
}
private void GoToSettings()
{
var vc = new SettingsViewController();
NavigationController?.PushViewController(vc, true);
}
private void GoToRepository(string owner, string name)
{
var vc = new Repositories.RepositoryViewController(owner, name);
NavigationController?.PushViewController(vc, true);
}
private void GoToSupport()
{
var vc = IsAccountEnterprise
? (UIViewController)new EnterpriseSupportViewController()
: new SupportViewController();
NavigationController?.PushViewController(vc, true);
}
private void GoToOwnedRepositories()
{
var vc = Repositories.RepositoriesViewController.CreateMineViewController();
NavigationController?.PushViewController(vc, true);
}
private void GoToStarredRepositories()
{
var vc = Repositories.RepositoriesViewController.CreateStarredViewController();
NavigationController?.PushViewController(vc, true);
}
private void GoToTrendingRepositories()
{
var vc = new Repositories.TrendingRepositoriesViewController();
NavigationController?.PushViewController(vc, true);
}
private void GoToOwnedGists()
{
var username = ViewModel.Account.Username;
var vc = Gists.GistsViewController.CreateUserGistsViewController(username);
NavigationController?.PushViewController(vc, true);
}
private void GoToStarredGists()
{
var vc = Gists.GistsViewController.CreateStarredGistsViewController();
NavigationController?.PushViewController(vc, true);
}
private void GoToPublicGists()
{
var vc = Gists.GistsViewController.CreatePublicGistsViewController();
NavigationController?.PushViewController(vc, true);
}
private void GoToUpgrades()
{
var vc = new UpgradeViewController();
NavigationController?.PushViewController(vc, true);
}
public override void DidRotate(UIInterfaceOrientation fromInterfaceOrientation)
{
base.DidRotate(fromInterfaceOrientation);
UpdateProfilePicture();
}
private void ProfileButtonClicked()
{
var vc = new AccountsViewController();
vc.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Cancel);
vc.NavigationItem.LeftBarButtonItem.Clicked += (sender, e) => DismissViewController(true, null);
PresentViewController(new ThemedNavigationController(vc), true, null);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
TableView.SeparatorInset = UIEdgeInsets.Zero;
TableView.SeparatorColor = UIColor.FromRGB(50, 50, 50);
TableView.TableFooterView = new UIView(new CGRect(0, 0, View.Bounds.Width, 0));
TableView.BackgroundColor = UIColor.FromRGB(34, 34, 34);
TableView.ScrollsToTop = false;
if (!string.IsNullOrEmpty(ViewModel.Account.AvatarUrl))
_profileButton.Uri = new Uri(ViewModel.Account.AvatarUrl);
ViewModel.Bind(x => x.Notifications).Subscribe(x =>
{
if (_notifications != null)
{
_notifications.NotificationNumber = x;
}
});
ViewModel.Bind(x => x.Organizations).Subscribe(x => CreateMenuRoot());
ViewModel.LoadCommand.Execute(null);
}
private class PinnedRepoElement : MenuElement
{
public Core.Data.PinnedRepository PinnedRepo { get; private set; }
public PinnedRepoElement(Core.Data.PinnedRepository pinnedRepo)
: base(pinnedRepo.Name, null, Octicon.Repo.ToImage())
{
PinnedRepo = pinnedRepo;
// BUG FIX: App keeps getting relocated so the URLs become off
if (new [] { "repository.png", "repository_fork.png" }.Any(x => PinnedRepo.ImageUri.EndsWith(x, StringComparison.Ordinal)))
{
ImageUri = new Uri("http://codehub-app.com/assets/repository_icon.png");
}
else
{
ImageUri = new Uri(PinnedRepo.ImageUri);
}
}
}
private void DeletePinnedRepo(PinnedRepoElement el)
{
ViewModel.DeletePinnedRepositoryCommand.Execute(el.PinnedRepo);
if (_favoriteRepoSection.Elements.Count == 1)
{
Root.Remove(_favoriteRepoSection);
_favoriteRepoSection = null;
}
else
{
_favoriteRepoSection.Remove(el);
}
}
public override DialogViewController.Source CreateSizingSource()
{
return new EditSource(this);
}
private class EditSource : Source
{
private readonly WeakReference<MenuViewController> _parent;
public EditSource(MenuViewController dvc)
: base (dvc)
{
_parent = new WeakReference<MenuViewController>(dvc);
}
public override bool CanEditRow(UITableView tableView, Foundation.NSIndexPath indexPath)
{
var view = _parent.Get();
if (view == null)
return false;
if (view._favoriteRepoSection == null)
return false;
if (view.Root[indexPath.Section] == view._favoriteRepoSection)
return true;
return false;
}
public override UITableViewCellEditingStyle EditingStyleForRow(UITableView tableView, Foundation.NSIndexPath indexPath)
{
var view = _parent.Get();
if (view == null)
return UITableViewCellEditingStyle.None;
if (view._favoriteRepoSection != null && view.Root[indexPath.Section] == view._favoriteRepoSection)
return UITableViewCellEditingStyle.Delete;
return UITableViewCellEditingStyle.None;
}
public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, Foundation.NSIndexPath indexPath)
{
var view = _parent.Get();
if (view == null)
return;
switch (editingStyle)
{
case UITableViewCellEditingStyle.Delete:
var section = view.Root[indexPath.Section];
var element = section[indexPath.Row];
view.DeletePinnedRepo(element as PinnedRepoElement);
break;
}
}
}
}
}