Skip to content

Commit

Permalink
Remove duplicates from selection
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Dec 6, 2024
1 parent d29a464 commit 665d51a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Telegram/Services/ClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,7 @@ public async Task<IDictionary<MessageId, MessageProperties>> GetMessagePropertie
{
var map = new Dictionary<MessageId, MessageProperties>();

foreach (var messageId in messageIds.ToArray())
foreach (var messageId in messageIds)
{
var properties = await SendAsync(new GetMessageProperties(messageId.ChatId, messageId.Id)) as MessageProperties;
if (properties != null)
Expand Down
30 changes: 10 additions & 20 deletions Telegram/ViewModels/DialogViewModel.Delegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public async void SendBotCommand(string command)



public async void Select(MessageViewModel message)
public void Select(MessageViewModel message)
{
if (message.IsService)
{
Expand Down Expand Up @@ -627,26 +627,10 @@ public async void Select(MessageViewModel message)
message.SelectionChanged();
}

RaisePropertyChanged(nameof(CanCopySelectedMessage));
RaisePropertyChanged(nameof(CanReportSelectedMessages));

RaisePropertyChanged(nameof(SelectedCount));

if (_type is DialogType.BusinessReplies)
{
CanDeleteSelectedMessages = true;
CanForwardSelectedMessages = false;
}
else
{
var properties = await ClientService.GetMessagePropertiesAsync(SelectedItems.Values.Select(x => new MessageId(x)));

CanDeleteSelectedMessages = properties.Count > 0 && properties.Values.All(x => x.CanBeDeletedForAllUsers || x.CanBeDeletedOnlyForSelf);
CanForwardSelectedMessages = properties.Count > 0 && properties.Values.All(x => x.CanBeForwarded);
}
UpdateSelectionState();
}

public async void Unselect(MessageViewModel message, bool updateSelection = false)
public void Unselect(MessageViewModel message, bool updateSelection = false)
{
if (message.MediaAlbumId != 0)
{
Expand Down Expand Up @@ -678,6 +662,11 @@ public async void Unselect(MessageViewModel message, bool updateSelection = fals
IsSelectionEnabled = false;
}

UpdateSelectionState();
}

private async void UpdateSelectionState()
{
RaisePropertyChanged(nameof(CanCopySelectedMessage));
RaisePropertyChanged(nameof(CanReportSelectedMessages));

Expand All @@ -690,7 +679,8 @@ public async void Unselect(MessageViewModel message, bool updateSelection = fals
}
else
{
var properties = await ClientService.GetMessagePropertiesAsync(SelectedItems.Values.Select(x => new MessageId(x)));
var selectedItems = SelectedItems.Values.ToList();
var properties = await ClientService.GetMessagePropertiesAsync(selectedItems.Select(x => new MessageId(x)));

CanDeleteSelectedMessages = properties.Count > 0 && properties.Values.All(x => x.CanBeDeletedForAllUsers || x.CanBeDeletedOnlyForSelf);
CanForwardSelectedMessages = properties.Count > 0 && properties.Values.All(x => x.CanBeForwarded);
Expand Down
11 changes: 8 additions & 3 deletions Telegram/ViewModels/DialogViewModel.Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Telegram.Common;
Expand Down Expand Up @@ -271,7 +270,10 @@ private async void DeleteMessages(Chat chat, IList<MessageViewModel> messages)
return;
}

var items = messages.Select(x => x.Get()).ToArray();
var items = messages
.DistinctBy(x => x.Id)
.Select(x => x.Get())
.ToList();

IDictionary<MessageId, MessageProperties> properties;
if (_type == DialogType.BusinessReplies)
Expand All @@ -287,7 +289,10 @@ private async void DeleteMessages(Chat chat, IList<MessageViewModel> messages)
properties = await ClientService.GetMessagePropertiesAsync(items.Select(x => new MessageId(x)));
}

var updated = items.Where(x => properties.ContainsKey(new MessageId(x))).ToArray();
var updated = items
.Where(x => properties.ContainsKey(new MessageId(x)))
.ToList();

if (updated.Empty())
{
return;
Expand Down
15 changes: 13 additions & 2 deletions Telegram/ViewModels/Profile/ProfileTabsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,21 @@ private async void DeleteMessages(Chat chat, IList<MessageWithOwner> messages)
return;
}

var items = messages.Select(x => x.Get()).ToArray();
var items = messages
.DistinctBy(x => x.Id)
.Select(x => x.Get())
.ToList();

var properties = await ClientService.GetMessagePropertiesAsync(items.Select(x => new MessageId(x)));

var updated = items.Where(x => properties.ContainsKey(new MessageId(x))).ToArray();
var updated = items
.Where(x => properties.ContainsKey(new MessageId(x)))
.ToList();

if (updated.Empty())
{
return;
}

var popup = new DeleteMessagesPopup(ClientService, SavedMessagesTopicId, chat, updated, properties);

Expand Down

0 comments on commit 665d51a

Please sign in to comment.