From 6098ed740b664626537a7fb63f766f5814781b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Moudr=C3=BD?= <62186860+MoudryDaniel@users.noreply.github.com> Date: Tue, 5 Dec 2023 20:06:17 +0100 Subject: [PATCH] Added view for place detail (#14) * Added view for place detail * Place detail view * Page viewmodels renaming --- .../Extensions/ServiceCollectionExtensions.cs | 9 ++- .../ShareLoc.Client.App/Models/PlaceModel.cs | 6 ++ .../ViewModels/CreatePlaceViewModel.cs | 56 +++++++------- .../ViewModels/MainMenuViewModel.cs | 6 +- .../ViewModels/MyGuessesViewModel.cs | 3 +- .../ViewModels/MyPlacesViewModel.cs | 4 +- .../ViewModels/PlaceDetailPageViewModel.cs | 5 ++ .../ViewModels/PlaceDetailViewModel.cs | 74 ++++++++++++++++++- .../Views/Pages/CreatePlacePage.xaml | 32 ++------ .../Views/Pages/CreatePlacePage.xaml.cs | 31 +------- .../Views/Pages/MainMenuPage.xaml | 5 +- .../Views/Pages/MainMenuPage.xaml.cs | 2 +- .../Views/Pages/MyGuessesPage.xaml | 1 + .../Views/Pages/MyPlacesPage.xaml | 4 +- .../Views/Pages/PlaceDetailPage.xaml | 2 +- .../Views/PlaceDetailView.xaml | 31 ++++++++ .../Views/PlaceDetailView.xaml.cs | 34 +++++++++ .../Services/LocalDbService.cs | 10 ++- 18 files changed, 207 insertions(+), 108 deletions(-) create mode 100644 src/Client/ShareLoc.Client.App/ViewModels/PlaceDetailPageViewModel.cs create mode 100644 src/Client/ShareLoc.Client.App/Views/PlaceDetailView.xaml create mode 100644 src/Client/ShareLoc.Client.App/Views/PlaceDetailView.xaml.cs diff --git a/src/Client/ShareLoc.Client.App/Extensions/ServiceCollectionExtensions.cs b/src/Client/ShareLoc.Client.App/Extensions/ServiceCollectionExtensions.cs index 4fb82a9..a44ace0 100644 --- a/src/Client/ShareLoc.Client.App/Extensions/ServiceCollectionExtensions.cs +++ b/src/Client/ShareLoc.Client.App/Extensions/ServiceCollectionExtensions.cs @@ -2,6 +2,7 @@ using ShareLoc.Client.App.Services; using ShareLoc.Client.App.ViewModels; +using ShareLoc.Client.App.Views; using ShareLoc.Client.App.Views.Pages; using ShareLoc.Client.DAL; @@ -11,14 +12,16 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddViewsAndViewModels(this IServiceCollection services) { - services.AddScoped(); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(); services.AddTransient(); services.AddTransient(); + return services; } diff --git a/src/Client/ShareLoc.Client.App/Models/PlaceModel.cs b/src/Client/ShareLoc.Client.App/Models/PlaceModel.cs index a19f644..be0c423 100644 --- a/src/Client/ShareLoc.Client.App/Models/PlaceModel.cs +++ b/src/Client/ShareLoc.Client.App/Models/PlaceModel.cs @@ -23,4 +23,10 @@ public PlaceModel(PlaceEntity placeEntity) public DateTime CreatedUTC => _placeEntity.CratedUTC; public bool IsShared => _placeEntity.IsShared; + + public double Latitude => _placeEntity.Latitude; + + public double Longitude => _placeEntity.Longitude; + + public string Message => _placeEntity.Message; } diff --git a/src/Client/ShareLoc.Client.App/ViewModels/CreatePlaceViewModel.cs b/src/Client/ShareLoc.Client.App/ViewModels/CreatePlaceViewModel.cs index 284d49f..22bd043 100644 --- a/src/Client/ShareLoc.Client.App/ViewModels/CreatePlaceViewModel.cs +++ b/src/Client/ShareLoc.Client.App/ViewModels/CreatePlaceViewModel.cs @@ -5,7 +5,8 @@ using ShareLoc.Client.BL.Services; using ShareLoc.Shared.Common.Models; using CommunityToolkit.Mvvm.ComponentModel; -using System.Diagnostics; +using ShareLoc.Client.App.Models; +using ShareLoc.Client.DAL.Entities; namespace ShareLoc.Client.App.ViewModels; public sealed partial class CreatePlaceViewModel : BaseViewModel @@ -16,50 +17,41 @@ public sealed partial class CreatePlaceViewModel : BaseViewModel private readonly LocalDbService _localDbService; [ObservableProperty] - public ImageSource? photo; + private PlaceDetailViewModel _placeDetailViewModel; [ObservableProperty] - public string mapHtml = ""; - [ObservableProperty] - public string message = "Guess where I am!"; + private string _message = string.Empty; - public Location? Location - { - get => _location; - set - { - _location = value; - MapHtml = GenerateHtml(); - } - } - private Location? _location; - private byte[]? _photoBytes; - private PlaceRequest? _placeRequest; - private Guid? _localPlaceId; + private PlaceRequest? _placeRequest = null; + private byte[]? _photoBytes = null; - public CreatePlaceViewModel(ApiClient apiClient, INavigationService navigationService, LocalDbService localDbService, IAlertService alertService) + public CreatePlaceViewModel(ApiClient apiClient, INavigationService navigationService, LocalDbService localDbService, IAlertService alertService, PlaceDetailViewModel placeDetailViewModel) { _apiClient = apiClient; _navigationService = navigationService; _localDbService = localDbService; _alertService = alertService; + + PlaceDetailViewModel = placeDetailViewModel; } protected override async Task LoadAsync(CancellationToken ct) { + Location? location = null; + ImageSource? photo = null; await MainThread.InvokeOnMainThreadAsync(async () => { - Location = await GetLocation(); - Photo = await TakePhoto(); + location = await GetLocation(); + photo = await TakePhoto(); }); - if (Location is null) + if (location is null) { await _alertService.ShowAlertAsync("Error", "Location not found", "OK"); await _navigationService!.ReturnToRootAsync(); return; } - if (Photo is null) + if (photo is null) { await _navigationService!.ReturnToRootAsync(); return; @@ -68,21 +60,23 @@ await MainThread.InvokeOnMainThreadAsync(async () => _placeRequest = new PlaceRequest() { Image = _photoBytes!, - Latitude = Location!.Latitude, - Longitude = Location!.Longitude, - Message = Message, + Latitude = location!.Latitude, + Longitude = location!.Longitude, + Message = PlaceDetailViewModel.Message! }; - _localPlaceId = await SavePlaceRequest(); - if (_localPlaceId is null) + PlaceEntity? placeEntity = await SavePlaceRequest(); + if (placeEntity is null) { await _alertService.ShowAlertAsync("Error", "Error while creating place", "OK"); await _navigationService!.ReturnToRootAsync(); return; } + + PlaceDetailViewModel.PlaceModel = new PlaceModel(placeEntity); } - private async Task SavePlaceRequest() + private async Task SavePlaceRequest() { var response = await _localDbService.SavePlaceAsync(_placeRequest!); if (!response.IsT0) @@ -110,7 +104,7 @@ function getMarkerObject(color) {{ }} function loadMap() {{ - const center = SMap.Coords.fromWGS84('{Location!.Longitude.ToString()}', '{Location!.Latitude.ToString()}'); + const center = SMap.Coords.fromWGS84('{_placeRequest!.Longitude}', '{_placeRequest.Latitude}'); map = new SMap(JAK.gel(""map""), center, 12); map.addDefaultLayer(SMap.DEF_BASE).enable(); @@ -177,7 +171,7 @@ public async Task ShareUrl() response.Switch( async (serverId) => { - await _localDbService.SharePlaceAsync(_localPlaceId!.Value, serverId, DateTime.UtcNow); + await _localDbService.SharePlaceAsync(PlaceDetailViewModel.PlaceModel!.LocalId, serverId, DateTime.UtcNow); await Share.RequestAsync(new ShareTextRequest { // eg. a2934fa2-6f7e-4ac9-8210-681814ac86c4 diff --git a/src/Client/ShareLoc.Client.App/ViewModels/MainMenuViewModel.cs b/src/Client/ShareLoc.Client.App/ViewModels/MainMenuViewModel.cs index b17f629..cf5579f 100644 --- a/src/Client/ShareLoc.Client.App/ViewModels/MainMenuViewModel.cs +++ b/src/Client/ShareLoc.Client.App/ViewModels/MainMenuViewModel.cs @@ -5,13 +5,13 @@ namespace ShareLoc.Client.App.ViewModels; -public sealed partial class MainMenuViewModel : BaseViewModel +public sealed partial class MainMenuPageViewModel : BaseViewModel { private readonly INavigationService _navigationService; public string AppVersion => AppInfo.Current.VersionString; - public MainMenuViewModel(INavigationService navigationService) + public MainMenuPageViewModel(INavigationService navigationService) { _navigationService = navigationService; } @@ -20,7 +20,7 @@ public MainMenuViewModel(INavigationService navigationService) public Task GoToCreatePlacePage() => _navigationService.GoToAsync(); [RelayCommand] - public Task GoToMyPlacesPage() => _navigationService.GoToAsync(); + public Task GoToMyPlacesPage() => _navigationService.GoToAsync(); [RelayCommand] public Task GoToMyGuessesPage() => _navigationService.GoToAsync(); diff --git a/src/Client/ShareLoc.Client.App/ViewModels/MyGuessesViewModel.cs b/src/Client/ShareLoc.Client.App/ViewModels/MyGuessesViewModel.cs index c6c5c09..5391468 100644 --- a/src/Client/ShareLoc.Client.App/ViewModels/MyGuessesViewModel.cs +++ b/src/Client/ShareLoc.Client.App/ViewModels/MyGuessesViewModel.cs @@ -1,5 +1,6 @@ namespace ShareLoc.Client.App.ViewModels; -public sealed class MyGuessesViewModel : BaseViewModel +public sealed class MyGuessesViewModel + : BaseViewModel { } diff --git a/src/Client/ShareLoc.Client.App/ViewModels/MyPlacesViewModel.cs b/src/Client/ShareLoc.Client.App/ViewModels/MyPlacesViewModel.cs index 72e658d..074d425 100644 --- a/src/Client/ShareLoc.Client.App/ViewModels/MyPlacesViewModel.cs +++ b/src/Client/ShareLoc.Client.App/ViewModels/MyPlacesViewModel.cs @@ -8,7 +8,7 @@ namespace ShareLoc.Client.App.ViewModels; -public sealed partial class MyPlacesViewModel : BaseViewModel +public sealed partial class MyPlacesPageViewModel : BaseViewModel { private readonly LocalDbService _dbService; private readonly INavigationService _navigationService; @@ -17,7 +17,7 @@ public sealed partial class MyPlacesViewModel : BaseViewModel [ObservableProperty] private List _myPlaces = []; - public MyPlacesViewModel(INavigationService navigationService, LocalDbService dbService, PlaceDetailViewModel placeDetailViewModel) + public MyPlacesPageViewModel(INavigationService navigationService, LocalDbService dbService, PlaceDetailViewModel placeDetailViewModel) { _dbService = dbService; _navigationService = navigationService; diff --git a/src/Client/ShareLoc.Client.App/ViewModels/PlaceDetailPageViewModel.cs b/src/Client/ShareLoc.Client.App/ViewModels/PlaceDetailPageViewModel.cs new file mode 100644 index 0000000..e4b96c5 --- /dev/null +++ b/src/Client/ShareLoc.Client.App/ViewModels/PlaceDetailPageViewModel.cs @@ -0,0 +1,5 @@ +namespace ShareLoc.Client.App.ViewModels; + +public sealed partial class PlaceDetailPageViewModel : BaseViewModel +{ +} diff --git a/src/Client/ShareLoc.Client.App/ViewModels/PlaceDetailViewModel.cs b/src/Client/ShareLoc.Client.App/ViewModels/PlaceDetailViewModel.cs index 90bb936..a8b45e0 100644 --- a/src/Client/ShareLoc.Client.App/ViewModels/PlaceDetailViewModel.cs +++ b/src/Client/ShareLoc.Client.App/ViewModels/PlaceDetailViewModel.cs @@ -6,6 +6,78 @@ namespace ShareLoc.Client.App.ViewModels; public sealed partial class PlaceDetailViewModel : BaseViewModel { + private PlaceModel? _placeModel = null; + public PlaceModel? PlaceModel + { + get => _placeModel; + set + { + if (value == null) return; + + SetProperty(ref _placeModel, value, nameof(PlaceModel)); + + MapHtml = GenerateHtml(value.Longitude, value.Latitude); + Image = value.Image; + Message = value.Message; + IsShared = value.IsShared; + } + } + + [ObservableProperty] + private string? _mapHtml = string.Empty; + [ObservableProperty] + private byte[]? _image = null; + [ObservableProperty] + private string? _message = "Guess where I am!"; [ObservableProperty] - private PlaceModel? _placeModel; + private bool _isShared = false; + + private static string GenerateHtml(double longitude, double latitude) + { + return $@" + + + + + + +
+ + + "; + } } diff --git a/src/Client/ShareLoc.Client.App/Views/Pages/CreatePlacePage.xaml b/src/Client/ShareLoc.Client.App/Views/Pages/CreatePlacePage.xaml index 6fb5765..65ff775 100644 --- a/src/Client/ShareLoc.Client.App/Views/Pages/CreatePlacePage.xaml +++ b/src/Client/ShareLoc.Client.App/Views/Pages/CreatePlacePage.xaml @@ -2,38 +2,16 @@ + - - - - - - - - - -