Skip to content

Commit

Permalink
Add an automatic download of authors' avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Jun 25, 2021
1 parent c58f4fc commit 9a6727e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
32 changes: 29 additions & 3 deletions Form1.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Threading;
using System.IO;
using System.Net.Http;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.Toolkit.Uwp.Notifications;
Expand All @@ -21,8 +23,12 @@ public partial class Form1 : Form
};

private bool cleanupOnShutDown;
private static readonly HttpClient client = new();
private readonly Timer interfaceUpdateTimer = new();

private const string directory = "images";
private const string fallbackAvatar = "https://files.facepunch.com/garry/f549bfc2-2a49-4eb8-a701-3efd7ae046ac.png";

/// <summary>
/// Initialize the form and all its components.
/// </summary>
Expand Down Expand Up @@ -58,11 +64,31 @@ private static void OpenURL(string url)
}
}

/// <summary>
/// Download images from the Internet to save them in the software folder.
/// There is an easier way, but Microsoft seems to restrict some of its features for UWP applications.
/// </summary>
private static async Task DownloadImage(string fileName, Uri uri)
{
// We check if the file already exists to avoid re-downloading it.
if (File.Exists(directory + "/" + fileName + ".jpg"))
return;

// Otherwise, we download it normally.
var path = Path.Combine(directory, $"{fileName}.jpg");

_ = Directory.CreateDirectory(directory);

await File.WriteAllBytesAsync(path, await client.GetByteArrayAsync(uri));
}

/// <summary>
/// Create a Toast notification (Windows) with custom settings.
/// </summary>
public static void CreateToastNotification(Commit data)
public static async Task CreateToastNotification(Commit data)
{
await DownloadImage(data.author, new Uri(string.IsNullOrWhiteSpace(data.avatar) ? fallbackAvatar : data.avatar));

new ToastContentBuilder()
.AddHeader(data.category, data.category, "")
.AddText("Repository: " + data.repository)
Expand All @@ -74,7 +100,7 @@ public static void CreateToastNotification(Commit data)
.AddArgument("url", "https://commits.facepunch.com/" + data.identifier)
)

.AddAppLogoOverride(new Uri(data.avatar), ToastGenericAppLogoCrop.Circle)
.AddAppLogoOverride(new Uri(Directory.GetCurrentDirectory() + "/images/" + data.author + ".jpg"), ToastGenericAppLogoCrop.Circle)
.AddAudio(new Uri("ms-winsoundevent:Notification.Mail"))

.Show(toast =>
Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private static async Task CheckForNewCommits(bool isFirstTime)
{
var userData = item.GetProperty("user");

Form1.CreateToastNotification(new Commit
await Form1.CreateToastNotification(new Commit
{
category = gameCategory,
identifier = stringIdentifier,
Expand Down

0 comments on commit 9a6727e

Please sign in to comment.