@@ -521,10 +521,8 @@ await dispatcherQueue.EnqueueAsync(() =>
521
521
// get the item that immediately follows matching item to be removed
522
522
// if the matching item is the last item, try to get the previous item; otherwise, null
523
523
// 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 ) ;
528
526
var removedItem = await RemoveFileOrFolderAsync ( itemPath ) ;
529
527
if ( removedItem != null )
530
528
{
@@ -1998,6 +1996,24 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
1998
1996
ListedItem nextOfLastItemRemoved = null ;
1999
1997
var rand = Guid . NewGuid ( ) ;
2000
1998
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
+
2001
2017
try
2002
2018
{
2003
2019
while ( ! cancellationToken . IsCancellationRequested )
@@ -2014,18 +2030,14 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
2014
2030
switch ( operation . Action )
2015
2031
{
2016
2032
case FILE_ACTION_ADDED :
2033
+ case FILE_ACTION_RENAMED_NEW_NAME :
2017
2034
lastItemAdded = await AddFileOrFolderAsync ( operation . FileName , returnformat ) ;
2018
2035
if ( lastItemAdded != null )
2019
2036
{
2020
2037
anyEdits = true ;
2021
2038
}
2022
2039
break ;
2023
2040
2024
- case FILE_ACTION_RENAMED_NEW_NAME :
2025
- await AddFileOrFolderAsync ( operation . FileName , returnformat ) ;
2026
- anyEdits = true ;
2027
- break ;
2028
-
2029
2041
case FILE_ACTION_MODIFIED :
2030
2042
if ( ! updateQueue . Contains ( operation . FileName ) )
2031
2043
{
@@ -2036,17 +2048,21 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
2036
2048
case FILE_ACTION_REMOVED :
2037
2049
// get the item that immediately follows matching item to be removed
2038
2050
// 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
+ }
2045
2058
break ;
2046
2059
2047
2060
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
+ }
2050
2066
break ;
2051
2067
}
2052
2068
}
@@ -2057,19 +2073,7 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
2057
2073
2058
2074
if ( anyEdits && sampler . CheckNow ( ) )
2059
2075
{
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 ( ) ;
2073
2077
}
2074
2078
}
2075
2079
@@ -2096,19 +2100,7 @@ private async Task ProcessOperationQueue(CancellationToken cancellationToken, bo
2096
2100
2097
2101
if ( anyEdits && sampler . CheckNow ( ) )
2098
2102
{
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 ( ) ;
2112
2104
}
2113
2105
}
2114
2106
}
0 commit comments