|
14 | 14 | using Windows.System; |
15 | 15 | using Windows.UI.Xaml.Navigation; |
16 | 16 | using System.ComponentModel; |
| 17 | +using System.Collections.Generic; |
17 | 18 |
|
18 | 19 | namespace Files |
19 | 20 | { |
@@ -199,114 +200,170 @@ public async void PopulateRecentsList() |
199 | 200 | dataFolder = Windows.Storage.ApplicationData.Current.LocalCacheFolder; |
200 | 201 | RecentsFile = await dataFolder.CreateFileAsync("recents.txt", CreationCollisionOption.OpenIfExists); |
201 | 202 | BitmapImage ItemImage = new BitmapImage(); |
202 | | - string ItemPath; |
| 203 | + string ItemPath = null; |
203 | 204 | string ItemName; |
204 | 205 | Visibility ItemFolderImgVis; |
205 | 206 | Visibility ItemEmptyImgVis; |
206 | 207 | Visibility ItemFileIconVis; |
207 | | - var lines = await FileIO.ReadLinesAsync(RecentsFile); |
| 208 | + IList<string> lines = new List<string>(); |
| 209 | + lines = await FileIO.ReadLinesAsync(RecentsFile); |
208 | 210 | if (lines.Count == 0) |
209 | 211 | { |
210 | 212 | Empty.Visibility = Visibility.Visible; |
211 | 213 | } |
212 | 214 | else if (lines.Count > 10) |
213 | 215 | { |
214 | | - for (int LineNum = 0; LineNum < 10; LineNum++) |
| 216 | + try |
215 | 217 | { |
216 | | - lines.RemoveAt(0); |
217 | | - } |
| 218 | + for (int LineNum = 0; LineNum < 10; LineNum++) |
| 219 | + { |
| 220 | + lines.RemoveAt(0); |
| 221 | + } |
218 | 222 |
|
219 | | - await FileIO.WriteLinesAsync(RecentsFile, lines); |
220 | | - Empty.Visibility = Visibility.Collapsed; |
221 | | - lines = await FileIO.ReadLinesAsync(RecentsFile); |
222 | | - foreach (string s in lines) |
223 | | - { |
224 | | - try |
| 223 | + await FileIO.WriteLinesAsync(RecentsFile, lines); |
| 224 | + Empty.Visibility = Visibility.Collapsed; |
| 225 | + lines = await FileIO.ReadLinesAsync(RecentsFile); |
| 226 | + foreach (string s in lines) |
225 | 227 | { |
226 | | - var item = await StorageFolder.GetFolderFromPathAsync(s); |
227 | | - ItemName = item.DisplayName; |
228 | | - ItemPath = item.Path; |
229 | | - ItemFolderImgVis = Visibility.Visible; |
230 | | - ItemEmptyImgVis = Visibility.Collapsed; |
231 | | - ItemFileIconVis = Visibility.Collapsed; |
232 | | - if(!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis })) |
| 228 | + try |
233 | 229 | { |
234 | | - recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); |
235 | | - } |
| 230 | + var item = await StorageFolder.GetFolderFromPathAsync(s); |
| 231 | + ItemName = item.DisplayName; |
| 232 | + ItemPath = item.Path; |
| 233 | + ItemFolderImgVis = Visibility.Visible; |
| 234 | + ItemEmptyImgVis = Visibility.Collapsed; |
| 235 | + ItemFileIconVis = Visibility.Collapsed; |
| 236 | + if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis })) |
| 237 | + { |
| 238 | + recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); |
| 239 | + } |
236 | 240 |
|
237 | | - } |
238 | | - catch (System.ArgumentException) |
239 | | - { |
240 | | - var item = await StorageFile.GetFileFromPathAsync(s); |
241 | | - ItemName = item.DisplayName; |
242 | | - ItemPath = item.Path; |
243 | | - ItemImage = new BitmapImage(); |
244 | | - var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail); |
245 | | - if (thumbnail == null) |
| 241 | + } |
| 242 | + catch (System.IO.FileNotFoundException) |
246 | 243 | { |
247 | | - ItemEmptyImgVis = Visibility.Visible; |
| 244 | + IList<string> modifyLines = new List<string>(); |
| 245 | + modifyLines = lines; |
| 246 | + modifyLines.Remove(s); |
| 247 | + await FileIO.WriteLinesAsync(RecentsFile, modifyLines); |
| 248 | + PopulateRecentsList(); |
248 | 249 | } |
249 | | - else |
| 250 | + catch (UnauthorizedAccessException) |
250 | 251 | { |
251 | | - await ItemImage.SetSourceAsync(thumbnail.CloneStream()); |
252 | | - ItemEmptyImgVis = Visibility.Collapsed; |
| 252 | + Empty.Visibility = Visibility.Visible; |
253 | 253 | } |
254 | | - ItemFolderImgVis = Visibility.Collapsed; |
255 | | - ItemFileIconVis = Visibility.Visible; |
256 | | - if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis })) |
| 254 | + catch (System.ArgumentException) |
257 | 255 | { |
258 | | - recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); |
| 256 | + var item = await StorageFile.GetFileFromPathAsync(s); |
| 257 | + ItemName = item.DisplayName; |
| 258 | + ItemPath = item.Path; |
| 259 | + ItemImage = new BitmapImage(); |
| 260 | + var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail); |
| 261 | + if (thumbnail == null) |
| 262 | + { |
| 263 | + ItemEmptyImgVis = Visibility.Visible; |
| 264 | + } |
| 265 | + else |
| 266 | + { |
| 267 | + await ItemImage.SetSourceAsync(thumbnail.CloneStream()); |
| 268 | + ItemEmptyImgVis = Visibility.Collapsed; |
| 269 | + } |
| 270 | + ItemFolderImgVis = Visibility.Collapsed; |
| 271 | + ItemFileIconVis = Visibility.Visible; |
| 272 | + if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis })) |
| 273 | + { |
| 274 | + recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); |
| 275 | + } |
259 | 276 | } |
260 | 277 | } |
261 | 278 | } |
| 279 | + catch (System.IO.FileNotFoundException) |
| 280 | + { |
| 281 | + if (ItemPath != null) |
| 282 | + { |
| 283 | + RemoveDeletedItemFromList(ItemPath, lines); |
| 284 | + } |
| 285 | + else |
| 286 | + { |
| 287 | + Debug.WriteLine("Attempted to delete redundant RecentItem from file when ItemPath was never set."); |
| 288 | + } |
| 289 | + } |
262 | 290 | } |
263 | 291 | else |
264 | 292 | { |
265 | | - Empty.Visibility = Visibility.Collapsed; |
266 | | - |
267 | | - foreach (string s in lines) |
| 293 | + try |
268 | 294 | { |
269 | | - try |
270 | | - { |
271 | | - var item = await StorageFolder.GetFolderFromPathAsync(s); |
272 | | - ItemName = item.DisplayName; |
273 | | - ItemPath = item.Path; |
274 | | - ItemFolderImgVis = Visibility.Visible; |
275 | | - ItemEmptyImgVis = Visibility.Collapsed; |
276 | | - ItemFileIconVis = Visibility.Collapsed; |
277 | | - if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis })) |
278 | | - { |
279 | | - recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); |
280 | | - } |
| 295 | + Empty.Visibility = Visibility.Collapsed; |
281 | 296 |
|
282 | | - } |
283 | | - catch (System.ArgumentException) |
| 297 | + foreach (string s in lines) |
284 | 298 | { |
285 | | - var item = await StorageFile.GetFileFromPathAsync(s); |
286 | | - ItemName = item.DisplayName; |
287 | | - ItemPath = item.Path; |
288 | | - ItemImage = new BitmapImage(); |
289 | | - var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail); |
290 | | - if (thumbnail == null) |
| 299 | + try |
291 | 300 | { |
292 | | - ItemEmptyImgVis = Visibility.Visible; |
| 301 | + ItemPath = s; |
| 302 | + var item = await StorageFolder.GetFolderFromPathAsync(s); |
| 303 | + ItemName = item.DisplayName; |
| 304 | + ItemPath = item.Path; |
| 305 | + ItemFolderImgVis = Visibility.Visible; |
| 306 | + ItemEmptyImgVis = Visibility.Collapsed; |
| 307 | + ItemFileIconVis = Visibility.Collapsed; |
| 308 | + if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis })) |
| 309 | + { |
| 310 | + recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); |
| 311 | + } |
| 312 | + |
293 | 313 | } |
294 | | - else |
| 314 | + catch (UnauthorizedAccessException) |
295 | 315 | { |
296 | | - await ItemImage.SetSourceAsync(thumbnail.CloneStream()); |
297 | | - ItemEmptyImgVis = Visibility.Collapsed; |
| 316 | + Empty.Visibility = Visibility.Visible; |
298 | 317 | } |
299 | | - ItemFolderImgVis = Visibility.Collapsed; |
300 | | - ItemFileIconVis = Visibility.Visible; |
301 | | - if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis })) |
| 318 | + catch (System.ArgumentException) |
302 | 319 | { |
303 | | - recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); |
| 320 | + var item = await StorageFile.GetFileFromPathAsync(s); |
| 321 | + ItemName = item.DisplayName; |
| 322 | + ItemPath = item.Path; |
| 323 | + ItemImage = new BitmapImage(); |
| 324 | + var thumbnail = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail); |
| 325 | + if (thumbnail == null) |
| 326 | + { |
| 327 | + ItemEmptyImgVis = Visibility.Visible; |
| 328 | + } |
| 329 | + else |
| 330 | + { |
| 331 | + await ItemImage.SetSourceAsync(thumbnail.CloneStream()); |
| 332 | + ItemEmptyImgVis = Visibility.Collapsed; |
| 333 | + } |
| 334 | + ItemFolderImgVis = Visibility.Collapsed; |
| 335 | + ItemFileIconVis = Visibility.Visible; |
| 336 | + if (!recentItemsCollection.Contains(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis })) |
| 337 | + { |
| 338 | + recentItemsCollection.Add(new RecentItem() { path = ItemPath, name = ItemName, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis }); |
| 339 | + } |
304 | 340 | } |
305 | 341 | } |
306 | 342 | } |
| 343 | + catch (System.IO.FileNotFoundException) |
| 344 | + { |
| 345 | + if(ItemPath != null) |
| 346 | + { |
| 347 | + RemoveDeletedItemFromList(ItemPath, lines); |
| 348 | + } |
| 349 | + else |
| 350 | + { |
| 351 | + Debug.WriteLine("Attempted to delete redundant RecentItem from file when ItemPath was never set."); |
| 352 | + } |
| 353 | + } |
| 354 | + |
307 | 355 | } |
308 | 356 | } |
309 | 357 |
|
| 358 | + private async void RemoveDeletedItemFromList(string s, IList<string> lines) |
| 359 | + { |
| 360 | + IList<string> modifyLines = new List<string>(); |
| 361 | + modifyLines = lines; |
| 362 | + modifyLines.Remove(s); |
| 363 | + await FileIO.WriteLinesAsync(RecentsFile, modifyLines); |
| 364 | + PopulateRecentsList(); |
| 365 | + } |
| 366 | + |
310 | 367 | private async void RecentsView_ItemClick(object sender, ItemClickEventArgs e) |
311 | 368 | { |
312 | 369 | var path = (e.ClickedItem as RecentItem).path; |
|
0 commit comments