diff --git a/GreadyPoang.Common/Base/BehaviorBase.cs b/GreadyPoang.Common/Base/BehaviorBase.cs new file mode 100644 index 0000000..0a2628e --- /dev/null +++ b/GreadyPoang.Common/Base/BehaviorBase.cs @@ -0,0 +1,29 @@ +namespace GreadyPoang.Common; + +public class BehaviorBase : Behavior where T : BindableObject +{ + public T AssociatedObject { get; private set; } + + protected override void OnAttachedTo(T bindable) + { + base.OnAttachedTo(bindable); + AssociatedObject = bindable; + + if (bindable.BindingContext != null) + BindingContext = bindable.BindingContext; + + bindable.BindingContextChanged += OnBindingContextChanged; + } + + protected override void OnDetachingFrom(T bindable) + { + base.OnDetachingFrom(bindable); + bindable.BindingContextChanged -= OnBindingContextChanged; + AssociatedObject = null; + } + + void OnBindingContextChanged(object sender, EventArgs e) + { + BindingContext = AssociatedObject.BindingContext; + } +} diff --git a/GreadyPoang.Common/GreadyPoang.Common.csproj b/GreadyPoang.Common/GreadyPoang.Common.csproj new file mode 100644 index 0000000..552d4ed --- /dev/null +++ b/GreadyPoang.Common/GreadyPoang.Common.csproj @@ -0,0 +1,14 @@ + + + + net9.0 + enable + enable + + + + + + + + diff --git a/GreadyPoang/ViewHelpers/DigitsOnlyBehavior.cs b/GreadyPoang.Common/Specifics/DigitsOnlyBehavior.cs similarity index 96% rename from GreadyPoang/ViewHelpers/DigitsOnlyBehavior.cs rename to GreadyPoang.Common/Specifics/DigitsOnlyBehavior.cs index 6ad6613..3d25498 100644 --- a/GreadyPoang/ViewHelpers/DigitsOnlyBehavior.cs +++ b/GreadyPoang.Common/Specifics/DigitsOnlyBehavior.cs @@ -1,6 +1,6 @@ using System.Text.RegularExpressions; -namespace GreadyPoang.ViewHelpers; +namespace GreadyPoang.Common; public class DigitsOnlyBehavior : Behavior { diff --git a/GreadyPoang.Common/Specifics/EventToCommandBehavior.cs b/GreadyPoang.Common/Specifics/EventToCommandBehavior.cs new file mode 100644 index 0000000..a888068 --- /dev/null +++ b/GreadyPoang.Common/Specifics/EventToCommandBehavior.cs @@ -0,0 +1,50 @@ +using System.Reflection; +using System.Windows.Input; + +namespace GreadyPoang.Common; +public class EventToCommandBehavior : BehaviorBase +{ + public string EventName { get; set; } + + public static readonly BindableProperty CommandProperty = + BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(EventToCommandBehavior)); + + public ICommand Command + { + get => (ICommand)GetValue(CommandProperty); + set => SetValue(CommandProperty, value); + } + + private EventInfo eventInfo; + private Delegate eventHandler; + + protected override void OnAttachedTo(VisualElement bindable) + { + base.OnAttachedTo(bindable); + + if (bindable == null || string.IsNullOrEmpty(EventName)) + return; + + eventInfo = bindable.GetType().GetRuntimeEvent(EventName); + if (eventInfo == null) + throw new ArgumentException($"Event '{EventName}' not found on type '{bindable.GetType().Name}'"); + + MethodInfo methodInfo = typeof(EventToCommandBehavior).GetTypeInfo().GetDeclaredMethod(nameof(OnEvent)); + eventHandler = methodInfo.CreateDelegate(eventInfo.EventHandlerType, this); + eventInfo.AddEventHandler(bindable, eventHandler); + } + + protected override void OnDetachingFrom(VisualElement bindable) + { + base.OnDetachingFrom(bindable); + + if (eventInfo != null && eventHandler != null) + eventInfo.RemoveEventHandler(bindable, eventHandler); + } + + private void OnEvent(object sender, object eventArgs) + { + if (Command?.CanExecute(eventArgs) == true) + Command.Execute(eventArgs); + } +} diff --git a/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderGroup.cs b/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderGroup.cs index e2e4a52..7e01867 100644 --- a/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderGroup.cs +++ b/GreadyPoang.EntityLayer/HelperEntities/RoundBuilderGroup.cs @@ -1,6 +1,8 @@ -namespace GreadyPoang.EntityLayer; +using Common.Library; -public class RoundBuilderGroup +namespace GreadyPoang.EntityLayer; + +public class RoundBuilderGroup : EntityBase { public RoundBuilderGroup() { @@ -47,6 +49,7 @@ public class RoundBuilderGroup set { _elements = value; + RaisePropertyChanged(nameof(Elements)); // No need to raise property changed for this example } } diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs index 6a07606..d846c6f 100644 --- a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs @@ -39,11 +39,12 @@ public class RoundRunningViewModel : ViewModelBase private readonly IMethodSharingService _sharingService; private readonly ICombinedRepository _combined; private readonly IObjectMessageService _objectMessage; - private ObservableCollection _roundElements; private ObservableCollection _GameRoundList = new(); private ObservableCollection _ParticipantList = new(); + private ObservableCollection _roundElements; private Collection _playerColumns; + private RoundBuilderElement _builderObject; public void TriggerRebuild() { @@ -71,10 +72,6 @@ public class RoundRunningViewModel : ViewModelBase } } - - - private RoundBuilderElement _builderObject; - public RoundBuilderElement BuilderObject { get { return _builderObject; } @@ -103,14 +100,20 @@ public class RoundRunningViewModel : ViewModelBase _roundElements.Add(item); } + // Räkna ut vem som är nästa spelare var nxt = nextPlayerElement(); + + // Aktuell spelare sätts som BuilderObject BuilderObject.ParticipantName = _objectMessage.CurrentGroup.Elements[nxt].ParticipantName; BuilderObject.GameRoundId = _objectMessage.CurrentGroup.GameRoundId; BuilderObject.ParticipantId = _objectMessage.CurrentGroup.Elements[nxt].ParticipantId; + BuilderObject.Status = _objectMessage.CurrentGroup.Status; + // Alla poängposter från samtliga spelare i rundan samlas ihop och fördelas per deltagare var localElements = _combined.roundBuilderElementsTotalById(_objectMessage.CurrentGroup.GameRoundId); - FillupResultTable(localElements); + FillupResultTable(localElements); + TriggerRebuild(); } return RoundElements; } @@ -118,7 +121,18 @@ public class RoundRunningViewModel : ViewModelBase public void StoreAndHandlePoints() { + var game = _roundsRepo.Get(BuilderObject.GameRoundId).GetAwaiter().GetResult(); var regNr = RoundElements.Count > 0 ? RoundElements.Max(e => e.GameRoundRegNr) + 1 : 1; + + if (game.GameStatus == GamePointStatus.New) + { + game.GameStatus = GamePointStatus.InProgress; + regNr = regNr == 0 ? 1 : regNr; + _roundsRepo.Save(game); + + } + + BuilderObject.Status = game.GameStatus; var points = BuilderObject.GameRegPoints; var newPoint = new GamePoint { @@ -135,6 +149,7 @@ public class RoundRunningViewModel : ViewModelBase RoundElements.Clear(); foreach (var item in tmpElements) { + item.Status = GamePointStatus.InProgress; RoundElements.Add(item); } @@ -179,7 +194,8 @@ public class RoundRunningViewModel : ViewModelBase _playerColumns = new Collection(); } - if (elements.Any(g => g.GameRegPoints > 0)) + // if (elements.Any(g => g.GameRegPoints > 0)) + if (BuilderObject.Status == GamePointStatus.InProgress) { PlayerColumn player = new PlayerColumn(); diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs index f37ea4d..aafa65c 100644 --- a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundStartingViewModel.cs @@ -169,7 +169,8 @@ public class RoundStartingViewModel : ViewModelBase GameRoundRegNr = p.GameRoundRegNr, GameRegPoints = p.GameRegPoints, GameRoundId = p.GameRoundId, - GameRoundStartDate = p.GameRoundStartDate + GameRoundStartDate = p.GameRoundStartDate, + Status = g.First().Status }).ToList() }) .ToList(); diff --git a/GreadyPoang.sln b/GreadyPoang.sln index 8fcaab8..fa48196 100644 --- a/GreadyPoang.sln +++ b/GreadyPoang.sln @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreadyPoang.Migrations", "G EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreadyPoang.Services", "GreadyPoang.Services\GreadyPoang.Services.csproj", "{1C70F0EC-370D-4F48-AEDD-DB171D71EA2A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreadyPoang.Common", "GreadyPoang.Common\GreadyPoang.Common.csproj", "{BEA80096-7267-4583-8982-A8000192CB31}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -52,6 +54,10 @@ Global {1C70F0EC-370D-4F48-AEDD-DB171D71EA2A}.Debug|Any CPU.Build.0 = Debug|Any CPU {1C70F0EC-370D-4F48-AEDD-DB171D71EA2A}.Release|Any CPU.ActiveCfg = Release|Any CPU {1C70F0EC-370D-4F48-AEDD-DB171D71EA2A}.Release|Any CPU.Build.0 = Release|Any CPU + {BEA80096-7267-4583-8982-A8000192CB31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEA80096-7267-4583-8982-A8000192CB31}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEA80096-7267-4583-8982-A8000192CB31}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEA80096-7267-4583-8982-A8000192CB31}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/GreadyPoang/GreadyPoang.csproj b/GreadyPoang/GreadyPoang.csproj index 42db7ff..a7fa4ac 100644 --- a/GreadyPoang/GreadyPoang.csproj +++ b/GreadyPoang/GreadyPoang.csproj @@ -67,6 +67,7 @@ + @@ -97,4 +98,8 @@ + + + + diff --git a/GreadyPoang/Platforms/Android/AndroidManifest.xml b/GreadyPoang/Platforms/Android/AndroidManifest.xml index e9937ad..f344a79 100644 --- a/GreadyPoang/Platforms/Android/AndroidManifest.xml +++ b/GreadyPoang/Platforms/Android/AndroidManifest.xml @@ -1,6 +1,6 @@  - - + + \ No newline at end of file diff --git a/GreadyPoang/Resources/Styles/AppStyles.xaml b/GreadyPoang/Resources/Styles/AppStyles.xaml index c163465..d8471f9 100644 --- a/GreadyPoang/Resources/Styles/AppStyles.xaml +++ b/GreadyPoang/Resources/Styles/AppStyles.xaml @@ -4,6 +4,41 @@ xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"> + + + + + + --> + @@ -33,6 +67,7 @@ @@ -51,32 +86,19 @@ Stroke="LightGray" StrokeThickness="1" x:Name="RoundElementBorder" - BackgroundColor="{Binding Status, Converter={StaticResource StatusToColorConverter}}" - StrokeShape="RoundRectangle 10"> + StrokeShape="RoundRectangle 10" + Style="{StaticResource StatusBorderStyle}" + > - - - - - - - - - - - - - - - @@ -113,8 +135,8 @@ - @@ -129,21 +151,7 @@ Stroke="Gray" Padding="5" Margin="5,0" - BackgroundColor="{Binding Status, Converter={StaticResource StatusToColorConverter}}" > - - - - - - - - - - - - - - + Style="{StaticResource StatusBorderStyle}" > -