Skip to content

Commit 5a38703

Browse files
authored
Fixed a couple issues in ItemViewModel (#2189)
1 parent 1a187b1 commit 5a38703

File tree

3 files changed

+97
-90
lines changed

3 files changed

+97
-90
lines changed

Files/App.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private async void Connection_RequestReceived(AppServiceConnection sender, AppSe
145145
var changeType = (string)args.Request.Message["Type"];
146146
var newItem = JsonConvert.DeserializeObject<ShellFileItem>(args.Request.Message.Get("Item", ""));
147147
Debug.WriteLine("{0}: {1}", folderPath, changeType);
148-
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
148+
await CoreApplication.MainView.ExecuteOnUIThreadAsync(async () =>
149149
{
150150
// If we are currently displaying the reycle bin lets refresh the items
151151
if (CurrentInstance.FilesystemViewModel?.CurrentFolder?.ItemPath == folderPath)
@@ -157,7 +157,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
157157
break;
158158

159159
case "Deleted":
160-
CurrentInstance.FilesystemViewModel.RemoveFileOrFolder(itemPath);
160+
await CurrentInstance.FilesystemViewModel.RemoveFileOrFolder(itemPath);
161161
break;
162162

163163
default:

Files/Commands/Delete.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private static async Task DeleteItem(StorageDeleteOption deleteOption, IShellPag
197197
await (await ItemViewModel.GetFileFromPathAsync(iFilePath)).DeleteAsync(StorageDeleteOption.PermanentDelete);
198198
}
199199

200-
AppInstance.FilesystemViewModel.RemoveFileOrFolder(storItem);
200+
await AppInstance.FilesystemViewModel.RemoveFileOrFolder(storItem);
201201
itemsDeleted++;
202202
}
203203
}

Files/View Models/ItemViewModel.cs

Lines changed: 94 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Files.View_Models;
77
using Files.Views;
88
using Microsoft.Toolkit.Uwp.Extensions;
9+
using Microsoft.Toolkit.Uwp.Helpers;
910
using Microsoft.Toolkit.Uwp.UI;
1011
using System;
1112
using System.Collections.Generic;
@@ -610,7 +611,7 @@ public async void RapidAddItemsToCollectionAsync(string path)
610611
// simply drop this instance
611612
await semaphoreSlim.WaitAsync(_semaphoreCTS.Token);
612613
}
613-
catch (OperationCanceledException)
614+
catch (Exception ex) when (ex is OperationCanceledException || ex is ObjectDisposedException)
614615
{
615616
return;
616617
}
@@ -877,19 +878,19 @@ await DialogDisplayHelper.ShowDialog(
877878
{
878879
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
879880
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
880-
881881
IntPtr hFile = FindFirstFileExFromApp(path + "\\*.*", findInfoLevel, out WIN32_FIND_DATA findData, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
882882
additionalFlags);
883-
FileTimeToSystemTime(ref findData.ftLastWriteTime, out SYSTEMTIME systemTimeOutput);
884-
var itemDate = new DateTime(
885-
systemTimeOutput.Year,
886-
systemTimeOutput.Month,
887-
systemTimeOutput.Day,
888-
systemTimeOutput.Hour,
889-
systemTimeOutput.Minute,
890-
systemTimeOutput.Second,
891-
systemTimeOutput.Milliseconds,
892-
DateTimeKind.Utc);
883+
884+
DateTime itemDate = DateTime.UtcNow;
885+
try
886+
{
887+
FileTimeToSystemTime(ref findData.ftLastWriteTime, out SYSTEMTIME systemTimeOutput);
888+
itemDate = new DateTime(
889+
systemTimeOutput.Year, systemTimeOutput.Month, systemTimeOutput.Day,
890+
systemTimeOutput.Hour, systemTimeOutput.Minute, systemTimeOutput.Second, systemTimeOutput.Milliseconds,
891+
DateTimeKind.Utc);
892+
}
893+
catch (ArgumentException) { }
893894

894895
CurrentFolder = new ListedItem(_rootFolder.FolderRelativeId, returnformat)
895896
{
@@ -1109,27 +1110,27 @@ private void WatchForDirectoryChanges(string path)
11091110
{
11101111
case FILE_ACTION_ADDED:
11111112
Debug.WriteLine("File " + FileName + " added to working directory.");
1112-
AddFileOrFolder(FileName, returnformat);
1113+
AddFileOrFolder(FileName, returnformat).GetAwaiter().GetResult();
11131114
break;
11141115

11151116
case FILE_ACTION_REMOVED:
11161117
Debug.WriteLine("File " + FileName + " removed from working directory.");
1117-
RemoveFileOrFolder(FileName);
1118+
RemoveFileOrFolder(FileName).GetAwaiter().GetResult();
11181119
break;
11191120

11201121
case FILE_ACTION_MODIFIED:
11211122
Debug.WriteLine("File " + FileName + " had attributes modified in the working directory.");
1122-
UpdateFileOrFolder(FilesAndFolders.ToList().First(x => x.ItemPath.Equals(FileName)));
1123+
UpdateFileOrFolder(FileName).GetAwaiter().GetResult();
11231124
break;
11241125

11251126
case FILE_ACTION_RENAMED_OLD_NAME:
11261127
Debug.WriteLine("File " + FileName + " will be renamed in the working directory.");
1127-
RemoveFileOrFolder(FileName);
1128+
RemoveFileOrFolder(FileName).GetAwaiter().GetResult();
11281129
break;
11291130

11301131
case FILE_ACTION_RENAMED_NEW_NAME:
11311132
Debug.WriteLine("File " + FileName + " was renamed in the working directory.");
1132-
AddFileOrFolder(FileName, returnformat);
1133+
AddFileOrFolder(FileName, returnformat).GetAwaiter().GetResult();
11331134
break;
11341135

11351136
default:
@@ -1233,13 +1234,13 @@ public void AddFileOrFolderFromShellFile(ShellFileItem item, string dateReturnFo
12331234
UpdateDirectoryInfo();
12341235
}
12351236

1236-
public void AddFileOrFolder(ListedItem item)
1237+
private void AddFileOrFolder(ListedItem item)
12371238
{
12381239
_filesAndFolders.Add(item);
12391240
IsFolderEmptyTextDisplayed = false;
12401241
}
12411242

1242-
private void AddFileOrFolder2(string fileOrFolderPath, string dateReturnFormat)
1243+
private async Task AddFileOrFolder(string fileOrFolderPath, string dateReturnFormat)
12431244
{
12441245
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
12451246
int additionalFlags = FIND_FIRST_EX_CASE_SENSITIVE;
@@ -1248,32 +1249,18 @@ private void AddFileOrFolder2(string fileOrFolderPath, string dateReturnFormat)
12481249
additionalFlags);
12491250
FindClose(hFile);
12501251

1251-
if ((findData.dwFileAttributes & 0x10) > 0) // FILE_ATTRIBUTE_DIRECTORY
1252-
{
1253-
AddFolder(findData, Directory.GetParent(fileOrFolderPath).FullName, dateReturnFormat);
1254-
}
1255-
else
1252+
await CoreApplication.MainView.ExecuteOnUIThreadAsync(() =>
12561253
{
1257-
AddFile(findData, Directory.GetParent(fileOrFolderPath).FullName, dateReturnFormat);
1258-
}
1259-
}
1260-
1261-
private async void AddFileOrFolder(string path, string dateReturnFormat)
1262-
{
1263-
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
1264-
() =>
1254+
if ((findData.dwFileAttributes & 0x10) > 0) // FILE_ATTRIBUTE_DIRECTORY
12651255
{
1266-
try
1267-
{
1268-
AddFileOrFolder2(path, dateReturnFormat);
1269-
}
1270-
catch (Exception)
1271-
{
1272-
// Ignore this..
1273-
}
1274-
1275-
UpdateDirectoryInfo();
1276-
});
1256+
AddFolder(findData, Directory.GetParent(fileOrFolderPath).FullName, dateReturnFormat);
1257+
}
1258+
else
1259+
{
1260+
AddFile(findData, Directory.GetParent(fileOrFolderPath).FullName, dateReturnFormat);
1261+
}
1262+
UpdateDirectoryInfo();
1263+
});
12771264
}
12781265

12791266
private void UpdateDirectoryInfo()
@@ -1291,7 +1278,7 @@ private void UpdateDirectoryInfo()
12911278
}
12921279
}
12931280

