-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathBackgroundViewModel.cs
601 lines (517 loc) · 19.9 KB
/
BackgroundViewModel.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//
// 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 System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Telegram.Collections;
using Telegram.Common;
using Telegram.Navigation;
using Telegram.Navigation.Services;
using Telegram.Services;
using Telegram.Td.Api;
using Telegram.ViewModels.Delegates;
using Telegram.Views.Popups;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml.Navigation;
namespace Telegram.ViewModels
{
public partial class BackgroundInfo
{
public InputBackground Background { get; set; }
public BackgroundType Type { get; set; }
public bool ForDarkTheme { get; set; }
int DarkWhatever { get; set; }
public BackgroundInfo(InputBackground background, BackgroundType type, bool forDarkTheme)
{
Background = background;
Type = type;
ForDarkTheme = forDarkTheme;
}
}
public partial class BackgroundParameters
{
public string Slug { get; }
public Background Background { get; }
public long? ChatId { get; }
public long? MessageId { get; }
public BackgroundParameters(string slug, long? chatId = null)
{
Slug = slug;
ChatId = chatId;
}
public BackgroundParameters(Background background, long? chatId = null, long? messageId = null)
{
Background = background;
ChatId = chatId;
MessageId = messageId;
}
}
public partial class PatternInfo
{
public long BackgroundId { get; }
public Document Document { get; }
public PatternInfo(long backgroundId, Document document)
{
BackgroundId = backgroundId;
Document = document;
}
}
public partial class BackgroundViewModel : ViewModelBase, IDelegable<IBackgroundDelegate>
{
public IBackgroundDelegate Delegate { get; set; }
private Background _background;
private long? _chatId;
private long? _messageId;
private bool _batchUpdate;
public BackgroundViewModel(IClientService clientService, ISettingsService settingsService, IEventAggregator aggregator)
: base(clientService, settingsService, aggregator)
{
Patterns = new MvxObservableCollection<PatternInfo>();
}
protected override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
BackgroundParameters data = parameter as BackgroundParameters;
Background background = data?.Background;
if (data?.Slug != null)
{
var split = data.Slug.Split('#');
if (split[0] == Constants.WallpaperLocalFileName)
{
var local = TdExtensions.GetLocalFile(System.IO.Path.Combine(ApplicationData.Current.TemporaryFolder.Path, Constants.WallpaperLocalFileName));
var document = new Document(Constants.WallpaperLocalFileName, "image/jpeg", null, null, local);
background = new Background(Constants.WallpaperLocalId, false, false, Constants.WallpaperLocalFileName, document, new BackgroundTypeWallpaper(false, false));
}
else if (split[0] == Constants.WallpaperColorFileName)
{
background = new Background(Constants.WallpaperColorId, false, false, Constants.WallpaperColorFileName, null, new BackgroundTypeFill(new BackgroundFillSolid(0xdfe4e8)));
}
else if (Uri.TryCreate("tg://bg/" + parameter, UriKind.Absolute, out Uri uri))
{
var type = TdBackground.FromUri(uri);
if (type is BackgroundTypeFill)
{
background = new Background(0, false, false, string.Empty, null, type);
}
else
{
var response = await ClientService.SendAsync(new SearchBackground(uri.Segments.Last()));
if (response is Background)
{
background = response as Background;
background.Type = type;
}
else if (response is Error error)
{
}
}
}
}
if (background == null)
{
return;
}
Item = background;
_background = background;
_chatId = data?.ChatId;
_messageId = data?.MessageId;
_batchUpdate = true;
BackgroundFill fill = null;
if (background.Type is BackgroundTypeFill typeFill)
{
fill = typeFill.Fill;
Intensity = 100;
IsBlurEnabled = false;
}
else if (background.Type is BackgroundTypePattern typePattern)
{
fill = typePattern.Fill;
Intensity = typePattern.IsInverted ? -typePattern.Intensity : typePattern.Intensity;
IsBlurEnabled = false;
}
else if (background.Type is BackgroundTypeWallpaper typeWallpaper)
{
fill = null;
Intensity = 100;
IsBlurEnabled = typeWallpaper.IsBlurred;
}
if (fill is BackgroundFillSolid fillSolid)
{
Color1 = fillSolid.Color.ToColor();
Color2 = BackgroundColor.Empty;
Rotation = 0;
}
else if (fill is BackgroundFillGradient fillGradient)
{
Color1 = fillGradient.TopColor.ToColor();
Color2 = fillGradient.BottomColor.ToColor();
Rotation = fillGradient.RotationAngle;
}
else if (fill is BackgroundFillFreeformGradient freeformGradient)
{
Color1 = freeformGradient.Colors[0].ToColor();
Color2 = freeformGradient.Colors[1].ToColor();
Color3 = freeformGradient.Colors[2].ToColor();
if (freeformGradient.Colors.Count > 3)
{
Color4 = freeformGradient.Colors[3].ToColor();
}
}
_batchUpdate = false;
Delegate?.UpdateBackground(_item);
if (_item.Type is BackgroundTypePattern or BackgroundTypeFill)
{
var response = await ClientService.SendAsync(new GetInstalledBackgrounds());
if (response is Backgrounds backgrounds)
{
var patterns = backgrounds.BackgroundsValue.Where(x => x.Type is BackgroundTypePattern)
.Distinct(new EqualityComparerDelegate<Background>((x, y) =>
{
return x.Document.DocumentValue.Id == y.Document.DocumentValue.Id;
}, obj =>
{
return obj.Document.DocumentValue.Id;
}))
.Select(x => new PatternInfo(x.Id, x.Document));
Patterns.ReplaceWith(new PatternInfo[] { null }.Union(patterns));
_selectedPattern = Patterns.FirstOrDefault(x => x?.Document.DocumentValue.Id == background.Document?.DocumentValue.Id);
RaisePropertyChanged(nameof(SelectedPattern));
}
}
}
public long ChatId => _chatId ?? 0;
public MvxObservableCollection<PatternInfo> Patterns { get; private set; }
private Background _item;
public Background Item
{
get => _item;
set => Set(ref _item, value);
}
private bool _isBlurEnabled;
public bool IsBlurEnabled
{
get => _isBlurEnabled;
set => SetComponent(ref _isBlurEnabled, value);
}
private BackgroundColor _color1 = BackgroundColor.Empty;
public BackgroundColor Color1
{
get => _color1;
set => SetComponent(ref _color1, value);
}
private BackgroundColor _color2 = BackgroundColor.Empty;
public BackgroundColor Color2
{
get => _color2;
set => SetComponent(ref _color2, value);
}
private BackgroundColor _color3 = BackgroundColor.Empty;
public BackgroundColor Color3
{
get => _color3;
set => SetComponent(ref _color3, value);
}
private BackgroundColor _color4 = BackgroundColor.Empty;
public BackgroundColor Color4
{
get => _color4;
set => SetComponent(ref _color4, value);
}
private void SetComponent<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (_batchUpdate)
{
Set(ref storage, value, propertyName);
return;
}
if (Set(ref storage, value, propertyName))
{
Item = new Background(Item.Id, false, Item.IsDark, Item.Name, Item.Document, Item.Type switch
{
BackgroundTypeFill => new BackgroundTypeFill(GetFill()),
BackgroundTypePattern => new BackgroundTypePattern(GetFill(), Math.Abs(_intensity), _intensity < 0, false),
BackgroundTypeWallpaper => new BackgroundTypeWallpaper(_isBlurEnabled, false),
_ => null
});
}
}
public BackgroundFill GetFill()
{
if (!_color1.IsEmpty && !_color2.IsEmpty)
{
if (!_color3.IsEmpty && !_color4.IsEmpty)
{
return new BackgroundFillFreeformGradient(new[] { _color1.Value, _color2.Value, _color3.Value, _color4.Value });
}
else if (!_color3.IsEmpty)
{
return new BackgroundFillFreeformGradient(new[] { _color1.Value, _color2.Value, _color3.Value });
}
return new BackgroundFillGradient(_color1.Value, _color2.Value, _rotation);
}
else if (!_color1.IsEmpty)
{
return new BackgroundFillSolid(_color1.Value);
}
else if (!_color2.IsEmpty)
{
return new BackgroundFillSolid(_color2.Value);
}
return null;
}
public Color GetPatternForeground()
{
if (_intensity < 0)
{
return Colors.Black;
}
if (!_color1.IsEmpty && !_color2.IsEmpty)
{
return ColorEx.GetPatternColor(ColorEx.GetAverageColor(_color1, _color2));
}
else if (!_color1.IsEmpty)
{
return ColorEx.GetPatternColor(_color1);
}
else if (!_color2.IsEmpty)
{
return ColorEx.GetPatternColor(_color2);
}
return Color.FromArgb(0x66, 0xFF, 0xFF, 0xFF);
}
private bool _isColor1Checked = true;
public bool IsColor1Checked
{
get => _isColor1Checked;
set => Set(ref _isColor1Checked, value);
}
private bool _isColor2Checked;
public bool IsColor2Checked
{
get => _isColor2Checked;
set => Set(ref _isColor2Checked, value);
}
private bool _isColor3Checked;
public bool IsColor3Checked
{
get => _isColor3Checked;
set => Set(ref _isColor3Checked, value);
}
private bool _isColor4Checked;
public bool IsColor4Checked
{
get => _isColor4Checked;
set => Set(ref _isColor4Checked, value);
}
private int _rotation;
public int Rotation
{
get => _rotation;
set => SetComponent(ref _rotation, value);
}
private int _intensity;
public int Intensity
{
get => _intensity;
set => SetComponent(ref _intensity, value);
}
private PatternInfo _selectedPattern;
public PatternInfo SelectedPattern
{
get => _selectedPattern;
set
{
Set(ref _selectedPattern, value);
if (value?.Document.DocumentValue.Id != _item.Document?.DocumentValue.Id && ((value != null && _item?.Type is BackgroundTypeFill) || _item?.Type is BackgroundTypePattern))
{
if (value == null)
{
Item = new Background(0, false, Item.IsDark, Item.Name, null, new BackgroundTypeFill(GetFill()));
}
else
{
Item = new Background(value.BackgroundId, false, Item.IsDark, Item.Name, value.Document, new BackgroundTypePattern(GetFill(), _intensity < 0 ? 100 + _intensity : _intensity, _intensity < 0, false));
}
Delegate?.UpdateBackground(Item);
}
}
}
public Background GetPattern(Document value)
{
if (value == null)
{
return new Background(Item.Id, false, Item.IsDark, Item.Name, null, new BackgroundTypeFill(GetFill()));
}
return new Background(Item.Id, false, Item.IsDark, Item.Name, value, new BackgroundTypePattern(GetFill(), 50, _intensity < 0, false));
}
public void RemoveColor(int index)
{
if (index <= 0)
{
Color1 = Color2;
}
if (index <= 1)
{
Color2 = Color3;
}
if (index <= 2)
{
Color3 = Color4;
}
if (index <= 3)
{
Color4 = BackgroundColor.Empty;
}
IsColor1Checked = true;
}
public void AddColor()
{
if (Color2.IsEmpty)
{
Color2 = Color1;
IsColor2Checked = true;
}
else if (Color3.IsEmpty)
{
Color3 = Color2;
IsColor3Checked = true;
}
else if (Color4.IsEmpty)
{
Color4 = Color3;
IsColor4Checked = true;
}
}
public void ChangeRotation()
{
Rotation = (_rotation + 45) % 360;
}
public async void Share()
{
var background = _item;
if (background == null)
{
return;
}
var response = await ClientService.SendAsync(new GetBackgroundUrl(background.Name, background.Type));
if (response is HttpUrl url)
{
await ShowPopupAsync(new ChooseChatsPopup(), new ChooseChatsConfigurationPostLink(url));
}
}
public async void Done(bool onlySelf)
{
var background = await GetBackgroundAsync();
if (background != null)
{
if (_chatId is long chatId)
{
if (_messageId is long messageId && background.Type.GetType() == _background?.Type.GetType())
{
ClientService.Send(new SetChatBackground(chatId, new InputBackgroundPrevious(messageId), background.Type, 30, onlySelf));
}
else
{
ClientService.Send(new SetChatBackground(chatId, background.Background, background.Type, 30, onlySelf));
}
}
else if (background.Background == null && background.Type == null)
{
ClientService.Send(new DeleteDefaultBackground(background.ForDarkTheme));
}
else
{
ClientService.Send(new SetDefaultBackground(background.Background, background.Type, background.ForDarkTheme));
}
}
}
public async Task<BackgroundInfo> GetBackgroundAsync()
{
var background = _item;
if (background == null)
{
return null;
}
var dark = Settings.Appearance.IsDarkTheme();
var freeform = dark ? new[] { 0x6C7FA6, 0x2E344B, 0x7874A7, 0x333258 } : new[] { 0xDBDDBB, 0x6BA587, 0xD5D88D, 0x88B884 };
// This is a new background and it has to be uploaded to Telegram servers
if (background.Id == Constants.WallpaperLocalId)
{
try
{
var item = await ApplicationData.Current.TemporaryFolder.GetFileAsync(Constants.WallpaperLocalFileName);
var generated = await item.ToGeneratedAsync(ConversionType.Copy, forceCopy: true);
return new BackgroundInfo(new InputBackgroundLocal(generated), new BackgroundTypeWallpaper(_isBlurEnabled, false), dark);
}
catch
{
return null;
}
}
else
{
var fill = GetFill();
if (background.Type is BackgroundTypePattern && fill is BackgroundFillFreeformGradient fillFreeform && fillFreeform.Colors.SequenceEqual(freeform))
{
return new BackgroundInfo(null, null, dark);
}
else
{
BackgroundType type = null;
if (background.Type is BackgroundTypeFill)
{
type = new BackgroundTypeFill(fill);
}
else if (background.Type is BackgroundTypePattern)
{
type = new BackgroundTypePattern(fill, Math.Abs(_intensity), _intensity < 0, false);
}
else if (background.Type is BackgroundTypeWallpaper)
{
type = new BackgroundTypeWallpaper(_isBlurEnabled, false);
}
if (type == null)
{
return null;
}
var input = background.Document != null
? new InputBackgroundRemote(background.Id)
: null;
return new BackgroundInfo(input, type, dark);
}
}
}
}
public struct BackgroundColor
{
private BackgroundColor(int value, bool empty)
{
Value = value;
IsEmpty = empty;
}
public static BackgroundColor FromValue(int value)
{
return new BackgroundColor(value, false);
}
public static BackgroundColor Empty = new BackgroundColor(0, true);
public int Value;
public bool IsEmpty;
public static implicit operator Color(BackgroundColor rhs)
{
if (rhs.IsEmpty)
{
return Color.FromArgb(0, 0, 0, 0);
}
return rhs.Value.ToColor();
}
public static implicit operator BackgroundColor(Color lhs)
{
return FromValue((lhs.R << 16) + (lhs.G << 8) + lhs.B);
}
}
}