public MainViewModel() { ApiBaseUrl = ""; ConsumerKey = ""; ConsumerSecret = ""; ProgressVisible = false; DataStorage = new DataStorage(); BookmarkList = new BookmarkList(); ReadabilityClient = new ReadabilityApi.ReadabilityClient(ApiBaseUrl, ConsumerKey, ConsumerSecret); Observable.Buffer(Observable.FromEventPattern(this, "ReadingListUpdated").Throttle(TimeSpan.FromSeconds(1)), 1) .Subscribe(e => { ShellTile tile = ShellTile.ActiveTiles.First(); if (tile != null && ReadingList.Count > 0) //Do nothing if there's no tile pinned or there are no items in the list. { var firstArticleInReadingList = ReadingList.First().Article; IconicTileData TileData = new IconicTileData() { Title = "Now Readable", Count = ReadingListCount, WideContent1 = firstArticleInReadingList.Title, WideContent2 = firstArticleInReadingList.Excerpt.Substring(0, 100), WideContent3 = firstArticleInReadingList.Author, SmallIconImage = new Uri("Assets/Tiles/SmallIconImage.png", UriKind.Relative), IconImage = new Uri("Assets/Tiles/IconImage.png", UriKind.Relative), BackgroundColor = System.Windows.Media.Colors.Red }; tile.Update(TileData); } }); }
/// <summary> /// Deletes the given BookmarkList and all contained articles from disk. /// </summary> /// <param name="bookmarkList">The bookmark list to be deleted.</param> public async Task DeleteBookmarkList(BookmarkList masterList, BookmarkList bookmarkList) { StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; foreach (var bookmark in bookmarkList.Bookmarks) { if (masterList.Bookmarks.Contains(bookmark)) { var targetBookmark = masterList.Bookmarks.First(mlBookmark => bookmark.Id == mlBookmark.Id); var folder = await local.CreateFolderAsync(targetBookmark.Article.Id, CreationCollisionOption.OpenIfExists); masterList.Bookmarks.Remove(bookmark); await folder.DeleteAsync(); } } var file = await local.CreateFileAsync("bookmarks", CreationCollisionOption.ReplaceExisting); var fileStream = await file.OpenStreamForWriteAsync(); using (var writer = new BsonWriter(new BinaryWriter(fileStream))) { var serializer = new JsonSerializer(); serializer.Serialize(writer, masterList); } }
/// <summary> /// Saves the given bookmark list to a file and downloads all the articles in those bookmarks. /// </summary> /// <param name="readabilityClient">The readability client to use to request the documents.</param> /// <param name="bookmarkList">The bookmark list to be saved.</param> /// <returns>A simple task to make this awaitable.</returns> public async Task SaveBookmarkList(ReadabilityClient readabilityClient, BookmarkList masterList) { StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; var file = await local.CreateFileAsync("bookmarks", CreationCollisionOption.ReplaceExisting); var fileStream = await file.OpenStreamForWriteAsync(); using (var writer = new BsonWriter(new BinaryWriter(fileStream))) { var serializer = new JsonSerializer(); serializer.Serialize(writer, masterList); } }
/// <summary> /// Merges the two lists and downloads their articles. /// </summary> /// <param name="readabilityClient">The readabilty client to use to request the documents.</param> /// <param name="masterList">The list that will contain the final list of bookmarks.</param> /// <param name="bookmarkList">The bookmark list to be saved.</param> /// <returns>An awaitable task.</returns> public async Task MergeSaveBookmarkList(MainViewModel mainViewModel, BookmarkList bookmarkList) { mainViewModel.ProgressValue = 0; mainViewModel.ProgressIndeterminate = false; mainViewModel.ProgressText = "Syncing..."; StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; var tasks = new List<Task>(); foreach (var bookmark in bookmarkList.Bookmarks) { var saveCompleteAction = new Action(() => { int position = mainViewModel.BookmarkList.Bookmarks.IndexOf(bookmark); mainViewModel.BookmarkList.Bookmarks.Remove(bookmark); if (position == -1) { mainViewModel.BookmarkList.Bookmarks.Add(bookmark); } else { mainViewModel.BookmarkList.Bookmarks.Insert(position, bookmark); } mainViewModel.ProgressValue += ((double)1 / (double)bookmarkList.Bookmarks.Count); }); tasks.Add(SaveArticle(bookmark, mainViewModel.ReadabilityClient, local, saveCompleteAction)); } await Task.WhenAll(tasks); mainViewModel.ProgressIndeterminate = true; mainViewModel.ProgressText = "Almost done..."; await SaveBookmarkList(mainViewModel.ReadabilityClient, mainViewModel.BookmarkList); }