Add project files.

This commit is contained in:
2025-10-11 08:15:33 +02:00
commit 5d1e7858f2
140 changed files with 7567 additions and 0 deletions

View File

@ -0,0 +1,21 @@
using Common.Library;
namespace GreadyPoang.ViewModelLayer;
public class AppShellViewModel : ViewModelBase
{
private bool _roundRounningVisible = true;
public bool RoundRunningVisible
{
get { return _roundRounningVisible; }
set
{
_roundRounningVisible = value;
RaisePropertyChanged(nameof(RoundRunningVisible));
}
}
}

View File

@ -0,0 +1,30 @@
using Common.Library;
using GreadyPoang.Services;
namespace GreadyPoang.ViewModelLayer;
public class MainPageViewModel : ViewModelBase
{
private readonly AppShellViewModel _appShell;
private readonly IObjectMessageService _messageService;
public MainPageViewModel(
AppShellViewModel appShell,
IObjectMessageService messageService
) : base()
{
_appShell = appShell;
_messageService = messageService;
}
public void InitMessage()
{
if (_appShell.RoundRunningVisible == false)
{
_messageService.Delivered = true;
}
}
}

View File

@ -0,0 +1,37 @@
using Common.Library;
using GreadyPoang.EntityLayer;
using System.Collections.ObjectModel;
namespace GreadyPoang.ViewModelLayer;
public class MethodSharingService : ViewModelBase, IMethodSharingService<Participant>
{
private readonly IRepository<Participant> _repository;
public MethodSharingService(IRepository<Participant> repository)
{
_repository = repository;
}
public ObservableCollection<Participant> Get()
{
ObservableCollection<Participant> _participantList = new();
if (_repository != null)
{
var participantsTask = _repository.Get();
var participants = participantsTask is Task<IEnumerable<Participant>> task
? task.GetAwaiter().GetResult()
: (IEnumerable<Participant>)participantsTask;
foreach (var participant in participants)
{
if (!_participantList.Any(p => p.ParticipantId == participant.ParticipantId))
{
_participantList.Add(participant);
}
}
}
return _participantList;
}
}

View File

@ -0,0 +1,109 @@
using Common.Library;
using GreadyPoang.EntityLayer;
using GreadyPoang.Services;
using System.Collections.ObjectModel;
namespace GreadyPoang.ViewModelLayer;
public class ParticipantViewModel : ViewModelBase
{
#region Constructors
public ParticipantViewModel() : base()
{
}
public ParticipantViewModel(
IRepository<Participant> repo,
IMethodSharingService<Participant> sharingService,
AppShellViewModel appShell,
IObjectMessageService objectMessage) : base()
{
_Repository = repo;
_sharingService = sharingService;
_appShell = appShell;
_objectMessage = objectMessage;
}
#endregion
#region Private Variables
private Participant? _ParticipantObject = new();
private ObservableCollection<Participant> _ParticipantList = new();
private readonly IRepository<Participant>? _Repository;
private readonly IMethodSharingService<Participant> _sharingService;
private readonly AppShellViewModel _appShell;
private readonly IObjectMessageService _objectMessage;
#endregion
#region public Properties
public Participant? ParticipantObject
{
get { return _ParticipantObject; }
set
{
_ParticipantObject = value;
RaisePropertyChanged(nameof(ParticipantObject));
}
}
public ObservableCollection<Participant> ParticipantList
{
get { return _ParticipantList; }
set
{
_ParticipantList = value;
RaisePropertyChanged(nameof(ParticipantList));
}
}
#endregion
#region Get Method
public ObservableCollection<Participant> Get()
{
if (_appShell.RoundRunningVisible == false)
{
_objectMessage.Delivered = true;
}
ParticipantList = _sharingService.Get();
return ParticipantList;
}
#endregion
#region Get(id) Method
public Participant? Get(int id)
{
try
{
ParticipantObject = _Repository?.Get(id).GetAwaiter().GetResult();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error in Get method: {ex.Message}");
}
return ParticipantObject;
}
public virtual bool Save()
{
if (_Repository == null || ParticipantObject == null)
{
return false;
}
var tmpTask = _Repository.Save(ParticipantObject);
int tmp = tmpTask.GetAwaiter().GetResult();
if (tmp != -1)
{
ParticipantObject = new Participant();
this.Get();
}
return tmp != -1;
}
#endregion
}

