Skip to content

Commit

Permalink
Refactor FormatDuration method and add System.Collections.Generic
Browse files Browse the repository at this point in the history
… namespace

Added the `System.Collections.Generic` namespace to `AfkModule.cs` for better type safety and performance. Refactored the `FormatDuration` method to use a `List<string>` instead of an array for storing duration parts, improving code readability and maintainability. The method now checks and adds hours, minutes, and seconds separately to the list, ensuring the seconds part is added even if the duration is zero.
  • Loading branch information
BoiHanny committed Apr 15, 2024
1 parent 0ec54b4 commit 780e59a
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions vrcosc-magicchatbox/Classes/Modules/AfkModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -273,16 +274,28 @@ private void ExitAfkMode()

private static string FormatDuration(TimeSpan duration)
{
var parts = new[]
var parts = new List<string>();

if (duration.TotalHours >= 1)
{
duration.TotalHours >= 1 ? $"{duration.Hours}h" : null,
duration.Minutes > 0 ? $"{duration.Minutes}m" : null,
$"{duration.Seconds}s"
};
parts.Add($"{duration.Hours}h");
}

if (duration.Minutes > 0)
{
parts.Add($"{duration.Minutes}m");
}

return string.Join(" ", parts.Where(part => part != null));

if (duration.Seconds > 0 || (duration.Hours == 0 && duration.Minutes == 0))
{
parts.Add($"{duration.Seconds}s");
}

return string.Join(" ", parts);
}


private static uint GetIdleTime()
{
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO { cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO)) };
Expand Down

0 comments on commit 780e59a

Please sign in to comment.