Skip to content

Commit

Permalink
Update MainWindowViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros committed Jan 7, 2024
1 parent 8c69de2 commit ab729bd
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/HUDMerger/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Discord;
using HUDMerger.Commands;
using HUDMerger.Models;
using Microsoft.Toolkit.Mvvm.Input;
Expand All @@ -18,6 +22,8 @@ public class MainWindowViewModel : ViewModelBase
InitialDirectory = Path.Join(((App)Application.Current).Settings.Value.TeamFortress2Folder, "tf\\custom\\")
};

private static readonly Channel<(string? sourceName, string? targetName)> DiscordChannel = Channel.CreateBounded<(string? sourceName, string? targetName)>(new BoundedChannelOptions(1) { FullMode = BoundedChannelFullMode.DropOldest });

// File
public ICommand LoadSourceHUDCommand { get; }
public ICommand LoadTargetHUDCommand { get; }
Expand All @@ -37,6 +43,7 @@ public HUD? SourceHUD
{
_sourceHUD = value;
OnPropertyChanged();
DiscordChannel.Writer.TryWrite((SourceHUD?.Name, TargetHUD?.Name));
}
}

Expand All @@ -48,6 +55,7 @@ public HUD? TargetHUD
{
_targetHUD = value;
OnPropertyChanged();
DiscordChannel.Writer.TryWrite((SourceHUD?.Name, TargetHUD?.Name));
}
}

Expand Down Expand Up @@ -94,6 +102,9 @@ public MainWindowViewModel()
_targetHUDPanelsListViewModel = new SelectHUDViewModel(LoadTargetHUDCommand);

MergeCommand = new MergeCommand(this);

ChannelReader<(string? sourceName, string? targetName)> reader = DiscordChannel.Reader;
Task.Run(async () => await DiscordRichPresence(reader));
}

private void ShowSettingsWindow()
Expand Down Expand Up @@ -175,4 +186,48 @@ private void HudPanelViewModel_PropertyChanged(object? sender, PropertyChangedEv
targetHUDPanelsListViewModel.HUDPanelsCollectionView.Refresh();
}
}

private static async Task DiscordRichPresence(ChannelReader<(string? sourceName, string? targetName)> reader)
{
Discord.Discord discord = new(1188809773664190524, (ulong)CreateFlags.Default);

try
{
Activity activity = new()
{
Type = ActivityType.Streaming,
Details = "Merging HUDs",
Timestamps = new ActivityTimestamps
{
Start = DateTimeOffset.Now.ToUnixTimeSeconds()
},
Assets = new()
{
LargeImage = "hud-merger"
},
};

while (true)
{
if (reader.TryRead(out var item))
{
activity.Details = item switch
{
(null, null) => "Merging HUDs",
(string sourceName, null) => $"Merging from {sourceName}",
(null, string targetName) => $"Merging into {targetName}",
(string sourceName, string targetName) => $"Merging from {sourceName} into {targetName}"
};
}

discord.GetActivityManager().UpdateActivity(activity, (result) => Console.WriteLine(result));
discord.RunCallbacks();
await Task.Delay(1000 / 60);
}
}
finally
{
discord.Dispose();
}
}
}

0 comments on commit ab729bd

Please sign in to comment.