@@ -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
63
61
public ITask SetupRemote ( string remote , string remoteUrl )
@@ -120,7 +118,7 @@ public ITask ReleaseLock(string file, bool force)
120
118
public void CheckLogChangedEvent ( CacheUpdateEvent cacheUpdateEvent )
121
119
{
122
120
var managedCache = cacheContainer . GitLogCache ;
123
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent , managedCache ) ;
121
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
124
122
125
123
Logger . Trace ( "Check GitLogCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
126
124
cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -136,7 +134,7 @@ public void CheckLogChangedEvent(CacheUpdateEvent cacheUpdateEvent)
136
134
public void CheckStatusChangedEvent ( CacheUpdateEvent cacheUpdateEvent )
137
135
{
138
136
var managedCache = cacheContainer . GitStatusCache ;
139
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent , managedCache ) ;
137
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
140
138
141
139
Logger . Trace ( "Check GitStatusCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
142
140
cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -167,7 +165,7 @@ public void CheckCurrentBranchAndRemoteChangedEvent(CacheUpdateEvent cacheUpdate
167
165
private void CheckRepositoryInfoCacheEvent ( CacheUpdateEvent cacheUpdateEvent )
168
166
{
169
167
var managedCache = cacheContainer . RepositoryInfoCache ;
170
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent , managedCache ) ;
168
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
171
169
172
170
Logger . Trace ( "Check RepositoryInfoCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
173
171
cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -184,7 +182,7 @@ public void CheckLocksChangedEvent(CacheUpdateEvent cacheUpdateEvent)
184
182
{
185
183
CacheUpdateEvent cacheUpdateEvent1 = cacheUpdateEvent ;
186
184
var managedCache = cacheContainer . GitLocksCache ;
187
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent1 , managedCache ) ;
185
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent1 ) ;
188
186
189
187
Logger . Trace ( "Check GitLocksCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
190
188
cacheUpdateEvent1 . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -247,7 +245,7 @@ public bool Equals(IRepository other)
247
245
private void CheckBranchCacheEvent ( CacheUpdateEvent cacheUpdateEvent )
248
246
{
249
247
var managedCache = cacheContainer . BranchCache ;
250
- var raiseEvent = ShouldRaiseCacheEvent ( cacheUpdateEvent , managedCache ) ;
248
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
251
249
252
250
Logger . Trace ( "Check BranchCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
253
251
cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
@@ -260,20 +258,6 @@ private void CheckBranchCacheEvent(CacheUpdateEvent cacheUpdateEvent)
260
258
}
261
259
}
262
260
263
- private static bool ShouldRaiseCacheEvent ( CacheUpdateEvent cacheUpdateEvent , IManagedCache managedCache )
264
- {
265
- bool raiseEvent ;
266
- if ( cacheUpdateEvent . UpdatedTimeString == null )
267
- {
268
- raiseEvent = managedCache . LastUpdatedAt != DateTimeOffset . MinValue ;
269
- }
270
- else
271
- {
272
- raiseEvent = managedCache . LastUpdatedAt . ToString ( ) != cacheUpdateEvent . UpdatedTimeString ;
273
- }
274
- return raiseEvent ;
275
- }
276
-
277
261
private void CacheContainer_OnCacheInvalidated ( CacheType cacheType )
278
262
{
279
263
switch ( cacheType )
@@ -642,27 +626,129 @@ public bool IsGitHub
642
626
"{0} Owner: {1} Name: {2} CloneUrl: {3} LocalPath: {4} Branch: {5} Remote: {6}" , GetHashCode ( ) , Owner , Name ,
643
627
CloneUrl , LocalPath , CurrentBranch , CurrentRemote ) ;
644
628
645
- public IUser User { get ; set ; }
646
-
647
629
protected static ILogging Logger { get ; } = Logging . GetLogger < Repository > ( ) ;
648
630
}
649
631
650
632
public interface IUser
651
633
{
652
- string Name { get ; set ; }
653
- string Email { get ; set ; }
634
+ string Name { get ; }
635
+ string Email { get ; }
636
+ event Action < CacheUpdateEvent > Changed ;
637
+ void CheckUserChangedEvent ( CacheUpdateEvent cacheUpdateEvent ) ;
638
+ void Initialize ( IGitClient client ) ;
639
+ void SetNameAndEmail ( string name , string email ) ;
654
640
}
655
641
656
642
[ Serializable ]
657
643
public class User : IUser
658
644
{
645
+ private ICacheContainer cacheContainer ;
646
+ private IGitClient gitClient ;
647
+
648
+ public event Action < CacheUpdateEvent > Changed ;
649
+
650
+ public User ( ICacheContainer cacheContainer )
651
+ {
652
+ this . cacheContainer = cacheContainer ;
653
+
654
+ cacheContainer . GitUserCache . CacheInvalidated += GitUserCacheOnCacheInvalidated ;
655
+ cacheContainer . GitUserCache . CacheUpdated += GitUserCacheOnCacheUpdated ;
656
+ }
657
+
658
+ public void CheckUserChangedEvent ( CacheUpdateEvent cacheUpdateEvent )
659
+ {
660
+ var managedCache = cacheContainer . GitUserCache ;
661
+ var raiseEvent = managedCache . IsLastUpdatedTimeDifferent ( cacheUpdateEvent ) ;
662
+
663
+ Logger . Trace ( "Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}" , managedCache . LastUpdatedAt ,
664
+ cacheUpdateEvent . UpdatedTimeString ?? "[NULL]" , raiseEvent ) ;
665
+
666
+ if ( raiseEvent )
667
+ {
668
+ var dateTimeOffset = managedCache . LastUpdatedAt ;
669
+ var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset . ToString ( ) } ;
670
+ HandleUserCacheUpdatedEvent ( updateEvent ) ;
671
+ }
672
+ }
673
+
674
+ public void Initialize ( IGitClient client )
675
+ {
676
+ Guard . ArgumentNotNull ( client , nameof ( client ) ) ;
677
+
678
+ Logger . Trace ( "Initialize" ) ;
679
+
680
+ gitClient = client ;
681
+ UpdateUserAndEmail ( ) ;
682
+ }
683
+
684
+ public void SetNameAndEmail ( string name , string email )
685
+ {
686
+ gitClient . SetConfigNameAndEmail ( name , email )
687
+ . ThenInUI ( ( success , value ) => {
688
+ if ( success )
689
+ {
690
+ Name = value . Name ;
691
+ Email = value . Email ;
692
+ }
693
+ } ) . Start ( ) ;
694
+ }
695
+
659
696
public override string ToString ( )
660
697
{
661
698
return String . Format ( "Name: {0} Email: {1}" , Name , Email ) ;
662
699
}
663
700
664
- public string Name { get ; set ; }
665
- public string Email { get ; set ; }
701
+ public string Name
702
+ {
703
+ get { return cacheContainer . GitUserCache . Name ; }
704
+ private set { cacheContainer . GitUserCache . Name = value ; }
705
+ }
706
+
707
+ public string Email
708
+ {
709
+ get { return cacheContainer . GitUserCache . Email ; }
710
+ private set { cacheContainer . GitUserCache . Email = value ; }
711
+ }
712
+
713
+ private void GitUserCacheOnCacheUpdated ( DateTimeOffset timeOffset )
714
+ {
715
+ HandleUserCacheUpdatedEvent ( new CacheUpdateEvent
716
+ {
717
+ UpdatedTimeString = timeOffset . ToString ( )
718
+ } ) ;
719
+ }
720
+
721
+ private void GitUserCacheOnCacheInvalidated ( )
722
+ {
723
+ Logger . Trace ( "GitUserCache Invalidated" ) ;
724
+ UpdateUserAndEmail ( ) ;
725
+ }
726
+
727
+ private void HandleUserCacheUpdatedEvent ( CacheUpdateEvent cacheUpdateEvent )
728
+ {
729
+ Logger . Trace ( "GitUserCache Updated {0}" , cacheUpdateEvent . UpdatedTimeString ) ;
730
+ Changed ? . Invoke ( cacheUpdateEvent ) ;
731
+ }
732
+
733
+ private void UpdateUserAndEmail ( )
734
+ {
735
+ if ( gitClient != null )
736
+ {
737
+ Logger . Trace ( "UpdateUserAndEmail" ) ;
738
+
739
+ gitClient . GetConfigUserAndEmail ( )
740
+ . ThenInUI ( ( success , value ) =>
741
+ {
742
+ if ( success )
743
+ {
744
+ Name = value . Name ;
745
+ Email = value . Email ;
746
+ }
747
+ } ) . Start ( ) ;
748
+ }
749
+ }
750
+
751
+ protected static ILogging Logger { get ; } = Logging . GetLogger < User > ( ) ;
666
752
}
667
753
668
754
[ Serializable ]
0 commit comments