Skip to content

Commit

Permalink
Better template for gifts
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Jul 29, 2024
1 parent 9f3b3e9 commit f0df84c
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Telegram/Controls/Chats/ChatHistoryViewItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public enum ChatHistoryViewItemType
ServiceUnread,
ServicePhoto,
ServiceBackground,
ServiceGift
ServiceGift,
ServiceGiftCode
}

public class ChatHistoryViewItem : ListViewItemEx
Expand Down
73 changes: 73 additions & 0 deletions Telegram/Controls/Messages/MessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,40 @@ private void UpdateContent(MessageViewModel message)
var animation = FindName("Animation") as AnimatedImage;
animation.Source = new DelayedFileSource(message.ClientService, premiumGiftCode.Sticker);
}
else if (message.Content is MessageGiftedPremium giftedPremium)
{
var title = FindName("Title") as TextBlock;
var subtitle = FindName("Subtitle") as TextBlock;
var button = FindName("ViewLabel") as TextBlock;

title.Text = Strings.ActionGiftPremiumTitle;
subtitle.Text = string.Format(Strings.ActionGiftPremiumSubtitle, Locale.Declension(Strings.R.Months, giftedPremium.MonthCount));
button.Text = Strings.ActionGiftPremiumView;

var animation = FindName("Animation") as AnimatedImage;
animation.Source = new DelayedFileSource(message.ClientService, giftedPremium.Sticker);
}
else if (message.Content is MessageGiftedStars giftedStars)
{
var title = FindName("Title") as TextBlock;
var subtitle = FindName("Subtitle") as TextBlock;
var button = FindName("ViewLabel") as TextBlock;

title.Text = Locale.Declension(Strings.R.ActionGiftStarsTitle, giftedStars.StarCount);
button.Text = Strings.ActionGiftStarsView;

if (message.ClientService.Options.MyId == giftedStars.ReceiverUserId)
{
subtitle.Text = Strings.ActionGiftStarsSubtitleYou;
}
else if (message.ClientService.TryGetUser(giftedStars.ReceiverUserId, out User receiver))
{
TextBlockHelper.SetMarkdown(subtitle, string.Format(Strings.ActionGiftStarsSubtitle, receiver.FirstName));
}

var animation = FindName("Animation") as AnimatedImage;
animation.Source = new DelayedFileSource(message.ClientService, giftedStars.Sticker);
}
else if (message.Content is MessageChatChangePhoto chatChangePhoto)
{
var segments = FindName("Segments") as ActiveStoriesSegments;
Expand Down Expand Up @@ -271,13 +305,15 @@ private static (string Text, IList<TextEntity> Entities) GetEntities(MessageView
MessageForumTopicIsClosedToggled forumTopicIsClosedToggled => UpdateForumTopicIsClosedToggled(message, forumTopicIsClosedToggled, active),
MessageGameScore gameScore => UpdateGameScore(message, gameScore, active),
MessageGiftedPremium giftedPremium => UpdateGiftedPremium(message, giftedPremium, active),
MessageGiftedStars giftedStars => UpdateGiftedStars(message, giftedStars, active),
MessageInviteVideoChatParticipants inviteVideoChatParticipants => UpdateInviteVideoChatParticipants(message, inviteVideoChatParticipants, active),
MessageProximityAlertTriggered proximityAlertTriggered => UpdateProximityAlertTriggered(message, proximityAlertTriggered, active),
MessagePremiumGiveawayCreated premiumGiveawayCreated => UpdatePremiumGiveawayCreated(message, premiumGiveawayCreated, active),
MessagePremiumGiveawayCompleted premiumGiveawayCompleted => UpdatePremiumGiveawayCompleted(message, premiumGiveawayCompleted, active),
MessagePremiumGiftCode premiumGiftCode => UpdatePremiumGiftCode(message, premiumGiftCode, active),
MessagePassportDataSent passportDataSent => UpdatePassportDataSent(message, passportDataSent, active),
MessagePaymentSuccessful paymentSuccessful => UpdatePaymentSuccessful(message, paymentSuccessful, active),
MessagePaymentRefunded paymentRefunded => UpdatePaymentRefunded(message, paymentRefunded, active),
MessagePinMessage pinMessage => UpdatePinMessage(message, pinMessage, active),
MessageScreenshotTaken screenshotTaken => UpdateScreenshotTaken(message, screenshotTaken, active),
MessageSuggestProfilePhoto suggestProfilePhoto => UpdateSuggestProfilePhoto(message, suggestProfilePhoto, active),
Expand Down Expand Up @@ -1780,6 +1816,26 @@ private static (string, IList<TextEntity>) UpdateGiftedPremium(MessageViewModel
return (formatted.Text, formatted.Entities);
}

private static (string, IList<TextEntity>) UpdateGiftedStars(MessageViewModel message, MessageGiftedStars giftedStars, bool active)
{
var content = string.Empty;
var entities = active ? new List<TextEntity>() : null;

if (message.SenderId is MessageSenderUser user && user.UserId == message.ClientService.Options.MyId)
{
content = ReplaceWithLink(Strings.ActionGiftOutbound, "un2", giftedStars, entities);
}
else if (message.ClientService.TryGetUser(message.SenderId, out User senderUser))
{
content = ReplaceWithLink(Strings.ActionGiftInbound, "un1", senderUser, entities);
content = ReplaceWithLink(content, "un2", giftedStars, entities);
}

var formatted = ClientEx.ParseMarkdown(content, (IList<TextEntity>)entities ?? Array.Empty<TextEntity>());

return (formatted.Text, formatted.Entities);
}

private static (string, IList<TextEntity>) UpdateVideoChatEnded(MessageViewModel message, MessageVideoChatEnded videoChatEnded, bool active)
{
var content = string.Empty;
Expand Down Expand Up @@ -2070,6 +2126,18 @@ private static (string, IList<TextEntity>) UpdatePaymentSuccessful(MessageViewMo
return (content, null);
}

private static (string, IList<TextEntity>) UpdatePaymentRefunded(MessageViewModel message, MessagePaymentRefunded paymentRefunded, bool active)
{
var entities = active ? new List<TextEntity>() : null;

var sender = message.GetSender();

var content = string.Format(Strings.ActionRefunded, Locale.FormatCurrency(paymentRefunded.TotalAmount, paymentRefunded.Currency));
content = ReplaceWithLink(content, "un1", sender, entities);

return (content, entities);
}

private static (string, IList<TextEntity>) UpdatePinMessage(MessageViewModel message, MessagePinMessage pinMessage, bool active)
{
var content = string.Empty;
Expand Down Expand Up @@ -2397,6 +2465,11 @@ public static string ReplaceWithLink(string source, string param, BaseObject obj
name = Locale.FormatCurrency(giftedPremium.Amount, giftedPremium.Currency);
id = null;
}
else if (obj is MessageGiftedStars giftedStars)
{
name = Locale.FormatCurrency(giftedStars.Amount, giftedStars.Currency);
id = null;
}
else if (obj is ForumTopicInfo forumTopicInfo)
{
name = $"\U0001F4C3 {forumTopicInfo.Name}";
Expand Down
38 changes: 37 additions & 1 deletion Telegram/Strings/en/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// <auto-generatedInfo>
// This code was generated by TdParseOptions (http://github.com/UnigramDev/UnigramUtils/)
//
// Generated: 07/22/2024 10:21:03
// Generated: 07/29/2024 15:05:50
// </auto-generatedInfo>
// --------------------------------------------------------------------------------------------------
namespace Telegram
Expand All @@ -23,6 +23,7 @@ public static class R
public const string AccDescrMentionCount = "AccDescrMentionCount";
public const string AccDescrNumberOfPeopleReactions = "AccDescrNumberOfPeopleReactions";
public const string AccDescrNumberOfViews = "AccDescrNumberOfViews";
public const string ActionGiftStarsTitle = "ActionGiftStarsTitle";
public const string AddManyMembersAlertNamesText = "AddManyMembersAlertNamesText";
public const string AddManyMembersAlertTitle = "AddManyMembersAlertTitle";
public const string AndMoreTyping = "AndMoreTyping";
Expand Down Expand Up @@ -806,6 +807,36 @@ public static class R
/// </summary>
public static string ActionGiftOutbound => Resource.GetString("ActionGiftOutbound");

/// <summary>
/// Localized resource similar to "for {0}"
/// </summary>
public static string ActionGiftPremiumSubtitle => Resource.GetString("ActionGiftPremiumSubtitle");

/// <summary>
/// Localized resource similar to "Telegram Premium"
/// </summary>
public static string ActionGiftPremiumTitle => Resource.GetString("ActionGiftPremiumTitle");

/// <summary>
/// Localized resource similar to "View"
/// </summary>
public static string ActionGiftPremiumView => Resource.GetString("ActionGiftPremiumView");

/// <summary>
/// Localized resource similar to "With Stars, **{0}** is able to unlock content and services on Telegram."
/// </summary>
public static string ActionGiftStarsSubtitle => Resource.GetString("ActionGiftStarsSubtitle");

/// <summary>
/// Localized resource similar to "Use Stars to unlock content and services on Telegram."
/// </summary>
public static string ActionGiftStarsSubtitleYou => Resource.GetString("ActionGiftStarsSubtitleYou");

/// <summary>
/// Localized resource similar to "View"
/// </summary>
public static string ActionGiftStarsView => Resource.GetString("ActionGiftStarsView");

/// <summary>
/// Localized resource similar to "Video chat ended ({0})"
/// </summary>
Expand Down Expand Up @@ -973,6 +1004,11 @@ public static class R
/// </summary>
public static string ActionReactionsChanged => Resource.GetString("ActionReactionsChanged");

/// <summary>
/// Localized resource similar to "un1 refunded back {0}"
/// </summary>
public static string ActionRefunded => Resource.GetString("ActionRefunded");

/// <summary>
/// Localized resource similar to "un1 removed the group photo"
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions Telegram/Strings/en/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,30 @@
<data name="ActionGiftOutbound" xml:space="preserve">
<value>You have sent a gift for **un2**</value>
</data>
<data name="ActionGiftPremiumSubtitle" xml:space="preserve">
<value>for {0}</value>
</data>
<data name="ActionGiftPremiumTitle" xml:space="preserve">
<value>Telegram Premium</value>
</data>
<data name="ActionGiftPremiumView" xml:space="preserve">
<value>View</value>
</data>
<data name="ActionGiftStarsSubtitle" xml:space="preserve">
<value>With Stars, **{0}** is able to unlock content and services on Telegram.</value>
</data>
<data name="ActionGiftStarsSubtitleYou" xml:space="preserve">
<value>Use Stars to unlock content and services on Telegram.</value>
</data>
<data name="ActionGiftStarsTitle_one" xml:space="preserve">
<value>{0} Star</value>
</data>
<data name="ActionGiftStarsTitle_other" xml:space="preserve">
<value>{0} Stars</value>
</data>
<data name="ActionGiftStarsView" xml:space="preserve">
<value>View</value>
</data>
<data name="ActionGroupCallEnded" xml:space="preserve">
<value>Video chat ended ({0})</value>
</data>
Expand Down Expand Up @@ -552,6 +576,9 @@
from: {0}
to: {1}</value>
</data>
<data name="ActionRefunded" xml:space="preserve">
<value>un1 refunded back {0}</value>
</data>
<data name="ActionRemovedPhoto" xml:space="preserve">
<value>un1 removed the group photo</value>
</data>
Expand Down
4 changes: 4 additions & 0 deletions Telegram/Views/ChatView.Bubbles.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,10 @@ private ChatHistoryViewItemType SelectTemplateCore(object item)
if (message.IsService)
{
if (message.Content is MessagePremiumGiftCode)
{
return ChatHistoryViewItemType.ServiceGiftCode;
}
else if (message.Content is MessageGiftedPremium or MessageGiftedStars)
{
return ChatHistoryViewItemType.ServiceGift;
}
Expand Down
77 changes: 76 additions & 1 deletion Telegram/Views/ChatView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,81 @@
</messages:MessageService>
</DataTemplate>

<DataTemplate x:Name="ServiceMessageGiftedTemplate">
<messages:MessageService>
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch" />
</ControlTemplate>
</Button.Template>
<StackPanel>
<messages:MessageService Click="ServiceMessage_Click"
Margin="0,4,0,0"
Padding="12,2,12,0"
MaxWidth="480">
<controls:FormattedTextBlock x:Name="Text"
Foreground="#FFFFFF"
TextAlignment="Center"
TextStyle="{StaticResource CaptionRichTextBlockStyle}"
FontFamily="{StaticResource EmojiThemeFontFamilyWithSymbols}"
AutoFontSize="False"
IsTextSelectionEnabled="False"
HyperlinkStyle="None"
HyperlinkForeground="#FFFFFF"
HyperlinkFontWeight="SemiBold"
Padding="0,0,0,4" />
</messages:MessageService>

<messages:MessageService x:Name="Service"
Click="ServiceMessage_Click"
Margin="0,4,0,0"
Padding="12,2,12,0"
Width="216">
<StackPanel>
<controls:AnimatedImage x:Name="Animation"
Width="120"
Height="120"
FrameSize="120,120"
DecodeFrameType="Logical"
IsViewportAware="True"
LoopCount="1"
Margin="0,-20,0,12" />

<TextBlock x:Name="Title"
Foreground="#FFFFFF"
Text="{CustomResource BoostingCongratulations}"
TextAlignment="Center"
Style="{StaticResource SubtitleTextBlockStyle}"
FontSize="16" />

<TextBlock x:Name="Subtitle"
Foreground="#FFFFFF"
TextAlignment="Center"
TextWrapping="Wrap"
Style="{StaticResource CaptionTextBlockStyle}"
FontFamily="{StaticResource EmojiThemeFontFamilyWithSymbols}"
Padding="0,0,0,4"
Margin="0,0,0,8" />

<Border x:Name="View"
Background="{ThemeResource MessageServiceBackgroundBrush}"
CornerRadius="12"
HorizontalAlignment="Center"
Padding="12,4,12,4"
Margin="0,0,0,12">
<TextBlock x:Name="ViewLabel"
Text="{CustomResource BoostingReceivedGiftOpenBtn}"
Foreground="#FFFFFF"
TextAlignment="Center"
Style="{StaticResource CaptionTextBlockStyle}" />
</Border>
</StackPanel>
</messages:MessageService>
</StackPanel>
</messages:MessageService>
</DataTemplate>

<DataTemplate x:Name="ServiceMessageTemplate">
<messages:MessageService Click="ServiceMessage_Click"
Margin="0,4,0,0"
Expand Down Expand Up @@ -1211,7 +1286,7 @@
VerticalAlignment="Bottom"
Margin="8,4">
<Border Background="{ThemeResource SystemColorControlAccentBrush}"
Opacity="0"/>
Opacity="0" />
<TextBlock x:Name="SendEffectText"
FontFamily="{StaticResource EmojiThemeFontFamilyWithSymbols}"
FontSize="16"
Expand Down
3 changes: 2 additions & 1 deletion Telegram/Views/ChatView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ void AddStrategy(ChatHistoryViewItemType type, DataTemplate template, int minimu
AddStrategy(ChatHistoryViewItemType.ServiceUnread, ServiceMessageUnreadTemplate);
AddStrategy(ChatHistoryViewItemType.ServicePhoto, ServiceMessagePhotoTemplate);
AddStrategy(ChatHistoryViewItemType.ServiceBackground, ServiceMessageBackgroundTemplate);
AddStrategy(ChatHistoryViewItemType.ServiceGift, ServiceMessageGiftTemplate);
AddStrategy(ChatHistoryViewItemType.ServiceGiftCode, ServiceMessageGiftTemplate);
AddStrategy(ChatHistoryViewItemType.ServiceGift, ServiceMessageGiftedTemplate);

_focusState = new DebouncedProperty<FocusState>(100, FocusText, CanFocusText);

Expand Down

0 comments on commit f0df84c

Please sign in to comment.