/// <summary> /// Remove all unused images from local storage /// </summary> /// <returns></returns> private async static Task RunCleanOperationAsync() { try { var storage = Windows.Storage.ApplicationData.Current.LocalFolder; var folder = await storage.CreateFolderAsync(FolderNames.Images.ToString(), Windows.Storage.CreationCollisionOption.OpenIfExists); if (folder == null) { return; } var notebooks = NotesDataSource.GetGroups().Cast <NoteBook>().ToList(); var files = await folder.GetFilesAsync(); var allImages = notebooks.SelectMany(n => n.Items).SelectMany(imgs => imgs.Images); foreach (var file in files) { if (!allImages.Any(img => img.Contains(file.Name))) { await file.DeleteAsync(); } } } catch (Exception) { //Helpers.ShowErrorMessage("Clean", ex); } }
/// <summary> /// Load all notebooks and for each note load all images under one collection. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UserControl_Loaded(object sender, RoutedEventArgs e) { var nbs = NotesDataSource.GetGroups(); var images = nbs.SelectMany(n => ((NoteBook)n).Items).SelectMany(i => i.Images).ToList(); imagesGrid.ItemsSource = images; }
/// <summary> /// Create new Note, if there is no notebooks available the show error massage. /// Create new Note from GroupedItemsPage (no selected notebook) then create note under the first Notebook. /// Create new Note from GroupDetailPage then create note under the notebook presented in GroupDetailPage. /// Create new Note from ItemDetailPage then create note under the current Note Notebook. /// </summary> /// <param name="item">New Note To Create</param> public void CreateNew(NoteDataCommon item) { if (NotesDataSource.GetGroups().Count == 0) { Helpers.ShowMessageAsync("You first need to create a notebook.", "Create Notebook"); return; } if (this.GetType() == typeof(GroupDetailPage)) { item.NoteBook = this.DefaultViewModel["Group"] as NoteBook; NotesDataSource.Add(item, item.NoteBookUniqueId); } else if (this.GetType() == typeof(ItemDetailPage)) { item.NoteBook = (this.DefaultViewModel["Item"] as NoteDataCommon).NoteBook; NotesDataSource.Add(item, item.NoteBookUniqueId); } else { item.NoteBook = NotesDataSource.GetGroups()[0] as NoteBook; NotesDataSource.Add(item, item.NoteBookUniqueId); } this.Frame.Navigate(typeof(ItemDetailPage), item.UniqueId); }
/// <summary> /// Build navigation popup menu when clicking on Notebook title. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MenuItem_Click(object sender, RoutedEventArgs e) { if (navigationMenu != null) { return; } navigationMenu = new MenuFlyout(); var navigationNotebooks = NotesDataSource.GetGroups(); for (int i = 0; i < navigationNotebooks.Count; i++) { MenuFlyoutItem command = new MenuFlyoutItem(); command.Text = navigationNotebooks[i].Title; command.Name = navigationNotebooks[i].UniqueId; command.Click += (s, args) => Navigate(command.Name); navigationMenu.Items.Add(command); } MenuFlyoutSeparator separator = new MenuFlyoutSeparator(); navigationMenu.Items.Add(separator); MenuFlyoutItem cmd = new MenuFlyoutItem(); cmd.Text = "Home"; cmd.Click += (s, args) => Navigate(null); navigationMenu.Items.Add(cmd); navigationMenu.ShowAt((FrameworkElement)pageTitle); navigationMenu = null; }
/// <summary> /// User clicked on "Move" appbar button, this method will build and display a popup menu with list of Notebooks. /// Allowing the user target notebook. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async void btnMoveNotebookClick(object sender, RoutedEventArgs e) { PopupMenu nbMenu = new PopupMenu(); var nbs = NotesDataSource.GetGroups(); foreach (NoteBook nb in nbs) { if (!nb.UniqueId.Equals(Note.NoteBookUniqueId)) { nbMenu.Commands.Add(new UICommand(nb.Title, null, nb.UniqueId)); } } var rect = Helpers.GetElementRect((FrameworkElement)sender); IUICommand command = await nbMenu.ShowForSelectionAsync(rect); if (command == null) { return; } var result = await Helpers.ShowDialog(string.Format(Consts.MoveNote, Note.NoteBook.Title, command.Label), "Move Note"); var nbUniqueId = (string)command.Id; if (result) { NotesDataSource.Move(Note.UniqueId, nbUniqueId); DrawDefaultAppBar(); } }
/// <summary> /// Set all notebooks as NotebooksGridView itemsSource. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void UserControl_Loaded(object sender, RoutedEventArgs e) { if (!NotesDataSource.DataLoaded) { await DataManager.LoadAsync(); } NotebooksGridView.ItemsSource = NotesDataSource.GetGroups(); }
/// <summary> /// Gets all used tags from all notebooks. /// </summary> /// <returns>List of all Tags</returns> private Dictionary <string, bool> BuildTagList() { var allTags = NotesDataSource.GetGroups().SelectMany(nb => ((NoteBook)nb).Items).SelectMany(t => t.Tags).Distinct(); var tags = new Dictionary <string, bool>(); foreach (var tag in allTags) { tags.Add(tag, this.Note.Tags.Contains(tag)); } return(tags); }
/// <summary> /// Populates the page with content passed during navigation. Any saved state is also /// provided when recreating a page from a prior session. /// </summary> /// <param name="navigationParameter">The parameter value passed to /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested. /// </param> /// <param name="pageState">A dictionary of state preserved by this page during an earlier /// session. This will be null the first time a page is visited.</param> protected override void LoadState(Object navigationParameter, Dictionary <String, Object> pageState) { // TODO: Create an appropriate data model for your problem domain to replace the sample data var sampleDataGroups = NotesDataSource.GetGroups(); this.DefaultViewModel["Groups"] = sampleDataGroups; this.groupGridView.ItemsSource = this.groupedItemsViewSource.View.CollectionGroups; if (sampleDataGroups.Count == 0) { EmptyCollectionGrid.Visibility = Windows.UI.Xaml.Visibility.Visible; } }
private List <ListViewItem> BuildTagList() { var allTags = NotesDataSource.GetGroups().SelectMany(nb => ((NoteBook)nb).Items).SelectMany(t => t.Tags).Distinct(); List <ListViewItem> tags = new List <ListViewItem>(); foreach (var tag in allTags) { var viewItem = new ListViewItem(); viewItem.Content = tag; viewItem.Style = Application.Current.Resources["TagSuggestionItemTemplate"] as Style; viewItem.IsSelected = this.Tags.Contains(tag); tags.Add(viewItem); } return(tags); }
/// <summary> /// Defines all known Note types for DataContractJsonSerializer, each notebook will be serialized and than converted to json format, finally a file will be created under Notebooks folder /// with the Notebook Unique id as file name. /// </summary> private async static void RunSaveOperation() { var storage = Windows.Storage.ApplicationData.Current.LocalFolder; List <Type> types = new List <Type>(); types.Add(typeof(FoodDataItem)); types.Add(typeof(NoteDataItem)); types.Add(typeof(ToDoDataItem)); types.Add(typeof(NoteBook)); var notebooks = NotesDataSource.GetGroups(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(NoteDataCommon), types); var folder = await storage.CreateFolderAsync(FolderNames.Notebooks.ToString(), Windows.Storage.CreationCollisionOption.OpenIfExists); foreach (var notebook in notebooks) { try { using (MemoryStream ms = new MemoryStream()) { ser.WriteObject(ms, notebook); byte[] json = ms.ToArray(); var value = Encoding.UTF8.GetString(json, 0, json.Length); var file = await folder.CreateFileAsync(notebook.UniqueId, Windows.Storage.CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(file, value); await Task.Delay(500); } } catch (Exception) { //Helpers.ShowErrorMessage("Save Data", ex); } } }
/// <summary> /// Build navigation popup menu when clicking on Notebook title. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async void MenuItem_Click(object sender, RoutedEventArgs e) { if (navigationMenu != null) { return; } var rect = new Rect(170, 110, 0, 0); navigationMenu = new PopupMenu(); var navigationNotebooks = NotesDataSource.GetGroups(); var count = navigationNotebooks.Count > 4 ? 4 : navigationNotebooks.Count; for (var i = 0; i < count; i++) { navigationMenu.Commands.Add(new UICommand(navigationNotebooks[i].Title, Navigate, navigationNotebooks[i].UniqueId)); } navigationMenu.Commands.Add(new UICommandSeparator()); navigationMenu.Commands.Add(new UICommand("Home", Navigate, null)); await navigationMenu.ShowForSelectionAsync(rect); navigationMenu = null; }
/// <summary> /// After DataManager has finished loading the data, based on the application launch type we redirect the user to the proper page. /// </summary> /// <param name="sender"></param> /// <param name="ex"></param> async void Data_Completed(object sender, Exception ex) { if (ex != null) { Helpers.ShowErrorMessageAsync("Loading Notebooks", "Unexpected error while loading notebooks."); } else { var rootFrame = new Frame(); SuspensionManager.RegisterFrame(rootFrame, "AppFrame"); DataManager.Clean(); if (NotesDataSource.GetGroups().Count == 0) { rootFrame.Navigate(typeof(RestoreWorkingPage), "Getting Started"); Window.Current.Content = rootFrame; return; } if (RestoreLastState) { // Restore the saved session state only when appropriate try { await SuspensionManager.RestoreAsync(); } catch (SuspensionManagerException) { rootFrame.Navigate(typeof(GroupedItemsPage)); Window.Current.Content = rootFrame; } } else if (ShareTargetOperation != null) { ShareTarget share = new ShareTarget(); await share.ActivateAsync(ShareTargetOperation); } else if (SearchArgs != null) { rootFrame.Navigate(typeof(GroupedItemsPage)); rootFrame.Navigate(typeof(SearchResults), SearchArgs.QueryText); Window.Current.Content = rootFrame; } else if (!string.IsNullOrEmpty(DirectLink)) { var note = NotesDataSource.Find(DirectLink); if (note == null) { rootFrame.Navigate(typeof(GroupedItemsPage)); Window.Current.Content = rootFrame; return; } rootFrame.Navigate(typeof(GroupedItemsPage)); if (note.GetType() == typeof(NoteBook)) { rootFrame.Navigate(typeof(GroupDetailPage), note.UniqueId); } else { rootFrame.Navigate(typeof(GroupDetailPage), note.NoteBookUniqueId); rootFrame.Navigate(typeof(ItemDetailPage), note.UniqueId); } Window.Current.Content = rootFrame; } else { rootFrame.Navigate(typeof(GroupedItemsPage)); Window.Current.Content = rootFrame; } } }
/// <summary> /// On ShareTarget activation we display the sahre source data /// Also we allow the user to choose what type of Note he would like to create and inside which notebook to created the new note. /// </summary> /// <param name="args"></param> /// <returns></returns> public async Task ActivateAsync(ShareTargetActivatedEventArgs args) { var groups = NotesDataSource.GetGroups(); if (groups.Count == 0) { Helpers.ShowMessageAsync("Please create notebook first.", "No Notebooks"); Window.Current.Activate(); return; } comboNotebooks.ItemsSource = groups; comboNotebooks.SelectedItem = comboNotebooks.Items[0]; var validNotes = System.Enum.GetValues(typeof(NoteTypes)).Cast <NoteTypes>(); comboType.ItemsSource = validNotes.Where(n => n != NoteTypes.Section && n != NoteTypes.Notebook); comboType.SelectedItem = comboType.Items[0]; this.shareOperation = (args as ShareTargetActivatedEventArgs).ShareOperation; txtTitle.Text = this.shareOperation.Data.Properties.Title; txtDescription.Text = this.shareOperation.Data.Properties.Description; if (this.shareOperation.Data.Contains(StandardDataFormats.WebLink)) { Uri uri = await this.shareOperation.Data.GetWebLinkAsync(); if (uri != null) { txtDescription.Text = "Uri: " + uri.AbsoluteUri + Environment.NewLine; } } if (this.shareOperation.Data.Contains(StandardDataFormats.Text)) { string text = await this.shareOperation.Data.GetTextAsync(); if (text != null) { txtDescription.Text += "Text: " + text + Environment.NewLine; } } if (this.shareOperation.Data.Contains(StandardDataFormats.StorageItems)) { IReadOnlyList <IStorageItem> storageItems = null; storageItems = await this.shareOperation.Data.GetStorageItemsAsync(); string fileList = String.Empty; for (int index = 0; index < storageItems.Count; index++) { fileList += storageItems[index].Name; if (index < storageItems.Count - 1) { fileList += ", "; } } txtDescription.Text += "StorageItems: " + fileList + Environment.NewLine; } if (this.shareOperation.Data.Contains(StandardDataFormats.Html)) { string htmlFormat = await this.shareOperation.Data.GetHtmlFormatAsync(); string htmlFragment = HtmlFormatHelper.GetStaticFragment(htmlFormat); txtDescription.Text += "Text: " + Windows.Data.Html.HtmlUtilities.ConvertToText(htmlFragment) + Environment.NewLine; } if (this.shareOperation.Data.Contains(StandardDataFormats.Bitmap)) { img.Visibility = Visibility.Visible; IRandomAccessStreamReference imageReceived = await this.shareOperation.Data.GetBitmapAsync(); var stream = await imageReceived.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); img.Source = bitmapImage; } Window.Current.Content = this; Window.Current.Activate(); }