diff --git a/Slackord/AppShell.xaml b/Slackord/AppShell.xaml
index dc46c29..02e0f8a 100644
--- a/Slackord/AppShell.xaml
+++ b/Slackord/AppShell.xaml
@@ -5,7 +5,7 @@
xmlns:local="clr-namespace:MenuApp"
Shell.FlyoutBehavior="Disabled">
-
diff --git a/Slackord/Classes/DiscordBot.cs b/Slackord/Classes/DiscordBot.cs
index 5a92cc4..52104d6 100644
--- a/Slackord/Classes/DiscordBot.cs
+++ b/Slackord/Classes/DiscordBot.cs
@@ -4,49 +4,56 @@
using Discord.Rest;
using Discord.WebSocket;
using MenuApp;
+using System;
namespace Slackord.Classes
{
class DiscordBot
{
- public DiscordSocketClient _discordClient;
+ public DiscordSocketClient DiscordClient { get; set; }
public IServiceProvider _services;
public async Task MainAsync(string discordToken)
{
- Editor debugWindow = new();
- MainThread.BeginInvokeOnMainThread(() =>
+ if (DiscordClient is not null)
{
- MainPage.WriteToDebugWindow("Starting Slackord Bot..." + "\n");
- });
- _discordClient = new DiscordSocketClient();
+ throw new InvalidOperationException("DiscordClient is already initialized.");
+ }
+ //Editor debugWindow = new();
+ MainPage.WriteToDebugWindow("Starting Slackord Bot..." + "\n");
+
+ //DiscordClient = new DiscordSocketClient();
DiscordSocketConfig _config = new();
{
_config.GatewayIntents = GatewayIntents.DirectMessages | GatewayIntents.GuildMessages | GatewayIntents.Guilds;
}
- _discordClient = new(_config);
+ DiscordClient = new(_config);
_services = new ServiceCollection()
- .AddSingleton(_discordClient)
+ .AddSingleton(DiscordClient)
.BuildServiceProvider();
- _discordClient.Log += DiscordClient_Log;
- await _discordClient.LoginAsync(TokenType.Bot, discordToken.Trim());
- await _discordClient.StartAsync();
- await _discordClient.SetActivityAsync(new Game("for the Slackord command!", ActivityType.Watching));
- _discordClient.Ready += ClientReady;
- _discordClient.LoggedOut += OnClientDisconnect;
- _discordClient.SlashCommandExecuted += SlashCommandHandler;
- await Task.Delay(-1);
+ DiscordClient.Log += DiscordClient_Log;
+ await DiscordClient.LoginAsync(TokenType.Bot, discordToken.Trim());
+ await DiscordClient.StartAsync();
+ await DiscordClient.SetActivityAsync(new Game("for the Slackord command!", ActivityType.Watching));
+ DiscordClient.Ready += ClientReady;
+ DiscordClient.LoggedOut += OnClientDisconnect;
+ DiscordClient.SlashCommandExecuted += SlashCommandHandler;
+ //await Task.Delay(-1);
}
private async Task SlashCommandHandler(SocketSlashCommand command)
{
- if (command.Data.Name.Equals("slackord"))
+ if (command.Data.Name.Equals("slackord") &&
+ DiscordClient.Guilds.FirstOrDefault() is { } guild)
{
- var guildID = _discordClient.Guilds.FirstOrDefault().Id;
- await MainPage.Current.Dispatcher.DispatchAsync(async () =>
- {
- await PostMessagesToDiscord(guildID, command);
- });
+ var guildID = guild.Id;
+ //TODO: Do we actually need to dispatch to the UI thread here?
+ //await MainPage.Current.Dispatcher.DispatchAsync(async () =>
+ //{
+ // await PostMessagesToDiscord(guildID, command);
+ //});
+ await PostMessagesToDiscord(guildID, command);
+
}
}
@@ -61,7 +68,7 @@ private async Task ClientReady()
MainPage.BotConnectionButtonInstance.BackgroundColor = new Microsoft.Maui.Graphics.Color(0, 255, 0);
});
- foreach (var guild in _discordClient.Guilds)
+ foreach (var guild in DiscordClient.Guilds)
{
var guildCommand = new SlashCommandBuilder();
guildCommand.WithName("slackord");
@@ -101,7 +108,7 @@ private Task DiscordClient_Log(LogMessage arg)
public async Task DisconectClient()
{
await MainPage.ChangeBotConnectionButton("Disconnecting", new Microsoft.Maui.Graphics.Color(255, 204, 0), new Microsoft.Maui.Graphics.Color(0, 0, 0));
- await _discordClient.StopAsync();
+ await DiscordClient.StopAsync();
await MainPage.ToggleBotTokenEnable(true, new Microsoft.Maui.Graphics.Color(255, 69, 0));
await MainPage.ChangeBotConnectionButton("Disconnected", new Microsoft.Maui.Graphics.Color(255, 0, 0), new Microsoft.Maui.Graphics.Color(255, 255, 255));
await Task.CompletedTask;
@@ -131,7 +138,7 @@ public async Task PostMessagesToDiscord(ulong guildID, SocketInteraction interac
await interaction.DeferAsync();
- SocketGuild guild = _discordClient.GetGuild(guildID);
+ SocketGuild guild = DiscordClient.GetGuild(guildID);
string categoryName = "Slackord Import";
var slackordCategory = await guild.CreateCategoryChannelAsync(categoryName);
ulong slackordCategoryId = slackordCategory.Id;
@@ -154,7 +161,7 @@ public async Task PostMessagesToDiscord(ulong guildID, SocketInteraction interac
try
{
- await _discordClient.SetActivityAsync(new Game("messages...", ActivityType.Streaming));
+ await DiscordClient.SetActivityAsync(new Game("messages...", ActivityType.Streaming));
int messageCount = 0;
if (channels.TryGetValue(channelName, out var messages))
@@ -209,7 +216,7 @@ public async Task PostMessagesToDiscord(ulong guildID, SocketInteraction interac
if (sendAsThread)
{
- if (_discordClient.GetChannel(createdChannelId) is SocketTextChannel textChannel)
+ if (DiscordClient.GetChannel(createdChannelId) is SocketTextChannel textChannel)
{
await textChannel.SendMessageAsync(messageToSend).ConfigureAwait(false);
var latestMessages = await textChannel.GetMessagesAsync(1).FlattenAsync();
@@ -228,12 +235,16 @@ public async Task PostMessagesToDiscord(ulong guildID, SocketInteraction interac
{
MainPage.WriteToDebugWindow("Caught a Slackdump thread reply exception where a JSON entry had thread_ts and wasn't actually a thread start or reply before it excepted. Sending as a normal message...");
});
- await _discordClient.GetGuild(guildID).GetTextChannel(createdChannelId).SendMessageAsync(messageToSend).ConfigureAwait(false);
+ await DiscordClient.GetGuild(guildID).GetTextChannel(createdChannelId).SendMessageAsync(messageToSend).ConfigureAwait(false);
}
}
else if (sendAsNormalMessage)
{
- await _discordClient.GetGuild(guildID).GetTextChannel(createdChannelId).SendMessageAsync(messageToSend).ConfigureAwait(false);
+ if (DiscordClient.GetGuild(guildID).GetTextChannel(createdChannelId) is { } channel)
+ {
+ await channel.SendMessageAsync(messageToSend).ConfigureAwait(false);
+ }
+ //TODO: else? Do we care?
}
progress += 1;
@@ -290,7 +301,7 @@ public async Task PostMessagesToDiscord(ulong guildID, SocketInteraction interac
}
}
}
- await _discordClient.SetActivityAsync(new Game("for the Slackord command...", ActivityType.Listening));
+ await DiscordClient.SetActivityAsync(new Game("for the Slackord command...", ActivityType.Listening));
}
catch (Exception ex)
{
@@ -298,8 +309,8 @@ public async Task PostMessagesToDiscord(ulong guildID, SocketInteraction interac
{
MainPage.WriteToDebugWindow($"\n{ex.Message}\n");
});
- Page page = new();
- await page.DisplayAlert("Error", ex.Message, "OK");
+ //Page page = new();
+ //await page.DisplayAlert("Error", ex.Message, "OK");
}
}
@@ -309,8 +320,7 @@ public async Task PostMessagesToDiscord(ulong guildID, SocketInteraction interac
});
await interaction.FollowupAsync("All messages sent to Discord successfully!", ephemeral: true);
- await _discordClient.SetActivityAsync(new Game("to some cool music!", ActivityType.Listening));
- await Task.CompletedTask;
+ await DiscordClient.SetActivityAsync(new Game("to some cool music!", ActivityType.Listening));
}
}
}
diff --git a/Slackord/Classes/ImportJson.cs b/Slackord/Classes/ImportJson.cs
index 0c446ef..bbd395b 100644
--- a/Slackord/Classes/ImportJson.cs
+++ b/Slackord/Classes/ImportJson.cs
@@ -4,7 +4,7 @@
namespace Slackord.Classes
{
- class ImportJson
+ static class ImportJson
{
public static readonly Dictionary> Channels = new();
@@ -21,40 +21,31 @@ public static async Task ImportJsonFolder(CancellationToken cancellationToken)
}
selectedFolder = result.Folder.Path;
- subDirectories = Directory.GetDirectories(selectedFolder).ToList();
+ //subDirectories = Directory.GetDirectories(selectedFolder).ToList();
- int folderCount = subDirectories.Count;
+ //int folderCount = subDirectories.Count;
int fileCount = 0;
- foreach (var subDir in subDirectories)
- {
- var folderName = Path.GetFileName(subDir);
- var files = Directory.EnumerateFiles(subDir, "*.*", SearchOption.TopDirectoryOnly)
- .Where(s => s.EndsWith(".JSON", StringComparison.OrdinalIgnoreCase));
-
- fileCount += files.Count();
- }
+ //foreach (var subDir in subDirectories)
+ //{
+ // var folderName = Path.GetFileName(subDir);
+ // var files = Directory.EnumerateFiles(subDir, "*.*", SearchOption.TopDirectoryOnly)
+ // .Where(s => s.EndsWith(".JSON", StringComparison.OrdinalIgnoreCase));
- await MainPage.Current.DisplayAlert("Information", $"Found {fileCount} JSON files in {folderCount} folders.", "OK");
+ // fileCount += files.Count();
+ //}
+ List fileList = new();
- foreach (var subDir in subDirectories)
+ foreach (var file in Directory.EnumerateFiles(selectedFolder, "*.json", SearchOption.AllDirectories))
{
- var folderName = Path.GetFileName(subDir);
- var files = Directory.EnumerateFiles(subDir, "*.*", SearchOption.TopDirectoryOnly)
- .Where(s => s.EndsWith(".JSON", StringComparison.OrdinalIgnoreCase));
-
- // Create a list to store the files for the channel
- List fileList = new();
+ fileCount++;
+ var folderName = Path.GetFileName(Path.GetDirectoryName(file));
- foreach (var file in files)
+ string fileName = Path.GetFileNameWithoutExtension(file);
+ if (DateTime.TryParseExact(fileName, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var fileDate))
{
- string fileName = Path.GetFileNameWithoutExtension(file);
- if (DateTime.TryParseExact(fileName, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var fileDate))
- {
- fileList.Add(file);
- }
+ fileList.Add(file);
}
-
if (fileList.Count > 0)
{
// Add the channel and its file list to the channels dictionary
@@ -65,10 +56,48 @@ public static async Task ImportJsonFolder(CancellationToken cancellationToken)
await parser.ParseJsonFiles(fileList, folderName, Channels);
}
}
+ int folderCount = Channels.Keys.Count;
+ await MainThread.InvokeOnMainThreadAsync(async () =>
+ {
+ await MainPage.Current.DisplayAlert("Information", $"Found {fileCount} JSON files in {folderCount} folders.", "OK");
+ });
+ MainPage.PushDebugText();
+
+ //foreach (var subDir in subDirectories)
+ //{
+ // var folderName = Path.GetFileName(subDir);
+ // var files = Directory.EnumerateFiles(subDir, "*.*", SearchOption.TopDirectoryOnly)
+ // .Where(s => s.EndsWith(".JSON", StringComparison.OrdinalIgnoreCase));
+ //
+ // // Create a list to store the files for the channel
+ // List fileList = new();
+ //
+ // foreach (var file in files)
+ // {
+ // string fileName = Path.GetFileNameWithoutExtension(file);
+ // if (DateTime.TryParseExact(fileName, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var fileDate))
+ // {
+ // fileList.Add(file);
+ // }
+ // }
+ //
+ // if (fileList.Count > 0)
+ // {
+ // // Add the channel and its file list to the channels dictionary
+ // Channels[folderName] = fileList;
+ //
+ // // Parse JSON files for the channel
+ // var parser = new Parser();
+ // await parser.ParseJsonFiles(fileList, folderName, Channels);
+ // }
+ //}
}
catch (Exception ex)
{
+ //writeLog($"\n\n{ex.Message}\n\n");
MainPage.WriteToDebugWindow($"\n\n{ex.Message}\n\n");
+ //TODO
+ //MainPage.DisplayAlert
Page page = new();
await page.DisplayAlert("Error", ex.Message, "OK");
}
diff --git a/Slackord/Classes/Parser.cs b/Slackord/Classes/Parser.cs
index 9de1951..6e31056 100644
--- a/Slackord/Classes/Parser.cs
+++ b/Slackord/Classes/Parser.cs
@@ -1,23 +1,24 @@
using MenuApp;
using Newtonsoft.Json.Linq;
+using System.Globalization;
namespace Slackord.Classes
{
class Parser
{
- public bool _isFileParsed;
+ //public bool _isFileParsed;
public static readonly List isThreadMessages = new();
public static readonly List isThreadStart = new();
public static int TotalMessageCount;
- public async Task ParseJsonFiles(List files, string channelName, Dictionary> channels)
+ public async Task ParseJsonFiles(IEnumerable files, string channelName, Dictionary> channels)
{
- string character = "⬓";
+ const char character = '⬓';
string parsingLine = $"Begin parsing JSON data for {channelName}...";
int lineLength = parsingLine.Length / 2 + 1;
int boxCharacters = lineLength + 6;
- string boxOutput = new(character[0], boxCharacters);
+ string boxOutput = new(character, boxCharacters);
string leadingSpaces = new(' ', (boxCharacters - lineLength - 2) / 2);
MainPage.WriteToDebugWindow($"{boxOutput}\n⬓ {leadingSpaces}{parsingLine}{leadingSpaces} ⬓\n{boxOutput}\n");
@@ -37,25 +38,9 @@ public async Task ParseJsonFiles(List files, string channelName, Diction
{
var rawTimeDate = pair["ts"];
double oldDateTime = (double)rawTimeDate;
- string convertDateTime = Helpers.ConvertFromUnixTimestampToHumanReadableTime(oldDateTime).ToString("g");
- string newDateTime = convertDateTime.ToString();
-
- // JSON message thread handling.
- if (pair.ContainsKey("reply_count") && pair.ContainsKey("thread_ts"))
- {
- isThreadStart.Add(true);
- isThreadMessages.Add(false);
- }
- else if (pair.ContainsKey("thread_ts") && !pair.ContainsKey("reply_count"))
- {
- isThreadStart.Add(false);
- isThreadMessages.Add(true);
- }
- else
- {
- isThreadStart.Add(false);
- isThreadMessages.Add(false);
- }
+ string convertDateTime = Helpers.ConvertFromUnixTimestampToHumanReadableTime(oldDateTime).ToString("g", CultureInfo.CurrentUICulture);
+ string newDateTime = convertDateTime;
+ ParseThreads(pair);
// JSON message parsing.
if (pair.ContainsKey("text") && !pair.ContainsKey("bot_profile"))
@@ -121,9 +106,10 @@ The following parse is over 2000 characters. Discord does not allow messages ove
if (pair.ContainsKey("bot_profile"))
{
+ //TODO: Clean up try/catch blocks.
try
{
- currentMessageParsing = pair["bot_profile"]["name"].ToString() + ": " + pair["text"] + "\n";
+ currentMessageParsing = pair["bot_profile"]?["name"]?.ToString() + ": " + pair["text"] + "\n";
parsedMessages.Add(currentMessageParsing);
TotalMessageCount += 1;
}
@@ -137,6 +123,7 @@ The following parse is over 2000 characters. Discord does not allow messages ove
}
catch (NullReferenceException)
{
+ //TODO: Include details on which message was ignored.
currentMessageParsing = "A bot message was ignored. Please submit an issue on Github for this.";
}
}
@@ -146,24 +133,44 @@ The following parse is over 2000 characters. Discord does not allow messages ove
}
channels[channelName] = parsedMessages;
- character = "⬓";
parsingLine = $"Parsing for [{channelName}]>[{currentFile}] complete!";
lineLength = parsingLine.Length / 2 + 1;
boxCharacters = lineLength + 6;
- boxOutput = new(character[0], boxCharacters);
+ boxOutput = new(character, boxCharacters);
leadingSpaces = new(' ', (boxCharacters - lineLength - 2) / 2);
- MainPage.WriteToDebugWindow($"{boxOutput}\n⬓ {leadingSpaces}{parsingLine}{leadingSpaces} ⬓\n{boxOutput}\n\n");
+ MainPage.WriteToDebugWindow($"{boxOutput}\n{character} {leadingSpaces}{parsingLine}{leadingSpaces} {character}\n{boxOutput}\n\n");
- _isFileParsed = true;
+ //_isFileParsed = true;
}
catch (Exception ex)
{
MainPage.WriteToDebugWindow($"\n\n{ex.Message}\n\n");
- Page page = new();
- await page.DisplayAlert("Error", ex.Message, "OK");
+ //TODO MainPage.DisplayAlert
+ //Page page = new();
+ //await page.DisplayAlert("Error", ex.Message, "OK");
+ }
+ }
+
+ private static void ParseThreads(JObject pair)
+ {
+
+ // JSON message thread handling.
+ if (pair.ContainsKey("reply_count") && pair.ContainsKey("thread_ts"))
+ {
+ isThreadStart.Add(true);
+ isThreadMessages.Add(false);
+ }
+ else if (pair.ContainsKey("thread_ts") && !pair.ContainsKey("reply_count"))
+ {
+ isThreadStart.Add(false);
+ isThreadMessages.Add(true);
+ }
+ else
+ {
+ isThreadStart.Add(false);
+ isThreadMessages.Add(false);
}
- await Task.CompletedTask;
}
}
}
diff --git a/Slackord/MainPage.xaml b/Slackord/MainPage.xaml
index b20a403..8528764 100644
--- a/Slackord/MainPage.xaml
+++ b/Slackord/MainPage.xaml
@@ -27,12 +27,12 @@
-
-
-
-
-
-
+
+
+
+
+
+
diff --git a/Slackord/MainPage.xaml.cs b/Slackord/MainPage.xaml.cs
index 223e8de..d22cf2b 100644
--- a/Slackord/MainPage.xaml.cs
+++ b/Slackord/MainPage.xaml.cs
@@ -1,10 +1,12 @@
using Discord;
using Slackord.Classes;
+using System.Text;
namespace MenuApp
{
public partial class MainPage : ContentPage
{
+ //TODO: private
public static MainPage Current { get; private set; }
public static Editor DebugWindowInstance { get; set; }
private CancellationTokenSource cancellationTokenSource;
@@ -13,13 +15,18 @@ public partial class MainPage : ContentPage
public static Label ProgressBarTextInstance { get; set; }
public static Button EnterBotTokenButtonInstance { get; set; }
public string DiscordToken;
- private DiscordBot discordBot;
+ private DiscordBot _discordBot;
private bool isFirstRun;
private bool hasValidBotToken;
public MainPage()
{
InitializeComponent();
+ if (Current is not null)
+ {
+ throw new InvalidOperationException("Too many windows");
+ }
+
DebugWindowInstance = DebugWindow;
ProgressBarInstance = ProgressBar;
BotConnectionButtonInstance = BotConnectionButton;
@@ -28,22 +35,35 @@ public MainPage()
ProgressBarTextInstance.IsVisible = false;
ProgressBarInstance.IsVisible = false;
Current = this;
- discordBot = new DiscordBot();
+ _discordBot = new DiscordBot();
+ Loaded += MainPage_Loaded;
+ }
+
+ private void MainPage_Loaded(object sender, EventArgs e)
+ {
Initialize();
}
- private async void Initialize()
+ private void Initialize()
{
- await CheckForFirstRun();
- await CheckForValidBotToken();
+ CheckForFirstRun();
+ CheckForValidBotToken();
}
- private void ImportJson_Clicked(object sender, EventArgs e)
+ private async void ImportJson_Clicked(object sender, EventArgs e)
{
- cancellationTokenSource = new CancellationTokenSource();
- CancellationToken cancellationToken = cancellationTokenSource.Token;
- _ = Slackord.Classes.ImportJson.ImportJsonFolder(cancellationToken);
+ var cts = new CancellationTokenSource();
+ Interlocked.Exchange(ref cancellationTokenSource, cts)?.Cancel();
+ CancellationToken cancellationToken = cts.Token;
+ try
+ {
+ await Task.Run(() => Slackord.Classes.ImportJson.ImportJsonFolder(cancellationToken), cancellationToken);
+ }
+ catch (OperationCanceledException)
+ {
+ //TODO Any thing here?
+ }
}
private void CancelImport_Clicked(object sender, EventArgs e)
@@ -51,14 +71,14 @@ private void CancelImport_Clicked(object sender, EventArgs e)
cancellationTokenSource?.Cancel();
}
- private void EnterBotToken_Clicked(object sender, EventArgs e)
+ private async void EnterBotToken_Clicked(object sender, EventArgs e)
{
- _ = CreateBotTokenPrompt();
+ await CreateBotTokenPrompt();
}
-
- private void ToggleBotConnection_Clicked(object sender, EventArgs e)
+
+ private async void ToggleBotConnection_Clicked(object sender, EventArgs e)
{
- _ = ToggleDiscordConnection();
+ await ToggleDiscordConnection();
}
private async Task ToggleDiscordConnection()
@@ -77,30 +97,31 @@ private async Task ToggleDiscordConnection()
DiscordToken = Preferences.Get("SlackordBotToken", string.Empty).Trim();
}
- if (discordBot._discordClient == null)
+ if (_discordBot.DiscordClient == null)
{
- discordBot = new DiscordBot();
+ //_discordBot = new DiscordBot();
await ChangeBotConnectionButton("Connecting", new Microsoft.Maui.Graphics.Color(255, 255, 0), new Microsoft.Maui.Graphics.Color(0, 0, 0));
- await discordBot.MainAsync(DiscordToken);
+ await _discordBot.MainAsync(DiscordToken);
}
- else if (discordBot._discordClient != null && discordBot._discordClient.ConnectionState == ConnectionState.Disconnected)
+ else if (_discordBot.DiscordClient is { ConnectionState: ConnectionState.Disconnected } disconnectedClient)
+ //else if (discordBot.DiscordClient != null && discordBot.DiscordClient.ConnectionState == ConnectionState.Disconnected)
{
await ChangeBotConnectionButton("Connecting", new Microsoft.Maui.Graphics.Color(255, 255, 0), new Microsoft.Maui.Graphics.Color(0, 0, 0));
- await discordBot._discordClient.StartAsync();
+ await disconnectedClient.StartAsync();
while (true)
{
- var connectionState = discordBot._discordClient.ConnectionState;
- if (connectionState.ToString() == "Connected")
+ var connectionState = disconnectedClient.ConnectionState;
+ if (connectionState == ConnectionState.Connected)
{
await ChangeBotConnectionButton("Connected", new Microsoft.Maui.Graphics.Color(0, 255, 0), new Microsoft.Maui.Graphics.Color(0, 0, 0));
break;
}
}
}
- else if (discordBot._discordClient.ConnectionState == ConnectionState.Connected)
+ else if (_discordBot.DiscordClient.ConnectionState == ConnectionState.Connected)
{
await ChangeBotConnectionButton("Disconnecting", new Microsoft.Maui.Graphics.Color(255, 255, 0), new Microsoft.Maui.Graphics.Color(0, 0, 0));
- await discordBot._discordClient.StopAsync();
+ await _discordBot.DiscordClient.StopAsync();
await ChangeBotConnectionButton("Disconnected", new Microsoft.Maui.Graphics.Color(255, 0, 0), new Microsoft.Maui.Graphics.Color(255, 255, 255));
}
}
@@ -112,7 +133,6 @@ private void CheckForUpdates_Clicked(object sender, EventArgs e)
private static async Task CheckForNewVersion()
{
await UpdateCheck.CheckForUpdates();
- await Task.CompletedTask;
}
private void About_Clicked(object sender, EventArgs e)
@@ -149,9 +169,9 @@ Would you like to open the donation page now?
}
}
- private void Exit_Clicked(object sender, EventArgs e)
+ private async void Exit_Clicked(object sender, EventArgs e)
{
- _ = ExitApplication();
+ await ExitApplication();
}
private async Task ExitApplication()
@@ -167,7 +187,7 @@ private async Task ExitApplication()
}
else if (operatingSystem == DevicePlatform.WinUI)
{
- Application.Current.Quit();
+ Application.Current.Quit();
}
}
}
@@ -202,7 +222,7 @@ private async Task CreateBotTokenPrompt()
{
Preferences.Default.Set("SlackordBotToken", discordToken);
DiscordToken = discordToken;
- await CheckForValidBotToken();
+ CheckForValidBotToken();
if (hasValidBotToken)
{
WriteToDebugWindow("Slackord received a valid bot token. Bot connection is enabled!\n");
@@ -216,17 +236,44 @@ private async Task CreateBotTokenPrompt()
else
{
Preferences.Default.Set("SlackordBotToken", string.Empty);
- await CheckForValidBotToken();
+ CheckForValidBotToken();
}
}
}
+ //This is just as an example
+ private static readonly StringBuilder debugOutput = new();
public static void WriteToDebugWindow(string text)
{
- DebugWindowInstance.Text += text;
+ debugOutput.Append(text);
+ //if (!MainThread.IsMainThread)
+ //{
+ // MainThread.BeginInvokeOnMainThread(() =>
+ // {
+ // WriteToDebugWindow(text);
+ // });
+ // return;
+ //}
+ //
+ //DebugWindowInstance.Text += text;
}
- private async Task CheckForFirstRun()
+ public static void PushDebugText()
+ {
+ if (!MainThread.IsMainThread)
+ {
+ MainThread.BeginInvokeOnMainThread(() =>
+ {
+ PushDebugText();
+ });
+ return;
+ }
+
+ DebugWindowInstance.Text += debugOutput.ToString();
+ debugOutput.Clear();
+ }
+
+ private void CheckForFirstRun()
{
if (Preferences.Default.ContainsKey("FirstRun"))
{
@@ -248,11 +295,10 @@ private async Task CheckForFirstRun()
isFirstRun = true;
WriteToDebugWindow("Welcome to Slackord!\n");
}
- await Task.CompletedTask;
}
- private async Task CheckForValidBotToken()
+ private void CheckForValidBotToken()
{
if (isFirstRun)
{
@@ -286,7 +332,7 @@ private async Task CheckForValidBotToken()
else
{
DiscordToken = Preferences.Default.Get("SlackordBotToken", string.Empty).Trim();
- if (DiscordToken .Length > 30)
+ if (DiscordToken.Length > 30)
{
BotConnectionButton.BackgroundColor = new Microsoft.Maui.Graphics.Color(255, 69, 0);
BotConnectionButton.IsEnabled = true;
@@ -301,15 +347,15 @@ private async Task CheckForValidBotToken()
WriteToDebugWindow("Slackord tried to load your last token, but wasn't successful. Please re-enter a new, valid token.\n");
}
}
- await Task.CompletedTask;
}
+ //TODO: Consider either making this just a void return
+ //Or change to MainThread.InvokeOnMainThreadAsync
public static async Task ChangeBotConnectionButton(string state, Microsoft.Maui.Graphics.Color backgroundColor, Microsoft.Maui.Graphics.Color textColor)
{
- Page currentPage = Current;
BotConnectionButtonInstance.BackgroundColor = backgroundColor;
- if (currentPage is MainPage mainPage)
+ if (Current is MainPage mainPage)
{
MainThread.BeginInvokeOnMainThread(() =>
{
@@ -321,7 +367,6 @@ public static async Task ChangeBotConnectionButton(string state, Microsoft.Maui.
}
});
}
- await Task.CompletedTask;
}
public static async Task ToggleBotTokenEnable(bool isEnabled, Microsoft.Maui.Graphics.Color color)
@@ -331,17 +376,31 @@ public static async Task ToggleBotTokenEnable(bool isEnabled, Microsoft.Maui.Gra
EnterBotTokenButtonInstance.BackgroundColor = color;
EnterBotTokenButtonInstance.IsEnabled = isEnabled;
});
- await Task.CompletedTask;
}
public static async Task CommitProgress(float progress, float totalMessagesToSend)
{
- ProgressBarInstance.IsVisible = true;
- ProgressBarTextInstance.IsVisible = true;
+ if (!MainThread.IsMainThread)
+ {
+ await MainThread.InvokeOnMainThreadAsync(async () =>
+ {
+ await CommitProgress(progress, totalMessagesToSend);
+ });
+ return;
+ }
+
+ Current.ProgressBar.IsVisible = true;
+ Current.ProgressBarText.IsVisible = true;
var currentProgress = progress / totalMessagesToSend;
- ProgressBarTextInstance.Text = $"{progress} of {totalMessagesToSend} messages sent.";
- ProgressBarInstance.Progress = currentProgress;
- await Task.CompletedTask;
+ Current.ProgressBarText.Text = $"{progress} of {totalMessagesToSend} messages sent.";
+ Current.ProgressBar.Progress = currentProgress;
+
+ //ProgressBarInstance.IsVisible = true;
+ //ProgressBarTextInstance.IsVisible = true;
+ //var currentProgress = progress / totalMessagesToSend;
+ //ProgressBarTextInstance.Text = $"{progress} of {totalMessagesToSend} messages sent.";
+ //ProgressBarInstance.Progress = currentProgress;
+ //await Task.CompletedTask;
}
}
}
\ No newline at end of file
diff --git a/Slackord/Resources/Styles/Colors.xaml b/Slackord/Resources/Styles/Colors.xaml
index 245758b..8018e9a 100644
--- a/Slackord/Resources/Styles/Colors.xaml
+++ b/Slackord/Resources/Styles/Colors.xaml
@@ -4,7 +4,7 @@
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
- #512BD4
+ OrangeRed
#DFD8F7
#2B0B98
White
diff --git a/Slackord/Resources/Styles/Styles.xaml b/Slackord/Resources/Styles/Styles.xaml
index dc4a034..73f21fb 100644
--- a/Slackord/Resources/Styles/Styles.xaml
+++ b/Slackord/Resources/Styles/Styles.xaml
@@ -24,8 +24,8 @@