/// <summary> /// Gets the bookmark folder, incredibly rarely by asking the user to pick a folder. /// </summary> /// <param name="forceUserPick"></param> /// <returns>Storage folder to use; can be null in case of horrific error</returns> /// <summary> /// Returns the number of files in the folder that are newer than recorded. /// </summary> /// <returns></returns> public static async Task <int> SmartReadAsync() { int retval = 0; var folder = await BookmarkFileDirectory.GetBookmarkFolderAsync(); if (folder == null) { folder = await SetSaveFolderAsync(); } if (folder == null) { return(retval); } var fileTimes = BookmarkFileDirectory.GetCachedFileTimes(); var bookmarkfiles = await BookmarkFileDirectory.GetAllBookmarkFilesSortedAsync(folder); var preferredName = ThisComputerName() + ".recent" + BookmarkFileDirectory.EXTENSION; // is a JSON file foreach (var bookmarkfilename in bookmarkfiles) { var modified = await BookmarkFileDirectory.GetLastModifiedDateAsync(folder, bookmarkfilename); var lastDateRead = DateTimeOffset.MinValue; // Set to very small so if the value can't be read, the default is such that the // file will be read. Will fail in the corner case of the bookmark file being // given an absurd file time. if (fileTimes.ContainsKey(bookmarkfilename)) { try { lastDateRead = (DateTimeOffset)fileTimes[bookmarkfilename]; } catch (Exception) { App.Error($"Getting last read file time for {bookmarkfilename} isn't a DateTimeOffset?"); } } if (modified > lastDateRead && bookmarkfilename != preferredName) { retval++; var bmf = await ReadFileAsBookMarkFileAsync(folder, bookmarkfilename); if (bmf != null) { var nchanges = await MergeAsync(bmf); fileTimes[bookmarkfilename] = modified; } else { App.Error($"Unable to read bookmark file {bookmarkfilename}"); } } } BookmarkFileDirectory.SaveCachedFileTimes(fileTimes); return(retval); }
public static async Task <StorageFolder> SetSaveFolderAsync() { // First tell people what they are about to do var showResult = await SimpleDialogs.HowToPickSaveFolder(); if (showResult == SimpleDialogs.Result.Cancel) { return(null); } var folder = await BookmarkFileDirectory.PickBookmarkFolderAsync(); if (folder != null) { BookmarkFileDirectory.UpdateOrReplaceFutureAccessList(folder); BookmarkFileDirectory.ResetCachedFileTimes(); } return(folder); }
/// <summary> /// Given a storage file, returns a read-in book mark file object. This is a little /// harder than it looks (and hence it's beeing a class. /// </summary> /// <param name="file"></param> /// <returns></returns> private static async Task <BookMarkFile> ReadFileAsBookMarkFileAsync(StorageFolder folder, string bookmarkfilename) { var str = await BookmarkFileDirectory.ReadFileAsync(folder, bookmarkfilename); BookMarkFile bmf = null; try { // Try to be a little smart just so that I get fewer exceptions. Old files start with [ because they are just // arrays; the new file starts with { because it's an object. // "old": version used for a couple of weeks early in 2020 // "new" every other version if (!str.StartsWith("[")) { bmf = JsonConvert.DeserializeObject <BookMarkFile>(str); } } catch (Exception) { } if (bmf == null) { // Try to read files which are just a raw list of books. try { var list = JsonConvert.DeserializeObject <List <BookData> >(str); if (list != null) { bmf = new BookMarkFile() { SavedFromName = "unknown-computer", SaveTime = DateTimeOffset.FromUnixTimeSeconds(24 * 60 * 60), BookMarkList = list, }; } } catch (Exception) { } } return(bmf); }
public static async Task SmartSaveAsync(BookMarkFileType saveType) { var preferredName = saveType == BookMarkFileType.RecentOnly ? ThisComputerName() + ".recent" + BookmarkFileDirectory.EXTENSION : "FullBookmarkFile" + BookmarkFileDirectory.EXTENSION ; StorageFolder folder = null; try { folder = await BookmarkFileDirectory.GetBookmarkFolderAsync(); if (folder == null) { folder = await SetSaveFolderAsync(); } if (folder == null) { return; } if (folder != null) { // Make sure the files are in sync first int nread = await SmartReadAsync(); System.Diagnostics.Debug.WriteLine($"Smart save: smart read {nread} files"); var bmf = CreateBookMarkFile(saveType); var str = bmf.AsFileString(); await BookmarkFileDirectory.WriteFileAsync(folder, preferredName, str); } } catch (Exception ex) { App.Error($"Unable to save file {preferredName} to folder {folder} exception {ex.Message}"); } }