Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Feb 8, 2024
1 parent 0dd9fff commit 45e3952
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
12 changes: 7 additions & 5 deletions Telegram/Collections/FlatteningCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

namespace Telegram.Collections
{
public interface IKeyedCollection : ICollection, INotifyCollectionChanged
public interface IKeyedCollection : ICollection, IList, INotifyCollectionChanged
{
int Index { get; set; }

int Displacement { get; }
int TotalIndex { get; }

int TotalCount { get; }

Expand Down Expand Up @@ -76,14 +76,14 @@ private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs

private void Insert(IKeyedCollection collection, int newStartingIndex, IList newItems)
{
if (newStartingIndex == 0 && newItems.Count == collection.Count)
if (newStartingIndex == 0 && newItems.Count == collection.Count && collection.Count > 0)
{
Insert(collection.Index, collection);
}

foreach (var item in newItems)
{
Insert(collection.Displacement + newStartingIndex, item);
Insert(collection.TotalIndex + newStartingIndex, item);
newStartingIndex++;
}
}
Expand All @@ -92,7 +92,7 @@ private void Remove(IKeyedCollection collection, int oldStartingIndex, int oldIt
{
for (int i = oldStartingIndex; i < oldStartingIndex + oldItemsCount; i++)
{
RemoveAt(collection.Displacement + i);
RemoveAt(collection.TotalIndex + i);
}

if (collection.Count == 0 && oldItemsCount > 0)
Expand All @@ -112,6 +112,8 @@ private void Reset(IKeyedCollection collection)
{
RemoveAt(collection.Index);
}

Insert(collection, 0, collection);
}

private void UpdateIndexes(IKeyedCollection from)
Expand Down
2 changes: 1 addition & 1 deletion Telegram/Collections/MvxObservableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public void RemoveRange(int start, int count)
}

var removedItems = new List<T>(count);
for (int i = start; i < count; i++)
for (int i = start; i <= end; i++)
{
removedItems.Add(this[i]);
}
Expand Down
32 changes: 15 additions & 17 deletions Telegram/Controls/AnimatedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,16 +1725,13 @@ static bool IsValid(AnimatedImagePresentation presentation)
if (IsValid(work.Presentation))
{
NotifyDelegate(work.CorrelationId, animation, new LottieAnimatedImageTask(animation, work.Presentation));
return;
}
else
{
animation.Dispose();
}
}
else
{
_delegates.TryRemove(work.CorrelationId, out _);

animation.Dispose();
}

_delegates.TryRemove(work.CorrelationId, out _);
}

private void LoadCachedVideo(WorkItem work)
Expand All @@ -1754,16 +1751,13 @@ static bool IsValid(CachedVideoAnimation animation)
if (IsValid(animation))
{
NotifyDelegate(work.CorrelationId, animation, new VideoAnimatedImageTask(animation, work.Presentation));
return;
}
else
{
animation.Dispose();
}
}
else
{
_delegates.TryRemove(work.CorrelationId, out _);

animation.Dispose();
}

_delegates.TryRemove(work.CorrelationId, out _);
}

private async void LoadWebP(WorkItem work, LocalFileSource local)
Expand All @@ -1783,6 +1777,7 @@ static bool IsValid(IBuffer animation, int pixelWidth, int pixelHeight)
if (IsValid(animation, pixelWidth, pixelHeight))
{
NotifyDelegate(work.CorrelationId, null, new WebpAnimatedImageTask(animation, pixelWidth, pixelHeight, work.Presentation));
return;
}
}
else
Expand Down Expand Up @@ -1822,13 +1817,16 @@ static bool IsValid(IBuffer animation, int pixelWidth, int pixelHeight)
if (IsValid(animation, pixelWidth, pixelHeight))
{
NotifyDelegate(work.CorrelationId, null, new WebpAnimatedImageTask(animation, pixelWidth, pixelHeight, work.Presentation));
return;
}
}
catch
{
_delegates.TryRemove(work.CorrelationId, out _);
// All the remote procedure calls must be wrapped in a try-catch block
}
}

_delegates.TryRemove(work.CorrelationId, out _);
}

private bool NotifyDelegate(int correlationId, IDisposable disposable, AnimatedImageTask task)
Expand Down
9 changes: 9 additions & 0 deletions Telegram/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,13 @@ public static unsafe string Dump()
}
}
}

public class RuntimeException : Exception
{
public RuntimeException(Exception innerException)
: base(innerException.Message, innerException)
{

}
}
}
2 changes: 1 addition & 1 deletion Telegram/ViewModels/SearchChatsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public class KeyedCollection<T> : DiffObservableCollection<T>, IKeyedCollection

public int Index { get; set; }

public int Displacement => Index + (Key != null && Count > 0 ? 1 : 0);
public int TotalIndex => Index + (Key != null && Count > 0 ? 1 : 0);

public int TotalCount => Count + (Key != null && Count > 0 ? 1 : 0);

Expand Down

0 comments on commit 45e3952

Please sign in to comment.