From d3c9dcb20814e5b5b4ee70c3b9bdde6f92e32b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Fri, 5 Sep 2025 14:52:08 +0200 Subject: [PATCH] =?UTF-8?q?B=C3=B6rjat=20p=C3=A5=20RoundBuilding=20avsnitt?= =?UTF-8?q?et=20,=20skall=20=C3=A4ndra=20save-svar=20fr=C3=A5n=20bool=20ti?= =?UTF-8?q?ll=20int?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interfaces/IMethodSharingService.cs | 9 + .../HelperEntities/RoundBuilderElement.cs | 16 ++ .../ViewModelClasses/MethodSharingService.cs | 37 ++++ .../ViewModelClasses/ParticipantViewModel.cs | 31 ++-- .../RoundStartingViewModel.cs | 165 ++++++++++++++++++ .../ParticipantViewModelCommands.cs | 2 +- .../RoundStartingViewModelCommands.cs | 64 +++++++ GreadyPoang/MauiProgram.cs | 10 ++ GreadyPoang/Views/RoundStartingView.xaml | 41 ++++- GreadyPoang/Views/RoundStartingView.xaml.cs | 27 ++- 10 files changed, 373 insertions(+), 29 deletions(-) create mode 100644 Common.Library/Interfaces/IMethodSharingService.cs create mode 100644 GreadyPoang.EntityLayer/HelperEntities/RoundBuilderElement.cs create mode 100644 GreadyPoang.ViewModelLayer/ViewModelClasses/MethodSharingService.cs create mode 100644 GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs create mode 100644 GreadyPoang/CommandClasses/RoundStartingViewModelCommands.cs diff --git a/Common.Library/Interfaces/IMethodSharingService.cs b/Common.Library/Interfaces/IMethodSharingService.cs new file mode 100644 index 0000000..3b8c11d --- /dev/null +++ b/Common.Library/Interfaces/IMethodSharingService.cs @@ -0,0 +1,9 @@ +using System.Collections.ObjectModel; + +namespace Common.Library +{ + public interface IMethodSharingService + { + ObservableCollection Get(); + } +} diff --git a/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderElement.cs b/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderElement.cs new file mode 100644 index 0000000..a486295 --- /dev/null +++ b/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderElement.cs @@ -0,0 +1,16 @@ +using Common.Library; + +namespace GreadyPoang.EntityLayer; + +public class RoundBuilderElement : EntityBase +{ + public int ParticipantId { get; set; } + public string ParticipantName { get; set; } = string.Empty; + public int GameRoundRegNr { get; set; } + public int GameRegPoints { get; set; } + public int GameRoundPoints { get; set; } + public GamePointStatus Status { get; set; } + public DateTime GameRoundStartDate { get; set; } + public int GameRoundId { get; set; } + +} diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/MethodSharingService.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/MethodSharingService.cs new file mode 100644 index 0000000..46a95c2 --- /dev/null +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/MethodSharingService.cs @@ -0,0 +1,37 @@ +using Common.Library; +using GreadyPoang.EntityLayer; +using System.Collections.ObjectModel; + +namespace GreadyPoang.ViewModelLayer; + +public class MethodSharingService : ViewModelBase, IMethodSharingService +{ + private readonly IRepository _repository; + + public MethodSharingService(IRepository repository) + { + _repository = repository; + } + + + public ObservableCollection Get() + { + ObservableCollection _participantList = new(); + + if (_repository != null) + { + var participantsTask = _repository.Get(); + var participants = participantsTask is Task> task + ? task.GetAwaiter().GetResult() + : (IEnumerable)participantsTask; + foreach (var participant in participants) + { + if (!_participantList.Any(p => p.ParticipantId == participant.ParticipantId)) + { + _participantList.Add(participant); + } + } + } + return _participantList; + } +} diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs index 637f8be..3edf334 100644 --- a/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs @@ -13,9 +13,10 @@ public class ParticipantViewModel : ViewModelBase } - public ParticipantViewModel(IRepository repo) : base() + public ParticipantViewModel(IRepository repo, IMethodSharingService sharingService) : base() { - Repository = repo; + _Repository = repo; + _sharingService = sharingService; } #endregion @@ -23,7 +24,8 @@ public class ParticipantViewModel : ViewModelBase #region Private Variables private Participant? _ParticipantObject = new(); private ObservableCollection _ParticipantList = new(); - private readonly IRepository? Repository; + private readonly IRepository? _Repository; + private readonly IMethodSharingService _sharingService; #endregion #region public Properties @@ -53,23 +55,12 @@ public class ParticipantViewModel : ViewModelBase #region Get Method public ObservableCollection Get() { - if (Repository != null) - { - var participantsTask = Repository.Get(); - var participants = participantsTask is Task> task - ? task.GetAwaiter().GetResult() - : (IEnumerable)participantsTask; - foreach (var participant in participants) - { - if (!_ParticipantList.Any(p => p.ParticipantId == participant.ParticipantId)) - { - ParticipantList.Add(participant); - } - } - } + ParticipantList = _sharingService.Get(); return ParticipantList; } + + #endregion #region Get(id) Method @@ -77,7 +68,7 @@ public class ParticipantViewModel : ViewModelBase { try { - ParticipantObject = Repository?.Get(id).GetAwaiter().GetResult(); + ParticipantObject = _Repository?.Get(id).GetAwaiter().GetResult(); } catch (Exception ex) { @@ -88,11 +79,11 @@ public class ParticipantViewModel : ViewModelBase } public virtual bool Save() { - if (Repository == null || ParticipantObject == null) + if (_Repository == null || ParticipantObject == null) { return false; } - var tmpTask = Repository.Save(ParticipantObject); + var tmpTask = _Repository.Save(ParticipantObject); bool tmp = tmpTask.GetAwaiter().GetResult(); if (tmp) { diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs new file mode 100644 index 0000000..bc425a0 --- /dev/null +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs @@ -0,0 +1,165 @@ +using Common.Library; +using GreadyPoang.EntityLayer; +using System.Collections.ObjectModel; +using System.Diagnostics; + +namespace GreadyPoang.ViewModelLayer; + +public class RoundStartingViewModel : ViewModelBase +{ + #region Constructors + public RoundStartingViewModel() : base() + { + + } + + public RoundStartingViewModel(IRepository repo, IMethodSharingService sharingService) : base() + { + _Repository = repo; + _sharingService = sharingService; + _roundElements = new ObservableCollection(); + } + + #endregion + + private GameRound? _GameRoundObject = new(); + private ObservableCollection _GameRoundList = new(); + private ObservableCollection _ParticipantList = new(); + private readonly IRepository? _Repository; + private readonly IMethodSharingService _sharingService; + private Participant _selectedItem; + + private ObservableCollection _roundElements; + + public ObservableCollection RoundElements + { + get { return _roundElements; } + set + { + _roundElements = value; + RaisePropertyChanged(nameof(RoundElements)); + } + } + + public Participant SelectedItem + { + get => _selectedItem; + set + { + if (_selectedItem != value) + { + + _selectedItem = value; + RaisePropertyChanged(nameof(SelectedItem)); + OnItemSelected(value); // Metod som triggas vid val + } + } + } + + private void OnItemSelected(Participant item) + { + if (_roundElements.Count == 0) + { + var GameRound = new GameRound + { + GameRoundStartDate = DateTime.Now, + GameRoundFinished = null + }; + _Repository?.Save(GameRound).GetAwaiter().GetResult(); + } + // Gör något med det valda objektet + Debug.WriteLine($"Du valde: {item.LastNameFirstName}"); + } + + + public GameRound? GameRoundObject + { + get { return _GameRoundObject; } + set + { + _GameRoundObject = value; + RaisePropertyChanged(nameof(GameRoundObject)); + } + } + + public ObservableCollection GameRoundList + { + get { return _GameRoundList; } + set + { + _GameRoundList = value; + RaisePropertyChanged(nameof(GameRoundList)); + } + } + + public ObservableCollection ParticipantList + { + get { return _ParticipantList; } + set + { + _ParticipantList = value; + RaisePropertyChanged(nameof(ParticipantList)); + } + } + + #region Get Method + public ObservableCollection Get() + { + if (_Repository != null) + { + var gameRoundsTask = _Repository.Get(); + var gameRounds = gameRoundsTask is Task> task + ? task.GetAwaiter().GetResult() + : (IEnumerable)gameRoundsTask; + foreach (var gameRound in gameRounds) + { + if (!_GameRoundList.Any(p => p.GameRoundId == gameRound.GameRoundId)) + { + GameRoundList.Add(gameRound); + } + } + } + return GameRoundList; + } + + public ObservableCollection GetParticipants() + { + ParticipantList = _sharingService.Get(); + return ParticipantList; + } + + #endregion + + #region Get(id) Method + public GameRound? Get(int id) + { + try + { + GameRoundObject = _Repository?.Get(id).GetAwaiter().GetResult(); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Error in Get method: {ex.Message}"); + } + + return GameRoundObject; + } + public virtual bool Save() + { + if (_Repository == null || GameRoundObject == null) + { + return false; + } + var tmpTask = _Repository.Save(GameRoundObject); + bool tmp = tmpTask.GetAwaiter().GetResult(); + if (tmp) + { + GameRoundObject = new GameRound(); + this.Get(); + } + return tmp; + } + + #endregion + +} diff --git a/GreadyPoang/CommandClasses/ParticipantViewModelCommands.cs b/GreadyPoang/CommandClasses/ParticipantViewModelCommands.cs index f8071d2..57d1806 100644 --- a/GreadyPoang/CommandClasses/ParticipantViewModelCommands.cs +++ b/GreadyPoang/CommandClasses/ParticipantViewModelCommands.cs @@ -12,7 +12,7 @@ public class ParticipantViewModelCommands : ParticipantViewModel { } - public ParticipantViewModelCommands(IRepository repo) : base(repo) + public ParticipantViewModelCommands(IRepository repo, IMethodSharingService sharingService) : base(repo, sharingService) { } diff --git a/GreadyPoang/CommandClasses/RoundStartingViewModelCommands.cs b/GreadyPoang/CommandClasses/RoundStartingViewModelCommands.cs new file mode 100644 index 0000000..4585778 --- /dev/null +++ b/GreadyPoang/CommandClasses/RoundStartingViewModelCommands.cs @@ -0,0 +1,64 @@ +using Common.Library; +using GreadyPoang.EntityLayer; +using GreadyPoang.ViewModelLayer; +using System.Windows.Input; + +namespace GreadyPoang.CommandClasses; + +public class RoundStartingViewModelCommands : RoundStartingViewModel +{ + public RoundStartingViewModelCommands() : base() + { + + } + public RoundStartingViewModelCommands(IRepository repo, IMethodSharingService sharingService) : base(repo, sharingService) + { + } + + #region Private Variables + private bool _IsSaveCommandEnabled = true; + #endregion + + #region Public Properties + public bool IsSaveCommandEnabled + { + get { return _IsSaveCommandEnabled; } + set + { + _IsSaveCommandEnabled = value; + RaisePropertyChanged(nameof(IsSaveCommandEnabled)); + } + } + #endregion + + #region Commands + public ICommand SaveCommand { get; private set; } + public ICommand EditCommand { get; private set; } + #endregion + + #region Init Method + public override void Init() + { + base.Init(); + SaveCommand = new Command(async () => SaveAsync(), () => IsSaveCommandEnabled); + EditCommand = new Command(async (id) => await EditAsync(id), (id) => id > 0); + } + #endregion + + protected async Task EditAsync(int id) + { + await Shell.Current.GoToAsync($"{nameof(Views.RoundStartingView)}?id={id}"); + } + + public async Task SaveAsync() + { + var ret = base.Save(); + if (ret) + { + await Shell.Current.GoToAsync(".."); + } + + return ret; + } + +} diff --git a/GreadyPoang/MauiProgram.cs b/GreadyPoang/MauiProgram.cs index fe35987..981a805 100644 --- a/GreadyPoang/MauiProgram.cs +++ b/GreadyPoang/MauiProgram.cs @@ -3,6 +3,7 @@ using GreadyPoang.CommandClasses; using GreadyPoang.DataLayer; using GreadyPoang.DataLayer.Database; using GreadyPoang.EntityLayer; +using GreadyPoang.ViewModelLayer; using GreadyPoang.Views; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -39,6 +40,15 @@ public static class MauiProgram builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped, GameRoundRepository>(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + + builder.Services.AddScoped, GamePointRepository>(); + + + builder.Services.AddScoped, MethodSharingService>(); + #if DEBUG builder.Logging.AddDebug(); #endif diff --git a/GreadyPoang/Views/RoundStartingView.xaml b/GreadyPoang/Views/RoundStartingView.xaml index b91a9a4..dbfb12d 100644 --- a/GreadyPoang/Views/RoundStartingView.xaml +++ b/GreadyPoang/Views/RoundStartingView.xaml @@ -3,6 +3,10 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:partial="clr-namespace:GreadyPoang.ViewsPartial" x:Class="GreadyPoang.Views.RoundStartingView" + xmlns:vm="clr-namespace:GreadyPoang.CommandClasses" + xmlns:model="clr-namespace:GreadyPoang.EntityLayer;assembly=GreadyPoang.EntityLayer" + x:DataType="vm:RoundStartingViewModelCommands" + x:Name="GameRoundStartingPage" Title="Starta ny Runda"> @@ -16,10 +20,39 @@ ViewTitle="Starta en Spelrunda" ViewDescription="Välj deltagare och initiera spel" /> -