Skip to content

Commit

Permalink
refactor(bingo): implement interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
DorielRivalet committed Aug 20, 2023
1 parent ff34d9c commit 17e8f47
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
11 changes: 6 additions & 5 deletions MHFZ_Overlay/Services/TimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace MHFZ_Overlay.Services;
using System.Threading.Tasks;
using MHFZ_Overlay.Models.Constant;
using MHFZ_Overlay.Models.Structures;
using MHFZ_Overlay.Services.Contracts;
using NLog;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;

Expand All @@ -23,12 +24,12 @@ public static class TimeService
{
private static readonly Logger LoggerInstance = LogManager.GetCurrentClassLogger();

public static double GetFramesFromTimeSpan(TimeSpan time)
private static double GetFramesFromTimeSpan(TimeSpan time)
{
return TimeSpan.FromSeconds(time.TotalSeconds * (double)Numbers.FramesPerSecond).TotalSeconds * (double)Numbers.FramesPerSecond;
}

public static TimeSpan GetTimeSpanFromFrames(decimal frames)
private static TimeSpan GetTimeSpanFromFrames(decimal frames)
{
return TimeSpan.FromSeconds((double)frames / (double)Numbers.FramesPerSecond);
}
Expand Down Expand Up @@ -99,7 +100,7 @@ public static string TestTimerMethods(decimal timeDefInt)
Simple: {timer3Result}";
}

public static string SimpleTimer(decimal timeInt, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
private static string SimpleTimer(decimal timeInt, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
// TODO wrong conditionals for timeint >= timedefint?
decimal time = timerMode == TimerMode.Elapsed && timeInt <= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
Expand All @@ -113,7 +114,7 @@ public static string SimpleTimer(decimal timeInt, decimal timeDefInt = 0, bool t
return $"{minutes:00}:{seconds:00}.{remainingMilliseconds:000}" + timeLeftPercent;
}

public static string StringBuilderTimer(decimal timeInt, TimerFormat timerFormat, bool isFrames = true, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
private static string StringBuilderTimer(decimal timeInt, TimerFormat timerFormat, bool isFrames = true, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
decimal time = timerMode == TimerMode.Elapsed && timeInt <= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
decimal framesPerSecond = isFrames ? Numbers.FramesPerSecond : 1;
Expand Down Expand Up @@ -142,7 +143,7 @@ public static string StringBuilderTimer(decimal timeInt, TimerFormat timerFormat
return sb.ToString();
}

public static string TimeSpanTimer(decimal timeInt, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
private static string TimeSpanTimer(decimal timeInt, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
decimal time = timerMode == TimerMode.Elapsed && timeInt <= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
decimal timeInSeconds = time / Numbers.FramesPerSecond;
Expand Down
28 changes: 13 additions & 15 deletions MHFZ_Overlay/ViewModels/Windows/BingoWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ namespace MHFZ_Overlay.ViewModels.Windows;
using LiveChartsCore.Kernel.Sketches;
using System.Windows.Markup;

public partial class BingoWindowViewModel : ObservableRecipient
public partial class BingoWindowViewModel : ObservableRecipient, IRecipient<QuestIDMessage>, IRecipient<RunIDMessage>
{
private ObservableCollection<ObservableValue> _observableValues { get; set; }
private ObservableCollection<ObservableValue>? _observableValues { get; set; }

private static readonly NLog.Logger LoggerInstance = NLog.LogManager.GetCurrentClassLogger();

public BingoWindowViewModel(SnackbarPresenter snackbarPresenter)
{
WeakReferenceMessenger.Default.Register<QuestIDMessage>(this, OnReceivedQuestID);
WeakReferenceMessenger.Default.Register<RunIDMessage>(this, OnReceivedRunID);
BingoWindowSnackbarPresenter = snackbarPresenter;
SetGraphs();
}

public void Receive(QuestIDMessage message) => OnReceivedQuestID(message);

public void Receive(RunIDMessage message) => OnReceivedRunID(message);

private void SetGraphs()
{
// Use ObservableCollections to let the chart listen for changes (or any INotifyCollectionChanged).
Expand Down Expand Up @@ -116,7 +120,7 @@ public IEnumerable<Difficulty> Difficulties
[ObservableProperty]
private BingoCell[,]? cells = new BingoCell[5, 5];

public ObservableCollection<ISeries> Series { get; set; }
public ObservableCollection<ISeries>? Series { get; set; }

// TODO
private void UpdateBingoBoard(int questID)
Expand Down Expand Up @@ -160,28 +164,22 @@ private void UpdateRunIDs(int runID)

partial void OnReceivedRunIDChanged(int value) => UpdateRunIDs(value);

private void OnReceivedQuestID(object recipient, QuestIDMessage message)
private void OnReceivedQuestID(QuestIDMessage message)
{
// Handle the message here, with r being the recipient and m being the
// input message. Using the recipient passed as input makes it so that
// the lambda expression doesn't capture "this", improving performance.
MessageBox.Show(nameof(recipient));
if (!IsBingoRunning)
{
LoggerInstance.Info("Received Quest {0} but bingo is not running.", message);
return;
}

ReceivedQuestID = message.Value;
}

private void OnReceivedRunID(object recipient, RunIDMessage message)
private void OnReceivedRunID(RunIDMessage message)
{
// Handle the message here, with r being the recipient and m being the
// input message. Using the recipient passed as input makes it so that
// the lambda expression doesn't capture "this", improving performance.
MessageBox.Show(nameof(recipient));
if (!IsBingoRunning)
{
LoggerInstance.Info("Received Run {0} but bingo is not running.", message);
return;
}

Expand Down

0 comments on commit 17e8f47

Please sign in to comment.