6
6
using System . Reactive . Linq ;
7
7
using System . Windows . Input ;
8
8
using System . Windows . Media . Imaging ;
9
+ using System . Windows . Threading ;
9
10
using GitHub . Collections ;
10
11
using GitHub . Exports ;
11
12
using GitHub . Models ;
12
13
using GitHub . Services ;
14
+ using GitHub . Settings ;
13
15
using GitHub . UI ;
14
16
using NullGuard ;
15
17
using ReactiveUI ;
@@ -25,17 +27,31 @@ public class PullRequestListViewModel : BaseViewModel, IPullRequestListViewModel
25
27
readonly ISimpleRepositoryModel repository ;
26
28
readonly TrackingCollection < IAccount > trackingAuthors ;
27
29
readonly TrackingCollection < IAccount > trackingAssignees ;
30
+ readonly IPackageSettings settings ;
31
+ readonly PullRequestListUIState listSettings ;
32
+ bool pullRequestsLoaded ;
28
33
29
34
[ ImportingConstructor ]
30
35
PullRequestListViewModel (
31
- IConnectionRepositoryHostMap connectionRepositoryHostMap , ITeamExplorerServiceHolder teservice )
32
- : this ( connectionRepositoryHostMap . CurrentRepositoryHost , teservice . ActiveRepo )
33
- { }
36
+ IConnectionRepositoryHostMap connectionRepositoryHostMap ,
37
+ ITeamExplorerServiceHolder teservice ,
38
+ IPackageSettings settings )
39
+ : this ( connectionRepositoryHostMap . CurrentRepositoryHost , teservice . ActiveRepo , settings )
40
+ {
41
+ }
34
42
35
- public PullRequestListViewModel ( IRepositoryHost repositoryHost , ISimpleRepositoryModel repository )
43
+ public PullRequestListViewModel (
44
+ IRepositoryHost repositoryHost ,
45
+ ISimpleRepositoryModel repository ,
46
+ IPackageSettings settings )
36
47
{
37
48
this . repositoryHost = repositoryHost ;
38
49
this . repository = repository ;
50
+ this . settings = settings ;
51
+
52
+ this . listSettings = settings . UIState
53
+ . GetOrCreateRepositoryState ( repository . CloneUrl )
54
+ . PullRequests ;
39
55
40
56
openPullRequestCommand = ReactiveCommand . Create ( ) ;
41
57
openPullRequestCommand . Subscribe ( _ =>
@@ -48,23 +64,10 @@ public PullRequestListViewModel(IRepositoryHost repositoryHost, ISimpleRepositor
48
64
new PullRequestState { IsOpen = false , Name = "Closed" } ,
49
65
new PullRequestState { Name = "All" }
50
66
} ;
51
- SelectedState = States [ 0 ] ;
52
-
53
- this . WhenAny ( x => x . SelectedState , x => x . Value )
54
- . Where ( x => PullRequests != null )
55
- . Subscribe ( s => UpdateFilter ( s , SelectedAssignee , SelectedAuthor ) ) ;
56
-
57
- this . WhenAny ( x => x . SelectedAssignee , x => x . Value )
58
- . Where ( x => PullRequests != null && x != EmptyUser )
59
- . Subscribe ( a => UpdateFilter ( SelectedState , a , SelectedAuthor ) ) ;
60
-
61
- this . WhenAny ( x => x . SelectedAuthor , x => x . Value )
62
- . Where ( x => PullRequests != null && x != EmptyUser )
63
- . Subscribe ( a => UpdateFilter ( SelectedState , SelectedAssignee , a ) ) ;
64
67
65
68
trackingAuthors = new TrackingCollection < IAccount > ( Observable . Empty < IAccount > ( ) ,
66
69
OrderedComparer < IAccount > . OrderByDescending ( x => x . Login ) . Compare ) ;
67
- trackingAssignees = new TrackingCollection < IAccount > ( Observable . Empty < IAccount > ( ) ,
70
+ trackingAssignees = new TrackingCollection < IAccount > ( Observable . Empty < IAccount > ( ) ,
68
71
OrderedComparer < IAccount > . OrderByDescending ( x => x . Login ) . Compare ) ;
69
72
trackingAuthors . Subscribe ( ) ;
70
73
trackingAssignees . Subscribe ( ) ;
@@ -74,20 +77,53 @@ public PullRequestListViewModel(IRepositoryHost repositoryHost, ISimpleRepositor
74
77
75
78
PullRequests = new TrackingCollection < IPullRequestModel > ( ) ;
76
79
pullRequests . Comparer = OrderedComparer < IPullRequestModel > . OrderByDescending ( x => x . UpdatedAt ) . Compare ;
77
- pullRequests . Filter = ( pr , i , l ) => pr . IsOpen ;
78
80
pullRequests . NewerComparer = OrderedComparer < IPullRequestModel > . OrderByDescending ( x => x . UpdatedAt ) . Compare ;
81
+
82
+ this . WhenAny ( x => x . SelectedState , x => x . Value )
83
+ . Where ( x => PullRequests != null )
84
+ . Subscribe ( s => UpdateFilter ( s , SelectedAssignee , SelectedAuthor ) ) ;
85
+
86
+ this . WhenAny ( x => x . SelectedAssignee , x => x . Value )
87
+ . Where ( x => PullRequests != null && x != EmptyUser && pullRequestsLoaded )
88
+ . Subscribe ( a => UpdateFilter ( SelectedState , a , SelectedAuthor ) ) ;
89
+
90
+ this . WhenAny ( x => x . SelectedAuthor , x => x . Value )
91
+ . Where ( x => PullRequests != null && x != EmptyUser && pullRequestsLoaded )
92
+ . Subscribe ( a => UpdateFilter ( SelectedState , SelectedAssignee , a ) ) ;
93
+
94
+ SelectedState = States . FirstOrDefault ( x => x . Name == listSettings . SelectedState ) ?? States [ 0 ] ;
79
95
}
80
96
81
97
public override void Initialize ( [ AllowNull ] ViewWithData data )
82
98
{
83
99
base . Initialize ( data ) ;
84
100
101
+ pullRequestsLoaded = false ;
102
+
85
103
PullRequests = repositoryHost . ModelService . GetPullRequests ( repository , pullRequests ) ;
86
104
pullRequests . Subscribe ( pr =>
87
105
{
88
106
trackingAssignees . AddItem ( pr . Assignee ) ;
89
107
trackingAuthors . AddItem ( pr . Author ) ;
90
108
} , ( ) => { } ) ;
109
+
110
+ pullRequests . OriginalCompleted
111
+ . ObserveOn ( RxApp . MainThreadScheduler )
112
+ . Subscribe ( _ =>
113
+ {
114
+ if ( listSettings . SelectedAuthor != null )
115
+ {
116
+ SelectedAuthor = Authors . FirstOrDefault ( x => x . Login == listSettings . SelectedAuthor ) ;
117
+ }
118
+
119
+ if ( listSettings . SelectedAssignee != null )
120
+ {
121
+ SelectedAssignee = Assignees . FirstOrDefault ( x => x . Login == listSettings . SelectedAssignee ) ;
122
+ }
123
+
124
+ pullRequestsLoaded = true ;
125
+ UpdateFilter ( SelectedState , SelectedAssignee , SelectedAuthor ) ;
126
+ } ) ;
91
127
}
92
128
93
129
void UpdateFilter ( PullRequestState state , [ AllowNull ] IAccount ass , [ AllowNull ] IAccount aut )
@@ -132,6 +168,7 @@ public IReadOnlyList<PullRequestState> States
132
168
PullRequestState selectedState ;
133
169
public PullRequestState SelectedState
134
170
{
171
+ [ return : AllowNull ]
135
172
get { return selectedState ; }
136
173
set { this . RaiseAndSetIfChanged ( ref selectedState , value ) ; }
137
174
}
@@ -174,7 +211,6 @@ public IAccount EmptyUser
174
211
get { return emptyUser ; }
175
212
}
176
213
177
-
178
214
bool disposed ;
179
215
protected void Dispose ( bool disposing )
180
216
{
@@ -184,6 +220,7 @@ protected void Dispose(bool disposing)
184
220
pullRequests . Dispose ( ) ;
185
221
trackingAuthors . Dispose ( ) ;
186
222
trackingAssignees . Dispose ( ) ;
223
+ SaveSettings ( ) ;
187
224
disposed = true ;
188
225
}
189
226
}
@@ -193,5 +230,13 @@ public void Dispose()
193
230
Dispose ( true ) ;
194
231
GC . SuppressFinalize ( this ) ;
195
232
}
233
+
234
+ void SaveSettings ( )
235
+ {
236
+ listSettings . SelectedState = SelectedState . Name ;
237
+ listSettings . SelectedAssignee = SelectedAssignee ? . Login ;
238
+ listSettings . SelectedAuthor = SelectedAuthor ? . Login ;
239
+ settings . Save ( ) ;
240
+ }
196
241
}
197
242
}
0 commit comments