1212using Prism . Services . Dialogs ;
1313using System ;
1414using System . Collections . Generic ;
15+ using System . Collections . ObjectModel ;
1516using System . Linq ;
1617using System . Threading . Tasks ;
1718using System . Windows . Input ;
@@ -20,18 +21,18 @@ namespace dataneo.TutorialLibs.WPF.UI.TutorialList
2021{
2122 internal sealed class TutorialListPageViewModel : BaseViewModel
2223 {
23- private const short TutorialOnPage = 40 ;
24+ private const short TutorialOnPage = 20 ;
2425
2526 private readonly IDialogService _dialogService ;
2627 private readonly ITutorialRespositoryAsync _tutorialRespositoryAsync ;
2728 private readonly ICategoryRespositoryAsync _categoryRespositoryAsync ;
2829 private readonly IAddTutorial _addTutorial ;
2930 private readonly ISettingManager _settingManager ;
3031
31- public readonly CategoryManager CategoriesManager ;
32+ public CategoryManager CategoriesManager { get ; }
3233
33- private IEnumerable < TutorialHeaderDto > tutorials ;
34- public IEnumerable < TutorialHeaderDto > Tutorials
34+ private ObservableCollection < TutorialHeaderDto > tutorials = new ObservableCollection < TutorialHeaderDto > ( ) ;
35+ public ObservableCollection < TutorialHeaderDto > Tutorials
3536 {
3637 get { return tutorials ; }
3738 set { tutorials = value ; RaisePropertyChanged ( ) ; }
@@ -62,6 +63,13 @@ public IEnumerable<CategoryMenuItem> TutorialCategories
6263 set { _tutorialCategories = value ; RaisePropertyChanged ( ) ; }
6364 }
6465
66+ private string searchTitle ;
67+ public string SearchTitle
68+ {
69+ get { return searchTitle ; }
70+ set { searchTitle = value ; RaisePropertyChanged ( ) ; }
71+ }
72+
6573 private short page ;
6674 public short Page
6775 {
@@ -111,6 +119,11 @@ public short TotalPage
111119 public ICommand DeleteTutorialCommand { get ; }
112120 public ICommand ShowTutorialCategoriesCommand { get ; }
113121 public ICommand TutorialCategoriesChangedCommand { get ; }
122+ public ICommand NextPageCommand { get ; }
123+ public ICommand PrevPageCommand { get ; }
124+ public ICommand SearchCommand { get ; }
125+ public ICommand SearchPrevCommand { get ; }
126+ public ICommand PageNoEnterCommand { get ; }
114127
115128 public TutorialListPageViewModel ( IRegionManager regionManager ,
116129 IDialogService dialogService ,
@@ -137,6 +150,11 @@ public TutorialListPageViewModel(IRegionManager regionManager,
137150 this . SetTutorialAsUnWatchedCommand = new Command ( SetTutorialAsUnWatchedCommandImpl ) ;
138151 this . ShowTutorialCategoriesCommand = new Command ( ShowTutorialCategoriesCommandImpl ) ;
139152 this . TutorialCategoriesChangedCommand = new Command ( TutorialCategoriesChangedCommandImpl ) ;
153+ this . NextPageCommand = new Command ( NextPageCommandImplAsync ) ;
154+ this . PrevPageCommand = new Command ( PrevPageCommandImplAsync ) ;
155+ this . SearchCommand = new Command ( SearchCommandImplAsync ) ;
156+ this . SearchPrevCommand = new Command < KeyEventArgs > ( SearchPrevCommandImplAsync ) ;
157+ this . PageNoEnterCommand = new Command < KeyEventArgs > ( PageNoEnterCommandImplAsync ) ;
140158 }
141159
142160 public async override void OnNavigatedTo ( NavigationContext navigationContext )
@@ -190,6 +208,40 @@ private void ShowTutorialCategoriesCommandImpl()
190208 this . TutorialCategories = GetCategoryMenuItem ( this . SelectedTutorial , this . CategoriesManager . Categories ) ;
191209 }
192210
211+ private async void PrevPageCommandImplAsync ( )
212+ {
213+ await Result . Try ( ( ) => LoadTutorialsDtoAsync ( this . Page - 1 ) )
214+ . OnFailure ( error => ShowError ( error ) ) ;
215+ }
216+
217+ private async void NextPageCommandImplAsync ( )
218+ {
219+ await Result . Try ( ( ) => LoadTutorialsDtoAsync ( this . Page + 1 ) )
220+ . OnFailure ( error => ShowError ( error ) ) ;
221+ }
222+
223+ private async void SearchCommandImplAsync ( )
224+ {
225+ await Result . Try ( LoadTutorialsDtoAsync )
226+ . OnFailure ( error => ShowError ( error ) ) ;
227+ }
228+
229+ private async void SearchPrevCommandImplAsync ( KeyEventArgs obj )
230+ {
231+ if ( obj . Key != Key . Enter )
232+ return ;
233+ await Result . Try ( LoadTutorialsDtoAsync )
234+ . OnFailure ( error => ShowError ( error ) ) ;
235+ }
236+
237+ private async void PageNoEnterCommandImplAsync ( KeyEventArgs obj )
238+ {
239+ if ( obj . Key != Key . Enter )
240+ return ;
241+ await Result . Try ( ( ) => LoadTutorialsDtoAsync ( this . Page ) )
242+ . OnFailure ( error => ShowError ( error ) ) ;
243+ }
244+
193245 private IReadOnlyList < CategoryMenuItem > GetCategoryMenuItem ( TutorialHeaderDto tutorialHeaderDto ,
194246 IEnumerable < CategoryMenuItem > categoryMenuItems )
195247 {
@@ -231,8 +283,11 @@ private async void TutorialCategoriesChangedCommandImpl()
231283 private void UpdateTutorialDtoCategories ( IReadOnlyList < Category > newTutorialCategories , TutorialHeaderDto tutorialHeaderDto )
232284 {
233285 var newTutorialDto = tutorialHeaderDto with { Categories = newTutorialCategories } ;
234- this . Tutorials = this . Tutorials . Select ( s => s == tutorialHeaderDto ? newTutorialDto : s )
286+ var newTutorials = this . Tutorials . Select ( s => s == tutorialHeaderDto ? newTutorialDto : s )
235287 . ToArray ( ) ;
288+
289+ this . Tutorials . Clear ( ) ;
290+ this . Tutorials . AddRange ( newTutorials ) ;
236291 }
237292
238293 private async Task UpdateTutorialCategoriesAsync ( IReadOnlyList < Category > newTutorialCategories , TutorialHeaderDto tutorialHeaderDto )
@@ -245,33 +300,41 @@ private async Task UpdateTutorialCategoriesAsync(IReadOnlyList<Category> newTuto
245300 private async Task LoadCategoriesAndTutorialsonSelectionChangeAsync ( )
246301 {
247302 await this . CategoriesManager . LoadCategoriesAsync ( ) ;
248- await LoadTutorialsDtoAsync ( ) ;
303+ await LoadTutorialsDtoAsync ( this . Page ) ;
249304 }
250305
251- private async Task LoadTutorialsDtoAsync ( )
306+ private Task LoadTutorialsDtoAsync ( )
307+ => LoadTutorialsDtoAsync ( Maybe < int > . None ) ;
308+
309+ private async Task LoadTutorialsDtoAsync ( Maybe < int > page )
252310 {
253- var specificationCount = GetSpecificationAccToFilterSelect ( ) ;
254- var totalRecords = await this . _tutorialRespositoryAsync . CountAsync ( specificationCount ) ;
255- this . TotalPage = ( short ) Math . Ceiling ( totalRecords / ( double ) TutorialOnPage ) ;
256- this . Page = 1 ;
311+ if ( page . HasNoValue )
312+ {
313+ var specificationCount = GetSpecificationAccToFilterSelect ( ) ;
314+ var totalRecords = await this . _tutorialRespositoryAsync . CountAsync ( specificationCount ) ;
315+ this . TotalPage = ( short ) Math . Ceiling ( totalRecords / ( double ) TutorialOnPage ) ;
316+ }
317+
318+ this . Page = ( short ) page . GetValueOrDefault ( 1 ) ;
257319 var specification = GetSpecificationAccToFilterSelect ( this . Page ) ;
258- this . Tutorials = await this . _tutorialRespositoryAsync . GetAllTutorialHeadersDtoAsync ( specification ) ;
320+ this . Tutorials . Clear ( ) ;
321+ this . Tutorials . AddRange ( await this . _tutorialRespositoryAsync . GetAllTutorialHeadersDtoAsync ( specification ) ) ;
259322 }
260323
261324 private ISpecification < Tutorial > GetSpecificationAccToFilterSelect ( short ? page = null )
262325 {
263326 var specBuilder = new SpecificationBuilder ( ) ;
264327 specBuilder
265328 . FilterByCategories ( this . CategoriesManager . GetFilteredCategories ( ) ,
266- this . CategoriesManager . IsFilterByNoCategory ( ) ) ;
329+ this . CategoriesManager . IsFilterByNoCategory ( ) )
330+ . TutorialTitleSearch ( this . SearchTitle ) ;
267331
268332 if ( page . HasValue )
269333 {
270334 specBuilder . Page ( page . Value , TutorialOnPage )
271335 . OrderBy ( this . SelectedTutorialsOrderType ) ;
272336 }
273337
274-
275338 return specBuilder . GetSpecification ( ) ;
276339 }
277340
0 commit comments