View File

@ -0,0 +1,309 @@
using Common.Library;
using GreadyPoang.Core;
using GreadyPoang.DataLayer;
using GreadyPoang.EntityLayer;
using GreadyPoang.Services;
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace GreadyPoang.ViewModelLayer;
public class RoundRunningViewModel : ViewModelBase
{
public event EventHandler RebuildRequested;
public RoundRunningViewModel() : base()
{
}
public RoundRunningViewModel(
IRepository<GameRound> roundsRepo,
IRepository<GamePoint> pointsRepo,
IMethodSharingService<Participant> sharingService,
ICombinedRepository combined,
IObjectMessageService objectMessage,
ISplashService splashService,
AppShellViewModel appShell
) : base()
{
_roundsRepo = roundsRepo;
_pointsRepo = pointsRepo;
_sharingService = sharingService;
_combined = combined;
_objectMessage = objectMessage;
_splashService = splashService;
_appShell = appShell;
_roundElements = new ObservableCollection<RoundBuilderElement>();
_builderObject = new();
_SplashShowing = false;
}
private bool _SplashShowing;
private readonly IRepository<GameRound>? _roundsRepo;
private readonly IRepository<GamePoint> _pointsRepo;
private readonly IMethodSharingService<Participant> _sharingService;
private readonly ICombinedRepository _combined;
private readonly IObjectMessageService _objectMessage;
private readonly ISplashService _splashService;
private readonly AppShellViewModel _appShell;
private ObservableCollection<RoundBuilderGroup> _GameRoundList = new();
private ObservableCollection<Participant> _ParticipantList = new();
private ObservableCollection<RoundBuilderElement> _roundElements;
private Collection<PlayerColumn> _playerColumns;
private RoundBuilderElement _builderObject;
public void TriggerRebuild()
{
// Trigga eventet
RebuildRequested?.Invoke(this, EventArgs.Empty);
}
// Översta raden
public ObservableCollection<RoundBuilderElement> RoundElements
{
get { return _roundElements; }
set
{
_roundElements = value;
RaisePropertyChanged(nameof(RoundElements));
}
}
// Nedersta strukturen
public Collection<PlayerColumn> PlayerColumns
{
get { return _playerColumns; }
set
{
_playerColumns = value;
RaisePropertyChanged(nameof(PlayerColumns));
}
}
public RoundBuilderElement BuilderObject
{
get { return _builderObject; }
set
{
_builderObject = value;
RaisePropertyChanged(nameof(BuilderObject));
}
}
private bool _gobackVisible = true;
public bool GobackVisible
{
get { return _gobackVisible; }
set
{
_gobackVisible = value;
RaisePropertyChanged(nameof(GobackVisible));
}
}
public ObservableCollection<RoundBuilderElement> Get()
{
//_overlay.ShowSplash("Laddar...", 30);
if (_objectMessage.CurrentGroup != null)
{
GobackVisible = _objectMessage.Delivered;
_objectMessage.Delivered = false;
//CurrentGroup är satt från RoundStarting ViewModel
Debug.WriteLine($"Chosen round: {_objectMessage.CurrentGroup.GameRoundId}");
if (RoundElements.Count > 0)
{
RoundElements.Clear();
}
foreach (var item in _objectMessage.CurrentGroup.Elements)
{
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);
foreach (var col in _playerColumns)
{
RoundElements.FirstOrDefault(e => e.ParticipantId == col.PlayerId).GameRegPoints = col.PlayerPoints;
}
TriggerRebuild();
}
return RoundElements;
}
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
{
ParticipantId = BuilderObject.ParticipantId,
GameRoundId = BuilderObject.GameRoundId,
GameDate = DateTime.Now,
GameRoundRegNr = regNr,
GameRegPoints = points
};
var pointId = _pointsRepo?.Save(newPoint).GetAwaiter().GetResult();
// Uppdatera listan med element
var tmpElements = _combined.roundBuilderElementsDbById(BuilderObject.GameRoundId);
RoundElements.Clear();
foreach (var item in tmpElements)
{
item.Status = GamePointStatus.InProgress;
RoundElements.Add(item);
}
// Uppdatera spelaren som skall spela nästa
var nxt = nextPlayerElement();
BuilderObject.GameRegPoints = 0;
BuilderObject.ParticipantName = RoundElements[nxt].ParticipantName;
BuilderObject.ParticipantId = RoundElements[nxt].ParticipantId;
BuilderObject.GameRoundId = RoundElements[0].GameRoundId;
var localElements = _combined.roundBuilderElementsTotalById(BuilderObject.GameRoundId);
FillupResultTable(localElements);
foreach (var col in _playerColumns)
{
RoundElements.FirstOrDefault(e => e.ParticipantId == col.PlayerId).GameRegPoints = col.PlayerPoints;
}
TriggerRebuild();
}
private int nextPlayerElement()
{
for (int i = 0; i < RoundElements.Count; i++)
{
if (RoundElements[i].GameRoundRegNr == -1 ||
(i > 0 && (RoundElements[i - 1].GameRoundRegNr > RoundElements[i].GameRoundRegNr)) ||
(i == 0 && RoundElements[i].GameRoundRegNr < RoundElements[RoundElements.Count - 1].GameRoundRegNr))
return i;
}
return -1;
}
private void FillupResultTable(IEnumerable<RoundBuilderElement> elements)
{
if (_playerColumns != null)
{
_playerColumns.Clear();
}
else
{
_playerColumns = new Collection<PlayerColumn>();
}
// if (elements.Any(g => g.GameRegPoints > 0))
if (BuilderObject.Status == GamePointStatus.InProgress)
{
PlayerColumn player = new PlayerColumn();
foreach (var element in elements)
{
player = _playerColumns.FirstOrDefault(p => p.PlayerId == element.ParticipantId);
if (player == null)
{
player = new PlayerColumn
{
PlayerName = element.ParticipantName,
PlayerId = element.ParticipantId,
PlayerPoints = 0
};
}
if (element.GameRegPoints > 0)
{
player.Values.Add(element.GameRegPoints.ToString());
player.PlayerPoints += element.GameRegPoints;
if (player.PlayerPoints > 10000)
{
var winner = RoundElements.FirstOrDefault(e => e.ParticipantId == player.PlayerId);
winner.Status = GamePointStatus.Winning;
}
}
//oldPart = element.ParticipantId;
if (!_playerColumns.Contains(player))
{
_playerColumns.Add(player);
}
}
}
else
{
Random slumper = new Random();
foreach (var element in elements)
{
var player = new PlayerColumn
{
PlayerName = element.ParticipantName
};
for (int i = 0; i < slumper.Next(6); i++)
{
player.Values.Add(slumper.Next(1, 10).ToString());
}
_playerColumns.Add(player);
}
}
}
public async void ToggleSplash()
{
if (!_SplashShowing)
{
//_overlay.ShowSplash("Clcicked!", 5000);
await _splashService.ShowSplash("Clicked", 0);
_SplashShowing = true;
}
else
{
await _splashService.HideAsync();
_SplashShowing = false;
}
}
public void GobackAsync()
{
_appShell.RoundRunningVisible = true;
}
}

