@@ -20,15 +20,31 @@ public interface IAvatarHost
2020 void OnAvatarResourceChanged ( string email ) ;
2121 }
2222
23- public static partial class AvatarManager
23+ public partial class AvatarManager
2424 {
25- public static string SelectedServer
25+ public static AvatarManager Instance
2626 {
27- get ;
28- set ;
29- } = "https://www.gravatar.com/avatar/" ;
27+ get
28+ {
29+ if ( _instance == null )
30+ _instance = new AvatarManager ( ) ;
31+
32+ return _instance ;
33+ }
34+ }
35+
36+ private static AvatarManager _instance = null ;
37+
38+ [ GeneratedRegex ( @"^(?:(\d+)\+)?(.+?)@users\.noreply\.github\.com$" ) ]
39+ private static partial Regex REG_GITHUB_USER_EMAIL ( ) ;
3040
31- static AvatarManager ( )
41+ private object _synclock = new object ( ) ;
42+ private string _storePath ;
43+ private List < IAvatarHost > _avatars = new List < IAvatarHost > ( ) ;
44+ private Dictionary < string , Bitmap > _resources = new Dictionary < string , Bitmap > ( ) ;
45+ private HashSet < string > _requesting = new HashSet < string > ( ) ;
46+
47+ public void Start ( )
3248 {
3349 _storePath = Path . Combine ( Native . OS . DataDir , "avatars" ) ;
3450 if ( ! Directory . Exists ( _storePath ) )
@@ -62,7 +78,7 @@ static AvatarManager()
6278 var matchGithubUser = REG_GITHUB_USER_EMAIL ( ) . Match ( email ) ;
6379 var url = matchGithubUser . Success ?
6480 $ "https://avatars.githubusercontent.com/{ matchGithubUser . Groups [ 2 ] . Value } " :
65- $ "{ SelectedServer } { md5 } ?d=404";
81+ $ "https://www.gravatar.com/avatar/ { md5 } ?d=404";
6682
6783 var localFile = Path . Combine ( _storePath , md5 ) ;
6884 var img = null as Bitmap ;
@@ -105,20 +121,22 @@ static AvatarManager()
105121 NotifyResourceChanged ( email ) ;
106122 } ) ;
107123 }
124+
125+ // ReSharper disable once FunctionNeverReturns
108126 } ) ;
109127 }
110128
111- public static void Subscribe ( IAvatarHost host )
129+ public void Subscribe ( IAvatarHost host )
112130 {
113131 _avatars . Add ( host ) ;
114132 }
115133
116- public static void Unsubscribe ( IAvatarHost host )
134+ public void Unsubscribe ( IAvatarHost host )
117135 {
118136 _avatars . Remove ( host ) ;
119137 }
120138
121- public static Bitmap Request ( string email , bool forceRefetch )
139+ public Bitmap Request ( string email , bool forceRefetch )
122140 {
123141 if ( forceRefetch )
124142 {
@@ -167,7 +185,7 @@ public static Bitmap Request(string email, bool forceRefetch)
167185 return null ;
168186 }
169187
170- private static string GetEmailHash ( string email )
188+ private string GetEmailHash ( string email )
171189 {
172190 var lowered = email . ToLower ( CultureInfo . CurrentCulture ) . Trim ( ) ;
173191 var hash = MD5 . Create ( ) . ComputeHash ( Encoding . Default . GetBytes ( lowered ) ) ;
@@ -177,21 +195,12 @@ private static string GetEmailHash(string email)
177195 return builder . ToString ( ) ;
178196 }
179197
180- private static void NotifyResourceChanged ( string email )
198+ private void NotifyResourceChanged ( string email )
181199 {
182200 foreach ( var avatar in _avatars )
183201 {
184202 avatar . OnAvatarResourceChanged ( email ) ;
185203 }
186204 }
187-
188- private static readonly object _synclock = new object ( ) ;
189- private static readonly string _storePath ;
190- private static readonly List < IAvatarHost > _avatars = new List < IAvatarHost > ( ) ;
191- private static readonly Dictionary < string , Bitmap > _resources = new Dictionary < string , Bitmap > ( ) ;
192- private static readonly HashSet < string > _requesting = new HashSet < string > ( ) ;
193-
194- [ GeneratedRegex ( @"^(?:(\d+)\+)?(.+?)@users\.noreply\.github\.com$" ) ]
195- private static partial Regex REG_GITHUB_USER_EMAIL ( ) ;
196205 }
197206}
0 commit comments