Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality: Changed asynchronous logic for loading icons #14925

Merged
merged 1 commit into from Mar 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
231 changes: 115 additions & 116 deletions src/Files.App/Data/Models/ItemViewModel.cs
Expand Up @@ -969,158 +969,157 @@ public async Task LoadExtendedItemPropertiesAsync(ListedItem item)

try
{
await Task.Run(async () =>
{
if (itemLoadQueue.TryGetValue(item.ItemPath, out var canceled) && canceled)
return;
cts.Token.ThrowIfCancellationRequested();
if (itemLoadQueue.TryGetValue(item.ItemPath, out var canceled) && canceled)
return;

item.ItemPropertiesInitialized = true;
var wasSyncStatusLoaded = false;
var loadGroupHeaderInfo = false;
ImageSource? groupImage = null;
GroupedCollection<ListedItem>? gp = null;
item.ItemPropertiesInitialized = true;
var wasSyncStatusLoaded = false;
var loadGroupHeaderInfo = false;
ImageSource? groupImage = null;
GroupedCollection<ListedItem>? gp = null;

try
try
{
var isFileTypeGroupMode = folderSettings.DirectoryGroupOption == GroupOption.FileType;
BaseStorageFile? matchingStorageFile = null;
if (item.Key is not null && FilesAndFolders.IsGrouped && FilesAndFolders.GetExtendedGroupHeaderInfo is not null)
{
var isFileTypeGroupMode = folderSettings.DirectoryGroupOption == GroupOption.FileType;
BaseStorageFile? matchingStorageFile = null;
if (item.Key is not null && FilesAndFolders.IsGrouped && FilesAndFolders.GetExtendedGroupHeaderInfo is not null)
{
gp = FilesAndFolders.GroupedCollection?.ToList().Where(x => x.Model.Key == item.Key).FirstOrDefault();
loadGroupHeaderInfo = gp is not null && !gp.Model.Initialized && gp.GetExtendedGroupHeaderInfo is not null;
}
gp = FilesAndFolders.GroupedCollection?.ToList().Where(x => x.Model.Key == item.Key).FirstOrDefault();
loadGroupHeaderInfo = gp is not null && !gp.Model.Initialized && gp.GetExtendedGroupHeaderInfo is not null;
}

cts.Token.ThrowIfCancellationRequested();
_ = LoadThumbnailAsync(item);
cts.Token.ThrowIfCancellationRequested();
await LoadThumbnailAsync(item);

if (item.IsLibrary || item.PrimaryItemAttribute == StorageItemTypes.File || item.IsArchive)
cts.Token.ThrowIfCancellationRequested();
if (item.IsLibrary || item.PrimaryItemAttribute == StorageItemTypes.File || item.IsArchive)
{
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
{
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
matchingStorageFile = await GetFileFromPathAsync(item.ItemPath, cts.Token);
if (matchingStorageFile is not null)
{
matchingStorageFile = await GetFileFromPathAsync(item.ItemPath, cts.Token);
if (matchingStorageFile is not null)
{
cts.Token.ThrowIfCancellationRequested();
cts.Token.ThrowIfCancellationRequested();

var syncStatus = await CheckCloudDriveSyncStatusAsync(matchingStorageFile);
var fileFRN = await FileTagsHelper.GetFileFRN(matchingStorageFile);
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);
var itemType = (item.ItemType == "Folder".GetLocalizedResource()) ? item.ItemType : matchingStorageFile.DisplayType;
var syncStatus = await CheckCloudDriveSyncStatusAsync(matchingStorageFile);
var fileFRN = await FileTagsHelper.GetFileFRN(matchingStorageFile);
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);
var itemType = (item.ItemType == "Folder".GetLocalizedResource()) ? item.ItemType : matchingStorageFile.DisplayType;

cts.Token.ThrowIfCancellationRequested();
cts.Token.ThrowIfCancellationRequested();

await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
item.FolderRelativeId = matchingStorageFile.FolderRelativeId;
item.ItemType = itemType;
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
item.FileFRN = fileFRN;
item.FileTags = fileTag;
item.IsElevationRequired = CheckElevationRights(item);
},
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);

SetFileTag(item);
wasSyncStatusLoaded = true;
}
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
item.FolderRelativeId = matchingStorageFile.FolderRelativeId;
item.ItemType = itemType;
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
item.FileFRN = fileFRN;
item.FileTags = fileTag;
item.IsElevationRequired = CheckElevationRights(item);
},
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);

SetFileTag(item);
wasSyncStatusLoaded = true;
}
}
else
}
else
{
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
{
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
BaseStorageFolder matchingStorageFolder = await GetFolderFromPathAsync(item.ItemPath, cts.Token);
if (matchingStorageFolder is not null)
{
BaseStorageFolder matchingStorageFolder = await GetFolderFromPathAsync(item.ItemPath, cts.Token);
if (matchingStorageFolder is not null)
if (matchingStorageFolder.DisplayName != item.Name && !matchingStorageFolder.DisplayName.StartsWith("$R", StringComparison.Ordinal))
{
if (matchingStorageFolder.DisplayName != item.Name && !matchingStorageFolder.DisplayName.StartsWith("$R", StringComparison.Ordinal))
{
cts.Token.ThrowIfCancellationRequested();
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
item.ItemNameRaw = matchingStorageFolder.DisplayName;
});
await fileListCache.SaveFileDisplayNameToCache(item.ItemPath, matchingStorageFolder.DisplayName);
if (folderSettings.DirectorySortOption == SortOption.Name && !isLoadingItems)
{
await OrderFilesAndFoldersAsync();
await ApplyFilesAndFoldersChangesAsync();
}
}

cts.Token.ThrowIfCancellationRequested();
var syncStatus = await CheckCloudDriveSyncStatusAsync(matchingStorageFolder);
var fileFRN = await FileTagsHelper.GetFileFRN(matchingStorageFolder);
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);
var itemType = (item.ItemType == "Folder".GetLocalizedResource()) ? item.ItemType : matchingStorageFolder.DisplayType;
cts.Token.ThrowIfCancellationRequested();

await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
item.FolderRelativeId = matchingStorageFolder.FolderRelativeId;
item.ItemType = itemType;
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
item.FileFRN = fileFRN;
item.FileTags = fileTag;
},
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);