View File

@ -0,0 +1,274 @@
using Common.Library;
using GreadyPoang.Core;
using GreadyPoang.DataLayer;
using GreadyPoang.EntityLayer;
using GreadyPoang.Services;
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace GreadyPoang.ViewModelLayer;
public class RoundStartingViewModel : ViewModelBase
{
#region Constructors
public RoundStartingViewModel() : base()
{
}
public RoundStartingViewModel(
IRepository<GameRound> roundsRepo,
IRepository<GamePoint> pointsRepo,
IMethodSharingService<Participant> sharingService,
ICombinedRepository combined,
IObjectMessageService objectMessage,
INavigationService nav,
IPageFactory factory,
ISplashService splashService,
AppShellViewModel appShellView
) : base()
{
_roundsRepo = roundsRepo;
_pointsRepo = pointsRepo;
_sharingService = sharingService;
_combined = combined;
_objectMessage = objectMessage;
_nav = nav;
_factory = factory;
_splashService = splashService;
_appShellView = appShellView;
_roundElements = new ObservableCollection<RoundBuilderElement>();
}
#endregion
private GameRound? _GameRoundObject = new();
private ObservableCollection<RoundBuilderGroup> _GameRoundList = new();
private ObservableCollection<Participant> _ParticipantList = new();
private readonly IRepository<GameRound>? _roundsRepo;
private readonly IRepository<GamePoint> _pointsRepo;
private readonly IMethodSharingService<Participant> _sharingService;
private readonly ICombinedRepository _combined;
private readonly IObjectMessageService _objectMessage;
private readonly INavigationService _nav;
private readonly IPageFactory _factory;
private readonly ISplashService _splashService;
private readonly AppShellViewModel _appShellView;
private Participant _selectedItem;
private ObservableCollection<RoundBuilderElement> _roundElements;
public ObservableCollection<RoundBuilderElement> 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,
GameStatus = GamePointStatus.New,
GameRoundFinished = null
};
var gameRoundId = _roundsRepo?.Save(GameRound).GetAwaiter().GetResult();
if (gameRoundId != null && gameRoundId != -1)
{
GameRound.GameRoundId = gameRoundId.Value;
}
GameRoundObject = GameRound;
}
var GamePointStart = new GamePoint
{
ParticipantId = item.ParticipantId,
GameRoundId = GameRoundObject?.GameRoundId ?? 0,
GameDate = DateTime.Now,
GameRoundRegNr = -1,
GameRegPoints = 0
};
var gamePointId = _pointsRepo.Save(GamePointStart).GetAwaiter().GetResult();
GamePointStart.GamePointId = gamePointId;
var newElement = new RoundBuilderElement();
newElement.ParticipantId = item.ParticipantId;
newElement.ParticipantName = item.LastNameFirstName;
newElement.GameRoundRegNr = GamePointStart.GameRoundRegNr;
newElement.GameRegPoints = GamePointStart.GameRegPoints;
newElement.Status = GameRoundObject!.GameStatus;
newElement.GameRoundStartDate = GameRoundObject?.GameRoundStartDate ?? DateTime.Now;
newElement.GameRoundId = GamePointStart.GameRoundId;
newElement.GamePointId = GamePointStart.GamePointId;
_roundElements.Add(newElement);
// 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<RoundBuilderGroup> GameRoundList
{
get { return _GameRoundList; }
set
{
_GameRoundList = value;
RaisePropertyChanged(nameof(GameRoundList));
}
}
public ObservableCollection<Participant> ParticipantList
{
get { return _ParticipantList; }
set
{
_ParticipantList = value;
RaisePropertyChanged(nameof(ParticipantList));
}
}
#region Get Method
public ObservableCollection<RoundBuilderGroup> Get()
{
if (_combined != null)
{
// var GameRoundSummary = _combined.roundBuilderElements();
var GameRoundSummary = _combined.roundBuilderElementsDb();
var groupedRounds = GameRoundSummary
.GroupBy(r => r.GameRoundId)
.Select(g => new RoundBuilderGroup
{
GameRoundId = g.Key,
GameRoundStartDate = g.First().GameRoundStartDate,
Status = g.First().Status,
Elements = g.Select(p => new RoundBuilderElement
{
ParticipantId = p.ParticipantId,
ParticipantName = p.ParticipantName,
GamePointId = p.GamePointId,
GameRoundRegNr = p.GameRoundRegNr,
GameRegPoints = p.GameRegPoints,
GameRoundId = p.GameRoundId,
GameRoundStartDate = p.GameRoundStartDate,
Status = g.First().Status
}).ToList()
})
.ToList();
GameRoundList = new ObservableCollection<RoundBuilderGroup>(groupedRounds);
}
return GameRoundList;
}
public ObservableCollection<Participant> GetParticipants()
{
ParticipantList = _sharingService.Get();
return ParticipantList;
}
#endregion
#region Get(id) Method
public GameRound? Get(int id)
{
try
{
GameRoundObject = _roundsRepo?.Get(id).GetAwaiter().GetResult();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error in Get method: {ex.Message}");
}
return GameRoundObject;
}
public virtual bool Save()
{
if (_roundsRepo == null || GameRoundObject == null)
{
return false;
}
var tmpTask = _roundsRepo.Save(GameRoundObject);
bool tmp = tmpTask.GetAwaiter().GetResult() != -1;
if (tmp)
{
GameRoundObject = new GameRound();
RoundElements.Clear();
this.Get();
}
return tmp;
}
public void Rensa()
{
foreach (var element in RoundElements)
{
_pointsRepo.DeleteById(element.GamePointId);
}
_roundsRepo?.DeleteById(GameRoundObject?.GameRoundId ?? 0);
RoundElements.Clear();
}
public async void RoundSelected(RoundBuilderElement element)
{
var rbGroup = GameRoundList.FirstOrDefault(g => g.GameRoundId == element.GameRoundId);
Debug.WriteLine($"Du valde raden med Runda {element.GameRoundId} och spelare: {element.ParticipantName}");
if (rbGroup != null)
{
_objectMessage.CurrentGroup = rbGroup;
_objectMessage.Delivered = true;
//await _splashService.ShowSplash("Runda vald, gå till\r\r 'Påbörja eller fortsätt Runda'", 3000);
await Shell.Current.GoToAsync("RoundRunningPage");
_appShellView.RoundRunningVisible = false;
//_roundRunning.GobackVisible = false;
//var page = _factory.CreateRoundPage();
//_nav.NavigateToPageAsync(page);
}
}
public void SelectNewlyAddedParticipant(RoundBuilderElement roundBuilder)
{
Debug.WriteLine($"Du valde raden med Runda {roundBuilder.GameRoundId} och spelare: {roundBuilder.ParticipantName}");
}
#endregion
}

