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

Commit

Permalink
Load DirectMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
Avid29 committed Apr 24, 2022
1 parent 94dcb40 commit 93af57c
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Quarrel.Client.Models.Channels.Interfaces
/// <summary>
/// An interface for direct message channels.
/// </summary>
public interface IDirectChannel : IPrivateChannel, IMessageChannel, IAudioChannel
public interface IDirectChannel : IPrivateChannel
{
ulong RecipientId { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Quarrel.Client.Models.Channels.Interfaces
/// <summary>
/// An interface for group channels.
/// </summary>
public interface IGroupChannel : IPrivateChannel, IMessageChannel, IAudioChannel
public interface IGroupChannel : IPrivateChannel
{
/// <summary>
/// The id of the user that owns the channel.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Quarrel.Client.Models.Channels.Interfaces
/// <summary>
/// An interface for channels in DMs.
/// </summary>
public interface IPrivateChannel : IChannel
public interface IPrivateChannel : IChannel, IMessageChannel, IAudioChannel
{
}
}
18 changes: 18 additions & 0 deletions src/Quarrel.Client/QuarrelClient.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Quarrel.Client.Models.Users;
using Refit;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Quarrel.Client
Expand All @@ -24,6 +26,12 @@ public partial class QuarrelClient
return CurrentUser;
}

public User? GetUser(ulong id)
{
_userMap.TryGetValue(id, out var user);
return user;
}

/// <summary>
/// Gets messages in a channel.
/// </summary>
Expand Down Expand Up @@ -123,6 +131,16 @@ public IPrivateChannel[] GetPrivateChannels()
}

Array.Resize(ref privateChannels, i);
Array.Sort(privateChannels, Comparer<IPrivateChannel>.Create((item1, item2) =>
{
if (!item2.LastMessageId.HasValue) return -1;
if (!item1.LastMessageId.HasValue) return 1;

long compare = (long)item2.LastMessageId.Value - (long)item1.LastMessageId.Value;
if (compare < 0) return -1;
if (compare > 0) return 1;
return 0;
}));

