Skip to content

Commit

Permalink
Speed limit toast
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Apr 17, 2024
1 parent ac6aa1a commit 2a230cc
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
Binary file added Telegram/Assets/Toasts/SpeedLimit.tgs
Binary file not shown.
35 changes: 24 additions & 11 deletions Telegram/Controls/ToastPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@

namespace Telegram.Controls
{
public class ToastPopup
public class ToastPopup : TeachingTip
{
public ToastPopup()
{
DefaultStyleKey = typeof(ToastPopup);
}

public static void ShowOption(INavigationService navigationService)
{
ShowPromo(navigationService, Strings.OptionPremiumRequiredMessage, Strings.OptionPremiumRequiredButton, null);
Expand Down Expand Up @@ -54,42 +59,42 @@ public static async void ShowPromo(INavigationService navigationService, string
}
}

public static TeachingTip Show(string text, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
public static ToastPopup Show(string text, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
{
return Show(null, text, null, TeachingTipPlacementMode.Center, requestedTheme, dismissAfter);
}

public static TeachingTip Show(string text, AnimatedImageSource icon, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
public static ToastPopup Show(string text, AnimatedImageSource icon, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
{
return Show(null, ClientEx.ParseMarkdown(text), icon, TeachingTipPlacementMode.Center, requestedTheme, dismissAfter);
}

public static TeachingTip Show(FormattedText text, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
public static ToastPopup Show(FormattedText text, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
{
return Show(null, text, null, TeachingTipPlacementMode.Center, requestedTheme, dismissAfter);
}

public static TeachingTip Show(FormattedText text, AnimatedImageSource icon, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
public static ToastPopup Show(FormattedText text, AnimatedImageSource icon, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
{
return Show(null, text, icon, TeachingTipPlacementMode.Center, requestedTheme, dismissAfter);
}

public static TeachingTip Show(FrameworkElement target, string text, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
public static ToastPopup Show(FrameworkElement target, string text, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
{
return Show(target, text, null, placement, requestedTheme, dismissAfter);
}

public static TeachingTip Show(FrameworkElement target, string text, AnimatedImageSource icon, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
public static ToastPopup Show(FrameworkElement target, string text, AnimatedImageSource icon, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
{
return Show(target, ClientEx.ParseMarkdown(text), icon, placement, requestedTheme, dismissAfter);
}

public static TeachingTip Show(FrameworkElement target, FormattedText text, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
public static ToastPopup Show(FrameworkElement target, FormattedText text, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
{
return Show(target, text, null, placement, requestedTheme, dismissAfter);
}

public static TeachingTip Show(FrameworkElement target, FormattedText text, AnimatedImageSource icon, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
public static ToastPopup Show(FrameworkElement target, FormattedText text, AnimatedImageSource icon, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
{
AnimatedImage animated = null;
if (icon != null)
Expand All @@ -111,7 +116,7 @@ public static TeachingTip Show(FrameworkElement target, FormattedText text, Anim
return ShowToastImpl(Window.Current, target, text, animated, placement, requestedTheme, dismissAfter);
}

public static TeachingTip ShowToastImpl(Window app, FrameworkElement target, FormattedText text, FrameworkElement icon, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
public static ToastPopup ShowToastImpl(Window app, FrameworkElement target, FormattedText text, FrameworkElement icon, TeachingTipPlacementMode placement, ElementTheme requestedTheme = ElementTheme.Dark, TimeSpan? dismissAfter = null)
{
Logger.Info();

Expand All @@ -134,7 +139,7 @@ public static TeachingTip ShowToastImpl(Window app, FrameworkElement target, For
content.Children.Add(icon);
}

var toast = new TeachingTip
var toast = new ToastPopup
{
Target = target,
PreferredPlacement = placement,
Expand Down Expand Up @@ -338,5 +343,13 @@ void handler(object sender, object e)

return ShowActionAsync(target, text, action, animated, placement, requestedTheme, dismissAfter);
}

public event EventHandler<TextUrlClickEventArgs> Click;

// Used by TextBlockHelper
public void OnClick(string url)
{
Click?.Invoke(this, new TextUrlClickEventArgs(url));
}
}
}
41 changes: 41 additions & 0 deletions Telegram/Services/NotificationsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Telegram.Controls.Cells;
using Telegram.Converters;
using Telegram.Navigation;
using Telegram.Streams;
using Telegram.Td;
using Telegram.Td.Api;
using Telegram.Views;
Expand Down Expand Up @@ -89,6 +90,7 @@ private void Subscribe()
.Subscribe<UpdateServiceNotification>(Handle)
.Subscribe<UpdateTermsOfService>(Handle)
.Subscribe<UpdateUser>(Handle)
.Subscribe<UpdateSpeedLimitNotification>(Handle)
.Subscribe<UpdateNotification>(Handle)
.Subscribe<UpdateNotificationGroup>(Handle)
.Subscribe<UpdateHavePendingNotifications>(Handle)
Expand All @@ -115,6 +117,45 @@ private void UpdateUnreadCount(int count)
catch { }
}

private void Handle(UpdateSpeedLimitNotification update)
{
var window = WindowContext.Active ?? WindowContext.Main;
var dispatcher = window?.Dispatcher;

if (dispatcher == null)
{
return;
}

var navigationService = window.NavigationServices?.GetByFrameId($"Main{_clientService.SessionId}");
if (navigationService == null)
{
return;
}

var text = update.IsUpload
? string.Format("**{0}**\n{1}", Strings.UploadSpeedLimited, string.Format(Strings.UploadSpeedLimitedMessage, _clientService.Options.PremiumUploadSpeedup))
: string.Format("**{0}**\n{1}", Strings.DownloadSpeedLimited, string.Format(Strings.DownloadSpeedLimitedMessage, _clientService.Options.PremiumDownloadSpeedup));

var markdown = ClientEx.ParseMarkdown(text);
if (markdown.Entities.Count == 2)
{
markdown.Entities[1].Type = new TextEntityTypeTextUrl();
}

dispatcher.Dispatch(() =>
{
var toast = ToastPopup.Show(markdown, new LocalFileSource("ms-appx:///Assets/Toasts/SpeedLimit.tgs"));
void handler(object sender, TextUrlClickEventArgs e)
{
toast.Click -= handler;
navigationService.ShowPromo(new PremiumSourceFeature(new PremiumFeatureImprovedDownloadSpeed()));
}
toast.Click += handler;
});
}

public async void Handle(UpdateTermsOfService update)
{
var terms = update.TermsOfService;
Expand Down
1 change: 1 addition & 0 deletions Telegram/Telegram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,7 @@
<Content Include="Assets\Animations\Business.tgs" />
<Content Include="Assets\Animations\CollectibleUsername.tgs" />
<Content Include="Assets\Animations\BusinessChatLinks.tgs" />
<Content Include="Assets\Toasts\SpeedLimit.tgs" />
<None Include="Assets\i420.hlsl" />
<Content Include="Assets\Images\Reply.scale-100.png" />
<Content Include="Assets\Images\Reply.scale-125.png" />
Expand Down
4 changes: 4 additions & 0 deletions Telegram/Themes/TeachingTip_themeresources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Microsoft.UI.Xaml.Controls"
xmlns:common="using:Telegram.Common"
xmlns:controls="using:Telegram.Controls"
xmlns:primitives="using:Microsoft.UI.Xaml.Controls.Primitives">

<Style TargetType="local:TeachingTip"
BasedOn="{StaticResource DefaultTeachingTipStyle}" />

<Style TargetType="controls:ToastPopup"
BasedOn="{StaticResource DefaultTeachingTipStyle}" />

<Style x:Key="DefaultTeachingTipStyle"
TargetType="local:TeachingTip">
<Setter Property="Background"
Expand Down

0 comments on commit 2a230cc

Please sign in to comment.