Skip to content

Commit 1258999

Browse files
authored
fix: file list randomly scrolling to top (#9130)
1 parent 3fcea26 commit 1258999

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

src/Files.Uwp/ViewModels/ItemViewModel.cs

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,8 @@ await dispatcherQueue.EnqueueAsync(() =>
521521
// get the item that immediately follows matching item to be removed
522522
// if the matching item is the last item, try to get the previous item; otherwise, null
523523
// case must be ignored since $Recycle.Bin != $RECYCLE.BIN
524-
var nextOfMatchingItem = filesAndFolders
525-
.SkipWhile((x) => !x.ItemPath.Equals(itemPath, StringComparison.OrdinalIgnoreCase)).Skip(1)
526-
.DefaultIfEmpty(filesAndFolders.TakeWhile((x) => !x.ItemPath.Equals(itemPath, StringComparison.OrdinalIgnoreCase)).LastOrDefault())
527-
.FirstOrDefault();
524+
var itemRemovedIndex = filesAndFolders.FindIndex(x => x.ItemPath.Equals(itemPath, StringComparison.OrdinalIgnoreCase));
525+
var nextOfMatchingItem = filesAndFolders.ElementAtOrDefault(itemRemovedIndex + 1 < filesAndFolders.Count() ? itemRemovedIndex + 1 : itemRemovedIndex - 1);
528526
var removedItem = await RemoveFileOrFolderAsync(itemPath);
529527
if (removedItem != null)
530528
{
@@ -1998,6 +1996,24 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
19981996
ListedItem nextOfLastItemRemoved = null;
19991997
var rand = Guid.NewGuid();
20001998

1999+
// call when any edits have occurred
2000+
async Task HandleChangesOccurredAsync()
2001+
{
2002+
await OrderFilesAndFoldersAsync();
2003+
await ApplyFilesAndFoldersChangesAsync();
2004+
if (lastItemAdded != null)
2005+
{
2006+
await RequestSelectionAsync(new List<ListedItem>() { lastItemAdded });
2007+
lastItemAdded = null;
2008+
}
2009+
if (nextOfLastItemRemoved != null)
2010+
{
2011+
await RequestSelectionAsync(new List<ListedItem>() { nextOfLastItemRemoved });
2012+
nextOfLastItemRemoved = null;
2013+
}
2014+
anyEdits = false;
2015+
}
2016+
20012017
try
20022018
{
20032019
while (!cancellationToken.IsCancellationRequested)
@@ -2014,18 +2030,14 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
20142030
switch (operation.Action)
20152031
{
20162032
case FILE_ACTION_ADDED:
2033+
case FILE_ACTION_RENAMED_NEW_NAME:
20172034
lastItemAdded = await AddFileOrFolderAsync(operation.FileName, returnformat);
20182035
if (lastItemAdded != null)
20192036
{
20202037
anyEdits = true;
20212038
}
20222039
break;
20232040

2024-
case FILE_ACTION_RENAMED_NEW_NAME:
2025-
await AddFileOrFolderAsync(operation.FileName, returnformat);
2026-
anyEdits = true;
2027-
break;
2028-
20292041
case FILE_ACTION_MODIFIED:
20302042
if (!updateQueue.Contains(operation.FileName))
20312043
{
@@ -2036,17 +2048,21 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
20362048
case FILE_ACTION_REMOVED:
20372049
// get the item that immediately follows matching item to be removed
20382050
// if the matching item is the last item, try to get the previous item; otherwise, null
2039-
nextOfLastItemRemoved = filesAndFolders
2040-
.SkipWhile(x => !x.ItemPath.Equals(operation.FileName)).Skip(1)
2041-
.DefaultIfEmpty(filesAndFolders.TakeWhile(x => !x.ItemPath.Equals(operation.FileName)).LastOrDefault())
2042-
.FirstOrDefault();
2043-
await RemoveFileOrFolderAsync(operation.FileName);
2044-
anyEdits = true;
2051+
var itemRemovedIndex = filesAndFolders.FindIndex(x => x.ItemPath.Equals(operation.FileName));
2052+
nextOfLastItemRemoved = filesAndFolders.ElementAtOrDefault(itemRemovedIndex + 1 < filesAndFolders.Count() ? itemRemovedIndex + 1 : itemRemovedIndex - 1);
2053+
var itemRemoved = await RemoveFileOrFolderAsync(operation.FileName);
2054+
if (itemRemoved != null)
2055+
{
2056+
anyEdits = true;
2057+
}
20452058
break;
20462059

20472060
case FILE_ACTION_RENAMED_OLD_NAME:
2048-
await RemoveFileOrFolderAsync(operation.FileName);
2049-
anyEdits = true;
2061+
var itemRenamedOld = await RemoveFileOrFolderAsync(operation.FileName);
2062+
if (itemRenamedOld != null)
2063+
{
2064+
anyEdits = true;
2065+
}
20502066
break;
20512067
}
20522068
}
@@ -2057,19 +2073,7 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
20572073

20582074
if (anyEdits && sampler.CheckNow())
20592075
{
2060-
await OrderFilesAndFoldersAsync();
2061-
await ApplyFilesAndFoldersChangesAsync();
2062-
if (lastItemAdded != null)
2063-
{
2064-
await RequestSelectionAsync(new List<ListedItem>() { lastItemAdded });
2065-
lastItemAdded = null;
2066-
}
2067-
if (nextOfLastItemRemoved != null)
2068-
{
2069-
await RequestSelectionAsync(new List<ListedItem>() { nextOfLastItemRemoved });
2070-
nextOfLastItemRemoved = null;
2071-
}
2072-
anyEdits = false;
2076+
await HandleChangesOccurredAsync();
20732077
}
20742078
}
20752079

@@ -2096,19 +2100,7 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
20962100

20972101
if (anyEdits && sampler.CheckNow())
20982102
{
2099-
await OrderFilesAndFoldersAsync();
2100-
await ApplyFilesAndFoldersChangesAsync();
2101-
if (lastItemAdded != null)
2102-
{
2103-
await RequestSelectionAsync(new List<ListedItem>() { lastItemAdded });
2104-
lastItemAdded = null;
2105-
}
2106-
if (nextOfLastItemRemoved != null)
2107-
{
2108-
await RequestSelectionAsync(new List<ListedItem>() { nextOfLastItemRemoved });
2109-
nextOfLastItemRemoved = null;
2110-
}
2111-
anyEdits = false;
2103+
await HandleChangesOccurredAsync();
21122104
}
21132105
}
21142106
}

0 commit comments

Comments
 (0)