Skip to content

Commit f2a926f

Browse files
chore: clean up code
This cleans up most of the design. #52
1 parent a1add74 commit f2a926f

25 files changed

+486
-280
lines changed

src/TwitchStreamingTools/App.axaml.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
using Nullinside.Api.Common.Twitch;
1111

12-
using TwitchStreamingTools.Models;
1312
using TwitchStreamingTools.Services;
1413
using TwitchStreamingTools.ViewModels;
1514
using TwitchStreamingTools.Views;
@@ -52,6 +51,11 @@ public override void OnFrameworkInitializationCompleted() {
5251
base.OnFrameworkInitializationCompleted();
5352
}
5453

54+
/// <summary>
55+
/// The handler for the "show" button on the taskbar context menu.
56+
/// </summary>
57+
/// <param name="sender">The invoker.</param>
58+
/// <param name="e">The event arguments.</param>
5559
private void ShowSettings_OnClick(object? sender, EventArgs e) {
5660
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
5761
if (null == desktop.MainWindow) {
@@ -62,6 +66,11 @@ private void ShowSettings_OnClick(object? sender, EventArgs e) {
6266
}
6367
}
6468

69+
/// <summary>
70+
/// The handler for the "close" button on the taskbar context menu.
71+
/// </summary>
72+
/// <param name="sender">The invoker.</param>
73+
/// <param name="e">The event arguments.</param>
6574
private void Close_OnClick(object? sender, EventArgs e) {
6675
Environment.Exit(0);
6776
}

src/TwitchStreamingTools/Models/Configuration.cs renamed to src/TwitchStreamingTools/Configuration.cs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
5+
using System.Speech.Synthesis;
46

57
using Newtonsoft.Json;
68

79
using Nullinside.Api.Common.Twitch;
810

9-
namespace TwitchStreamingTools.Models;
11+
using TwitchStreamingTools.Models;
12+
using TwitchStreamingTools.Utilities;
13+
14+
namespace TwitchStreamingTools;
1015

1116
/// <summary>
1217
/// The configuration of the application.
@@ -16,6 +21,9 @@ public class Configuration {
1621
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "nullinside",
1722
"twitch-streaming-tools", "config.json");
1823

24+
/// <summary>
25+
/// The singleton instance.
26+
/// </summary>
1927
private static Configuration? s_instance;
2028

2129
/// <summary>
@@ -61,6 +69,31 @@ public static Configuration Instance {
6169
/// </summary>
6270
public IDictionary<string, string>? TtsPhoneticUsernames { get; set; }
6371

72+
/// <summary>
73+
/// Retrieves the default audio device configured in the application.
74+
/// </summary>
75+
/// <returns>The default audio device, if any audio device exists.</returns>
76+
public static string? GetDefaultAudioDevice() {
77+
return Instance.TwitchChats?.FirstOrDefault()?.OutputDevice ?? NAudioUtilities.GetDefaultOutputDevice();
78+
}
79+
80+
/// <summary>
81+
/// Retrieves the default TTS voice configured in the application.
82+
/// </summary>
83+
/// <returns>The default tts voice, if any tts voice exists.</returns>
84+
public static string? GetDefaultTtsVoice() {
85+
using var speech = new SpeechSynthesizer();
86+
return Instance.TwitchChats?.FirstOrDefault()?.TtsVoice ?? speech.GetInstalledVoices().FirstOrDefault()?.VoiceInfo.Name;
87+
}
88+
89+
/// <summary>
90+
/// Retrieves the default TTS volume configured in the application.
91+
/// </summary>
92+
/// <returns>The default tts voice, if any tts voice exists.</returns>
93+
public static uint? GetDefaultTtsVolume() {
94+
return Instance.TwitchChats?.FirstOrDefault()?.TtsVolume;
95+
}
96+
6497
/// <summary>
6598
/// Reads the configuration from disk.
6699
/// </summary>
@@ -89,34 +122,4 @@ public bool WriteConfiguration() {
89122
return false;
90123
}
91124
}
92-
93-
/// <summary>
94-
/// Represents a single connection to a twitch chat by a single user.
95-
/// </summary>
96-
public class TwitchChatConfiguration {
97-
/// <summary>
98-
/// Gets or sets the output device to send audio to.
99-
/// </summary>
100-
public string? OutputDevice { get; set; }
101-
102-
/// <summary>
103-
/// Gets or sets a value indicating whether text to speech is on.
104-
/// </summary>
105-
public bool TtsOn { get; set; }
106-
107-
/// <summary>
108-
/// Gets or sets the selected Microsoft Text to Speech voice.
109-
/// </summary>
110-
public string? TtsVoice { get; set; }
111-
112-
/// <summary>
113-
/// Gets or sets the volume of the text to speech voice.
114-
/// </summary>
115-
public uint TtsVolume { get; set; }
116-
117-
/// <summary>
118-
/// Gets or sets the twitch channel to read chat from.
119-
/// </summary>
120-
public string? TwitchChannel { get; set; }
121-
}
122125
}