1294-
public async void UpdateFileOrFolder(ListedItem item)
1281+
private async Task UpdateFileOrFolder(ListedItem item)
12951282
{
12961283
IStorageItem storageItem = null;
12971284
if (item.PrimaryItemAttribute == StorageItemTypes.File)
@@ -1305,36 +1292,43 @@ public async void UpdateFileOrFolder(ListedItem item)
13051292
if (storageItem != null)
13061293
{
13071294
var syncStatus = await CheckCloudDriveSyncStatus(storageItem);
1308-
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
1309-
() =>
1310-
{
1311-
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
1312-
});
1295+
await CoreApplication.MainView.ExecuteOnUIThreadAsync(() =>
1296+
{
1297+
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
1298+
});
13131299
}
13141300
}
13151301

1316-
public async void RemoveFileOrFolder(ListedItem item)
1302+
private async Task UpdateFileOrFolder(string path)
13171303
{
1318-
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
1319-
() =>
1304+
var matchingItem = FilesAndFolders.ToList().FirstOrDefault(x => x.ItemPath.Equals(path));
1305+
if (matchingItem != null)
1306+
{
1307+
await UpdateFileOrFolder(matchingItem);
1308+
}
1309+
}
1310+
1311+
public async Task RemoveFileOrFolder(ListedItem item)
1312+
{
1313+
await CoreApplication.MainView.ExecuteOnUIThreadAsync(() =>
1314+
{
1315+
_filesAndFolders.Remove(item);
1316+
if (_filesAndFolders.Count == 0)
13201317
{
1321-
_filesAndFolders.Remove(item);
1322-
if (_filesAndFolders.Count == 0)
1323-
{
1324-
IsFolderEmptyTextDisplayed = true;
1325-
}
1326-
App.JumpList.RemoveFolder(item.ItemPath);
1318+
IsFolderEmptyTextDisplayed = true;
1319+
}
1320+
App.JumpList.RemoveFolder(item.ItemPath);
13271321

1328-
UpdateDirectoryInfo();
1329-
});
1322+
UpdateDirectoryInfo();
1323+
});
13301324
}
13311325

