HotelsViewModel.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using ReactiveUI;
  5. using TourAgent.Models;
  6. namespace TourAgent.ViewModels
  7. {
  8. public class HotelsViewModel : ReactiveObject
  9. {
  10. UP_SmirnovContext DB = new UP_SmirnovContext();
  11. private List<Hotel>? _hotels;
  12. public List<Hotel>? Hotels { get => _hotels; set => this.RaiseAndSetIfChanged(ref _hotels, value); }
  13. private int countElements = 10;
  14. public int CountElements { get => countElements; set => this.RaiseAndSetIfChanged(ref countElements, value); }
  15. private int _page = 1;
  16. public int Page { get => _page; set => this.RaiseAndSetIfChanged(ref _page, value); }
  17. private bool _enableOrNotRight = true;
  18. public bool EnableOrNotRight { get => _enableOrNotRight; set => this.RaiseAndSetIfChanged(ref _enableOrNotRight, value); }
  19. private bool _enableOrNotLeft = false;
  20. public bool EnableOrNotLeft { get => _enableOrNotLeft; set => this.RaiseAndSetIfChanged(ref _enableOrNotLeft, value); }
  21. int flagElementCount = 0;
  22. public HotelsViewModel()
  23. {
  24. Hotels = DB.Hotels.ToList();
  25. Hotels = Hotels.Take(CountElements).ToList();
  26. }
  27. public void NextPage()
  28. {
  29. Page++;
  30. Hotels = DB.Hotels.ToList();
  31. Hotels = Hotels.Skip((Page - 1) * CountElements).ToList();
  32. flagElementCount = Hotels.Count();
  33. Hotels = Hotels.Take(CountElements).ToList();
  34. EnableOrNotLeft = true;
  35. if (flagElementCount <= CountElements)
  36. {
  37. EnableOrNotRight = false;
  38. }
  39. else
  40. {
  41. EnableOrNotRight = true;
  42. }
  43. }
  44. public void PrevPage()
  45. {
  46. Page--;
  47. Hotels = DB.Hotels.ToList();
  48. Hotels = Hotels.Skip((Page-1)*CountElements).ToList();
  49. Hotels = Hotels.Take(CountElements).ToList();
  50. EnableOrNotRight = true;
  51. if (Page == 1)
  52. {
  53. EnableOrNotLeft = false;
  54. }
  55. else
  56. {
  57. EnableOrNotLeft = true;
  58. }
  59. }
  60. public void ToFirstPage()
  61. {
  62. Page = 1;
  63. Hotels = DB.Hotels.ToList();
  64. Hotels = Hotels.Take(CountElements).ToList();
  65. EnableOrNotLeft = false;
  66. EnableOrNotRight = true;
  67. }
  68. public void ToLastPage()
  69. {
  70. Hotels = DB.Hotels.ToList();
  71. Page = Hotels.Count / CountElements + 1;
  72. Hotels = Hotels.Skip(Page * CountElements).ToList();
  73. Hotels = Hotels.Take(CountElements).ToList();
  74. EnableOrNotRight = false;
  75. EnableOrNotLeft = true;
  76. }
  77. }
  78. }