From 5c85ce545f818b7985a94b5c7688a4d1628b31b0 Mon Sep 17 00:00:00 2001 From: Max Mannstein Date: Sun, 24 Aug 2025 14:20:19 +0200 Subject: [PATCH] Refactor HomeWalkerViewModel and HomeWalkerView to manage and display the first 10 public walk requests; remove unused properties and commands for improved performance and clarity. --- .../ViewModels/HomeWalkerViewModel.cs | 55 +-- .../gehGassiApp/Views/HomeWalkerView.xaml | 329 ++++++++---------- .../gehGassiApp/Views/HomeWalkerView.xaml.cs | 79 +---- 3 files changed, 149 insertions(+), 314 deletions(-) diff --git a/gehGassiApp/gehGassiApp/ViewModels/HomeWalkerViewModel.cs b/gehGassiApp/gehGassiApp/ViewModels/HomeWalkerViewModel.cs index 7b08141..74ebcbd 100644 --- a/gehGassiApp/gehGassiApp/ViewModels/HomeWalkerViewModel.cs +++ b/gehGassiApp/gehGassiApp/ViewModels/HomeWalkerViewModel.cs @@ -112,6 +112,9 @@ namespace gehGassiApp.ViewModels [ObservableProperty] private ObservableCollection _publicWalkRequests; + [ObservableProperty] + private ObservableCollection _first10PublicWalkRequests; + [ObservableProperty] private PublicWalkRequestWithNamesAndResponseStatus _selectedPublicWalkRequest; @@ -124,9 +127,6 @@ namespace gehGassiApp.ViewModels [ObservableProperty] private double _nextWalkWidth; - [ObservableProperty] - private double _refreshGridHeight; - [ObservableProperty] private Thickness _bannerMargin; @@ -148,6 +148,7 @@ namespace gehGassiApp.ViewModels _skip = 0; SelectedPublicWalkRequest = null; PublicWalkRequests = null; + First10PublicWalkRequests = null; await LoadPublicWalkRequestsAsync(); IsBusy = false; PublicWalkRequestsRefreshing = false; @@ -245,49 +246,6 @@ namespace gehGassiApp.ViewModels await Shell.Current.GoToAsync($"{nameof(WalkWalkerView)}?id={id}&menu={menu}", Core.Common.Constants.AnimateNavigation); } - /// - /// Command für das Laden weiterer Listungen - /// - [RelayCommand] - public async Task LoadMore() - { - if (PublicWalkRequests != null && PublicWalkRequests.Count < _total && !IsBusy) - { - try - { - IsBusy = true; - - var sortOrders = new List() { new() { Property = "UpdatedAt", SortDirection = SortDirection.Descending } }; - - using var cts = new CancellationTokenSource(Core.Common.Constants.ListTimeout); - var publicWalkRequestsResult = await _publicWalkRequestService.GetWithResponseStatusAsync(App.CurrentAppUser.Id, string.Empty, string.Empty, PublicWalkRequestStatus.Open, App.CurrentAppUser.Id, PublicWalkResponseStatus.Awaiting, false, sortOrders, CurrentLocation, false, _take, _skip, App.AppMode, SelectedLanguage, App.CurrentUser.AccessToken, cts.Token); - if (publicWalkRequestsResult.Success) - { - foreach (var request in publicWalkRequestsResult.Value) - { - if (CurrentLocation.Lat != null && CurrentLocation.Lng != null) - request.Distance = Location.CalculateDistance(CurrentLocation.Lat.Value, CurrentLocation.Lng.Value, request.Lat, request.Lng, DistanceUnits.Kilometers); - request.HasResponse = await _publicWalkResponseService.HasResponseAsync(App.CurrentAppUser.Id, request.Id); - PublicWalkRequests.Add(request); - } - } - - _total = publicWalkRequestsResult.Total; - _skip = publicWalkRequestsResult.Skip + publicWalkRequestsResult.Take; - - IsBusy = false; - } - catch (Exception ex) - { - await TrackErrorAsync(ex, true); - } - finally - { - IsBusy = false; - } - } - } - /// /// Command für das Blockieren eines Hundebesitzers über die öffentliche Anfrage /// @@ -513,6 +471,9 @@ namespace gehGassiApp.ViewModels } PublicWalkRequests = publicWalkRequestsResult.Value.OrderBy(c => c.Distance).ToObservableCollection(); + + // Erste 10 für die Home-Ansicht + First10PublicWalkRequests = PublicWalkRequests.Take(10).ToObservableCollection(); _total = publicWalkRequestsResult.Total; _skip = publicWalkRequestsResult.Skip + publicWalkRequestsResult.Take; @@ -522,6 +483,7 @@ namespace gehGassiApp.ViewModels _total = 0; _skip = 0; PublicWalkRequests = null; + First10PublicWalkRequests = null; } } catch (Exception ex) @@ -530,6 +492,7 @@ namespace gehGassiApp.ViewModels _total = 0; _skip = 0; PublicWalkRequests = null; + First10PublicWalkRequests = null; } } diff --git a/gehGassiApp/gehGassiApp/Views/HomeWalkerView.xaml b/gehGassiApp/gehGassiApp/Views/HomeWalkerView.xaml index daeb681..071ac1f 100644 --- a/gehGassiApp/gehGassiApp/Views/HomeWalkerView.xaml +++ b/gehGassiApp/gehGassiApp/Views/HomeWalkerView.xaml @@ -19,201 +19,150 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - + RowDefinitions="Auto" + VerticalOptions="FillAndExpand"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + diff --git a/gehGassiApp/gehGassiApp/Views/HomeWalkerView.xaml.cs b/gehGassiApp/gehGassiApp/Views/HomeWalkerView.xaml.cs index 48712e4..aca1bb0 100644 --- a/gehGassiApp/gehGassiApp/Views/HomeWalkerView.xaml.cs +++ b/gehGassiApp/gehGassiApp/Views/HomeWalkerView.xaml.cs @@ -10,9 +10,6 @@ public partial class HomeWalkerView : BaseContentPage IDeviceInstallationService _deviceInstallationService; IDeviceInstallationService DeviceInstallationService => _deviceInstallationService ??= ServiceHelper.GetService(); private HomeWalkerViewModel ViewModel => BindingContext as HomeWalkerViewModel; - private bool _isRowHidden = false; - private double _initialRowHeight; - private bool _animationRunning = false; /// /// Erstellt eine Instanz @@ -31,16 +28,12 @@ public partial class HomeWalkerView : BaseContentPage await ViewModel.InitializeAsync(this); var width = DeviceDisplay.Current.MainDisplayInfo.Width / DeviceDisplay.Current.MainDisplayInfo.Density; ViewModel.NextWalkWidth = width * 0.8; - _isRowHidden = false; - _initialRowHeight = 0; - _animationRunning = false; - rootVerticalStackLayout.HeightRequest = -1; + #if IOS if (DeviceInstallationService.NotificationsSupported) { AppDelegate.AskForPermissionForRemoteNotificationsAndRegisteriOS(); } - #endif } @@ -50,74 +43,4 @@ public partial class HomeWalkerView : BaseContentPage base.OnDisappearing(); await ViewModel.DisappearingAsync(this); } - - //Workaround f�r den IOS-Bug der H�henberechnung! Siehe https://github.com/dotnet/maui/issues/8820 - private void rootVerticalStackLayout_SizeChanged(object sender, EventArgs e) - { - if (!_isRowHidden && rootVerticalStackLayout.Height > _initialRowHeight) - _initialRowHeight = rootVerticalStackLayout.Height; - } - - private async void homeCollectionView_Scrolled(object sender, ItemsViewScrolledEventArgs e) - { - if (_animationRunning) - return; - - if (e.VerticalDelta > 0 && e.VerticalOffset >= 50 && !_isRowHidden) - { - await HideHeader(); - } - else if (e.VerticalDelta < 0 && e.VerticalOffset <= 50 && _isRowHidden) - { - await ShowHeader(); - } - } - - private async Task ShowHeader() - { - homeRefreshView.IsEnabled = false; - homeCollectionView.IsEnabled = false; - _isRowHidden = false; - _animationRunning = true; - var animation = new Animation(v => rootVerticalStackLayout.HeightRequest = v, 0, _initialRowHeight); - - MainThread.BeginInvokeOnMainThread(() => - { - animation.Commit(this, "HideRowAnimation", length: 500, easing: Easing.SinIn); - - }); - - await Task.Delay(500); - _animationRunning = false; - homeCollectionView.IsEnabled = true; - homeRefreshView.IsEnabled = true; - } - - private async Task HideHeader() - { - homeRefreshView.IsEnabled = false; - homeCollectionView.IsEnabled = false; - _isRowHidden = true; - _animationRunning = true; - var animation = new Animation(v => rootVerticalStackLayout.HeightRequest = v, _initialRowHeight, 0); - - MainThread.BeginInvokeOnMainThread(() => - { - animation.Commit(this, "HideRowAnimation", length: 500, easing: Easing.SinOut); - - }); - - await Task.Delay(500); - _animationRunning = false; - homeCollectionView.IsEnabled = true; - homeRefreshView.IsEnabled = true; - } - - private async void RefreshView_Refreshing(object sender, EventArgs e) - { - if (_isRowHidden) - { - await ShowHeader(); - } - } } \ No newline at end of file