SetFileTag(item);
wasSyncStatusLoaded = true;
item.ItemNameRaw = matchingStorageFolder.DisplayName;
});
await fileListCache.SaveFileDisplayNameToCache(item.ItemPath, matchingStorageFolder.DisplayName);
if (folderSettings.DirectorySortOption == SortOption.Name && !isLoadingItems)
{
await OrderFilesAndFoldersAsync();
await ApplyFilesAndFoldersChangesAsync();
}
}
}
}

if (loadGroupHeaderInfo && isFileTypeGroupMode)
groupImage = await GetItemTypeGroupIcon(item, matchingStorageFile);
}
catch (Exception)
{
}
finally
{
if (!wasSyncStatusLoaded)
{
cts.Token.ThrowIfCancellationRequested();
await FilesystemTasks.Wrap(async () =>
{
cts.Token.ThrowIfCancellationRequested();
var syncStatus = await CheckCloudDriveSyncStatusAsync(matchingStorageFolder);
var fileFRN = await FileTagsHelper.GetFileFRN(matchingStorageFolder);
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);
var itemType = (item.ItemType == "Folder".GetLocalizedResource()) ? item.ItemType : matchingStorageFolder.DisplayType;
cts.Token.ThrowIfCancellationRequested();

await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
// Reset cloud sync status icon
item.SyncStatusUI = new CloudDriveSyncStatusUI();

item.FolderRelativeId = matchingStorageFolder.FolderRelativeId;
item.ItemType = itemType;
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
item.FileFRN = fileFRN;
item.FileTags = fileTag;
},
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);

SetFileTag(item);
});
wasSyncStatusLoaded = true;
}
}
else
}

if (loadGroupHeaderInfo && isFileTypeGroupMode)
groupImage = await GetItemTypeGroupIcon(item, matchingStorageFile);
}
catch (Exception)
{
}
finally
{
if (!wasSyncStatusLoaded)
{
cts.Token.ThrowIfCancellationRequested();
await FilesystemTasks.Wrap(async () =>
{
// Try loading thumbnail for cloud files in case they weren't cached the first time
if (item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.NotSynced && item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.Unknown)
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);

await dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
_ = Task.Run(async () => {
await Task.Delay(500);
await LoadThumbnailAsync(item);
});
}
}
// Reset cloud sync status icon
item.SyncStatusUI = new CloudDriveSyncStatusUI();

if (loadGroupHeaderInfo)
item.FileTags = fileTag;
},
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);

SetFileTag(item);
});
}
else
{
// Try loading thumbnail for cloud files in case they weren't cached the first time
if (item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.NotSynced && item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.Unknown)
{
cts.Token.ThrowIfCancellationRequested();
await SafetyExtensions.IgnoreExceptions(() =>
dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
gp.Model.ImageSource = groupImage;
gp.InitializeExtendedGroupHeaderInfoAsync();
}));
_ = Task.Run(async () => {
await Task.Delay(500);
await LoadThumbnailAsync(item);
});
}
}
}, cts.Token);

if (loadGroupHeaderInfo)
{
cts.Token.ThrowIfCancellationRequested();
await SafetyExtensions.IgnoreExceptions(() =>
dispatcherQueue.EnqueueOrInvokeAsync(() =>
{
gp.Model.ImageSource = groupImage;
gp.InitializeExtendedGroupHeaderInfoAsync();
}));
}
}
}
catch (OperationCanceledException)
{
Expand Down