View File

@ -0,0 +1,103 @@
using Common.Library;
namespace GreadyPoang.ViewModelLayer;
public class SplashViewModel : ViewModelBase
{
// public event PropertyChangedEventHandler PropertyChanged;
private Color _splashBackgroundColor = Colors.DarkSlateBlue;
public Color SplashBackgroundColor
{
get => _splashBackgroundColor;
set
{
_splashBackgroundColor = value;
RaisePropertyChanged(nameof(SplashBackgroundColor));
}
}
private bool _isSplashVisible = false;
public bool IsSplashVisible
{
get => _isSplashVisible;
set
{
_isSplashVisible = value;
RaisePropertyChanged(nameof(IsSplashVisible));
}
}
private double _splashOpacity = 1.0;
public double SplashOpacity
{
get => _splashOpacity;
set
{
_splashOpacity = value;
RaisePropertyChanged(nameof(SplashOpacity));
}
}
private Color _splashTextColor = Colors.White;
public Color SplashTextColor
{
get { return _splashTextColor; }
set
{
_splashTextColor = value;
RaisePropertyChanged(nameof(SplashTextColor));
}
}
private double _splashTranslationY = 0;
public double SplashTranslationY
{
get => _splashTranslationY;
set
{
_splashTranslationY = value;
RaisePropertyChanged(nameof(SplashTranslationY));
}
}
public async Task HideSplashAsync()
{
await Task.Delay(1000); // Simulera laddning
await AnimateSplashOut();
IsSplashVisible = false;
}
private string _splashText = "Välkommen!";
public string SplashText
{
get => _splashText;
set
{
_splashText = value;
RaisePropertyChanged(nameof(SplashText));
}
}
//public Color SplashBackgroundColor { get; set; } = Colors.DarkSlateBlue;
//public string SplashImage { get; set; } = "splash_icon.png";
//protected void OnPropertyChanged(string name) =>
// PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private async Task AnimateSplashOut()
{
for (int i = 0; i < 10; i++)
{
SplashOpacity -= 0.1;
//SplashTranslationY += 5;
await Task.Delay(30);
}
}
}