return privateChannels;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Quarrel.Client/QuarrelClient.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ internal bool AddChannel(JsonChannel jsonChannel, ulong? guildId = null)
{
guild.AddChannel(channel.Id);
}
else
else if (jsonChannel.Recipients is not null)
{
foreach (var recipient in jsonChannel.Recipients)
{
AddUser(recipient);
}

_privateChannels.Add(channel.Id);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Quarrel © 2022

using CommunityToolkit.Diagnostics;
using Quarrel.Bindables.Channels.Abstract;
using Quarrel.Bindables.Channels.Interfaces;
using Quarrel.Bindables.Users;
using Quarrel.Client.Models.Channels;
using Quarrel.Client.Models.Channels.Interfaces;
using Quarrel.Services.Discord;
Expand All @@ -11,11 +14,34 @@ namespace Quarrel.Bindables.Channels
/// <summary>
/// A wrapper of an <see cref="IDirectChannel"/> that can be bound to the UI.
/// </summary>
public class BindableDirectChannel : BindablePrivateChannel
public class BindableDirectChannel : BindablePrivateChannel, IBindableMessageChannel
{
internal BindableDirectChannel(IDiscordService discordService, IDispatcherService dispatcherService, DirectChannel directChannel) :
base(discordService, dispatcherService, directChannel)
{
BindableUser? user = _discordService.GetUser(DirectChannel.RecipientId);
Guard.IsNotNull(user);
Recipient = user;
}

/// <inheritdoc/>
public ulong Id => Channel.Id;

/// <inheritdoc/>
public bool IsAccessible => true;

/// <inheritdoc/>
public IMessageChannel MessageChannel => (IMessageChannel)Channel;

/// <inheritdoc/>
public IDirectChannel DirectChannel => (IDirectChannel)Channel;

/// <inheritdoc/>
public override string? Name => Recipient.User.Username;

/// <summary>
/// Gets the recipient of the direct messages as a <see cref="BindableUser"/>.
/// </summary>
public BindableUser Recipient { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,19 @@ public partial class DiscordService

return new BindableSelfUser(this, _dispatcherService, user);
}


/// <inheritdoc/>
public BindableUser? GetUser(ulong id)
{
var user = _quarrelClient.GetUser(id);
if (user is not null)
{
return new BindableUser(this, _dispatcherService, user);
}

return null;
}

/// <inheritdoc/>
public BindableGuild[] GetMyGuilds()
{
Expand Down Expand Up @@ -145,6 +157,7 @@ public async Task<BindableMessage[]> GetChannelMessagesAsync(IBindableMessageCha
foreach (var channel in rawChannels)
{
channels[i] = BindablePrivateChannel.Create(this, _dispatcherService, channel);

if (channels[i] is IBindableSelectableChannel selectableChannel &&
selectableChannel.Id == home.SelectedChannelId)
{
Expand Down
7 changes: 7 additions & 0 deletions src/Quarrel.ViewModels/Services/Discord/IDiscordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public interface IDiscordService
/// <returns>The current user as a <see cref="BindableSelfUser"/>.</returns>
BindableSelfUser? GetMe();

/// <summary>
/// Gets a user by id.
/// </summary>
/// <param name="userId">The id of the user to get.</param>
/// <returns>The user of an id.</returns>
BindableUser? GetUser(ulong userId);

/// <summary>
/// Logs into the discord service by token.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<selectors:ChannelTemplateSelector x:Key="ChannelTemplateSelector"
TextChannelTemplate="{StaticResource TextChannelTemplate}"
DirectChannelTemplate="{StaticResource DirectChannelTemplate}"
VoiceChannelTemplate="{StaticResource VoiceChannelTemplate}"
CategoryChannelTemplate="{StaticResource CategoryZoomInChannelTemplate}"/>

Expand Down
37 changes: 34 additions & 3 deletions src/Quarrel/DataTemplates/ChannelTemplates.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,59 @@
x:Class="Quarrel.DataTemplates.ChannelTemplates"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:a="using:Quarrel.Attached"
xmlns:bindablechannels="using:Quarrel.Bindables.Channels">

<DataTemplate x:Key="TextChannelTemplate" x:DataType="bindablechannels:BindableTextChannel">
<Grid Height="40">
<Grid Visibility="{x:Bind IsSelected, Mode=OneWay}" Opacity=".2" Background="{ThemeResource SystemControlBackgroundAccentBrush}"/>

<Border x:Name="UnreadIndicator"
Visibility="{x:Bind MessageChannel.IsUnread, Mode=OneWay}"
Height="24" Width="2"
Background="{ThemeResource InvertedBackground}"
Margin="2,0,0,0"
HorizontalAlignment="Left" Opacity=".75"/>

<!--Main Details-->
<StackPanel Grid.Column="1" VerticalAlignment="Center" Orientation="Horizontal" Margin="0,6" Padding="6,0,-12,0">
<StackPanel VerticalAlignment="Center" Orientation="Horizontal" Margin="0,6" Padding="6,0,-12,0">
<TextBlock Text="#" Foreground="{ThemeResource SystemControlHighlightAltBaseHighBrush}" Opacity=".6" FontSize="20" FontWeight="Light" Margin="6,-2,4,0"/>
<TextBlock Text="{x:Bind Name, Mode=OneWay}" Opacity=".6" Margin="0,1,0,0"/>
</StackPanel>
</Grid>
</DataTemplate>

<DataTemplate x:Key="DirectChannelTemplate" x:DataType="bindablechannels:BindableDirectChannel">
<Grid Height="48">
<Grid Visibility="{x:Bind IsSelected, Mode=OneWay}" Opacity=".2" Background="{ThemeResource SystemControlBackgroundAccentBrush}"/>

<Border x:Name="UnreadIndicator"
Visibility="{x:Bind MessageChannel.IsUnread, Mode=OneWay}"
Height="24" Width="2"
Background="{ThemeResource InvertedBackground}"
Margin="2,0,0,0"
HorizontalAlignment="Left" Opacity=".75"/>

<!--Main Details-->
<Grid VerticalAlignment="Center" Margin="0,6" Padding="0,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>

<Border Margin="8,0" CornerRadius="18">
<toolkit:ImageEx Width="36" Height="36" Source="{x:Bind Recipient.AvatarUri, Mode=OneWay}"/>
</Border>

<TextBlock Grid.Column="1"
Text="{x:Bind Name, Mode=OneWay}"
Opacity=".7"
VerticalAlignment="Center"/>
</Grid>
</Grid>
</DataTemplate>

<DataTemplate x:Key="VoiceChannelTemplate" x:DataType="bindablechannels:BindableVoiceChannel">
<Grid Height="40" Padding="6,0,0,0">
<Grid Visibility="{x:Bind IsSelected, Mode=OneWay}" Opacity=".2" Background="{ThemeResource SystemControlBackgroundAccentBrush}" Margin="-6,0,0,0"/>
Expand Down
8 changes: 7 additions & 1 deletion src/Quarrel/Selectors/ChannelTemplateSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public class ChannelTemplateSelector : DataTemplateSelector
/// </summary>
public DataTemplate? TextChannelTemplate { get; set; }

/// <summary>
/// Gets or sets the direct channel template.
/// </summary>
public DataTemplate? DirectChannelTemplate { get; set; }

/// <summary>
/// Gets or sets the voice channel template.
/// </summary>
Expand All @@ -35,8 +40,9 @@ public class ChannelTemplateSelector : DataTemplateSelector
return channel switch
{
BindableTextChannel => TextChannelTemplate,
BindableCategoryChannel => CategoryChannelTemplate,
BindableVoiceChannel => VoiceChannelTemplate,
BindableDirectChannel => DirectChannelTemplate,
BindableCategoryChannel => CategoryChannelTemplate,
_ => null,
};
}
Expand Down

0 comments on commit 93af57c

Please sign in to comment.