From ab729bd063d9a2fa5564a6a0c9156de48efc1fad Mon Sep 17 00:00:00 2001 From: cooolbros Date: Sun, 7 Jan 2024 15:51:10 +1100 Subject: [PATCH] Update MainWindowViewModel --- .../ViewModels/MainWindowViewModel.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/HUDMerger/ViewModels/MainWindowViewModel.cs b/src/HUDMerger/ViewModels/MainWindowViewModel.cs index b0cd0e6..b5f3bc2 100644 --- a/src/HUDMerger/ViewModels/MainWindowViewModel.cs +++ b/src/HUDMerger/ViewModels/MainWindowViewModel.cs @@ -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; @@ -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; } @@ -37,6 +43,7 @@ public HUD? SourceHUD { _sourceHUD = value; OnPropertyChanged(); + DiscordChannel.Writer.TryWrite((SourceHUD?.Name, TargetHUD?.Name)); } } @@ -48,6 +55,7 @@ public HUD? TargetHUD { _targetHUD = value; OnPropertyChanged(); + DiscordChannel.Writer.TryWrite((SourceHUD?.Name, TargetHUD?.Name)); } } @@ -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() @@ -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(); + } + } }