Compare commits

...

2 Commits

Author SHA1 Message Date
Ross Luengen
64f5309851 initialize properties on a background thread to not block the UI thread 2025-09-23 12:54:42 -07:00
Ross Luengen
a690ea96a5 Realize list item properties when the container changes for UI 2025-09-23 10:04:13 -07:00
3 changed files with 10 additions and 46 deletions

View File

@@ -67,7 +67,6 @@ public partial class ListViewModel : PageViewModel, IDisposable
private bool _isDynamic;
private Task? _initializeItemsTask;
private CancellationTokenSource? _cancellationTokenSource;
private CancellationTokenSource? _fetchItemsCancellationTokenSource;
@@ -200,13 +199,6 @@ public partial class ListViewModel : PageViewModel, IDisposable
// Check for cancellation before initializing first twenty items
cancellationToken.ThrowIfCancellationRequested();
var firstTwenty = newViewModels.Take(20);
foreach (var item in firstTwenty)
{
cancellationToken.ThrowIfCancellationRequested();
item?.SafeInitializeProperties();
}
// Cancel any ongoing search
_cancellationTokenSource?.Cancel();
@@ -262,18 +254,6 @@ public partial class ListViewModel : PageViewModel, IDisposable
_cancellationTokenSource = new CancellationTokenSource();
_initializeItemsTask = new Task(() =>
{
try
{
InitializeItemsTask(_cancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
}
});
_initializeItemsTask.Start();
DoOnUiThread(
() =>
{
@@ -301,32 +281,6 @@ public partial class ListViewModel : PageViewModel, IDisposable
});
}
private void InitializeItemsTask(CancellationToken ct)
{
// Were we already canceled?
ct.ThrowIfCancellationRequested();
ListItemViewModel[] iterable;
lock (_listLock)
{
iterable = Items.ToArray();
}
foreach (var item in iterable)
{
ct.ThrowIfCancellationRequested();
// TODO: GH #502
// We should probably remove the item from the list if it
// entered the error state. I had issues doing that without having
// multiple threads muck with `Items` (and possibly FilteredItems!)
// at once.
item.SafeInitializeProperties();
ct.ThrowIfCancellationRequested();
}
}
/// <summary>
/// Apply our current filter text to the list of items, and update
/// FilteredItems to match the results.

View File

@@ -276,6 +276,7 @@
IsDoubleTapEnabled="True"
IsItemClickEnabled="True"
ItemClick="Items_ItemClick"
ContainerContentChanging="ItemsList_ContainerContentChanging"
ItemTemplate="{StaticResource ListItemViewModelTemplate}"
ItemsSource="{x:Bind ViewModel.FilteredItems, Mode=OneWay}"
RightTapped="Items_RightTapped"
@@ -295,6 +296,7 @@
IsDoubleTapEnabled="True"
IsItemClickEnabled="True"
ItemClick="Items_ItemClick"
ContainerContentChanging="ItemsList_ContainerContentChanging"
ItemTemplateSelector="{StaticResource GridItemTemplateSelector}"
ItemsSource="{x:Bind ViewModel.FilteredItems, Mode=OneWay}"
RightTapped="Items_RightTapped"

View File

@@ -427,4 +427,12 @@ public sealed partial class ListPage : Page,
Keyboard,
Pointer,
}
private void ItemsList_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (args.Item is ListItemViewModel item)
{
_ = Task.Run(() => item.SafeInitializeProperties());
}
}
}