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 pathUserViewController.cs
More file actions
127 lines (105 loc) · 4.67 KB
/
UserViewController.cs
File metadata and controls
127 lines (105 loc) · 4.67 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
using CodeHub.iOS.ViewControllers;
using CodeHub.Core.ViewModels.User;
using UIKit;
using System;
using CodeHub.iOS.DialogElements;
using System.Reactive.Linq;
using CodeHub.iOS.ViewControllers.Gists;
namespace CodeHub.iOS.ViewControllers.Users
{
public class UserViewController : PrettyDialogViewController
{
private readonly Lazy<UIBarButtonItem> _actionButton;
public new UserViewModel ViewModel
{
get { return (UserViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
public UserViewController(string username, Octokit.User user = null)
: this()
{
ViewModel = new UserViewModel();
ViewModel.Init(new UserViewModel.NavObject { Username = username });
ViewModel.User = user;
}
public UserViewController(Octokit.User user)
: this(user.Login, user)
{
}
public UserViewController()
{
_actionButton = new Lazy<UIBarButtonItem>(() =>
new UIBarButtonItem(UIBarButtonSystemItem.Action, (s, e) => ShowExtraMenu()));
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
HeaderView.SetImage(null, Images.Avatar);
HeaderView.Text = ViewModel.Username;
var split = new SplitButtonElement();
var followers = split.AddButton("Followers", "-");
var following = split.AddButton("Following", "-");
var events = new StringElement("Events", Octicon.Rss.ToImage());
var organizations = new StringElement("Organizations", Octicon.Organization.ToImage());
var repos = new StringElement("Repositories", Octicon.Repo.ToImage());
var gists = new StringElement("Gists", Octicon.Gist.ToImage());
Root.Add(new [] { new Section { split }, new Section { events, organizations, repos, gists } });
ViewModel.Bind(x => x.User).Subscribe(x => {
followers.Text = x?.Followers.ToString() ?? "-";
following.Text = x?.Following.ToString() ?? "-";
HeaderView.SubText = string.IsNullOrWhiteSpace(x?.Name) ? null : x.Name;
HeaderView.SetImage(x?.AvatarUrl, Images.Avatar);
RefreshHeaderView();
});
OnActivation(d =>
{
d(followers.Clicked
.Select(x => UsersViewController.CreateFollowersViewController(ViewModel.Username))
.Subscribe(x => NavigationController.PushViewController(x, true)));
d(following.Clicked
.Select(x => UsersViewController.CreateFollowingViewController(ViewModel.Username))
.Subscribe(x => NavigationController.PushViewController(x, true)));
d(events.Clicked.BindCommand(ViewModel.GoToEventsCommand));
d(organizations.Clicked.BindCommand(ViewModel.GoToOrganizationsCommand));
d(gists.Clicked
.Select(x => GistsViewController.CreateUserGistsViewController(ViewModel.Username))
.Subscribe(x => NavigationController.PushViewController(x, true)));
d(ViewModel.Bind(x => x.Title, true).Subscribe(x => Title = x));
d(repos.Clicked.Subscribe(_ =>
{
var vc = Repositories.RepositoriesViewController.CreateUserViewController(ViewModel.Username);
NavigationController?.PushViewController(vc, true);
}));
});
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (!ViewModel.IsLoggedInUser)
NavigationItem.RightBarButtonItem = _actionButton.Value;
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
NavigationItem.RightBarButtonItem = null;
}
private void ShowExtraMenu()
{
var sheet = new UIActionSheet();
var followButton = sheet.AddButton(ViewModel.IsFollowing ? "Unfollow" : "Follow");
var cancelButton = sheet.AddButton("Cancel");
sheet.CancelButtonIndex = cancelButton;
sheet.Dismissed += (s, e) => {
BeginInvokeOnMainThread(() =>
{
if (e.ButtonIndex == followButton)
{
ViewModel.ToggleFollowingCommand.Execute(null);
}
});
sheet.Dispose();
};
sheet.ShowInView(this.View);
}
}
}