1332-
public void RemoveFileOrFolder(string path)
1326+
public async Task RemoveFileOrFolder(string path)
13331327
{
13341328
var matchingItem = FilesAndFolders.ToList().FirstOrDefault(x => x.ItemPath.Equals(path));
13351329
if (matchingItem != null)
13361330
{
1337-
RemoveFileOrFolder(matchingItem);
1331+
await RemoveFileOrFolder(matchingItem);
13381332
}
13391333
}
13401334

@@ -1346,16 +1340,20 @@ private void AddFolder(WIN32_FIND_DATA findData, string pathRoot, string dateRet
13461340
return;
13471341
}
13481342

1349-
FileTimeToSystemTime(ref findData.ftLastWriteTime, out SYSTEMTIME systemTimeOutput);
1350-
var itemDate = new DateTime(
1351-
systemTimeOutput.Year,
1352-
systemTimeOutput.Month,
1353-
systemTimeOutput.Day,
1354-
systemTimeOutput.Hour,
1355-
systemTimeOutput.Minute,
1356-
systemTimeOutput.Second,
1357-
systemTimeOutput.Milliseconds,
1358-
DateTimeKind.Utc);
1343+
DateTime itemDate;
1344+
try
1345+
{
1346+
FileTimeToSystemTime(ref findData.ftLastWriteTime, out SYSTEMTIME systemTimeOutput);
1347+
itemDate = new DateTime(
1348+
systemTimeOutput.Year, systemTimeOutput.Month, systemTimeOutput.Day,
1349+
systemTimeOutput.Hour, systemTimeOutput.Minute, systemTimeOutput.Second, systemTimeOutput.Milliseconds,
1350+
DateTimeKind.Utc);
1351+
}
1352+
catch (ArgumentException)
1353+
{
1354+
// Invalid date means invalid findData, do not add to list
1355+
return;
1356+
}
13591357
var itemPath = Path.Combine(pathRoot, findData.cFileName);
13601358

