Skip to content
This repository was archived by the owner on Apr 30, 2024. It is now read-only.

Commit 4af5d75

Browse files
committed
Navigate to Guild with launch args
1 parent 592ba9b commit 4af5d75

16 files changed

+217
-29
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Quarrel © 2022
2+
3+
using System;
4+
using System.Text.RegularExpressions;
5+
6+
namespace Quarrel.Helpers.LaunchArgs.Models
7+
{
8+
/// <summary>
9+
/// The base type for a launch argument class.
10+
/// </summary>
11+
public abstract class LaunchArgsBase
12+
{
13+
/// <summary>
14+
/// A regular expression pattern to determine if a launchUri is valid.
15+
/// </summary>
16+
/// <remarks>
17+
/// Group 1 is all segments.
18+
/// Group 2 is just the first segment of the args.
19+
/// </remarks>
20+
private const string BaseRegex = @"^(?:(?:quarrel)|(?:discord)):\/\/(([^/]*)/.*)$";
21+
22+
/// <summary>
23+
/// Runs after launch.
24+
/// </summary>
25+
public abstract void RunPostLoad(IServiceProvider serviceProvider);
26+
27+
/// <summary>
28+
/// Parses a <see cref="LaunchArgsBase"/> from the launch uri.
29+
/// </summary>
30+
public static LaunchArgsBase? Parse(string launchUri)
31+
{
32+
Match match = Regex.Match(launchUri, BaseRegex);
33+
if (match.Success)
34+
{
35+
switch (match.Groups[2].Value)
36+
{
37+
case "c":
38+
case "channel":
39+
case "g":
40+
case "guild":
41+
return NavigateLaunchArgs.Parse(match.Groups[1].Value);
42+
}
43+
}
44+
45+
return null;
46+
}
47+
}
48+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Quarrel © 2022
2+
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Toolkit.Mvvm.Messaging;
5+
using Quarrel.Helpers.LaunchArgs.Models;
6+
using Quarrel.Messages.Navigation;
7+
using System;
8+
using System.Text.RegularExpressions;
9+
10+
namespace Quarrel.Helpers.LaunchArgs
11+
{
12+
/// <summary>
13+
/// A class for launch args that navigate to a channel or guild on opening.
14+
/// </summary>
15+
public class NavigateLaunchArgs : LaunchArgsBase
16+
{
17+
/// <summary>
18+
/// A regular expression to determine if the launch args are a valid navigate argument.
19+
/// </summary>
20+
/// <remarks>
21+
/// Group 1 is the guildId
22+
/// Group 2 is the channelId
23+
/// </remarks>
24+
private const string NavigateArgsRegex = @"^(?:(?:(?:guild)|g)/([0-9]*)/?)?(?:(?:(?:channel)|c)/([0-9]*))?$";
25+
26+
private NavigateLaunchArgs(ulong? guildId, ulong? channelId)
27+
{
28+
GuildId = guildId;
29+
ChannelId = channelId;
30+
}
31+
32+
/// <summary>
33+
/// The guild id to navigate to.
34+
/// </summary>
35+
public ulong? GuildId { get; }
36+
37+
/// <summary>
38+
/// The channel id to navigate to.
39+
/// </summary>
40+
public ulong? ChannelId { get; }
41+
42+
/// <summary>
43+
/// Parses a <see cref="NavigateLaunchArgs"/> from a launch uri.
44+
/// </summary>
45+
public static new NavigateLaunchArgs? Parse(string args)
46+
{
47+
Match match = Regex.Match(args, NavigateArgsRegex);
48+
if (match.Success)
49+
{
50+
ulong? nullableGuildId = null;
51+
ulong? nullableChannelId = null;
52+
bool hasGuild = ulong.TryParse(match.Groups[1].Value, out ulong guildId);
53+
bool hasChannel = ulong.TryParse(match.Groups[2].Value, out ulong channelId);
54+
if (hasGuild) nullableGuildId = guildId;
55+
if (hasChannel) nullableChannelId = channelId;
56+
return new NavigateLaunchArgs(nullableGuildId, nullableChannelId);
57+
}
58+
59+
return null;
60+
}
61+
62+
/// <inheritdoc/>
63+
public override void RunPostLoad(IServiceProvider serviceProvider)
64+
{
65+
var messenger = serviceProvider.GetRequiredService<IMessenger>();
66+
if (GuildId.HasValue)
67+
{
68+
messenger.Send(new NavigateToGuildMessage<ulong>(GuildId.Value));
69+
}
70+
}
71+
}
72+
}

src/Quarrel.ViewModels/Messages/Discord/ConnectingMessage.cs renamed to src/Quarrel.ViewModels/Messages/ConnectingMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Quarrel © 2022
22

3-
namespace Quarrel.Messages.Discord
3+
namespace Quarrel.Messages
44
{
55
/// <summary>
66
/// A message sent when the app begins connecting to the gateway.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Quarrel © 2022
2+
3+
namespace Quarrel.Messages
4+
{
5+
/// <summary>
6+
/// A message sent when the app connects to the gateway.
7+
/// </summary>
8+
public class GuildsLoadedMessage
9+
{
10+
}
11+
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
// Quarrel © 2022
22

3-
using Quarrel.Bindables.Channels.Interfaces;
4-
53
namespace Quarrel.Messages.Navigation
64
{
75
/// <summary>
86
/// A message sent when navigation to a channel is requested.
97
/// </summary>
10-
public class NavigateToChannelMessage
8+
public class NavigateToChannelMessage<T>
119
{
1210
/// <summary>
13-
/// Initializes a new instance of the <see cref="NavigateToChannelMessage"/> class.
11+
/// Initializes a new instance of the <see cref="NavigateToChannelMessage{T}"/> class.
1412
/// </summary>
15-
public NavigateToChannelMessage(IBindableSelectableChannel channel)
13+
public NavigateToChannelMessage(T channel)
1614
{
1715
Channel = channel;
1816
}
1917

2018
/// <summary>
2119
/// Gets the channel being navigated to.
2220
/// </summary>
23-
public IBindableSelectableChannel Channel { get; }
21+
public T Channel { get; }
2422
}
2523
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
// Quarrel © 2022
22

3-
using Quarrel.Bindables.Guilds;
4-
53
namespace Quarrel.Messages.Navigation
64
{
75
/// <summary>
86
/// A message sent when navigation to a guild is requested.
97
/// </summary>
10-
public class NavigateToGuildMessage
8+
public class NavigateToGuildMessage<T>
119
{
1210
/// <summary>
13-
/// Initializes a new instance of the <see cref="NavigateToGuildMessage"/> class.
11+
/// Initializes a new instance of the <see cref="NavigateToGuildMessage{T}"/> class.
1412
/// </summary>
15-
public NavigateToGuildMessage(BindableGuild guild)
13+
public NavigateToGuildMessage(T guild)
1614
{
1715
Guild = guild;
1816
}
1917

2018
/// <summary>
2119
/// Gets the guild being navigated to.
2220
/// </summary>
23-
public BindableGuild Guild { get; }
21+
public T Guild { get; }
2422
}
2523
}

src/Quarrel.ViewModels/Messages/Discord/UserLoggedInMessage.cs renamed to src/Quarrel.ViewModels/Messages/UserLoggedInMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using Quarrel.Services.Storage.Accounts.Models;
44

5-
namespace Quarrel.Messages.Discord
5+
namespace Quarrel.Messages
66
{
77
/// <summary>
88
/// A message sent when a user is logged in to the discord service.

src/Quarrel.ViewModels/Services/Discord/DiscordService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Microsoft.Toolkit.Mvvm.Messaging;
55
using Quarrel.Client;
66
using Quarrel.Client.Models.Users;
7-
using Quarrel.Messages.Discord;
7+
using Quarrel.Messages;
88
using Quarrel.Services.Analytics;
99
using Quarrel.Services.Analytics.Enums;
1010
using Quarrel.Services.Analytics.Models;
@@ -34,7 +34,7 @@ public DiscordService(IAnalyticsService analyticsService, IMessenger messenger)
3434
_quarrelClient.LoggedIn += OnLoggedIn;
3535
_quarrelClient.HttpExceptionHandled += OnHttpExceptionHandled;
3636
_quarrelClient.GatewayExceptionHandled += OnGatewayExceptionHandled;
37-
37+
3838
_quarrelClient.UnknownGatewayOperationEncountered += OnUnknownGatewayOperationEncountered;
3939
_quarrelClient.UnknownGatewayEventEncountered += OnUnknownGatewayEventEncountered;
4040
_quarrelClient.KnownGatewayEventEncountered += OnKnownGatewayEventEncountered;

src/Quarrel.ViewModels/ViewModels/CurrentUserViewModel.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
using Microsoft.Toolkit.Mvvm.Input;
55
using Microsoft.Toolkit.Mvvm.Messaging;
66
using Quarrel.Bindables.Users;
7-
using Quarrel.Messages.Discord;
8-
using Quarrel.Messages.Navigation.SubPages;
7+
using Quarrel.Messages;
98
using Quarrel.Services.Discord;
109
using Quarrel.Services.Dispatcher;
11-
using Quarrel.ViewModels.SubPages.Meta;
1210

1311
namespace Quarrel.ViewModels
1412
{

src/Quarrel.ViewModels/ViewModels/Panels/ChannelsViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Microsoft.Toolkit.Mvvm.ComponentModel;
44
using Microsoft.Toolkit.Mvvm.Messaging;
55
using Quarrel.Bindables.Channels;
6-
using Quarrel.Bindables.Channels.Abstract;
76
using Quarrel.Bindables.Channels.Interfaces;
87
using Quarrel.Bindables.Guilds;
98
using Quarrel.Messages.Navigation;
@@ -33,7 +32,7 @@ public ChannelsViewModel(IMessenger messenger, IDiscordService discordService)
3332
_messenger = messenger;
3433
_discordService = discordService;
3534

36-
_messenger.Register<NavigateToGuildMessage>(this, (_, m) => LoadChannels(m.Guild));
35+
_messenger.Register<NavigateToGuildMessage<BindableGuild>>(this, (_, m) => LoadChannels(m.Guild));
3736
}
3837

3938
/// <summary>
@@ -56,7 +55,7 @@ public IBindableSelectableChannel? SelectedChannel
5655
{
5756
value.IsSelected = true;
5857
_currentGuild.SelectedChannel = value.Id;
59-
_messenger.Send(new NavigateToChannelMessage(value));
58+
_messenger.Send(new NavigateToChannelMessage<IBindableSelectableChannel>(value));
6059
}
6160
}
6261
}

0 commit comments

Comments
 (0)