public static ActionsFromStrings ( IList |
||
strings | IList |
|
리턴 | List |
public async Task SetActionsAsDelivered(IList <HistoryAction> sendActions) { StorageFolder folder = await GetFolder(BackgroundActionsFolder, true); StorageFile deliveredActionsFile = await folder.CreateFileAsync(ActionsFileName, CreationCollisionOption.OpenIfExists); IReadOnlyList <StorageFolder> folders = await folder.GetFoldersAsync(); foreach (StorageFolder storageFolder in folders) { IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync(); //ignore unlocked folders if (files.FirstOrDefault(f => f.Name == FolderLockFile) == null) { continue; } StorageFile actionFile = files.FirstOrDefault(f => f.Name == ActionsFileName); if (actionFile != null) { List <HistoryAction> actions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(actionFile)); List <string> stringActions = new List <string>(); foreach (HistoryAction historyAction in actions) { historyAction.Delivered = true; stringActions.Add(FileStorageHelper.ActionToString(historyAction)); } await FileIO.AppendLinesAsync(deliveredActionsFile, stringActions); } await storageFolder.DeleteAsync(); } if (ForegroundHistoryActionWriter != null) { await ForegroundHistoryActionWriter.RewriteFile((l, l2) => { foreach (string s in l) { HistoryAction action = FileStorageHelper.ActionFromString(s); if (!sendActions.Contains(action)) { l2.Add(s); } } }); } }
private async Task <IList <HistoryAction> > GetActions(bool lockFolder) { IList <HistoryAction> actions = new List <HistoryAction>(); StorageFolder folder = await GetFolder(BackgroundActionsFolder); if (lockFolder) { await CreateEventMarker(folder); } StorageFolder parentFolder = await folder.GetParentAsync(); IReadOnlyList <StorageFolder> folders = await parentFolder.GetFoldersAsync(); foreach (StorageFolder storageFolder in folders) { try { IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync(); StorageFile first = null; foreach (var f in files) { if (f.Name == ActionsFileName) { first = f; break; } } if (first != null) { List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(first)); if (fileActions != null) { foreach (HistoryAction historyAction in fileActions) { actions.Add(historyAction); } } } } catch (SEHException) { } catch (FileNotFoundException) { } } if (ForegroundHistoryActionWriter != null) { List <HistoryAction> foreGroundfileActions = FileStorageHelper.ActionsFromStrings(await ForegroundHistoryActionWriter.ReadLines()); if (foreGroundfileActions != null) { foreach (HistoryAction historyAction in foreGroundfileActions) { actions.Add(historyAction); } } } return(actions); }
public async Task <List <HistoryAction> > GetActionsForForeground(bool doNotDelete = false) { List <HistoryAction> actions = new List <HistoryAction>(); try { StorageFolder folder = await GetFolder(BackgroundActionsFolder, true); StorageFile deliveredActionsFile = await folder.CreateFileAsync(ActionsFileName, CreationCollisionOption.OpenIfExists); List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(deliveredActionsFile)); if (fileActions != null) { foreach (HistoryAction historyAction in fileActions) { if (historyAction.Background) { actions.Add(historyAction); } } if (!doNotDelete && fileActions.Count != 0) { StringBuilder sb = new StringBuilder(); foreach (HistoryAction historyAction in fileActions) { historyAction.Background = false; sb.Append(FileStorageHelper.ActionToString(historyAction)); sb.Append('\n'); } if (!await RetryWriting(deliveredActionsFile, sb.ToString())) { Logger.Error("GetActionsForForeground#1: Writing failed "); } } } IReadOnlyList <StorageFolder> folders = await folder.GetFoldersAsync(); foreach (StorageFolder storageFolder in folders) { try { IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync(); StorageFile first = null; foreach (var f in files) { if (f.Name == ActionsFileName) { first = f; break; } } if (first != null) { fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(first)); if (fileActions != null && fileActions.Count != 0) { foreach (HistoryAction historyAction in fileActions) { if (historyAction.Background) { actions.Add(historyAction); } } if (!doNotDelete) { StringBuilder sb = new StringBuilder(); foreach (HistoryAction historyAction in fileActions) { historyAction.Background = false; sb.Append(FileStorageHelper.ActionToString(historyAction)); sb.Append('\n'); } if (!await RetryWriting(first, sb.ToString())) { Logger.Error("GetActionsForForeground#2: Writing failed "); } } } } } catch (UnauthorizedAccessException) { } catch (SEHException) { } catch (FileNotFoundException) { } } } catch (UnauthorizedAccessException) { } catch (SEHException) { } catch (FileNotFoundException) { } return(actions); }
public async Task CleanupDatabase() { string minDateTime = DateTime.Now.AddDays(-1).ToString(History.Timeformat); { StorageFolder folder = await GetFolder(BackgroundActionsFolder, true); IReadOnlyList <IStorageItem> folders = await folder.GetItemsAsync(); foreach (IStorageItem storageItem in folders) { try { if (storageItem.IsOfType(StorageItemTypes.Folder)) { StorageFolder storageFolder = (StorageFolder)storageItem; IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync(); foreach (var f in files) { if (f.Name == ActionsFileName) { List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(f)); if (fileActions.All(a => a.Delivered && a.ActionTime.CompareTo(minDateTime) < 0)) { await f.DeleteAsync(); break; } } } if ((await storageFolder.GetFilesAsync()).Count == 0) { await storageFolder.DeleteAsync(); } } else { if (storageItem.Name == ActionsFileName) { StorageFile storageFile = (StorageFile)storageItem; List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(storageFile)); if (fileActions.RemoveAll(a => a.Delivered && a.ActionTime.CompareTo(minDateTime) < 0) > 0) { await RetryWriting(storageFile, FileStorageHelper.ActionsToString(fileActions)); } } } } catch (SEHException) { } catch (FileNotFoundException) { } } } await ForegroundHistoryActionWriter.RewriteFile((lines, linesToWrite) => { List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(lines); fileActions.RemoveAll(a => a.Delivered && a.ActionTime.CompareTo(minDateTime) < 0); foreach (HistoryAction historyAction in fileActions) { linesToWrite.Add(FileStorageHelper.ActionToString(historyAction)); } }); //Events are deleted when delivered so no cleanup need }