-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Background notifications, inline replies, stickers. (#56)
* initial background notifications * initial sticker/inline reply work * Inline replies, namespace consolidation * Bugfixes, background notification toggle * update D#+ * bugfix
- Loading branch information
Showing
150 changed files
with
2,541 additions
and
836 deletions.
There are no files selected for viewing
Submodule DSharpPlus
updated
22 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using DSharpPlus; | ||
using Windows.Data.Xml.Dom; | ||
using Windows.UI.Notifications; | ||
|
||
namespace Unicord.Universal.Background | ||
{ | ||
public class BadgeManager | ||
{ | ||
private BadgeUpdater _badgeUpdateManager; | ||
private DiscordClient _discord; | ||
|
||
public BadgeManager(DiscordClient client) | ||
{ | ||
_discord = client; | ||
_badgeUpdateManager = BadgeUpdateManager.CreateBadgeUpdaterForApplication("App"); | ||
} | ||
|
||
public void Update() | ||
{ | ||
try | ||
{ | ||
var unread = _discord.Guilds.Values.Any(g => g.Unread); | ||
if (!unread) | ||
return; | ||
|
||
var badgeNumber = _discord.Guilds.Values.Sum(g => g.MentionCount) + _discord.PrivateChannels.Values.Sum(g => g.ReadState?.MentionCount); | ||
if (badgeNumber != 0) | ||
{ | ||
var badgeXml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber); | ||
var badgeElement = badgeXml.SelectSingleNode("/badge") as XmlElement; | ||
|
||
badgeElement.SetAttribute("value", badgeNumber.ToString()); | ||
_badgeUpdateManager.Update(new BadgeNotification(badgeXml)); | ||
} | ||
else if (unread) | ||
{ | ||
var badgeXml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeGlyph); | ||
var badgeElement = badgeXml.SelectSingleNode("/badge") as XmlElement; | ||
|
||
badgeElement.SetAttribute("value", "available"); | ||
_badgeUpdateManager.Update(new BadgeNotification(badgeXml)); | ||
} | ||
else | ||
{ | ||
_badgeUpdateManager.Clear(); | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
// TODO: log | ||
} | ||
} | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
Unicord.Universal.Background/NotificationApplicationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
using DSharpPlus; | ||
using DSharpPlus.Entities; | ||
using DSharpPlus.EventArgs; | ||
using Unicord.Universal.Background.Properties; | ||
using Windows.Storage; | ||
using Windows.UI.Notifications; | ||
|
||
namespace Unicord.Universal.Background | ||
{ | ||
class NotificationApplicationContext : ApplicationContext | ||
{ | ||
private DiscordClient _discord = null; | ||
private BadgeManager _badgeManager = null; | ||
private ToastManager _toastManager = null; | ||
|
||
private NotifyIcon _notifyIcon; | ||
private ContextMenuStrip _contextMenu; | ||
private ToolStripMenuItem _closeMenuItem; | ||
|
||
private Task _connectTask; | ||
private string _token = null; | ||
|
||
public NotificationApplicationContext() | ||
{ | ||
Application.ApplicationExit += OnApplicationExit; | ||
|
||
if (!TryGetToken(out _token)) | ||
{ | ||
ExitThread(); | ||
} | ||
|
||
_notifyIcon = new NotifyIcon(); | ||
_notifyIcon.Icon = Resources.TrayIcon; | ||
_notifyIcon.Text = "Unicord"; | ||
|
||
_contextMenu = new ContextMenuStrip(); | ||
_contextMenu.SuspendLayout(); | ||
|
||
_closeMenuItem = new ToolStripMenuItem("Close"); | ||
_closeMenuItem.Click += OnCloseMenuItemClicked; | ||
_contextMenu.Items.Add(_closeMenuItem); | ||
|
||
_contextMenu.ResumeLayout(false); | ||
_notifyIcon.ContextMenuStrip = _contextMenu; | ||
|
||
_connectTask = Task.Run(async () => await InitialiseAsync()); | ||
_notifyIcon.Visible = true; | ||
} | ||
|
||
private void OnCloseMenuItemClicked(object sender, EventArgs e) | ||
{ | ||
this.ExitThread(); | ||
} | ||
|
||
private async Task InitialiseAsync() | ||
{ | ||
try | ||
{ | ||
_discord = new DiscordClient(new DiscordConfiguration() | ||
{ | ||
LogLevel = LogLevel.Debug, | ||
TokenType = TokenType.User, | ||
Token = _token, | ||
MessageCacheSize = 0, | ||
UseInternalLogHandler = true | ||
}); | ||
|
||
_badgeManager = new BadgeManager(_discord); | ||
_toastManager = new ToastManager(); | ||
|
||
_discord.MessageCreated += OnDiscordMessage; | ||
_discord.MessageAcknowledged += OnMessageAcknowledged; | ||
|
||
await _discord.ConnectAsync(status: UserStatus.Invisible, idlesince: DateTimeOffset.Now); | ||
} | ||
catch (Exception) | ||
{ | ||
this.ExitThread(); | ||
} | ||
} | ||
|
||
private void OnApplicationExit(object sender, EventArgs e) | ||
{ | ||
if (_discord != null) | ||
_discord.DisconnectAsync().GetAwaiter().GetResult(); | ||
|
||
if (_notifyIcon != null) | ||
{ | ||
_notifyIcon.Visible = false; | ||
_notifyIcon.Dispose(); | ||
} | ||
} | ||
|
||
private Task OnDiscordMessage(MessageCreateEventArgs e) | ||
{ | ||
if (Tools.WillShowToast(e.Message)) | ||
{ | ||
_toastManager?.HandleMessage(e.Message); | ||
_badgeManager?.Update(); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private Task OnMessageAcknowledged(MessageAcknowledgeEventArgs e) | ||
{ | ||
try | ||
{ | ||
_badgeManager?.Update(); | ||
_toastManager?.HandleAcknowledge(e.Channel); | ||
} | ||
catch (Exception) | ||
{ | ||
// TODO: log | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private bool TryGetToken(out string token) | ||
{ | ||
if (ApplicationData.Current.LocalSettings.Values.TryGetValue("Token", out var s)) | ||
{ | ||
token = (string)s; | ||
return true; | ||
} | ||
|
||
token = null; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
using DSharpPlus; | ||
using DSharpPlus.Entities; | ||
using DSharpPlus.EventArgs; | ||
using Windows.Storage; | ||
using Windows.UI.Notifications; | ||
|
||
namespace Unicord.Universal.Background | ||
{ | ||
class Program | ||
{ | ||
private static Mutex _mutex; | ||
|
||
[STAThread] | ||
static void Main(string[] args) | ||
{ | ||
_mutex = new Mutex(true, "{88FE061B-B4D8-41F4-99FE-15870E0F535B}", out var createdNew); | ||
if (!createdNew) return; | ||
|
||
try | ||
{ | ||
Application.EnableVisualStyles(); | ||
Application.SetCompatibleTextRenderingDefault(false); | ||
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2); | ||
Application.Run(new NotificationApplicationContext()); | ||
} | ||
finally | ||
{ | ||
_mutex.Dispose(); | ||
} | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
Unicord.Universal.Background/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.