Skip to content

Commit e08ce3c

Browse files
Merge pull request #57 from nullinside-development-group/chore/rework
chore: extracting Configuration.Instance to DI
2 parents 9708469 + 62db17d commit e08ce3c

18 files changed

+170
-70
lines changed

src/TwitchStreamingTools/App.axaml.cs

Lines changed: 0 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.Services;
1312
using TwitchStreamingTools.ViewModels;
1413
using TwitchStreamingTools.Views;
1514

src/TwitchStreamingTools/Configuration.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ namespace TwitchStreamingTools;
1616
/// <summary>
1717
/// The configuration of the application.
1818
/// </summary>
19-
public class Configuration {
19+
public class Configuration : IConfiguration {
20+
/// <summary>
21+
/// The location of the configuration file.
22+
/// </summary>
2023
private static readonly string CONFIG_LOCATION =
2124
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "nullinside",
2225
"twitch-streaming-tools", "config.json");
@@ -26,6 +29,12 @@ public class Configuration {
2629
/// </summary>
2730
private static Configuration? s_instance;
2831

32+
/// <summary>
33+
/// Do not allow other people to create instances of our class.
34+
/// </summary>
35+
protected Configuration() {
36+
}
37+
2938
/// <summary>
3039
/// The singleton instance of the class.
3140
/// </summary>
@@ -69,6 +78,23 @@ public static Configuration Instance {
6978
/// </summary>
7079
public IDictionary<string, string>? TtsPhoneticUsernames { get; set; }
7180

81+
/// <summary>
82+
/// Writes the configuration file to disk.
83+
/// </summary>
84+
/// <returns>True if successful, false otherwise.</returns>
85+
public bool WriteConfiguration() {
86+
try {
87+
Directory.CreateDirectory(Path.GetDirectoryName(CONFIG_LOCATION)!);
88+
89+
string json = JsonConvert.SerializeObject(this);
90+
File.WriteAllText(CONFIG_LOCATION, json);
91+
return true;
92+
}
93+
catch {
94+
return false;
95+
}
96+
}
97+
7298
/// <summary>
7399
/// Retrieves the default audio device configured in the application.
74100
/// </summary>
@@ -105,21 +131,4 @@ public static Configuration Instance {
105131
}
106132
catch { return null; }
107133
}
108-
109-
/// <summary>
110-
/// Writes the configuration file to disk.
111-
/// </summary>
112-
/// <returns>True if successful, false otherwise.</returns>
113-
public bool WriteConfiguration() {
114-
try {
115-
Directory.CreateDirectory(Path.GetDirectoryName(CONFIG_LOCATION)!);
116-
117-
string json = JsonConvert.SerializeObject(this);
118-
File.WriteAllText(CONFIG_LOCATION, json);
119-
return true;
120-
}
121-
catch {
122-
return false;
123-
}
124-
}
125134
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
3+
using Nullinside.Api.Common.Twitch;
4+
5+
using TwitchStreamingTools.Models;
6+
7+
namespace TwitchStreamingTools;
8+
9+
/// <summary>
10+
/// The contract for the configuration of the application.
11+
/// </summary>
12+
public interface IConfiguration {
13+
/// <summary>
14+
/// The username of the user logged in through the <see cref="OAuth" /> token.
15+
/// </summary>
16+
string? TwitchUsername { get; set; }
17+
18+
/// <summary>
19+
/// The twitch OAuth token.
20+
/// </summary>
21+
OAuthResponse? OAuth { get; set; }
22+
23+
/// <summary>
24+
/// The twitch application configuration for getting OAuth tokens.
25+
/// </summary>
26+
TwitchAppConfig? TwitchAppConfig { get; set; }
27+
28+
/// <summary>
29+
/// The collection of twitch chats we should read from.
30+
/// </summary>
31+
IEnumerable<TwitchChatConfiguration>? TwitchChats { get; set; }
32+
33+
/// <summary>
34+
/// The collection of usernames to skip reading the messages of.
35+
/// </summary>
36+
IEnumerable<string>? TtsUsernamesToSkip { get; set; }
37+
38+
/// <summary>
39+
/// The collection of phonetic pronunciations of words.
40+
/// </summary>
41+
IDictionary<string, string>? TtsPhoneticUsernames { get; set; }
42+
43+
/// <summary>
44+
/// Writes the configuration file to disk.
45+
/// </summary>
46+
/// <returns>True if successful, false otherwise.</returns>
47+
bool WriteConfiguration();
48+
}

src/TwitchStreamingTools/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static void AddCommonServices(this IServiceCollection collection) {
2222
collection.AddSingleton<ITwitchAccountService, TwitchAccountService>();
2323
collection.AddSingleton<ITwitchClientProxy, TwitchClientProxy>(_ => TwitchClientProxy.Instance);
2424
collection.AddSingleton<ITwitchTtsService, TwitchTtsService>();
25+
collection.AddSingleton<IConfiguration, Configuration>(_ => Configuration.Instance);
2526

2627
collection.AddTransient<MainWindowViewModel>();
2728
collection.AddTransient<AccountViewModel>();

src/TwitchStreamingTools/Services/TwitchAccountService.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111
namespace TwitchStreamingTools.Services;
1212

1313
/// <summary>
14-
/// Manages the credentials in the application, ensuring credentials are kept up-to-date.
14+
/// Manages the credentials in the application, ensuring credentials are kept up to date.
1515
/// </summary>
1616
public class TwitchAccountService : ITwitchAccountService {
17+
/// <summary>
18+
/// The application configuration.
19+
/// </summary>
20+
private readonly IConfiguration _configuration;
21+
1722
/// <summary>
1823
/// The timer used to check the twitch OAuth token against the API.
1924
/// </summary>
@@ -28,7 +33,9 @@ public class TwitchAccountService : ITwitchAccountService {
2833
/// Initializes a new instance of the <see cref="TwitchAccountService" /> class.
2934
/// </summary>
3035
/// <param name="twitchClient">The twitch chat client.</param>
31-
public TwitchAccountService(ITwitchClientProxy twitchClient) {
36+
/// <param name="configuration">The application configuration.</param>
37+
public TwitchAccountService(ITwitchClientProxy twitchClient, IConfiguration configuration) {
38+
_configuration = configuration;
3239
_twitchClient = twitchClient;
3340
_timer = new DispatcherTimer {
3441
Interval = TimeSpan.FromSeconds(5)
@@ -68,14 +75,14 @@ public async Task UpdateCredentials(string bearer, string refresh, DateTime expi
6875
// Do nothing
6976
}
7077

71-
Configuration.Instance.OAuth = new OAuthResponse {
78+
_configuration.OAuth = new OAuthResponse {
7279
Bearer = bearer,
7380
Refresh = refresh,
7481
ExpiresUtc = expires
7582
};
7683

77-
Configuration.Instance.TwitchUsername = user?.username;
78-
Configuration.Instance.WriteConfiguration();
84+
_configuration.TwitchUsername = user?.username;
85+
_configuration.WriteConfiguration();
7986
_twitchClient.TwitchOAuthToken = bearer;
8087
_twitchClient.TwitchUsername = user?.username;
8188

@@ -85,13 +92,13 @@ public async Task UpdateCredentials(string bearer, string refresh, DateTime expi
8592

8693
/// <inheritdoc />
8794
public void DeleteCredentials() {
88-
Configuration.Instance.OAuth = null;
89-
Configuration.Instance.TwitchUsername = null;
95+
_configuration.OAuth = null;
96+
_configuration.TwitchUsername = null;
9097
_twitchClient.TwitchOAuthToken = null;
9198
_twitchClient.TwitchUsername = null;
9299
CredentialsAreValid = false;
93100
TwitchUsername = null;
94-
Configuration.Instance.WriteConfiguration();
101+
_configuration.WriteConfiguration();
95102

96103
OnCredentialsChanged?.Invoke(null);
97104
OnCredentialsStatusChanged?.Invoke(false);
@@ -152,11 +159,11 @@ private async Task DoTokenRefreshIfNearExpiration() {
152159
await twitchApi.RefreshAccessToken();
153160

154161
// Update the configuration
155-
Configuration.Instance.OAuth = new OAuthResponse {
162+
_configuration.OAuth = new OAuthResponse {
156163
Bearer = twitchApi.OAuth.AccessToken,
157164
Refresh = twitchApi.OAuth.RefreshToken,
158165
ExpiresUtc = twitchApi.OAuth.ExpiresUtc ?? DateTime.MinValue
159166
};
160-
Configuration.Instance.WriteConfiguration();
167+
_configuration.WriteConfiguration();
161168
}
162169
}

src/TwitchStreamingTools/Services/TwitchTtsService.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public class TwitchTtsService : ITwitchTtsService {
2525
/// </summary>
2626
private readonly IList<TwitchChatTts> _chats = new List<TwitchChatTts>();
2727

28+
/// <summary>
29+
/// The application configuration.
30+
/// </summary>
31+
private readonly IConfiguration _configuration;
32+
2833
/// <summary>
2934
/// The logger.
3035
/// </summary>
@@ -44,7 +49,9 @@ public class TwitchTtsService : ITwitchTtsService {
4449
/// Initializes a new instance of the <see cref="TwitchTtsService" /> class.
4550
/// </summary>
4651
/// <param name="twitchClientProxy">The twitch chat client.</param>
47-
public TwitchTtsService(ITwitchClientProxy twitchClientProxy) {
52+
/// <param name="configuration">The application configuration.</param>
53+
public TwitchTtsService(ITwitchClientProxy twitchClientProxy, IConfiguration configuration) {
54+
_configuration = configuration;
4855
_twitchClientProxy = twitchClientProxy;
4956
_thread = new Thread(Main) {
5057
IsBackground = true
@@ -78,7 +85,7 @@ private void Main() {
7885
/// Connects to any configuration found in the config file that we are not currently connected to.
7986
/// </summary>
8087
private void ConnectChatsInConfig() {
81-
List<string?>? missing = Configuration.Instance.TwitchChats?
88+
List<string?>? missing = _configuration.TwitchChats?
8289
.Select(c => c.TwitchChannel)
8390
.Except(_chats?.Select(c => c.ChatConfig?.TwitchChannel) ?? [])
8491
.Where(i => !string.IsNullOrWhiteSpace(i))
@@ -89,7 +96,7 @@ private void ConnectChatsInConfig() {
8996
}
9097

9198
foreach (string? newChat in missing) {
92-
var tts = new TwitchChatTts(_twitchClientProxy, Configuration.Instance.TwitchChats?.FirstOrDefault(t => t.TwitchChannel == newChat));
99+
var tts = new TwitchChatTts(_configuration, _twitchClientProxy, _configuration.TwitchChats?.FirstOrDefault(t => t.TwitchChannel == newChat));
93100
tts.Connect();
94101
_chats?.Add(tts);
95102
}
@@ -101,7 +108,7 @@ private void ConnectChatsInConfig() {
101108
private void DisconnectChatsNotInConfig() {
102109
List<string?>? chatsNotInConfig = _chats?
103110
.Select(c => c.ChatConfig?.TwitchChannel)
104-
.Except(Configuration.Instance.TwitchChats?.Select(c => c?.TwitchChannel) ?? [])
111+
.Except(_configuration.TwitchChats?.Select(c => c?.TwitchChannel) ?? [])
105112
.Where(i => !string.IsNullOrWhiteSpace(i))
106113
.ToList();
107114

src/TwitchStreamingTools/Tts/TtsFilter/CommandFilter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ public class CommandFilter : ITtsFilter {
1111
/// <summary>
1212
/// Handles filtering commands from being spoken in chat.
1313
/// </summary>
14+
/// <param name="configuration">The application configuration.</param>
1415
/// <param name="twitchInfo">The information on the original chat message.</param>
1516
/// <param name="username">The username of the twitch chatter for TTS to say.</param>
1617
/// <param name="currentMessage">The message from twitch chat.</param>
1718
/// <returns>The new TTS message and username.</returns>
18-
public Tuple<string, string> Filter(OnMessageReceivedArgs twitchInfo, string username, string currentMessage) {
19+
public Tuple<string, string> Filter(IConfiguration configuration, OnMessageReceivedArgs twitchInfo, string username, string currentMessage) {
1920
if (currentMessage.StartsWith("!")) {
2021
currentMessage = "";
2122
}

src/TwitchStreamingTools/Tts/TtsFilter/EmojiDeduplicationFilter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ public class EmojiDeduplicationFilter : ITtsFilter {
2121
/// <summary>
2222
/// Removes duplicate emotes from a message.
2323
/// </summary>
24+
/// <param name="configuration">The application configuration.</param>
2425
/// <param name="twitchInfo">The information on the original chat message.</param>
2526
/// <param name="username">The username of the twitch chatter for TTS to say.</param>
2627
/// <param name="currentMessage">The message from twitch chat.</param>
2728
/// <returns>The new TTS message and username.</returns>
28-
public Tuple<string, string> Filter(OnMessageReceivedArgs twitchInfo, string username, string currentMessage) {
29+
public Tuple<string, string> Filter(IConfiguration configuration, OnMessageReceivedArgs twitchInfo, string username, string currentMessage) {
2930
// The list of all recognized emotes.
3031
var emoteText = new HashSet<string>();
3132

src/TwitchStreamingTools/Tts/TtsFilter/ITtsFilter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ internal interface ITtsFilter {
1111
/// <summary>
1212
/// Filters a message from TTS.
1313
/// </summary>
14+
/// <param name="configuration">The application configuration.</param>
1415
/// <param name="twitchInfo">The information on the original chat message.</param>
1516
/// <param name="username">The username of the twitch chatter for TTS to say.</param>
1617
/// <param name="currentMessage">The message from twitch chat.</param>
1718
/// <returns>The new TTS message and username.</returns>
18-
Tuple<string, string> Filter(OnMessageReceivedArgs twitchInfo, string username, string currentMessage);
19+
Tuple<string, string> Filter(IConfiguration configuration, OnMessageReceivedArgs twitchInfo, string username, string currentMessage);
1920
}

src/TwitchStreamingTools/Tts/TtsFilter/LinkFilter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ internal class LinkFilter : ITtsFilter {
1212
/// <summary>
1313
/// Filters out links from text to speech.
1414
/// </summary>
15+
/// <param name="configuration">The application configuration.</param>
1516
/// <param name="twitchInfo">The information on the original chat message.</param>
1617
/// <param name="username">The username of the twitch chatter for TTS to say.</param>
1718
/// <param name="currentMessage">The message from twitch chat.</param>
1819
/// <returns>The new TTS message and username.</returns>
19-
public Tuple<string, string> Filter(OnMessageReceivedArgs twitchInfo, string username, string currentMessage) {
20+
public Tuple<string, string> Filter(IConfiguration configuration, OnMessageReceivedArgs twitchInfo, string username, string currentMessage) {
2021
// Protect against removing ellipsis
2122
MatchCollection matches = Regex.Matches(currentMessage, Constants.REGEX_URL, RegexOptions.CultureInvariant);
2223
foreach (Match match in matches) {

0 commit comments

Comments
 (0)