Skip to content

Commit 3fe0e47

Browse files
authored
Code Quality: Changed asynchronous logic for loading icons (#14925)
1 parent 302161c commit 3fe0e47

File tree

1 file changed

+115
-116
lines changed

1 file changed

+115
-116
lines changed

src/Files.App/Data/Models/ItemViewModel.cs

Lines changed: 115 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -969,158 +969,157 @@ public async Task LoadExtendedItemPropertiesAsync(ListedItem item)
969969

970970
try
971971
{
972-
await Task.Run(async () =>
973-
{
974-
if (itemLoadQueue.TryGetValue(item.ItemPath, out var canceled) && canceled)
975-
return;
972+
cts.Token.ThrowIfCancellationRequested();
973+
if (itemLoadQueue.TryGetValue(item.ItemPath, out var canceled) && canceled)
974+
return;
976975

977-
item.ItemPropertiesInitialized = true;
978-
var wasSyncStatusLoaded = false;
979-
var loadGroupHeaderInfo = false;
980-
ImageSource? groupImage = null;
981-
GroupedCollection<ListedItem>? gp = null;
976+
item.ItemPropertiesInitialized = true;
977+
var wasSyncStatusLoaded = false;
978+
var loadGroupHeaderInfo = false;
979+
ImageSource? groupImage = null;
980+
GroupedCollection<ListedItem>? gp = null;
982981

983-
try
982+
try
983+
{
984+
var isFileTypeGroupMode = folderSettings.DirectoryGroupOption == GroupOption.FileType;
985+
BaseStorageFile? matchingStorageFile = null;
986+
if (item.Key is not null && FilesAndFolders.IsGrouped && FilesAndFolders.GetExtendedGroupHeaderInfo is not null)
984987
{
985-
var isFileTypeGroupMode = folderSettings.DirectoryGroupOption == GroupOption.FileType;
986-
BaseStorageFile? matchingStorageFile = null;
987-
if (item.Key is not null && FilesAndFolders.IsGrouped && FilesAndFolders.GetExtendedGroupHeaderInfo is not null)
988-
{
989-
gp = FilesAndFolders.GroupedCollection?.ToList().Where(x => x.Model.Key == item.Key).FirstOrDefault();
990-
loadGroupHeaderInfo = gp is not null && !gp.Model.Initialized && gp.GetExtendedGroupHeaderInfo is not null;
991-
}
988+
gp = FilesAndFolders.GroupedCollection?.ToList().Where(x => x.Model.Key == item.Key).FirstOrDefault();
989+
loadGroupHeaderInfo = gp is not null && !gp.Model.Initialized && gp.GetExtendedGroupHeaderInfo is not null;
990+
}
992991

993-
cts.Token.ThrowIfCancellationRequested();
994-
_ = LoadThumbnailAsync(item);
992+
cts.Token.ThrowIfCancellationRequested();
993+
await LoadThumbnailAsync(item);
995994

996-
if (item.IsLibrary || item.PrimaryItemAttribute == StorageItemTypes.File || item.IsArchive)
995+
cts.Token.ThrowIfCancellationRequested();
996+
if (item.IsLibrary || item.PrimaryItemAttribute == StorageItemTypes.File || item.IsArchive)
997+
{
998+
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
997999
{
998-
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
1000+
matchingStorageFile = await GetFileFromPathAsync(item.ItemPath, cts.Token);
1001+
if (matchingStorageFile is not null)
9991002
{
1000-
matchingStorageFile = await GetFileFromPathAsync(item.ItemPath, cts.Token);
1001-
if (matchingStorageFile is not null)
1002-
{
1003-
cts.Token.ThrowIfCancellationRequested();
1003+
cts.Token.ThrowIfCancellationRequested();
10041004

1005-
var syncStatus = await CheckCloudDriveSyncStatusAsync(matchingStorageFile);
1006-
var fileFRN = await FileTagsHelper.GetFileFRN(matchingStorageFile);
1007-
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);
1008-
var itemType = (item.ItemType == "Folder".GetLocalizedResource()) ? item.ItemType : matchingStorageFile.DisplayType;
1005+
var syncStatus = await CheckCloudDriveSyncStatusAsync(matchingStorageFile);
1006+
var fileFRN = await FileTagsHelper.GetFileFRN(matchingStorageFile);
1007+
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);
1008+
var itemType = (item.ItemType == "Folder".GetLocalizedResource()) ? item.ItemType : matchingStorageFile.DisplayType;
10091009

1010-
cts.Token.ThrowIfCancellationRequested();
1010+
cts.Token.ThrowIfCancellationRequested();
10111011

1012-
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
1013-
{
1014-
item.FolderRelativeId = matchingStorageFile.FolderRelativeId;
1015-
item.ItemType = itemType;
1016-
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
1017-
item.FileFRN = fileFRN;
1018-
item.FileTags = fileTag;
1019-
item.IsElevationRequired = CheckElevationRights(item);
1020-
},
1021-
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1022-
1023-
SetFileTag(item);
1024-
wasSyncStatusLoaded = true;
1025-
}
1012+
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
1013+
{
1014+
item.FolderRelativeId = matchingStorageFile.FolderRelativeId;
1015+
item.ItemType = itemType;
1016+
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
1017+
item.FileFRN = fileFRN;
1018+
item.FileTags = fileTag;
1019+
item.IsElevationRequired = CheckElevationRights(item);
1020+
},
1021+
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1022+
1023+
SetFileTag(item);
1024+
wasSyncStatusLoaded = true;
10261025
}
10271026
}
1028-
else
1027+
}
1028+
else
1029+
{
1030+
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
10291031
{
1030-
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
1032+
BaseStorageFolder matchingStorageFolder = await GetFolderFromPathAsync(item.ItemPath, cts.Token);
1033+
if (matchingStorageFolder is not null)
10311034
{
1032-
BaseStorageFolder matchingStorageFolder = await GetFolderFromPathAsync(item.ItemPath, cts.Token);
1033-
if (matchingStorageFolder is not null)
1035+
if (matchingStorageFolder.DisplayName != item.Name && !matchingStorageFolder.DisplayName.StartsWith("$R", StringComparison.Ordinal))
10341036
{
1035-
if (matchingStorageFolder.DisplayName != item.Name && !matchingStorageFolder.DisplayName.StartsWith("$R", StringComparison.Ordinal))
1036-
{
1037-
cts.Token.ThrowIfCancellationRequested();
1038-
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
1039-
{
1040-
item.ItemNameRaw = matchingStorageFolder.DisplayName;
1041-
});
1042-
await fileListCache.SaveFileDisplayNameToCache(item.ItemPath, matchingStorageFolder.DisplayName);
1043-
if (folderSettings.DirectorySortOption == SortOption.Name && !isLoadingItems)
1044-
{
1045-
await OrderFilesAndFoldersAsync();
1046-
await ApplyFilesAndFoldersChangesAsync();
1047-
}
1048-
}
1049-
1050-
cts.Token.ThrowIfCancellationRequested();
1051-
var syncStatus = await CheckCloudDriveSyncStatusAsync(matchingStorageFolder);
1052-
var fileFRN = await FileTagsHelper.GetFileFRN(matchingStorageFolder);
1053-
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);
1054-
var itemType = (item.ItemType == "Folder".GetLocalizedResource()) ? item.ItemType : matchingStorageFolder.DisplayType;
10551037
cts.Token.ThrowIfCancellationRequested();
1056-
10571038
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
10581039
{
1059-
item.FolderRelativeId = matchingStorageFolder.FolderRelativeId;
1060-
item.ItemType = itemType;
1061-
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
1062-
item.FileFRN = fileFRN;
1063-
item.FileTags = fileTag;
1064-
},
1065-
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1066-
1067-
SetFileTag(item);
1068-
wasSyncStatusLoaded = true;
1040+
item.ItemNameRaw = matchingStorageFolder.DisplayName;
1041+
});
1042+
await fileListCache.SaveFileDisplayNameToCache(item.ItemPath, matchingStorageFolder.DisplayName);
1043+
if (folderSettings.DirectorySortOption == SortOption.Name && !isLoadingItems)
1044+
{
1045+
await OrderFilesAndFoldersAsync();
1046+
await ApplyFilesAndFoldersChangesAsync();
1047+
}
10691048
}
1070-
}
1071-
}
10721049

