OtelsViewModel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls;
  5. using Microsoft.EntityFrameworkCore;
  6. using MsBox.Avalonia;
  7. using MsBox.Avalonia.Enums;
  8. using ReactiveUI;
  9. using TourAgent.Models;
  10. using TourAgent.Views;
  11. namespace MPrakt.ViewModels
  12. {
  13. public class OtelsViewModel : ReactiveObject
  14. {
  15. UP_SmirnovContext DB = new UP_SmirnovContext();
  16. UserControl us = new TourAgent.Views.Menu();
  17. public UserControl US
  18. {
  19. get => us;
  20. set => this.RaiseAndSetIfChanged(ref us, value);
  21. }
  22. private List<Otel>? _hotels;//листы отелей
  23. public List<Otel>? Hotels { get => _hotels; set => this.RaiseAndSetIfChanged(ref _hotels, value); }
  24. private int countElements = 10;
  25. public int CountElements { get => countElements; set => this.RaiseAndSetIfChanged(ref countElements, value); }
  26. private int _page = 1;
  27. public int Page { get => _page; set => this.RaiseAndSetIfChanged(ref _page, value); }
  28. private bool _enableOrNotRight = true;
  29. public bool EnableOrNotRight { get => _enableOrNotRight; set => this.RaiseAndSetIfChanged(ref _enableOrNotRight, value); }
  30. private bool _enableOrNotLeft = false;
  31. public bool EnableOrNotLeft { get => _enableOrNotLeft; set => this.RaiseAndSetIfChanged(ref _enableOrNotLeft, value); }
  32. int flagElementCount = 0;
  33. public void ChangePageToHotels()
  34. {
  35. US = new Hotels();
  36. }
  37. public void ChangePageToTours()
  38. {
  39. US = new TourAgent.Views.Hotels();
  40. }
  41. public void ToMenu()
  42. {
  43. US = new TourAgent.Views.Menu();
  44. }
  45. public OtelsViewModel()
  46. {
  47. Hotels = DB.Hotels.ToList();//вывод первых записей
  48. Hotels = Hotels.Take(CountElements).ToList();
  49. }
  50. public void NextPage()//переход вперёд
  51. {
  52. Page++;
  53. Hotels = DB.Hotels.ToList();
  54. Hotels = Hotels.Skip((Page - 1) * CountElements).ToList();
  55. flagElementCount = Hotels.Count();
  56. Hotels = Hotels.Take(CountElements).ToList();
  57. EnableOrNotLeft = true;
  58. if (flagElementCount <= CountElements)
  59. {
  60. EnableOrNotRight = false;
  61. }
  62. else
  63. {
  64. EnableOrNotRight = true;
  65. }
  66. }
  67. public void PrevPage()//Переход назад
  68. {
  69. Page--;
  70. Hotels = DB.Hotels.ToList();
  71. Hotels = Hotels.Skip((Page-1)*CountElements).ToList();
  72. Hotels = Hotels.Take(CountElements).ToList();
  73. EnableOrNotRight = true;
  74. if (Page == 1)
  75. {
  76. EnableOrNotLeft = false;
  77. }
  78. else
  79. {
  80. EnableOrNotLeft = true;
  81. }
  82. }
  83. public void ToFirstPage()//Переход в начало
  84. {
  85. Page = 1;
  86. Hotels = DB.Hotels.ToList();
  87. Hotels = Hotels.Take(CountElements).ToList();
  88. EnableOrNotLeft = false;
  89. EnableOrNotRight = true;
  90. }
  91. public void ToLastPage()//Переход в конец
  92. {
  93. Hotels = DB.Hotels.ToList();
  94. Page = Hotels.Count / CountElements + 1;
  95. Hotels = Hotels.Skip(Page * CountElements).ToList();
  96. Hotels = Hotels.Take(CountElements).ToList();
  97. EnableOrNotRight = false;
  98. EnableOrNotLeft = true;
  99. }
  100. public override bool Equals(object? obj)
  101. {
  102. return base.Equals(obj);
  103. }
  104. public override int GetHashCode()
  105. {
  106. return base.GetHashCode();
  107. }
  108. public override string? ToString()
  109. {
  110. return base.ToString();
  111. }
  112. }
  113. }