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

Commit

Permalink
Adjusted dispatcher call pattern in MessageViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
Avid29 committed May 10, 2022
1 parent 19679e9 commit 4387fa6
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions src/Quarrel.ViewModels/ViewModels/Panels/MessagesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ public MessagesViewModel(IMessenger messenger, IDiscordService discordService, I
_messenger.Register<NavigateToChannelMessage<IBindableSelectableChannel>>(this, (_, m) => SelectedChannel = m.Channel);
_messenger.Register<MessageCreatedMessage>(this, (_, m) =>
{
if (SelectedChannel?.Id == m.Message.ChannelId)
{
AppendMessage(m.Message);
}
if (SelectedChannel?.Id != m.Message.ChannelId) return;
_dispatcherService.RunOnUIThread(() => AppendMessage(m.Message));
});
}

Expand All @@ -60,7 +58,7 @@ public IBindableSelectableChannel? SelectedChannel
{
if (SetProperty(ref _selectedChannel, value))
{
LoadChannel(value);
LoadChannel();
}
}
}
Expand All @@ -74,47 +72,48 @@ public bool IsLoading
private set => SetProperty(ref _isLoading, value);
}

private void LoadChannel(IBindableSelectableChannel? channel)
private void LoadChannel()
{
if (channel is IBindableMessageChannel messageChannel)
if (SelectedChannel is IBindableMessageChannel)
{
LoadInitialMessages(messageChannel);
LoadInitialMessages();
}
}

private void LoadInitialMessages(IBindableMessageChannel? channel)
/// <remarks>
/// Must be called on the UI thread.
/// </remarks>
private async void LoadInitialMessages()
{
IBindableMessageChannel? channel = SelectedChannel as IBindableMessageChannel;
Guard.IsNotNull(channel, nameof(channel));
_dispatcherService.RunOnUIThread(async () =>
{
// Clear the messages and begin loading
Source.Clear();
IsLoading = true;
// Clear the messages and begin loading
Source.Clear();
IsLoading = true;

// Load messages
var messages = await _discordService.GetChannelMessagesAsync(channel);
BindableMessage[] bindableMessages = new BindableMessage[messages.Length];
if (bindableMessages.Length > 0)
// Load messages
var messages = await _discordService.GetChannelMessagesAsync(channel);
BindableMessage[] bindableMessages = new BindableMessage[messages.Length];
if (bindableMessages.Length > 0)
{
bindableMessages[0] = new BindableMessage(_messenger, _discordService, _dispatcherService, messages[messages.Length - 1]);
for (int i = 1; i < messages.Length; i++)
{
bindableMessages[0] = new BindableMessage(_messenger, _discordService, _dispatcherService, messages[messages.Length - 1]);
for (int i = 1; i < messages.Length; i++)
{
bindableMessages[i] = new BindableMessage(_messenger, _discordService, _dispatcherService, messages[messages.Length - 1 - i], messages[messages.Length - i]);
}
bindableMessages[i] = new BindableMessage(_messenger, _discordService, _dispatcherService, messages[messages.Length - 1 - i], messages[messages.Length - i]);
}
}

// Add messages to the UI and mark loading as finished
Source.AddRange(bindableMessages);
IsLoading = false;
});
// Add messages to the UI and mark loading as finished
Source.AddRange(bindableMessages);
IsLoading = false;
}

/// <remarks>
/// Must be called on the UI thread.
/// </remarks>
private void AppendMessage(Message message)
{
_dispatcherService.RunOnUIThread(() =>
{
Source.Add(new BindableMessage(_messenger, _discordService, _dispatcherService, message, Source.Count > 0 ? Source[Source.Count-1].Message : null));
});
Source.Add(new BindableMessage(_messenger, _discordService, _dispatcherService, message, Source.Count > 0 ? Source[Source.Count - 1].Message : null));
}
}
}

0 comments on commit 4387fa6

Please sign in to comment.