Skip to content

Commit 5bd39d5

Browse files
authored
Code Quality: Fixed non-thread-safe access to ItemViewModel.FilesAndFolders (#14626)
1 parent 633bae0 commit 5bd39d5

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ await Task.Run(async () =>
11271127
BaseStorageFile? matchingStorageFile = null;
11281128
if (item.Key is not null && FilesAndFolders.IsGrouped && FilesAndFolders.GetExtendedGroupHeaderInfo is not null)
11291129
{
1130-
gp = FilesAndFolders.GroupedCollection?.Where(x => x.Model.Key == item.Key).FirstOrDefault();
1130+
gp = FilesAndFolders.GroupedCollection?.ToList().Where(x => x.Model.Key == item.Key).FirstOrDefault();
11311131
loadGroupHeaderInfo = gp is not null && !gp.Model.Initialized && gp.GetExtendedGroupHeaderInfo is not null;
11321132
}
11331133

src/Files.App/Views/Layouts/BaseLayoutPage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public string JumpString
187187
if (previouslySelectedItem is not null)
188188
{
189189
// Use FilesAndFolders because only displayed entries should be jumped to
190-
IEnumerable<ListedItem> candidateItems = ParentShellPageInstance!.FilesystemViewModel.FilesAndFolders
190+
IEnumerable<ListedItem> candidateItems = ParentShellPageInstance!.FilesystemViewModel.FilesAndFolders.ToList()
191191
.SkipWhile(x => x != previouslySelectedItem)
192192
.Skip(value.Length == 1 ? 1 : 0) // User is trying to cycle through items starting with the same letter
193193
.Where(f => f.Name.Length >= value.Length && string.Equals(f.Name.Substring(0, value.Length), value, StringComparison.OrdinalIgnoreCase));
@@ -197,7 +197,7 @@ public string JumpString
197197
if (jumpedToItem is null)
198198
{
199199
// Use FilesAndFolders because only displayed entries should be jumped to
200-
IEnumerable<ListedItem> candidateItems = ParentShellPageInstance!.FilesystemViewModel.FilesAndFolders
200+
IEnumerable<ListedItem> candidateItems = ParentShellPageInstance!.FilesystemViewModel.FilesAndFolders.ToList()
201201
.Where(f => f.Name.Length >= value.Length && string.Equals(f.Name.Substring(0, value.Length), value, StringComparison.OrdinalIgnoreCase));
202202
jumpedToItem = candidateItems.FirstOrDefault();
203203
}
@@ -496,7 +496,7 @@ navigationArguments.SelectItems is not null &&
496496
navigationArguments.SelectItems.Any())
497497
{
498498
List<ListedItem> listedItemsToSelect = new();
499-
listedItemsToSelect.AddRange(ParentShellPageInstance!.FilesystemViewModel.FilesAndFolders.Where((li) => navigationArguments.SelectItems.Contains(li.ItemNameRaw)));
499+
listedItemsToSelect.AddRange(ParentShellPageInstance!.FilesystemViewModel.FilesAndFolders.ToList().Where((li) => navigationArguments.SelectItems.Contains(li.ItemNameRaw)));
500500

501501
ItemManipulationModel.SetSelectedItems(listedItemsToSelect);
502502
ItemManipulationModel.FocusSelectedItems();

src/Files.App/Views/Layouts/ColumnLayoutPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ protected override void FileList_SelectionChanged(object sender, SelectionChange
248248
else if (SelectedItems?.Count > 1
249249
|| SelectedItem?.PrimaryItemAttribute is StorageItemTypes.File
250250
|| openedFolderPresenter != null && ParentShellPageInstance != null
251-
&& !ParentShellPageInstance.FilesystemViewModel.FilesAndFolders.Contains(FileList.ItemFromContainer(openedFolderPresenter))
251+
&& !ParentShellPageInstance.FilesystemViewModel.FilesAndFolders.ToList().Contains(FileList.ItemFromContainer(openedFolderPresenter))
252252
&& !isDraggingSelectionRectangle) // Skip closing if dragging since nothing should be open
253253
{
254254
CloseFolder();

src/Files.App/Views/Shells/BaseShellPage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ protected void FilesystemViewModel_ItemLoadStatusChanged(object sender, ItemLoad
679679
if (folderToSelect.EndsWith('\\'))
680680
folderToSelect = folderToSelect.Remove(folderToSelect.Length - 1, 1);
681681

682-
var itemToSelect = FilesystemViewModel.FilesAndFolders.Where((item) => item.ItemPath == folderToSelect).FirstOrDefault();
682+
var itemToSelect = FilesystemViewModel.FilesAndFolders.ToList().Where((item) => item.ItemPath == folderToSelect).FirstOrDefault();
683683

684684
if (itemToSelect is not null && ContentPage is not null)
685685
{

src/Files.App/Views/Shells/ModernShellPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected override void FolderSettings_LayoutPreferencesUpdateRequired(object se
8585

8686
LayoutPreferencesManager.SetLayoutPreferencesForPath(FilesystemViewModel.WorkingDirectory, e.LayoutPreference);
8787
if (e.IsAdaptiveLayoutUpdateRequired)
88-
AdaptiveLayoutHelpers.ApplyAdaptativeLayout(InstanceViewModel.FolderSettings, FilesystemViewModel.WorkingDirectory, FilesystemViewModel.FilesAndFolders);
88+
AdaptiveLayoutHelpers.ApplyAdaptativeLayout(InstanceViewModel.FolderSettings, FilesystemViewModel.WorkingDirectory, FilesystemViewModel.FilesAndFolders.ToList());
8989
}
9090

9191
protected override void OnNavigatedTo(NavigationEventArgs eventArgs)

0 commit comments

Comments
 (0)