Skip to content

Commit

Permalink
Properly support replacing message media
Browse files Browse the repository at this point in the history
Resolves #2844
  • Loading branch information
FrayxRulez committed Oct 29, 2022
1 parent f431825 commit 1ac98a0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 58 deletions.
16 changes: 0 additions & 16 deletions Unigram/Unigram/ViewModels/DialogViewModel.Handle.cs
Expand Up @@ -828,22 +828,6 @@ public void Handle(UpdateAnimatedEmojiMessageClicked update)
}
}

public void Handle(UpdateFile update)
{
var chat = _chat;
if (chat == null)
{
return;
}

var header = _composerHeader;
if (header?.EditingMessageMedia != null && header?.EditingMessageFileId == update.File.Id && update.File.Size == update.File.Remote.UploadedSize)
{
BeginOnUIThread(() => ComposerHeader = null);
ClientService.Send(new EditMessageMedia(chat.Id, header.EditingMessage.Id, null, header.EditingMessageMedia.Delegate(new InputFileId(update.File.Id), header.EditingMessageCaption)));
}
}

private void Handle(long messageId, Action<MessageViewModel> update, Action<MessageBubble, MessageViewModel> action = null)
{
var field = ListField;
Expand Down
49 changes: 19 additions & 30 deletions Unigram/Unigram/ViewModels/DialogViewModel.cs
Expand Up @@ -31,14 +31,15 @@ namespace Unigram.ViewModels
{
public partial class DialogViewModel : TLViewModelBase, IDelegable<IDialogDelegate>
{
private readonly ConcurrentDictionary<long, MessageViewModel> _selectedItems = new ConcurrentDictionary<long, MessageViewModel>();
private readonly ConcurrentDictionary<long, MessageViewModel> _selectedItems = new();
public IDictionary<long, MessageViewModel> SelectedItems => _selectedItems;

public int SelectedCount => SelectedItems.Count;

protected readonly ConcurrentDictionary<long, MessageViewModel> _groupedMessages = new ConcurrentDictionary<long, MessageViewModel>();
protected readonly ConcurrentDictionary<long, MessageViewModel> _groupedMessages = new();

protected static readonly ConcurrentDictionary<long, IList<ChatAdministrator>> _admins = new ConcurrentDictionary<long, IList<ChatAdministrator>>();
protected static readonly Dictionary<long, IList<ChatAdministrator>> _admins = new();
protected static readonly Dictionary<string, MessageContent> _contentOverrides = new();

protected readonly DisposableMutex _loadMoreLock = new DisposableMutex();

Expand Down Expand Up @@ -1476,6 +1477,11 @@ protected Task ProcessMessagesAsync(Chat chat, IList<MessageViewModel> messages)

foreach (var message in messages)
{
if (_contentOverrides.TryGetValue(message.CombinedId, out MessageContent content))
{
message.Content = content;
}

if (message.Content is MessageDice dice)
{
if (message.Id > chat.LastReadInboxMessageId)
Expand Down Expand Up @@ -2268,11 +2274,6 @@ public void ClearReply()
{
if (container.EditingMessage != null)
{
if (container.EditingMessageFileId is int fileId)
{
ClientService.Send(new CancelPreliminaryUploadFile(fileId));
}

var chat = _chat;
if (chat != null)
{
Expand Down Expand Up @@ -2405,31 +2406,20 @@ public async Task SendMessageAsync(string text, IList<TextEntity> entities = nul
if (header?.EditingMessage != null)
{
var editing = header.EditingMessage;

var factory = header.EditingMessageMedia;
if (factory != null)
{
var response = await ClientService.SendAsync(new PreliminaryUploadFile(factory.InputFile, factory.Type, 32));
if (response is File file)
{
if (file.Remote.IsUploadingCompleted)
{
ComposerHeader = null;
ClientService.Send(new EditMessageMedia(chat.Id, header.EditingMessage.Id, null, factory.Delegate(new InputFileId(file.Id), header.EditingMessageCaption)));
}
else
{
ComposerHeader = new MessageComposerHeader
{
EditingMessage = editing,
EditingMessageMedia = factory,
EditingMessageCaption = formattedText,
EditingMessageFileId = file.Id
};
}
}
else
var input = factory.Delegate(factory.InputFile, header.EditingMessageCaption);

var response = await ClientService.SendAsync(new SendMessageAlbum(editing.ChatId, editing.MessageThreadId, editing.ReplyToMessageId, null, new[] { input }, true));
if (response is Messages messages && messages.MessagesValue.Count == 1)
{
// TODO: ...
_contentOverrides[editing.CombinedId] = messages.MessagesValue[0].Content;
Aggregator.Publish(new UpdateMessageContent(editing.ChatId, editing.Id, messages.MessagesValue[0].Content));

ComposerHeader = null;
ClientService.Send(new EditMessageMedia(editing.ChatId, editing.Id, null, input));
}
}
else
Expand Down Expand Up @@ -3994,7 +3984,6 @@ public class MessageComposerHeader
public MessageViewModel ReplyToMessage { get; set; }
public MessageViewModel EditingMessage { get; set; }

public int? EditingMessageFileId { get; set; }
public InputMessageFactory EditingMessageMedia { get; set; }
public FormattedText EditingMessageCaption { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions Unigram/Unigram/ViewModels/MessageViewModel.cs
Expand Up @@ -307,6 +307,8 @@ public override string ToString()
return _message.ToString();
}

public string CombinedId => $"{ChatId},{Id}";

public IClientService ClientService => _clientService;

public ReplyMarkup ReplyMarkup { get => _message.ReplyMarkup; set => _message.ReplyMarkup = value; }
Expand Down
12 changes: 0 additions & 12 deletions Unigram/Unigram/Views/ChatView.xaml.cs
Expand Up @@ -4586,18 +4586,6 @@ public void UpdateGroupCall(Chat chat, GroupCall groupCall)
}
}



public void UpdateFile(File file)
{
var header = ViewModel.ComposerHeader;
if (header?.EditingMessageFileId == file.Id)
{
var size = Math.Max(file.Size, file.ExpectedSize);
ComposerHeaderUpload.Value = (double)file.Remote.UploadedSize / size;
}
}

private void UpdateSticker(object target, File file)
{
var content = target as Grid;
Expand Down

0 comments on commit 1ac98a0

Please sign in to comment.