13611359
_filesAndFolders.Add(new ListedItem(null, dateReturnFormat)
@@ -1399,23 +1397,32 @@ private void AddFile(WIN32_FIND_DATA findData, string pathRoot, string dateRetur
13991397
}
14001398
}
14011399

1402-
FileTimeToSystemTime(ref findData.ftLastWriteTime, out SYSTEMTIME systemModifiedDateOutput);
1403-
var itemModifiedDate = new DateTime(
1404-
systemModifiedDateOutput.Year, systemModifiedDateOutput.Month, systemModifiedDateOutput.Day,
1405-
systemModifiedDateOutput.Hour, systemModifiedDateOutput.Minute, systemModifiedDateOutput.Second, systemModifiedDateOutput.Milliseconds,
1406-
DateTimeKind.Utc);
1400+
DateTime itemModifiedDate, itemCreatedDate, itemLastAccessDate;
1401+
try
1402+
{
1403+
FileTimeToSystemTime(ref findData.ftLastWriteTime, out SYSTEMTIME systemModifiedDateOutput);
1404+
itemModifiedDate = new DateTime(
1405+
systemModifiedDateOutput.Year, systemModifiedDateOutput.Month, systemModifiedDateOutput.Day,
1406+
systemModifiedDateOutput.Hour, systemModifiedDateOutput.Minute, systemModifiedDateOutput.Second, systemModifiedDateOutput.Milliseconds,
1407+
DateTimeKind.Utc);
14071408

1408-
FileTimeToSystemTime(ref findData.ftCreationTime, out SYSTEMTIME systemCreatedDateOutput);
1409-
var itemCreatedDate = new DateTime(
1410-
systemCreatedDateOutput.Year, systemCreatedDateOutput.Month, systemCreatedDateOutput.Day,
1411-
systemCreatedDateOutput.Hour, systemCreatedDateOutput.Minute, systemCreatedDateOutput.Second, systemCreatedDateOutput.Milliseconds,
1412-
DateTimeKind.Utc);
1409+
FileTimeToSystemTime(ref findData.ftCreationTime, out SYSTEMTIME systemCreatedDateOutput);
1410+
itemCreatedDate = new DateTime(
1411+
systemCreatedDateOutput.Year, systemCreatedDateOutput.Month, systemCreatedDateOutput.Day,
1412+
systemCreatedDateOutput.Hour, systemCreatedDateOutput.Minute, systemCreatedDateOutput.Second, systemCreatedDateOutput.Milliseconds,
1413+
DateTimeKind.Utc);
14131414

1414-
FileTimeToSystemTime(ref findData.ftLastAccessTime, out SYSTEMTIME systemLastAccessOutput);
1415-
var itemLastAccessDate = new DateTime(
1416-
systemLastAccessOutput.Year, systemLastAccessOutput.Month, systemLastAccessOutput.Day,
1417-
systemLastAccessOutput.Hour, systemLastAccessOutput.Minute, systemLastAccessOutput.Second, systemLastAccessOutput.Milliseconds,
1418-
DateTimeKind.Utc);
1415+
FileTimeToSystemTime(ref findData.ftLastAccessTime, out SYSTEMTIME systemLastAccessOutput);
1416+
itemLastAccessDate = new DateTime(
1417+
systemLastAccessOutput.Year, systemLastAccessOutput.Month, systemLastAccessOutput.Day,
1418+
systemLastAccessOutput.Hour, systemLastAccessOutput.Minute, systemLastAccessOutput.Second, systemLastAccessOutput.Milliseconds,
1419+
DateTimeKind.Utc);
1420+
}
1421+
catch (ArgumentException)
1422+
{
1423+
// Invalid date means invalid findData, do not add to list
1424+
return;
1425+
}
14191426

14201427
long itemSizeBytes = findData.GetSize();
14211428
var itemSize = ByteSize.FromBytes(itemSizeBytes).ToBinaryString().ConvertSizeAbbreviation();

0 commit comments

Comments
 (0)