RoomsPage.xaml.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. namespace practiceApplicaiton;
  2. public partial class RoomsPage : ContentPage
  3. {
  4. private Room? _selected;
  5. public RoomsPage() => InitializeComponent();
  6. protected override async void OnAppearing()
  7. {
  8. base.OnAppearing();
  9. await LoadAsync();
  10. }
  11. private async Task LoadAsync()
  12. {
  13. try { RoomsList.ItemsSource = await Repository.GetRoomsAsync(); }
  14. catch (Exception ex) { await DisplayAlertAsync("Ошибка", ex.Message, "OK"); }
  15. }
  16. private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  17. {
  18. _selected = e.CurrentSelection.FirstOrDefault() as Room;
  19. EditRatingButton.IsEnabled = _selected != null;
  20. }
  21. private async void OnEditRatingClicked(object sender, EventArgs e)
  22. {
  23. if (_selected == null) return;
  24. var result = await DisplayPromptAsync(
  25. "Рейтинг комнаты",
  26. $"Введите новый рейтинг для комнаты №{_selected.RoomNumber} (0.0 — 5.0):",
  27. initialValue: _selected.Rating.ToString("F1"),
  28. keyboard: Keyboard.Numeric);
  29. if (result == null) return;
  30. if (!decimal.TryParse(result.Replace(',', '.'),
  31. System.Globalization.NumberStyles.Any,
  32. System.Globalization.CultureInfo.InvariantCulture, out var rating)
  33. || rating < 0 || rating > 5)
  34. {
  35. await DisplayAlertAsync("Ошибка", "Рейтинг должен быть от 0.0 до 5.0", "OK");
  36. return;
  37. }
  38. try
  39. {
  40. await Repository.UpdateRoomRatingAsync(_selected.RoomId, rating);
  41. await LoadAsync();
  42. }
  43. catch (Exception ex) { await DisplayAlertAsync("Ошибка", ex.Message, "OK"); }
  44. }
  45. }