Skip to content

Commit

Permalink
work on displaying unread chats
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPilgaard committed Aug 20, 2023
1 parent ddad0f6 commit 477e0bc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/web/Client/Features/Chat/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public async ValueTask<List<ChatDto>> GetChats(string userId)
var newChats = HandleApiResponse(await _chatClient.GetChats(userId, cachedChats?.Count ?? 0));
if (cachedChats is null)
{
return newChats;
return newChats.OrderByDescending(chat => chat.HasUnreadMessages).ToList();
}

cachedChats.AddRange(newChats);

cachedChats = cachedChats.OrderByDescending(chat => chat.HasUnreadMessages).ToList();

return cachedChats;
}

Expand Down
19 changes: 15 additions & 4 deletions src/web/Client/Features/Chat/LargeChatComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@
<MudAvatar Size="Size.Large" Class="mr-3">
<MudImage Src="@chat.GetChatImage(_currentUser.Id)" loading="lazy" Alt="Avatar" />
</MudAvatar>
<MudText>@chat.GetDisplayName(_currentUser.Id)</MudText>
<MudText>
@if (chat.HasUnreadMessages)
{
<b>@chat.GetDisplayName(_currentUser.Id) (@chat.UnreadMessageCount}</b>
}
else
{
@chat.GetDisplayName(_currentUser.Id)
}
</MudText>

</MudListItem>
<MudDivider DividerType="DividerType.FullWidth" />
Expand Down Expand Up @@ -63,9 +72,9 @@

</Virtualize>
<MudTextField @bind-Value="_userText"
FullWidth
Immediate
AutoFocus
FullWidth
Immediate
AutoFocus
id="chat-message-input"
Class="chat-message-input"
Adornment="Adornment.End"
Expand Down Expand Up @@ -96,6 +105,8 @@
[SupplyParameterFromQuery]
public Guid? ChatId { get; set; }

// TODO: Implement logic for marking messages as read, make sure to update the local cache
protected override async Task OnInitializedAsync()
{
_currentUser = await ProfileCache.GetOrCreateProfileAsync();
Expand Down
6 changes: 4 additions & 2 deletions src/web/Server/Features/Chat/ChatApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ async Task<Results<Ok<List<ChatDto>>, UnauthorizedHttpResult>> (
LastMessageSentUtc = chat.LastMessageSentUtc,
StartedUtc = chat.StartedUtc,
Recipients = chat.Recipients.Select(recipient => recipient.ToUserSlim()).ToList(),
IsUnread = context.UnreadMessages.Any(unreadMessage => unreadMessage.ChatId == chat.Id &&
unreadMessage.RecipientId == userId)
UnreadMessageCount = context.UnreadMessages
.Count(unreadMessage =>
unreadMessage.ChatId == chat.Id &&
unreadMessage.RecipientId == userId)
})
.AsSingleQuery()
.ToListAsync(cancellationToken);
Expand Down
4 changes: 3 additions & 1 deletion src/web/Shared/Chat/ChatDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public class ChatDto
/// </summary>
public string? DisplayName { get; init; }

public bool IsUnread { get; init; } = false;
public int UnreadMessageCount { get; init; }

public List<ChatMessageDto> Messages { get; set; } = new();
public List<UserSlim> Recipients { get; init; } = new();

public DateTime LastMessageSentUtc { get; init; }
public DateTime StartedUtc { get; init; } = DateTime.UtcNow;

public bool HasUnreadMessages => UnreadMessageCount > 0;
}

0 comments on commit 477e0bc

Please sign in to comment.