Skip to content

Commit

Permalink
wip avalonia dms
Browse files Browse the repository at this point in the history
still needs usernames
  • Loading branch information
exp111 committed Nov 27, 2023
1 parent 2ab7299 commit 944f6cb
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 15 deletions.
8 changes: 7 additions & 1 deletion Turbulence.Core/ViewModels/ChannelListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Turbulence.Core.ViewModels;

public partial class ChannelListViewModel : ViewModelBase, IRecipient<SetChannelsMsg>, IRecipient<ServerSelectedMsg>
public partial class ChannelListViewModel : ViewModelBase, IRecipient<SetChannelsMsg>, IRecipient<ServerSelectedMsg>, IRecipient<DMsSelectedMsg>
{
public ObservableList<Channel> Channels { get; } = new();
private readonly IPlatformClient _client = Ioc.Default.GetService<IPlatformClient>()!;
Expand Down Expand Up @@ -126,6 +126,12 @@ public async void Receive(ServerSelectedMsg m)
}
}
}

public void Receive(DMsSelectedMsg message)
{
//TODO: sort?
Channels.ReplaceAll(message.Channels);
}
}

public record SetChannelsMsg(List<Channel> Channels);
Expand Down
19 changes: 18 additions & 1 deletion Turbulence.Core/ViewModels/Design/DesignChannelListViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Turbulence.Discord.Models.DiscordChannel;
using Turbulence.Discord.Models;
using Turbulence.Discord.Models.DiscordChannel;

namespace Turbulence.Core.ViewModels.Design;

Expand Down Expand Up @@ -34,6 +35,22 @@ public DesignChannelListViewModel()
Name = "Category"
},
new()
{
Id = new(0),
RecipientIDs = new Snowflake[]
{
new(12345678)
},
Type = ChannelType.DM,
Name = "DMs"
},
new()
{
Id = new(0),
Type = ChannelType.GUILD_CATEGORY,
Name = "DMs"
},
new()
{
Id = new(4),
Type = ChannelType.GUILD_ANNOUNCEMENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public class DesignServerListViewModel : ServerListViewModel
{
public DesignServerListViewModel()
{
Connected = true;
Servers.AddRange(new List<Guild>()
{
CreateServer(0, "Server"),
CreateServer(1, "Server 2"),
CreateServer(0, "Server"),
});
}

Expand Down
29 changes: 29 additions & 0 deletions Turbulence.Core/ViewModels/ServerListViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Turbulence.Discord;
using Turbulence.Discord.Models.DiscordChannel;
using Turbulence.Discord.Models.DiscordGuild;
using Turbulence.Discord.Models.DiscordUser;
Expand All @@ -9,23 +11,49 @@ namespace Turbulence.Core.ViewModels;

public partial class ServerListViewModel : ViewModelBase, IRecipient<SetServersMsg>
{
private readonly IPlatformClient _client = Ioc.Default.GetService<IPlatformClient>()!;

public List<Channel> PrivateChannels = new();
public List<User> Users = new();
public ObservableList<Guild> Servers { get; } = new();

[ObservableProperty]
private Guild? _selectedServer;

[ObservableProperty]
private bool _DMsSelected = false;

[ObservableProperty]
private bool _connected = false;

public event EventHandler? TreeUpdated;

public ServerListViewModel()
{
_client.OnConnectionStatusChanged += (_, args) => Connected = args.Data;
PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(SelectedServer) && SelectedServer is { } server)
Messenger.Send(new ServerSelectedMsg(server));
else if (args.PropertyName == nameof(DMsSelected))
Messenger.Send(new DMsSelectedMsg(PrivateChannels));
};
}

//TODO: selectdms
[RelayCommand]
public void SelectDMs()
{
DMsSelected = true;
SelectedServer = null;
}

[RelayCommand]
public void SelectServer(Guild server)
{
DMsSelected = false;
SelectedServer = server;
}

[RelayCommand]
private void SelectionChanged(Channel channel) => Messenger.Send(new ChannelSelectedMsg(channel));
Expand All @@ -44,3 +72,4 @@ public void Receive(SetServersMsg message)

public record SetServersMsg(List<Channel> PrivateChannels, List<User> Users, List<Guild> Guilds);
public record ServerSelectedMsg(Guild Server);
public record DMsSelectedMsg(List<Channel> Channels);
27 changes: 27 additions & 0 deletions Turbulence.Desktop/Converters/DMNameConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Avalonia.Data.Converters;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Turbulence.Discord.Models;

namespace Turbulence.Desktop.Converters;

public class DMNameConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not Snowflake[] ids)
return null;

//TODO: fetch names from client (cache)
return string.Join<Snowflake>(", ", ids);
}

