This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathAccount.cs
More file actions
177 lines (148 loc) · 5.78 KB
/
Copy pathAccount.cs
File metadata and controls
177 lines (148 loc) · 5.78 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
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reactive.Linq;
using System.Windows.Media.Imaging;
using GitHub.Extensions;
using GitHub.Primitives;
using Octokit;
using ReactiveUI;
namespace GitHub.Models
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Account : ReactiveObject, IAccount
{
BitmapSource avatar;
IObservable<BitmapSource> bitmapSource;
IDisposable bitmapSourceSubscription;
public Account(
string login,
bool isUser,
bool isEnterprise,
int ownedPrivateRepositoryCount,
long privateRepositoryInPlanCount,
string avatarUrl,
IObservable<BitmapSource> bitmapSource)
{
Guard.ArgumentNotEmptyString(login, nameof(login));
Login = login;
IsUser = isUser;
IsEnterprise = isEnterprise;
OwnedPrivateRepos = ownedPrivateRepositoryCount;
PrivateReposInPlan = privateRepositoryInPlanCount;
IsOnFreePlan = privateRepositoryInPlanCount == 0;
HasMaximumPrivateRepositories = OwnedPrivateRepos >= PrivateReposInPlan;
AvatarUrl = avatarUrl;
this.bitmapSource = bitmapSource;
bitmapSourceSubscription = bitmapSource
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => Avatar = x);
}
public Account(Octokit.Account account)
{
Guard.ArgumentNotNull(account, nameof(account));
Login = account.Login;
IsUser = (account as User) != null;
Uri htmlUrl;
IsEnterprise = Uri.TryCreate(account.HtmlUrl, UriKind.Absolute, out htmlUrl)
&& !HostAddress.IsGitHubDotComUri(htmlUrl);
PrivateReposInPlan = account.Plan != null ? account.Plan.PrivateRepos : 0;
OwnedPrivateRepos = account.OwnedPrivateRepos;
IsOnFreePlan = PrivateReposInPlan == 0;
HasMaximumPrivateRepositories = OwnedPrivateRepos >= PrivateReposInPlan;
AvatarUrl = account.AvatarUrl;
}
public Account(Octokit.Account account, IObservable<BitmapSource> bitmapSource)
: this(account)
{
bitmapSource.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => Avatar = x);
}
public bool IsOnFreePlan { get; private set; }
public bool HasMaximumPrivateRepositories { get; private set; }
public bool IsUser { get; private set; }
public bool IsEnterprise { get; private set; }
public string Login { get; private set; }
public int OwnedPrivateRepos { get; private set; }
public long PrivateReposInPlan { get; private set; }
public string AvatarUrl { get; set; }
public BitmapSource Avatar
{
get { return avatar; }
set { avatar = value; this.RaisePropertyChanged(); }
}
#region Equality things
public void CopyFrom(IAccount other)
{
if (!Equals(other))
throw new ArgumentException("Instance to copy from doesn't match this instance. this:(" + this + ") other:(" + other + ")", nameof(other));
OwnedPrivateRepos = other.OwnedPrivateRepos;
PrivateReposInPlan = other.PrivateReposInPlan;
IsOnFreePlan = other.IsOnFreePlan;
HasMaximumPrivateRepositories = other.HasMaximumPrivateRepositories;
Avatar = other.Avatar;
var otherAccount = other as Account;
if (otherAccount != null)
{
bitmapSourceSubscription.Dispose();
bitmapSourceSubscription = otherAccount.bitmapSource
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => Avatar = x);
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
return true;
var other = obj as Account;
return other != null && Login == other.Login && IsUser == other.IsUser && IsEnterprise == other.IsEnterprise;
}
public override int GetHashCode()
{
return (Login?.GetHashCode() ?? 0) ^ IsUser.GetHashCode() ^ IsEnterprise.GetHashCode();
}
bool IEquatable<IAccount>.Equals(IAccount other)
{
if (ReferenceEquals(this, other))
return true;
return other != null && Login == other.Login && IsUser == other.IsUser && IsEnterprise == other.IsEnterprise;
}
public int CompareTo(IAccount other)
{
return other != null ? String.Compare(Login, other.Login, StringComparison.CurrentCulture) : 1;
}
public static bool operator >(Account lhs, Account rhs)
{
if (ReferenceEquals(lhs, rhs))
return false;
return lhs?.CompareTo(rhs) > 0;
}
public static bool operator <(Account lhs, Account rhs)
{
if (ReferenceEquals(lhs, rhs))
return false;
return (object)lhs == null || lhs.CompareTo(rhs) < 0;
}
public static bool operator ==(Account lhs, Account rhs)
{
return Equals(lhs, rhs) && ((object)lhs == null || lhs.CompareTo(rhs) == 0);
}
public static bool operator !=(Account lhs, Account rhs)
{
return !(lhs == rhs);
}
#endregion
public override string ToString()
{
return Login;
}
internal string DebuggerDisplay
{
get
{
return String.Format(CultureInfo.InvariantCulture,
"Account: Login: {0} IsUser: {1}", Login, IsUser);
}
}
}
}