ShowHotelsPageViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Avalonia.Threading;
  2. using AvaloniaApplicationTest.Models;
  3. using ReactiveUI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace AvaloniaApplicationTest.ViewModels
  10. {
  11. public class ShowHotelsPageViewModel : ReactiveObject
  12. {
  13. public List<Hotel> LBHotels
  14. {
  15. get => lBHotels;
  16. set => this.RaiseAndSetIfChanged(ref lBHotels, value);
  17. }
  18. public string HowManyShow
  19. {
  20. get
  21. {
  22. return GetShowed();
  23. }
  24. set => this.RaiseAndSetIfChanged(ref text1, value);
  25. }
  26. public string PageNumber
  27. {
  28. get
  29. {
  30. return GetPages();
  31. }
  32. set => this.RaiseAndSetIfChanged(ref text2, value);
  33. }
  34. public int UserCount
  35. {
  36. get => user_count;
  37. set {
  38. this.RaiseAndSetIfChanged(ref user_count, value);
  39. try
  40. {
  41. all_page_number = Convert.ToInt32(Math.Round((double)MainWindowViewModel.myConnection.Hotels.Count() / user_count, MidpointRounding.ToPositiveInfinity));
  42. LBHotels = MainWindowViewModel.myConnection.Hotels.Take(value).ToList();
  43. }
  44. catch { }
  45. }
  46. }
  47. public bool CanBack
  48. {
  49. get => visibility_b;
  50. set => this.RaiseAndSetIfChanged(ref visibility_b, value);
  51. }
  52. public bool CanNext
  53. {
  54. get => visibility_n;
  55. set => this.RaiseAndSetIfChanged(ref visibility_n, value);
  56. }
  57. int page_number = 1;
  58. int all_page_number = Convert.ToInt32(Math.Round((double)MainWindowViewModel.myConnection.Hotels.Count()/ 10, MidpointRounding.ToPositiveInfinity));
  59. int all_count = MainWindowViewModel.myConnection.Hotels.Count();
  60. List<Hotel> lBHotels = MainWindowViewModel.myConnection.Hotels.Take(10).ToList();
  61. bool visibility_b = false;
  62. bool visibility_n = true;
  63. string text1 = "";
  64. string text2 = "";
  65. int user_count = 10;
  66. int count_showed = 10;
  67. string GetShowed()
  68. {
  69. string text = count_showed + " / " + all_count + " отелей";
  70. return text;
  71. }
  72. string GetPages()
  73. {
  74. all_page_number = Convert.ToInt32(Math.Round((double)MainWindowViewModel.myConnection.Hotels.Count() / user_count, MidpointRounding.ToPositiveInfinity));
  75. string text = page_number + " / " + all_page_number;
  76. return text;
  77. }
  78. public void Next()
  79. {
  80. CanBack = true;
  81. int count = (all_count - count_showed);
  82. if (count<= user_count)
  83. {
  84. CanNext = false;
  85. }
  86. else
  87. {
  88. count = user_count;
  89. }
  90. page_number++;
  91. LBHotels = MainWindowViewModel.myConnection.Hotels.Skip(count_showed).Take(count).ToList();
  92. count_showed += count;
  93. HowManyShow = GetShowed();
  94. PageNumber = GetPages();
  95. }
  96. public void Back()
  97. {
  98. CanNext = true;
  99. if (page_number == 2)
  100. {
  101. CanBack = false;
  102. }
  103. int count = user_count;
  104. if (page_number == all_page_number)
  105. {
  106. count = all_count - (page_number - 1)* user_count + user_count;
  107. }
  108. page_number--;
  109. count_showed = page_number * user_count;
  110. HowManyShow = GetShowed();
  111. PageNumber = GetPages();
  112. LBHotels = MainWindowViewModel.myConnection.Hotels.Skip(count_showed-count).Take(user_count).ToList();
  113. }
  114. }
  115. }