public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
9 changes: 6 additions & 3 deletions Turbulence.Desktop/DataTemplates/ChannelTemplateSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public class ChannelTemplateSelector : IDataTemplate
Control? ITemplate<object?, Control?>.Build(object? param)
{
var channel = (Channel)param!;
var type = "channel";
if (channel.Type == ChannelType.GUILD_CATEGORY)
type = "category";
var type = channel.Type switch
{
ChannelType.DM => "dm",
ChannelType.GUILD_CATEGORY => "category",
_ => "channel",
};
if (!Templates.TryGetValue(type, out var template))
return Templates["unknown"].Build(param);
return template.Build(param);
Expand Down
20 changes: 20 additions & 0 deletions Turbulence.Desktop/Views/Main/ChannelListView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</UserControl.DataContext>
<UserControl.Resources>
<converters:ObjectEqualsMultiConverter x:Key="EqualsConverter" />
<converters:DMNameConverter x:Key="DMNameConverter" />
</UserControl.Resources>
<UserControl.Styles>
<Style Selector="Button">
Expand All @@ -32,6 +33,25 @@
Background="#2B2D31">
<ItemsControl.DataTemplates>
<dataTemplates:ChannelTemplateSelector>
<!--DMs-->
<DataTemplate x:Key="dm" x:DataType="channel:Channel">
<Button Command="{Binding $parent[ItemsControl].((vm:ChannelListViewModel)DataContext).SelectChannelCommand}"
CommandParameter="{Binding .}"
HorizontalAlignment="Stretch" Margin="4,1">
<Classes.Checked>
<MultiBinding Converter="{StaticResource EqualsConverter}">
<Binding Path="$parent[ItemsControl].((vm:ChannelListViewModel)DataContext).SelectedChannel" />
<Binding Path="." />
</MultiBinding>
</Classes.Checked>
<Grid ColumnDefinitions="20,*">
<!--The icon-->
<TextBlock HorizontalAlignment="Center">#</TextBlock>
<!--User Name-->
<TextBlock Text="{Binding RecipientIDs, Converter={StaticResource DMNameConverter}}" Grid.Column="1" Margin="5,0" />
</Grid>
</Button>
</DataTemplate>
<!--Channels that can be clicked-->
<DataTemplate x:Key="channel" x:DataType="channel:Channel">
<Button Command="{Binding $parent[ItemsControl].((vm:ChannelListViewModel)DataContext).SelectChannelCommand}"
Expand Down
47 changes: 38 additions & 9 deletions Turbulence.Desktop/Views/Main/ServerListView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,48 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Turbulence.Core.ViewModels;assembly=Turbulence.Core"
xmlns:converters="using:Turbulence.Desktop.Converters"
x:Class="Turbulence.Desktop.Views.Main.ServerListView"
mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="500"
x:DataType="vm:ServerListViewModel">
<UserControl.DataContext>
<vm:ServerListViewModel />
</UserControl.DataContext>
<ListBox ItemsSource="{Binding Servers}"
Background="#1E1F22"
SelectedValue="{Binding SelectedServer}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<UserControl.Resources>
<converters:ObjectEqualsMultiConverter x:Key="EqualsConverter" />
</UserControl.Resources>
<UserControl.Styles>
<Style Selector="Button">
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="Button.Checked /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="#404249" />
</Style>
</UserControl.Styles>
<ScrollViewer Background="#1E1F22">
<StackPanel Orientation="Vertical"
IsVisible="{Binding Connected}">
<Button Command="{Binding SelectDMsCommand}"
HorizontalAlignment="Stretch"
Classes.Checked="{Binding DMsSelected}"
Content="DMs" />
<ItemsControl ItemsSource="{Binding Servers}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding $parent[ItemsControl].((vm:ServerListViewModel)DataContext).SelectServerCommand}"
CommandParameter="{Binding .}"
Content="{Binding Name}"
HorizontalAlignment="Stretch">
<Classes.Checked>
<MultiBinding Converter="{StaticResource EqualsConverter}">
<Binding Path="$parent[ItemsControl].((vm:ServerListViewModel)DataContext).SelectedServer" />
<Binding Path="." />
</MultiBinding>
</Classes.Checked>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
</UserControl>
2 changes: 2 additions & 0 deletions Turbulence.Discord/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Client : IPlatformClient
public event EventHandler<Event<Message>>? MessageUpdated;
public event EventHandler<Event<MessageDeleteEvent>>? MessageDeleted;
public event EventHandler<Event<ThreadListSyncEvent>>? ThreadListSync;
public event EventHandler<Event<bool>>? OnConnectionStatusChanged;

public bool Connected => WebSocket.State == WebSocketState.Open;
public HttpClient HttpClient { get; } = new();
Expand Down Expand Up @@ -63,6 +64,7 @@ public async Task Start(string token)

SetWebsocketHeaders();
await WebSocket.ConnectAsync(new Uri($"{gateway.AbsoluteUri}/?encoding=json&v={Api.Version}"), default);
OnConnectionStatusChanged?.Invoke(this, new(true));
await SendIdentify(token);
// Start the tasks // TODO: save the tasks?
_ = Task.Run(ReceiveTask);
Expand Down
1 change: 1 addition & 0 deletions Turbulence.Discord/IPlatformClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface IPlatformClient
public Task<Message[]> GetPinnedMessages(Snowflake channelId);
public Task<SearchResult> SearchMessages(SearchRequest request);
public Task<Guild> GetGuild(Snowflake guildId);
public event EventHandler<Event<bool>>? OnConnectionStatusChanged;
public event EventHandler<Event<Ready>>? Ready;
public event EventHandler<Event<Message>>? MessageCreated;
public event EventHandler<Event<TypingStartEvent>>? TypingStart;
Expand Down

0 comments on commit 944f6cb

Please sign in to comment.