-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathDialogBusinessRepliesViewModel.cs
115 lines (96 loc) · 4.65 KB
/
DialogBusinessRepliesViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using Rg.DiffUtils;
using System.Linq;
using System.Threading.Tasks;
using Telegram.Services;
using Telegram.Td.Api;
using Telegram.ViewModels.Delegates;
namespace Telegram.ViewModels
{
public partial class QuickReplyMessageViewModel : MessageViewModel
{
public QuickReplyMessageViewModel(IClientService clientService, IPlaybackService playbackService, IMessageDelegate delegato, Chat chat, Message message, bool processText = false)
: base(clientService, playbackService, delegato, chat, null, message, processText)
{
}
public bool CanBeEdited { get; set; }
}
public partial class DialogBusinessRepliesViewModel : DialogViewModel, IDiffHandler<MessageViewModel>
{
public DialogBusinessRepliesViewModel(IClientService clientService, ISettingsService settingsService, IEventAggregator aggregator, ILocationService locationService, INotificationsService pushService, IPlaybackService playbackService, IVoipService voipService, INetworkService networkService, IStorageService storageService, ITranslateService translateService)
: base(clientService, settingsService, aggregator, locationService, pushService, playbackService, voipService, networkService, storageService, translateService)
{
}
public override DialogType Type => DialogType.BusinessReplies;
public override void Subscribe()
{
Aggregator.Subscribe<UpdateQuickReplyShortcutMessages>(this, Handle);
base.Subscribe();
}
public override async Task LoadQuickReplyShortcutSliceAsync()
{
IsFirstSliceLoaded = true;
IsLastSliceLoaded = true;
Handle(new UpdateQuickReplyShortcutMessages(QuickReplyShortcut.Id, ClientService.GetQuickReplyMessages(QuickReplyShortcut.Id)));
var response = await ClientService.SendAsync(new LoadQuickReplyShortcutMessages(QuickReplyShortcut.Id));
}
private void Handle(UpdateQuickReplyShortcutMessages update)
{
var chat = Chat;
if (chat == null)
{
return;
}
var replied = update.Messages.OrderBy(x => x.Id).Select(x =>
{
var message = new Message(x.Id, new MessageSenderUser(ClientService.Options.MyId), ClientService.Options.MyId, x.SendingState, null, true, false, false, false, false, false, false, false, 0, 0, null, null, null, null, null, null, 0, 0, null, 0, 0, x.ViaBotUserId, 0, 0, 0, string.Empty, x.MediaAlbumId, 0, false, string.Empty, x.Content, x.ReplyMarkup);
var model = new QuickReplyMessageViewModel(ClientService, PlaybackService, _messageDelegate, _chat, message, true)
{
CanBeEdited = x.CanBeEdited
};
return model as MessageViewModel;
}).ToList();
BeginOnUIThread(() =>
{
ProcessMessages(chat, replied);
var diff = DiffUtil.CalculateDiff(Items, replied, this, Constants.DiffOptions);
foreach (var step in diff.Steps)
{
if (step.Status == DiffStatus.Add)
{
Items.Insert(step.NewStartIndex, step.Items[0].NewValue);
}
else if (step.Status == DiffStatus.Move && step.OldStartIndex < Items.Count && step.NewStartIndex < Items.Count)
{
Items.Move(step.OldStartIndex, step.NewStartIndex);
}
else if (step.Status == DiffStatus.Remove && step.OldStartIndex < Items.Count)
{
Items.RemoveAt(step.OldStartIndex);
}
}
foreach (var item in diff.NotMovedItems)
{
UpdateItem(item.OldValue, item.NewValue);
Delegate?.UpdateBubbleWithMessageId(item.OldValue.Id, bubble => bubble.UpdateMessage(item.OldValue));
}
IsFirstSliceLoaded = true;
IsLastSliceLoaded = true;
});
}
public bool CompareItems(MessageViewModel oldItem, MessageViewModel newItem)
{
return oldItem.Id == newItem.Id;
}
public void UpdateItem(MessageViewModel oldItem, MessageViewModel newItem)
{
oldItem.UpdateWith(newItem);
oldItem.Content = newItem.Content;
}
}
}