@@ -35,7 +35,6 @@ public Repository(NPath localPath, ICacheContainer container)
35
35
Guard . ArgumentNotNull ( localPath , nameof ( localPath ) ) ;
36
36
37
37
LocalPath = localPath ;
38
- User = new User ( ) ;
39
38
40
39
cacheContainer = container ;
41
40
cacheContainer . CacheInvalidated += CacheContainer_OnCacheInvalidated ;
@@ -57,7 +56,6 @@ public void Initialize(IRepositoryManager initRepositoryManager)
57
56
repositoryManager . OnLocalBranchRemoved += RepositoryManager_OnLocalBranchRemoved ;
58
57
repositoryManager . OnRemoteBranchAdded += RepositoryManager_OnRemoteBranchAdded ;
59
58
repositoryManager . OnRemoteBranchRemoved += RepositoryManager_OnRemoteBranchRemoved ;
60
- repositoryManager . OnGitUserLoaded += user => User = user ;
61
59
62
60
UpdateGitStatus ( ) ;
63
61
UpdateGitLog ( ) ;
@@ -150,7 +148,7 @@ public void UpdateConfigData()
150
148
public void CheckLogChangedEvent ( CacheUpdateEvent cacheUpdateEvent )
151
149
{
152
150
var managedCache = cacheContainer . GitLogCache ;
153
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent , managedCache ) ;
151
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
154
152
155
153
Logger . Trace ( "Check GitLogCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
156
154
cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -166,7 +164,7 @@ public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent)
166
164
public void CheckStatusChangedEvent ( CacheUpdateEvent cacheUpdateEvent )
167
165
{
168
166
var managedCache = cacheContainer . GitStatusCache ;
169
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent , managedCache ) ;
167
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
170
168
171
169
Logger . Trace ( "Check GitStatusCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
172
170
cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -197,7 +195,7 @@ public void CheckCurrentBranchAndRemoteChangedEvent(CacheUpdateEvent cacheUpdate
197
195
private void CheckRepositoryInfoCacheEvent ( CacheUpdateEvent cacheUpdateEvent )
198
196
{
199
197
var managedCache = cacheContainer . RepositoryInfoCache ;
200
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent , managedCache ) ;
198
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
201
199
202
200
Logger . Trace ( "Check RepositoryInfoCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
203
201
cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -214,7 +212,7 @@ public void CheckLocksChangedEvent(CacheUpdateEvent cacheUpdateEvent)
214
212
{
215
213
CacheUpdateEvent cacheUpdateEvent1 = cacheUpdateEvent ;
216
214
var managedCache = cacheContainer . GitLocksCache ;
217
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent1 , managedCache ) ;
215
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent1 ) ;
218
216
219
217
Logger . Trace ( "Check GitLocksCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
220
218
cacheUpdateEvent1 . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -277,7 +275,7 @@ public bool Equals(IRepository other)
277
275
private void CheckBranchCacheEvent ( CacheUpdateEvent cacheUpdateEvent )
278
276
{
279
277
var managedCache = cacheContainer . BranchCache ;
280
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent , managedCache ) ;
278
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
281
279
282
280
Logger . Trace ( "Check BranchCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
283
281
cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -290,20 +288,6 @@ private void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent)
290
288
}
291
289
}
292
290
293
- private static bool ShouldRaiseCacheEvent ( CacheUpdateEvent cacheUpdateEvent , IManagedCache managedCache )
294
- {
295
- bool raiseEvent ;
296
- if ( cacheUpdateEvent . UpdatedTimeString == null )
297
- {
298
- raiseEvent = managedCache . LastUpdatedAt != DateTimeOffset . MinValue ;
299
- }
300
- else
301
- {
302
- raiseEvent = managedCache . LastUpdatedAt . ToString ( ) != cacheUpdateEvent . UpdatedTimeString ;
303
- }
304
- return raiseEvent ;
305
- }
306
-
307
291
private void CacheContainer_OnCacheInvalidated ( CacheType cacheType )
308
292
{
309
293
switch ( cacheType )
@@ -669,27 +653,129 @@ public bool IsGitHub
669
653
"{0} Owner: {1} Name: {2} CloneUrl: {3} LocalPath: {4} Branch: {5} Remote: {6}" , GetHashCode ( ) , Owner , Name ,
670
654
CloneUrl , LocalPath , CurrentBranch , CurrentRemote ) ;
671
655
672
- public IUser User { get ; set ; }
673
-
674
656
protected static ILogging Logger { get ; } = Logging . GetLogger < Repository > ( ) ;
675
657
}
676
658
677
659
public interface IUser
678
660
{
679
- string Name { get ; set ; }
680
- string Email { get ; set ; }
661
+ string Name { get ; }
662
+ string Email { get ; }
663
+ event Action < CacheUpdateEvent > Changed ;
664
+ void CheckUserChangedEvent ( CacheUpdateEvent cacheUpdateEvent ) ;
665
+ void Initialize ( IGitClient client ) ;
666
+ void SetNameAndEmail ( string name , string email ) ;
681
667
}
682
668
683
669
[ Serializable ]
684
670
public class User : IUser
685
671
{
672
+ private ICacheContainer cacheContainer ;
673
+ private IGitClient gitClient ;
674
+
675
+ public event Action < CacheUpdateEvent > Changed ;
676
+
677
+ public User ( ICacheContainer cacheContainer )
678
+ {
679
+ this . cacheContainer = cacheContainer ;
680
+
681
+ cacheContainer . GitUserCache . CacheInvalidated += GitUserCacheOnCacheInvalidated ;
682
+ cacheContainer . GitUserCache . CacheUpdated += GitUserCacheOnCacheUpdated ;
683
+ }
684
+
685
+ public void CheckUserChangedEvent ( CacheUpdateEvent cacheUpdateEvent )
686
+ {
687
+ var managedCache = cacheContainer . GitUserCache ;
688
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
689
+
690
+ Logger . Trace ( "Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
691
+ cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
692
+
693
+ if ( raiseEvent )
694
+ {
695
+ var dateTimeOffset = managedCache . LastUpdatedAt ;
696
+ var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset . ToString ( ) } ;
697
+ HandleUserCacheUpdatedEvent ( updateEvent ) ;
698
+ }
699
+ }
700
+
701
+ public void Initialize ( IGitClient client )
702
+ {
703
+ Guard . ArgumentNotNull ( client , nameof ( client ) ) ;
704
+
705
+ Logger . Trace ( "Initialize" ) ;
706
+
707
+ gitClient = client ;
708
+ UpdateUserAndEmail ( ) ;
709
+ }
710
+
711
+ public void SetNameAndEmail ( string name , string email )
712
+ {
713
+ gitClient . SetConfigNameAndEmail ( name , email )
714
+ . ThenInUI ( ( success , value ) => {
715
+ if ( success )
716
+ {
717
+ Name = value . Name ;
718
+ Email = value . Email ;
719
+ }
720
+ } ) . Start ( ) ;
721
+ }
722
+
686
723
public override string ToString ( )
687
724
{
688
725
return String . Format ( "Name: {0} Email: {1}" , Name , Email ) ;
689
726
}
690
727
691
- public string Name { get ; set ; }
692
- public string Email { get ; set ; }
728
+ public string Name
729
+ {
730
+ get { return cacheContainer . GitUserCache . Name ; }
731
+ private set { cacheContainer . GitUserCache . Name = value ; }
732
+ }
733
+
734
+ public string Email
735
+ {
736
+ get { return cacheContainer . GitUserCache . Email ; }
737
+ private set { cacheContainer . GitUserCache . Email = value ; }
738
+ }
739
+
740
+ private void GitUserCacheOnCacheUpdated ( DateTimeOffset timeOffset )
741
+ {
742
+ HandleUserCacheUpdatedEvent ( new CacheUpdateEvent
743
+ {
744
+ UpdatedTimeString = timeOffset . ToString ( )
745
+ } ) ;
746
+ }
747
+
748
+ private void GitUserCacheOnCacheInvalidated ( )
749
+ {
750
+ Logger . Trace ( "GitUserCache Invalidated" ) ;
751
+ UpdateUserAndEmail ( ) ;
752
+ }
753
+
754
+ private void HandleUserCacheUpdatedEvent ( CacheUpdateEvent cacheUpdateEvent )
755
+ {
756
+ Logger . Trace ( "GitUserCache Updated {0}" , cacheUpdateEvent . UpdatedTimeString ) ;
757
+ Changed ? . Invoke ( cacheUpdateEvent ) ;
758
+ }
759
+
760
+ private void UpdateUserAndEmail ( )
761
+ {
762
+ if ( gitClient != null )
763
+ {
764
+ Logger . Trace ( "UpdateUserAndEmail" ) ;
765
+
766
+ gitClient . GetConfigUserAndEmail ( )
767
+ . ThenInUI ( ( success , value ) =>
768
+ {
769
+ if ( success )
770
+ {
771
+ Name = value . Name ;
772
+ Email = value . Email ;
773
+ }
774
+ } ) . Start ( ) ;
775
+ }
776
+ }
777
+
778
+ protected static ILogging Logger { get ; } = Logging . GetLogger < User > ( ) ;
693
779
}
694
780
695
781
[ Serializable ]
0 commit comments