@@ -126,9 +126,26 @@ public partial class MobileFileManager : ComponentBase
126126 private bool SortAscending { get ; set ; } = true ;
127127
128128 private List < FileManagerDirectoryContent > Files { get ; set ; } = new ( ) ;
129- private List < FileManagerDirectoryContent > DisplayFiles => GetDisplayFiles ( ) ;
129+
130+ // Cached display files - invalidated when data/filters change
131+ private List < FileManagerDirectoryContent > ? _cachedDisplayFiles ;
132+ private bool _displayFilesDirty = true ;
133+ private List < FileManagerDirectoryContent > DisplayFiles
134+ {
135+ get
136+ {
137+ if ( _displayFilesDirty || _cachedDisplayFiles == null )
138+ {
139+ _cachedDisplayFiles = GetDisplayFiles ( ) ;
140+ _displayFilesDirty = false ;
141+ }
142+ return _cachedDisplayFiles ;
143+ }
144+ }
145+
130146 private List < FileManagerDirectoryContent > PagedFiles => GetPagedFiles ( ) ;
131147 private List < FileManagerDirectoryContent > SelectedItems { get ; set ; } = new ( ) ;
148+ private HashSet < string > _selectedIds = new ( ) ; // O(1) lookup for selection state
132149 private List < FileManagerDirectoryContent > ClipboardItems { get ; set ; } = new ( ) ;
133150 private bool IsCutOperation { get ; set ; } = false ;
134151
@@ -223,6 +240,7 @@ private async Task LoadFiles()
223240 f . FilterPath = NormalizePath ( f . FilterPath ) ;
224241 return f ;
225242 } ) . ToList ( ) ;
243+ InvalidateDisplayFilesCache ( ) ;
226244 }
227245
228246 if ( args . Response ? . CWD != null )
@@ -246,6 +264,16 @@ public async Task RefreshFilesAsync()
246264 await LoadFiles ( ) ;
247265 }
248266
267+ private void InvalidateDisplayFilesCache ( )
268+ {
269+ _displayFilesDirty = true ;
270+ }
271+
272+ private bool IsItemSelected ( FileManagerDirectoryContent file )
273+ {
274+ return _selectedIds . Contains ( file . Id ) ;
275+ }
276+
249277 private List < FileManagerDirectoryContent > GetDisplayFiles ( )
250278 {
251279 var files = Files ?? new List < FileManagerDirectoryContent > ( ) ;
@@ -451,31 +479,35 @@ private void OnFileLongPress(FileManagerDirectoryContent file)
451479
452480 private void ToggleSelection ( FileManagerDirectoryContent file )
453481 {
454- if ( SelectedItems . Contains ( file ) )
482+ if ( _selectedIds . Contains ( file . Id ) )
455483 {
456484 SelectedItems . Remove ( file ) ;
485+ _selectedIds . Remove ( file . Id ) ;
457486 }
458487 else
459488 {
460489 SelectedItems . Add ( file ) ;
490+ _selectedIds . Add ( file . Id ) ;
461491 }
462492
463- OnSelectedItemsChanged . InvokeAsync ( SelectedItems . Select ( f => f . Id ) . ToArray ( ) ) ;
493+ OnSelectedItemsChanged . InvokeAsync ( _selectedIds . ToArray ( ) ) ;
464494 StateHasChanged ( ) ;
465495 }
466496
467497 private void ClearSelection ( )
468498 {
469499 SelectedItems . Clear ( ) ;
500+ _selectedIds . Clear ( ) ;
470501 OnSelectedItemsChanged . InvokeAsync ( Array . Empty < string > ( ) ) ;
471502 StateHasChanged ( ) ;
472503 }
473504
474505 private void SelectAll ( )
475506 {
476507 SelectedItems = new List < FileManagerDirectoryContent > ( DisplayFiles ) ;
508+ _selectedIds = new HashSet < string > ( SelectedItems . Select ( f => f . Id ) ) ;
477509 ShowMoreMenu = false ;
478- OnSelectedItemsChanged . InvokeAsync ( SelectedItems . Select ( f => f . Id ) . ToArray ( ) ) ;
510+ OnSelectedItemsChanged . InvokeAsync ( _selectedIds . ToArray ( ) ) ;
479511 StateHasChanged ( ) ;
480512 }
481513
@@ -969,6 +1001,7 @@ await InvokeAsync(async () =>
9691001 f . FilterPath = NormalizePath ( f . FilterPath ) ;
9701002 return f ;
9711003 } ) . ToList ( ) ;
1004+ InvalidateDisplayFilesCache ( ) ;
9721005 }
9731006 }
9741007 else
@@ -1032,13 +1065,15 @@ private void SortFiles()
10321065 "Type" => "Name" ,
10331066 _ => "Name"
10341067 } ;
1068+ InvalidateDisplayFilesCache ( ) ;
10351069 ResetPagination ( ) ;
10361070 StateHasChanged ( ) ;
10371071 }
10381072
10391073 private void ToggleSortDirection ( )
10401074 {
10411075 SortAscending = ! SortAscending ;
1076+ InvalidateDisplayFilesCache ( ) ;
10421077 ResetPagination ( ) ;
10431078 StateHasChanged ( ) ;
10441079 }
@@ -1088,20 +1123,23 @@ private void ToggleTypeFilter(string type)
10881123 {
10891124 SelectedTypeFilters . Add ( type ) ;
10901125 }
1126+ InvalidateDisplayFilesCache ( ) ;
10911127 ResetPagination ( ) ;
10921128 StateHasChanged ( ) ;
10931129 }
10941130
10951131 private void SelectAllTypeFilters ( )
10961132 {
10971133 SelectedTypeFilters = new HashSet < string > ( AvailableFileTypes ) ;
1134+ InvalidateDisplayFilesCache ( ) ;
10981135 ResetPagination ( ) ;
10991136 StateHasChanged ( ) ;
11001137 }
11011138
11021139 private void ClearTypeFilters ( )
11031140 {
11041141 SelectedTypeFilters . Clear ( ) ;
1142+ InvalidateDisplayFilesCache ( ) ;
11051143 ResetPagination ( ) ;
11061144 StateHasChanged ( ) ;
11071145 }
0 commit comments