-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathInitialNameStringConverter.cs
113 lines (99 loc) · 3.31 KB
/
InitialNameStringConverter.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
//
// Copyright Fela Ameghino & Contributors 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.Globalization;
using Telegram.Td.Api;
using Windows.UI.Xaml.Data;
namespace Telegram.Converters
{
public partial class InitialNameStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return Convert(value);
}
public static string Convert(object value)
{
if (value == null)
{
return null;
}
var word1 = string.Empty;
var word2 = string.Empty;
if (value is User user)
{
word1 = user.FirstName ?? string.Empty;
word2 = user.LastName ?? string.Empty;
}
else if (value is Chat chat)
{
var words = chat.Title.Split(new char[] { ' ' });
if (words.Length > 1 && chat.Type is ChatTypePrivate || chat.Type is ChatTypeSecret)
{
word1 = words[0];
word2 = words[words.Length - 1];
}
else if (words.Length > 0)
{
word1 = words[0];
word2 = string.Empty;
}
}
else if (value is ChatInviteLinkInfo info)
{
var words = info.Title.Split(new char[] { ' ' });
if (words.Length > 0)
{
word1 = words[0];
word2 = string.Empty;
}
}
else if (value is string str)
{
var words = str.Split(new char[] { ' ' });
if (words.Length > 1)
{
word1 = words[0];
word2 = words[words.Length - 1];
}
else
{
word1 = words[0];
word2 = string.Empty;
}
}
return Convert(word1, word2);
}
public static string Convert(string title)
{
title ??= string.Empty;
var word1 = string.Empty;
var word2 = string.Empty;
var words = title.Split(new char[] { ' ' });
if (words.Length > 0)
{
word1 = words[0];
word2 = string.Empty;
}
return Convert(word1, word2);
}
public static string Convert(string word1, string word2)
{
word1 ??= string.Empty;
word2 ??= string.Empty;
var si1 = StringInfo.GetTextElementEnumerator(word1);
var si2 = StringInfo.GetTextElementEnumerator(word2);
word1 = si1.MoveNext() ? si1.GetTextElement() : string.Empty;
word2 = si2.MoveNext() ? si2.GetTextElement() : string.Empty;
return string.Format("{0}{1}", word1, word2).Trim().ToUpperInvariant();
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotSupportedException();
}
}
}