@@ -30,11 +30,19 @@ public partial class AssignmentOverview : Page
3030 {
3131 public AssignmentOverview ( )
3232 {
33+ const int DEFAULT_PAGE_SIZE = 15 ;
3334 InitializeComponent ( ) ;
34- this . PagingManager = new PagingManager < AssignmentViewModel > ( 15 ) ;
35+ this . PagingManager = new PagingManager < AssignmentViewModel > ( DEFAULT_PAGE_SIZE ) ;
3536 this . LoadProjectViewModels ( ) ;
3637 this . Load ( ) ;
3738 this . RefreshBindings ( 1 ) ;
39+ this . SetDefaultFilters ( ) ;
40+ }
41+
42+ private void SetDefaultFilters ( )
43+ {
44+ this . cb_ProjectSelection . SelectedItem = this . ProjectViewModels . Single ( pvm => pvm . Id == 0 ) ;
45+ this . cb_StatusFilter . SelectedItem = cbi_Future ;
3846 }
3947
4048 private void LoadProjectViewModels ( )
@@ -53,17 +61,8 @@ private void Load()
5361 {
5462 var assignmentLogic = new AssignmentLogic ( ) ;
5563 this . AssignmentViewModels = assignmentLogic . GetAllAssignments ( ) . Select ( a => new AssignmentViewModel ( a ) ) ;
64+ this . FilteredAssignmentViewModels = PipeThroughFilter ( this . AssignmentViewModels ) ;
5665 assignmentLogic . Dispose ( ) ;
57- if ( this . SelectedProject != null )
58- {
59- this . FilteredAssignmentViewModels = this . AssignmentViewModels
60- . Where ( av => av . ProjectId == this . SelectedProject ? . Id && av . StatusAsEnum == this . SelectedFilter )
61- . ToList ( ) ;
62- }
63- else
64- {
65- this . FilteredAssignmentViewModels = this . AssignmentViewModels ;
66- }
6766 this . PagingManager . Data = this . FilteredAssignmentViewModels . ToList ( ) ;
6867 this . LoadFromAllProjects = true ;
6968 this . lv_Assignments . ItemsSource = this . PagingManager . Page ( this . PagingManager . CurrentPage ) ;
@@ -75,14 +74,31 @@ private void Load()
7574
7675 private void RefreshBindings ( int page )
7776 {
78- this . PagingManager . Data = this . FilteredAssignmentViewModels . ToList ( ) ;
77+ this . PagingManager . Data = PipeThroughFilter ( this . AssignmentViewModels ) . ToList ( ) ;
7978 this . lv_Assignments . ItemsSource = this . PagingManager . Page ( page ) ;
8079 this . btn_CurrentPage . Content = this . PagingManager . CurrentPage ;
8180 this . btn_allPages . Content = this . PagingManager . MaxPage ;
8281 this . btn_PagingForward . IsEnabled = this . PagingManager . CanPageForward ;
8382 this . btn_PagingBack . IsEnabled = this . PagingManager . CanPageBack ;
8483 }
8584
85+ private IEnumerable < AssignmentViewModel > PipeThroughFilter ( IEnumerable < AssignmentViewModel > viewModels )
86+ {
87+ return viewModels . Where ( GetFilterCondition ( ) ) . ToList ( ) ;
88+ }
89+
90+ private Func < AssignmentViewModel , bool > GetFilterCondition ( )
91+ {
92+ Func < AssignmentViewModel , bool > filterByProject = fav => fav . ProjectId == this . SelectedProject . Id ;
93+ Func < AssignmentViewModel , bool > filterByStatus = fav => this . SelectedFilter . HasFlag ( fav . StatusAsEnum ) ;
94+ Func < AssignmentViewModel , bool > finalCondition = fav => filterByStatus ( fav ) ; ;
95+ if ( this . SelectedProject != null && this . SelectedProject . Id > 0 )
96+ {
97+ finalCondition = fav => filterByProject ( fav ) && filterByStatus ( fav ) ;
98+ }
99+ return finalCondition ;
100+ }
101+
86102 private void btn_NewAssignment_Click ( object sender , RoutedEventArgs e )
87103 {
88104 ShowAddAssignmentDialog ( false ) ;
@@ -92,7 +108,10 @@ private void ShowAddAssignmentDialog(bool editMode)
92108 {
93109 AddAssignment addAssignmentWindow = null ;
94110 var projectLogic = new ProjectLogic ( ) ;
95- var project = projectLogic . GetProjectById ( this . SelectedProject . Id ) ;
111+ int ? projectId = null ;
112+ if ( this . SelectedProject != null ) projectId = this . SelectedProject . Id ;
113+ if ( this . SelectedAssignment != null ) projectId = this . SelectedAssignment . ProjectId ;
114+ var project = projectLogic . GetProjectById ( projectId . Value ) ;
96115 projectLogic . Dispose ( ) ;
97116
98117 if ( editMode )
@@ -168,68 +187,26 @@ private void btn_SetAborted_Click(object sender, RoutedEventArgs e)
168187 }
169188 }
170189
171- [ Refactor ]
172190 private void cb_ProjectSelection_SelectionChanged ( object sender , SelectionChangedEventArgs e )
173191 {
174192 if ( e . AddedItems . Count > 0 )
175193 {
176194 this . SelectedProject = e . AddedItems [ 0 ] as Projects . ProjectViewModel ;
177- if ( this . SelectedProject . Id != 0 && this . SelectedFilter != EnumDefinition . AssignmentStatus . Default )
178- {
179- this . FilteredAssignmentViewModels = this . FilteredAssignmentViewModels
180- . Where ( avm => avm . ProjectId == SelectedProject . Id && avm . StatusAsEnum == this . SelectedFilter ) ;
181- this . LoadFromAllProjects = false ;
182- }
183- else if ( this . SelectedProject . Id == 0 && this . SelectedFilter != EnumDefinition . AssignmentStatus . Default )
184- {
185- this . FilteredAssignmentViewModels = this . AssignmentViewModels . Where ( avm => avm . StatusAsEnum == this . SelectedFilter ) ; ;
186- this . LoadFromAllProjects = true ;
187- }
188- else if ( this . SelectedProject . Id != 0 && this . SelectedFilter == EnumDefinition . AssignmentStatus . Default )
189- {
190- this . FilteredAssignmentViewModels = this . AssignmentViewModels . Where ( avm => avm . ProjectId == this . SelectedProject . Id ) ;
191- }
192- else if ( this . SelectedProject . Id == 0 && this . SelectedFilter == EnumDefinition . AssignmentStatus . Default )
193- {
194- this . FilteredAssignmentViewModels = this . AssignmentViewModels ;
195- }
196195 }
197196
198197 this . btn_NewAssignment . IsEnabled = this . SelectedProject != null && this . SelectedProject . Id != 0 ;
199198 RefreshBindings ( this . PagingManager . CurrentPage ) ;
200199 }
201200
202- [ Refactor ]
203201 private void cb_StatusFilter_SelectionChanged ( object sender , SelectionChangedEventArgs e )
204202 {
205203 if ( e . AddedItems . Count == 0 || this . SelectedProject == null )
206204 {
207- this . FilteredAssignmentViewModels = this . AssignmentViewModels ;
208- return ;
205+ this . SelectedFilter = EnumDefinition . AssignmentStatus . All ;
209206 }
210207 else
211208 {
212- var selectedFilter = GetStatusByItemName ( ( ( e . AddedItems [ 0 ] as ComboBoxItem ) . Name ) ) ;
213- if ( selectedFilter != EnumDefinition . AssignmentStatus . Default && SelectedProject . Id != 0 )
214- {
215- this . FilteredAssignmentViewModels = this . AssignmentViewModels
216- . Where ( avm => avm . StatusAsEnum == selectedFilter &&
217- avm . ProjectId == SelectedProject . Id ) ;
218- }
219- else if ( selectedFilter == EnumDefinition . AssignmentStatus . Default && ! this . LoadFromAllProjects )
220- {
221- this . FilteredAssignmentViewModels = this . AssignmentViewModels
222- . Where ( avm => avm . ProjectId == SelectedProject . Id ) ;
223- }
224- else if ( selectedFilter == EnumDefinition . AssignmentStatus . Default && this . LoadFromAllProjects )
225- {
226- this . FilteredAssignmentViewModels = this . AssignmentViewModels ;
227- }
228- else
229- {
230- this . FilteredAssignmentViewModels = this . AssignmentViewModels
231- . Where ( avm => avm . StatusAsEnum == selectedFilter ) ;
232- }
209+ var selectedFilter = GetStatusByItemName ( ( e . AddedItems [ 0 ] as ComboBoxItem ) . Name ) ;
233210 this . SelectedFilter = selectedFilter ;
234211 }
235212 RefreshBindings ( this . PagingManager . CurrentPage ) ;
@@ -273,7 +250,7 @@ private void btn_DeleteAssignment_Click(object sender, RoutedEventArgs e)
273250 catch ( Exception ex )
274251 {
275252 Logger . LogException ( ex ) ;
276- }
253+ }
277254 }
278255 }
279256
@@ -291,44 +268,44 @@ private void btn_PagingForward_Click(object sender, RoutedEventArgs e)
291268
292269 #region Utility
293270
294- private EnumDefinition . AssignmentStatus GetStatusByItemName ( string itemName )
271+ private EnumDefinition . AssignmentStatus GetStatusByItemName ( string item )
295272 {
296- return itemName switch
273+ return item switch
297274 {
298- "cbi_All" => EnumDefinition . AssignmentStatus . Default ,
275+ "cbi_All" => EnumDefinition . AssignmentStatus . All ,
299276 "cbi_Active" => EnumDefinition . AssignmentStatus . InProgress ,
300277 "cbi_Future" => EnumDefinition . AssignmentStatus . Future ,
301278 "cbi_Done" => EnumDefinition . AssignmentStatus . Done ,
302279 "cbi_Aborted" => EnumDefinition . AssignmentStatus . Aborted ,
303280 "cbi_Postponed" => EnumDefinition . AssignmentStatus . Postponed ,
304- _ => EnumDefinition . AssignmentStatus . Default
281+ _ => EnumDefinition . AssignmentStatus . None
305282 } ;
306283 }
307284
308- private void ShowErrorOnStatusChangeDialog ( )
309- {
310- string title = ResourceStringManager . GetResourceByKey ( "CannotSetDoneOrAbortedErrorTitle" ) ;
311- string message = ResourceStringManager . GetResourceByKey ( "CannotSetDoneErrorMessage" ) ;
312- MessageBox . Show ( message , title , MessageBoxButton . OK , MessageBoxImage . Error ) ;
313- }
285+ private void ShowErrorOnStatusChangeDialog ( )
286+ {
287+ string title = ResourceStringManager . GetResourceByKey ( "CannotSetDoneOrAbortedErrorTitle" ) ;
288+ string message = ResourceStringManager . GetResourceByKey ( "CannotSetDoneErrorMessage" ) ;
289+ MessageBox . Show ( message , title , MessageBoxButton . OK , MessageBoxImage . Error ) ;
290+ }
314291
315- #endregion
292+ #endregion
316293
317- #region Properties
294+ #region Properties
318295
319- public List < Projects . ProjectViewModel > ProjectViewModels { get ; set ; }
320- public IEnumerable < AssignmentViewModel > AssignmentViewModels { get ; set ; }
321- public IEnumerable < AssignmentViewModel > FilteredAssignmentViewModels { get ; set ; }
322- public EnumDefinition . AssignmentStatus SelectedFilter { get ; set ; }
323- public Projects . ProjectViewModel SelectedProject { get ; set ; }
324- public AssignmentViewModel SelectedAssignment { get ; set ; }
325- public bool LoadFromAllProjects { get ; set ; }
326- public PagingManager < AssignmentViewModel > PagingManager { get ; set ; }
296+ public List < Projects . ProjectViewModel > ProjectViewModels { get ; set ; }
297+ public IEnumerable < AssignmentViewModel > AssignmentViewModels { get ; set ; }
298+ public IEnumerable < AssignmentViewModel > FilteredAssignmentViewModels { get ; set ; }
299+ public EnumDefinition . AssignmentStatus SelectedFilter { get ; set ; }
300+ public Projects . ProjectViewModel SelectedProject { get ; set ; }
301+ public AssignmentViewModel SelectedAssignment { get ; set ; }
302+ public bool LoadFromAllProjects { get ; set ; }
303+ public PagingManager < AssignmentViewModel > PagingManager { get ; set ; }
327304
328305
329306
330- #endregion
307+ #endregion
331308
332-
333- }
309+
310+ }
334311}
0 commit comments