-
Notifications
You must be signed in to change notification settings - Fork 7
/
ChatFMX.Utils.pas
301 lines (270 loc) · 9.87 KB
/
ChatFMX.Utils.pas
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
unit ChatFMX.Utils;
interface
uses
VK.Types, System.SysUtils, VK.Entity.Message, FMX.Ani, FMX.Types, FMX.Controls,
System.Types, System.Classes;
type
TAnimatorHelper = class helper for TAnimator
class procedure DetachPropertyAnimation(const Target: TFmxObject; const APropertyName: string);
class procedure AnimateFloat(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Update: TNotifyEvent; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear); overload;
end;
function AttachmentToText(const Value: TVkAttachmentType): string;
function WordOfCount(const Count: Integer; const Words: TArrayOfString): string;
function WordOfSex(const Sex: TVkSex; const Words: TArrayOfString): string;
/// <summary>
/// Пример: 2 авг 2022 в 14:32 / вчера в 9:15
/// </summary>
function HumanDateTime(Value: TDateTime; ShowTime: Boolean = False; ShortDate: Boolean = False): string;
/// <summary>
/// Пример: 2 авг 2022, 14:32
/// </summary>
function HumanDateTimeSimple(Value: TDateTime; ShowTime: Boolean = False; ShortDate: Boolean = False): string;
function MessageActionToText(const Value: TVkMessageAction; FromId: TVkPeerId; const FromText, MemberText: string): string;
function ParseMention(const Value: string): string;
function PrepareForPreview(const Value: string): string;
implementation
uses
System.DateUtils, System.StrUtils, System.RegularExpressions;
function PrepareForPreview(const Value: string): string;
begin
Result := Value.Replace(#$A, ' ').Replace(' ', ' ', [rfReplaceAll]);
end;
function ParseMention(const Value: string): string;
const
Pattern1 = '\[(.+?)\|(.+?)\]';
Pattern2 = '(@.+?)\ \((.+?)\)';
begin
Result := Value;
//[id5555555|UserName]
var RegExp := TRegEx.Create(Pattern1);
var Matches := RegExp.Matches(Value);
for var Match in Matches do
if Match.Groups.Count > 2 then
Result := Result.Replace(Match.Value, Match.Groups[2].Value, [rfReplaceAll]);
//@username (User Name)
RegExp := TRegEx.Create(Pattern2);
Matches := RegExp.Matches(Value);
for var Match in Matches do
if Match.Groups.Count > 2 then
Result := Result.Replace(Match.Value, Match.Groups[2].Value, [rfReplaceAll]);
end;
function WordOfCount(const Count: Integer; const Words: TArrayOfString): string;
begin
if Length(Words) < 3 then
Exit('');
var Num := Count.ToString;
case Num[High(Num)] of
'1':
Exit(Words[0]);
'2', '3', '4':
Exit(Words[1]);
'5', '6', '7', '8', '9', '0':
Exit(Words[2]);
end;
end;
function WordOfSex(const Sex: TVkSex; const Words: TArrayOfString): string;
begin
if Length(Words) < 2 then
Exit('');
case Sex of
TVkSex.None, TVkSex.Male:
Result := Words[0];
TVkSex.Female:
Result := Words[1];
end;
end;
function MonthTextOf(Value: TDateTime; Short: Boolean): string;
begin
case MonthOf(Value) of
1:
Result := IfThen(Short, 'янв', 'января');
2:
Result := IfThen(Short, 'фев', 'февраля');
3:
Result := IfThen(Short, 'мар', 'марта');
4:
Result := IfThen(Short, 'апр', 'апреля');
5:
Result := IfThen(Short, 'мая', 'мая');
6:
Result := IfThen(Short, 'июн', 'июня');
7:
Result := IfThen(Short, 'июл', 'июля');
8:
Result := IfThen(Short, 'авг', 'августа');
9:
Result := IfThen(Short, 'сен', 'сентября');
10:
Result := IfThen(Short, 'окт', 'октября');
11:
Result := IfThen(Short, 'ноя', 'ноября');
12:
Result := IfThen(Short, 'дек', 'декабря');
else
Result := '?????';
end;
end;
function HumanDateTime(Value: TDateTime; ShowTime: Boolean; ShortDate: Boolean): string;
begin
if IsSameDay(Value, Today) then
Result := 'сегодня'
else if IsSameDay(Value, Yesterday) then
Result := 'вчера'
else if YearOf(Value) = YearOf(Now) then
Result := FormatDateTime('d ' + MonthTextOf(Value, ShortDate), Value)
else
Result := FormatDateTime('d ' + MonthTextOf(Value, ShortDate) + ' YYYY', Value);
if ShowTime then
Result := Result + ' в ' + FormatDateTime('H:nn', Value);
end;
function HumanDateTimeSimple(Value: TDateTime; ShowTime: Boolean; ShortDate: Boolean): string;
begin
if YearOf(Value) = YearOf(Now) then
Result := FormatDateTime('d ' + MonthTextOf(Value, ShortDate), Value)
else
Result := FormatDateTime('d ' + MonthTextOf(Value, ShortDate) + ' YYYY', Value);
if ShowTime then
Result := Result + ', ' + FormatDateTime('H:nn', Value);
end;
function MessageActionToText(const Value: TVkMessageAction; FromId: TVkPeerId; const FromText, MemberText: string): string;
begin
Result := '';
case Value.&Type of
TVkMessageActionType.ChatPhotoUpdate:
Result := FromText + ' обновил(а) фотографию чата';
TVkMessageActionType.ChatPhotoRemove:
Result := FromText + ' удалил(а) фотографию чата';
TVkMessageActionType.ChatCreate:
Result := FromText + ' создал(а) чат «' + Value.Text + '»';
TVkMessageActionType.ChatTitleUpdate:
begin
Result := FromText + ' изменил(а) название чата';
if not Value.Text.IsEmpty then
Result := Result + ' на «' + Value.Text + '»';
end;
TVkMessageActionType.ChatInviteUser:
if Value.MemberId = FromId then
Result := FromText + ' вошел(ла) в чат'
else
Result := FromText + ' пригласил(а) ' + MemberText;
TVkMessageActionType.ChatKickUser:
if Value.MemberId = FromId then
Result := FromText + ' вышел(ла) из чата'
else
Result := FromText + ' исключил(а) ' + MemberText;
TVkMessageActionType.ChatPinMessage:
begin
Result := FromText + ' закрепил(а) сообщение';
if not Value.Message.IsEmpty then
Result := Result + ' «' + Value.Message.Replace(' ', ' ', [rfReplaceAll]) + '»';
end;
TVkMessageActionType.ChatUnpinMessage:
begin
Result := FromText + ' открепил(а) сообщение';
if not Value.Message.IsEmpty then
Result := Result + ' «' + Value.Message.Replace(' ', ' ', [rfReplaceAll]) + '»';
end;
TVkMessageActionType.ChatInviteUserByLink:
Result := FromText + ' присоединился к чату по ссылке';
TVkMessageActionType.ConversationStyleUpdate:
Result := FromText + ' изменил(а) оформление чата на «' + Value.Style + '». Оформление чата доступно в мобильном приложении';
end;
end;
function AttachmentToText(const Value: TVkAttachmentType): string;
begin
case Value of
TVkAttachmentType.Photo:
Result := 'Фотография';
TVkAttachmentType.Video:
Result := 'Видео';
TVkAttachmentType.Audio:
Result := 'Аудиозапись';
TVkAttachmentType.Doc:
Result := 'Файл';
TVkAttachmentType.Link:
Result := 'Ссылка';
TVkAttachmentType.Market:
Result := 'Товар';
TVkAttachmentType.MarketAlbum:
Result := 'Товары';
TVkAttachmentType.Wall:
Result := 'Запись на стене';
TVkAttachmentType.WallReply:
Result := 'Запись на стене';
TVkAttachmentType.Sticker:
Result := 'Стикер';
TVkAttachmentType.Gift:
Result := 'Подарок';
TVkAttachmentType.Call:
Result := 'Звонок';
TVkAttachmentType.AudioMessage:
Result := 'Голосовое сообщение';
TVkAttachmentType.PostedPhoto:
Result := 'Фотография';
TVkAttachmentType.Graffiti:
Result := 'Граффити';
TVkAttachmentType.Note:
Result := 'Заметка';
TVkAttachmentType.App:
Result := 'Приложение';
TVkAttachmentType.Poll:
Result := 'Опрос';
TVkAttachmentType.Page:
Result := 'Страница';
TVkAttachmentType.Album:
Result := 'Альбом';
TVkAttachmentType.PhotosList:
Result := 'Фотографии';
TVkAttachmentType.PrettyCards:
Result := 'Карточки';
TVkAttachmentType.Event:
Result := 'Событие';
TVkAttachmentType.MoneyTransfer:
Result := 'Денежный перевод';
TVkAttachmentType.Story:
Result := 'История';
else
Result := '';
end;
end;
{ TAnimatorHelper }
class procedure TAnimatorHelper.AnimateFloat(const Target: TFmxObject; const APropertyName: string; const NewValue: Single; Update: TNotifyEvent; Duration: Single = 0.2; AType: TAnimationType = TAnimationType.in; AInterpolation: TInterpolationType = TInterpolationType.Linear);
var
Animation: TFloatAnimation;
begin
StopPropertyAnimation(Target, APropertyName);
with Self do
CreateDestroyer;
Animation := TFloatAnimation.Create(nil);
Animation.Parent := Target;
Animation.AnimationType := AType;
Animation.Interpolation := AInterpolation;
Animation.Duration := Duration;
Animation.PropertyName := APropertyName;
Animation.StartFromCurrent := True;
Animation.StopValue := NewValue;
Animation.OnProcess := Update;
with Self do
FDestroyer.RegisterAnimation(Animation);
Animation.Start;
end;
class procedure TAnimatorHelper.DetachPropertyAnimation(const Target: TFmxObject; const APropertyName: string);
var
I: Integer;
begin
I := Target.ChildrenCount - 1;
while I >= 0 do
begin
if (Target.Children[I] is TCustomPropertyAnimation) and
(CompareText(TCustomPropertyAnimation(Target.Children[I]).PropertyName, APropertyName) = 0) then
begin
var Anim := TFloatAnimation(Target.Children[I]);
Anim.Parent := nil;
Anim.Stop;
end;
if I > Target.ChildrenCount then
I := Target.ChildrenCount;
Dec(I);
end;
end;
end.