src/TwitchStreamingTools/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static class Constants {
4242
public const string TWITCH_CLIENT_REDIRECT = $"{API_SITE_DOMAIN}/api/v1/user/twitch-login/twitch-streaming-tools";
4343

4444
/// <summary>
45-
/// The version of the application being run right now.
45+
/// The version of the application being run right now.
4646
/// </summary>
4747
public static readonly string? APP_VERSION = Assembly.GetEntryAssembly()?.GetName().Version?.ToString()[..^2];
4848

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace TwitchStreamingTools.Models;
2+
3+
/// <summary>
4+
/// The twitch chat TTS configuration
5+
/// </summary>
6+
public class TwitchChatConfiguration {
7+
/// <summary>
8+
/// Gets or sets the output device to send audio to.
9+
/// </summary>
10+
public string? OutputDevice { get; set; }
11+
12+
/// <summary>
13+
/// Gets or sets a value indicating whether text to speech is on.
14+
/// </summary>
15+
public bool TtsOn { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the selected Microsoft Text to Speech voice.
19+
/// </summary>
20+
public string? TtsVoice { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the volume of the text to speech voice.
24+
/// </summary>
25+
public uint TtsVolume { get; set; }
26+
27+
/// <summary>
28+
/// Gets or sets the twitch channel to read chat from.
29+
/// </summary>
30+
public string? TwitchChannel { get; set; }
31+
}

src/TwitchStreamingTools/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@ internal sealed class Program {
1212
// Initialization code. Don't use any Avalonia, third-party APIs or any
1313
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
1414
// yet and stuff might break.
15+
16+
/// <summary>
17+
/// Main entrypoint of the application.
18+
/// </summary>
19+
/// <param name="args">The arguments passed to the application.</param>
1520
[STAThread]
1621
public static void Main(string[] args) {
1722
BuildAvaloniaApp()
1823
.StartWithClassicDesktopLifetime(args);
1924
}
2025

2126
// Avalonia configuration, don't remove; also used by visual designer.
27+
28+
/// <summary>
29+
/// Builds the avalonia application.
30+
/// </summary>
31+
/// <returns>The application builder.</returns>
2232
public static AppBuilder BuildAvaloniaApp() {
2333
return AppBuilder.Configure<App>()
2434
.UsePlatformDetect()

src/TwitchStreamingTools/Services/ServiceCollectionExtensions.cs renamed to src/TwitchStreamingTools/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class ServiceCollectionExtensions {
2020
public static void AddCommonServices(this IServiceCollection collection) {
2121
collection.AddSingleton<ITwitchAccountService, TwitchAccountService>();
2222
collection.AddSingleton<ITwitchClientProxy, TwitchClientProxy>(_ => TwitchClientProxy.Instance);
23-
collection.AddSingleton<TwitchTtsService>();
23+
collection.AddSingleton<ITwitchTtsService, TwitchTtsService>();
2424

2525
collection.AddTransient<MainWindowViewModel>();
2626
collection.AddTransient<AccountViewModel>();
@@ -35,6 +35,6 @@ public static void AddCommonServices(this IServiceCollection collection) {
3535
/// <param name="provider">The service provider.</param>
3636
public static void StartupServices(this IServiceProvider provider) {
3737
provider.GetRequiredService<ITwitchAccountService>();
38-
provider.GetRequiredService<TwitchTtsService>();
38+
provider.GetRequiredService<ITwitchTtsService>();
3939
}
4040
}

src/TwitchStreamingTools/Services/ITwitchAccountService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
namespace TwitchStreamingTools.Services;
77

88
/// <summary>
9-
/// The contract for management credentials in the application.
9+
/// The contract for credential management in the application.
1010
/// </summary>
1111
public interface ITwitchAccountService {
1212
/// <summary>
13-
/// The current OAuth's twitch username.
13+
/// The current oauth token's twitch username.
1414
/// </summary>
1515
string? TwitchUsername { get; set; }
1616

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TwitchStreamingTools.Services;
2+
3+
/// <summary>
4+
/// The contract for a service that handles TTS reading of twitch chat.
5+
/// </summary>
6+
public interface ITwitchTtsService {
7+
}

src/TwitchStreamingTools/Services/TwitchAccountService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,21 @@ public void DeleteCredentials() {
103103
private async Task OnCheckCredentials() {
104104
_timer.Stop();
105105
try {
106+
// Grab the value so we can check if the value changed
106107
bool previousValue = CredentialsAreValid;
107108

109+
// Refresh the token
108110
await DoTokenRefreshIfNearExpiration();
109111

112+
// Make sure the new token works
110113
var twitchApi = new TwitchApiWrapper();
111114
string? username = (await twitchApi.GetUser()).username;
115+
116+
// Update the credentials
112117
CredentialsAreValid = !string.IsNullOrWhiteSpace(username);
113118
TwitchUsername = username;
114119

120+
// Fire off the event if something changed
115121
if (previousValue != CredentialsAreValid) {
116122
OnCredentialsStatusChanged?.Invoke(CredentialsAreValid);
117123
}
@@ -129,6 +135,8 @@ private async Task OnCheckCredentials() {
129135
/// </summary>
130136
private async Task DoTokenRefreshIfNearExpiration() {
131137
var twitchApi = new TwitchApiWrapper();
138+
139+
// Don't wait until the token is expired, refresh it ~1 hour before it expires
132140
DateTime expiration = twitchApi.OAuth?.ExpiresUtc ?? DateTime.MaxValue;
133141
TimeSpan timeUntil = expiration - (DateTime.UtcNow + TimeSpan.FromHours(1));
134142
if (timeUntil.Ticks >= 0) {
@@ -140,8 +148,10 @@ private async Task DoTokenRefreshIfNearExpiration() {
140148
return;
141149
}
142150

151+
// Refresh the token
143152
await twitchApi.RefreshAccessToken();
144153

154+
// Update the configuration
145155
Configuration.Instance.OAuth = new OAuthResponse {
146156
Bearer = twitchApi.OAuth.AccessToken,
147157
Refresh = twitchApi.OAuth.RefreshToken,

0 commit comments

Comments
 (0)