1073-
if (loadGroupHeaderInfo && isFileTypeGroupMode)
1074-
groupImage = await GetItemTypeGroupIcon(item, matchingStorageFile);
1075-
}
1076-
catch (Exception)
1077-
{
1078-
}
1079-
finally
1080-
{
1081-
if (!wasSyncStatusLoaded)
1082-
{
1083-
cts.Token.ThrowIfCancellationRequested();
1084-
await FilesystemTasks.Wrap(async () =>
1085-
{
1050+
cts.Token.ThrowIfCancellationRequested();
1051+
var syncStatus = await CheckCloudDriveSyncStatusAsync(matchingStorageFolder);
1052+
var fileFRN = await FileTagsHelper.GetFileFRN(matchingStorageFolder);
10861053
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);
1054+
var itemType = (item.ItemType == "Folder".GetLocalizedResource()) ? item.ItemType : matchingStorageFolder.DisplayType;
1055+
cts.Token.ThrowIfCancellationRequested();
10871056

10881057
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
10891058
{
1090-
// Reset cloud sync status icon
1091-
item.SyncStatusUI = new CloudDriveSyncStatusUI();
1092-
1059+
item.FolderRelativeId = matchingStorageFolder.FolderRelativeId;
1060+
item.ItemType = itemType;
1061+
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
1062+
item.FileFRN = fileFRN;
10931063
item.FileTags = fileTag;
10941064
},
10951065
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
10961066

10971067
SetFileTag(item);
1098-
});
1068+
wasSyncStatusLoaded = true;
1069+
}
10991070
}
1100-
else
1071+
}
1072+
1073+
if (loadGroupHeaderInfo && isFileTypeGroupMode)
1074+
groupImage = await GetItemTypeGroupIcon(item, matchingStorageFile);
1075+
}
1076+
catch (Exception)
1077+
{
1078+
}
1079+
finally
1080+
{
1081+
if (!wasSyncStatusLoaded)
1082+
{
1083+
cts.Token.ThrowIfCancellationRequested();
1084+
await FilesystemTasks.Wrap(async () =>
11011085
{
1102-
// Try loading thumbnail for cloud files in case they weren't cached the first time
1103-
if (item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.NotSynced && item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.Unknown)
1086+
var fileTag = FileTagsHelper.ReadFileTag(item.ItemPath);
1087+
1088+
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
11041089
{
1105-
_ = Task.Run(async () => {
1106-
await Task.Delay(500);
1107-
await LoadThumbnailAsync(item);
1108-
});
1109-
}
1110-
}
1090+
// Reset cloud sync status icon
1091+
item.SyncStatusUI = new CloudDriveSyncStatusUI();
11111092

1112-
if (loadGroupHeaderInfo)
1093+
item.FileTags = fileTag;
1094+
},
1095+
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1096+
1097+
SetFileTag(item);
1098+
});
1099+
}
1100+
else
1101+
{
1102+
// Try loading thumbnail for cloud files in case they weren't cached the first time
1103+
if (item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.NotSynced && item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.Unknown)
11131104
{
1114-
cts.Token.ThrowIfCancellationRequested();
1115-
await SafetyExtensions.IgnoreExceptions(() =>
1116-
dispatcherQueue.EnqueueOrInvokeAsync(() =>
1117-
{
1118-
gp.Model.ImageSource = groupImage;
1119-
gp.InitializeExtendedGroupHeaderInfoAsync();
1120-
}));
1105+
_ = Task.Run(async () => {
1106+
await Task.Delay(500);
1107+
await LoadThumbnailAsync(item);
1108+
});
11211109
}
11221110
}
1123-
}, cts.Token);
1111+
1112+
if (loadGroupHeaderInfo)
1113+
{
1114+
cts.Token.ThrowIfCancellationRequested();
1115+
await SafetyExtensions.IgnoreExceptions(() =>
1116+
dispatcherQueue.EnqueueOrInvokeAsync(() =>
1117+
{
1118+
gp.Model.ImageSource = groupImage;
1119+
gp.InitializeExtendedGroupHeaderInfoAsync();
1120+
}));
1121+
}
1122+
}
11241123
}
11251124
catch (OperationCanceledException)
11261125
{

0 commit comments

Comments
 (0)