Skip to content

Commit

Permalink
Fix common exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Dec 4, 2024
1 parent 0087383 commit a6e25d4
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Telegram/Common/MediaDeviceList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public bool? HasValues
{
lock (_cache)
{
return _cache.Count > 0 ? true : _watcher.Status == DeviceWatcherStatus.EnumerationCompleted ? false : null;
return _cache.Count > 0 ? true : _watcher?.Status == DeviceWatcherStatus.EnumerationCompleted ? false : null;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Telegram/Controls/ChatListListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void Continue(bool restore)
}
}

if (_changingView || direction == CarouselDirection.None || !IsConnected || !PowerSavingPolicy.AreSmoothTransitionsEnabled)
if (_changingView || _tracker == null || direction == CarouselDirection.None || !IsConnected || !PowerSavingPolicy.AreSmoothTransitionsEnabled)
{
Continue(true);
return;
Expand Down
12 changes: 9 additions & 3 deletions Telegram/Controls/FileButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public partial class FileButton : GlyphHyperlinkButton
public FileButton()
{
DefaultStyleKey = typeof(FileButton);
Connected += OnConnected;
}

private void OnConnected(object sender, RoutedEventArgs e)
{
// Used to enable FrameworkElementEx
}

public MessageContentState State => _state;
Expand Down Expand Up @@ -112,7 +118,7 @@ public double Progress
{
set
{
if (_shouldEnqueueProgress)
if (_shouldEnqueueProgress || !IsConnected)
{
_enqueuedProgress = value;
}
Expand Down Expand Up @@ -240,7 +246,7 @@ private void OnGlyphChanged(string newValue, string oldValue, bool animate, stri

_label.Text = newValue;

if (_hasContainer && (clearContainer || !animate) && this.IsConnected())
if (_hasContainer && (clearContainer || !animate) && IsConnected)
{
_hasContainer = false;
ElementComposition.SetElementChildVisual(RootGrid, null);
Expand Down Expand Up @@ -370,7 +376,7 @@ private void OnDownloadingCompleted(object sender, CompositionBatchCompletedEven
{
try
{
if (_state == MessageContentState.Downloading && this.IsConnected())
if (_state == MessageContentState.Downloading && IsConnected)
{
OnGlyphChanged(Icons.Cancel, Icons.ArrowDownload, true, Strings.AccActionCancelDownload, false);
InternalProgress = _enqueuedProgress;
Expand Down
80 changes: 80 additions & 0 deletions Telegram/Controls/FrameworkElementEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,84 @@ private void OnChanged(object sender, RoutedEventArgs e)
}
}
}

public partial class HyperlinkButtonEx : HyperlinkButton
{
private bool _loaded;
private bool _unloaded;

public bool IsConnected => _loaded;
public bool IsDisconnected => _unloaded;

private event RoutedEventHandler _connected;
public event RoutedEventHandler Connected
{
add
{
if (_connected == null && _disconnected == null)
{
Loaded += OnChanged;
Unloaded += OnChanged;
}

_connected += value;
}
remove
{
_connected -= value;

if (_connected == null && _disconnected == null)
{
Loaded -= OnChanged;
Unloaded -= OnChanged;
}
}
}

private event RoutedEventHandler _disconnected;
public event RoutedEventHandler Disconnected
{
add
{
if (_connected == null && _disconnected == null)
{
Loaded += OnChanged;
Unloaded += OnChanged;
}

_disconnected += value;
}
remove
{
_disconnected -= value;

if (_connected == null && _disconnected == null)
{
Loaded -= OnChanged;
Unloaded -= OnChanged;
}
}
}

private void OnChanged(object sender, RoutedEventArgs e)
{
// TODO: unfortunately FrameworkElement.Parent returns null
// whenever the control is a DataTemplate root or similar,
// hence we're forced to use VisualTreeHelper here, but I'm quite sure it's slower.

var parent = Parent ?? Windows.UI.Xaml.Media.VisualTreeHelper.GetParent(this);
if (parent != null && !_loaded)
{
_loaded = true;
_unloaded = false;
_connected?.Invoke(this, e);
}
else if (parent == null && _loaded)
{
_loaded = false;
_unloaded = true;
_disconnected?.Invoke(sender, e);
}
}
}
}
2 changes: 1 addition & 1 deletion Telegram/Controls/GlyphButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public string Glyph
#endregion
}

public partial class GlyphHyperlinkButton : HyperlinkButton
public partial class GlyphHyperlinkButton : HyperlinkButtonEx
{
public GlyphHyperlinkButton()
{
Expand Down
5 changes: 4 additions & 1 deletion Telegram/Services/ClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,10 @@ private void UpdateTimeZones()

public bool TryGetTimeZone(string timeZoneId, out TimeZone timeZone)
{
return _timezones.TryGetValue(timeZoneId, out timeZone);
lock (_timezones)
{
return _timezones.TryGetValue(timeZoneId, out timeZone);
}
}

private void UpdateGreetingStickers()
Expand Down

0 comments on commit a6e25d4

Please sign in to comment.