Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices.WindowsRuntime;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Data;
- using Windows.UI.Xaml.Input;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Navigation;
- using HackathonOctober.Database;
- using SQLite;
- using System.Diagnostics;
- using Windows.UI.Popups;
- // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
- namespace HackathonOctober
- {
- /// <summary>
- /// An empty page that can be used on its own or navigated to within a Frame.
- /// </summary>
- public sealed partial class MainPage : Page
- {
- public int TempNoteID { get; set; }
- List<string> SuggestionList { get; set; }
- public ObservableCollection<NoteDetails> Notes = new ObservableCollection<NoteDetails>();
- public MainPage()
- {
- this.InitializeComponent();
- ShowRecords("ORDER BY NoteID DESC");
- }
- #region EventHandlers
- private void AddNoteBtn_Tapped(object sender, TappedRoutedEventArgs e)
- {
- Frame.Navigate(typeof(AddNote));
- }
- private void SortBox_DropDownClosed(object sender, object e)
- {
- if (SortBox.SelectedIndex == 0)
- ShowRecords("ORDER BY NoteTitle ASC");
- else if (SortBox.SelectedIndex == 1)
- ShowRecords("ORDER BY NoteDate DESC");
- else if (SortBox.SelectedIndex == 2)
- ShowRecords("ORDER BY NoteID DESC");
- else if (SortBox.SelectedIndex == 3)
- ShowRecords("ORDER BY NoteID ASC");
- }
- private void ShowAllNotes_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- NoteDetails selectItem = (NoteDetails)ShowAllNotes.SelectedItem;
- if (e.AddedItems.Count > 0)
- {
- FetchSelectedItem(selectItem.NoteID);
- BorderNoteTitle.Visibility = Visibility.Visible;
- BorderNoteContent.Visibility = Visibility.Visible;
- PreviewText.Visibility = Visibility.Visible;
- EditBtn.Visibility = Visibility.Visible;
- DeleteBtn.Visibility = Visibility.Visible;
- }
- }
- private async void DeleteBtn_Tapped(object sender, TappedRoutedEventArgs e)
- {
- MessageDialog msg = new MessageDialog("This action cannot be undone. Delete note?", "Confirm Action");
- //Commands
- msg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(CommandHandlersBeforeDelete)));
- msg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(CommandHandlersBeforeDelete)));
- await msg.ShowAsync();
- }
- private void EditBtn_Tapped(object sender, TappedRoutedEventArgs e)
- {
- Frame.Navigate(typeof(EditNote), this.TempNoteID);
- }
- private void SearchBox_QuerySubmitted(SearchBox sender, SearchBoxQuerySubmittedEventArgs args)
- {
- SearchQuery(args.QueryText);
- }
- #endregion
- #region Functions
- private async void SearchQuery(string query)
- {
- HideElements();
- using (var db = new SQLiteConnection(NoteDetails.DBPath()))
- {
- List<NoteDetails> AllRecords = db.Query<NoteDetails>(string.Format("SELECT * FROM NoteDetails WHERE UPPER(NoteTitle) LIKE UPPER({0})",
- "'%" + query + "%'"));
- if (AllRecords.Count() > 0)
- ShowAllNotes.ItemsSource = AllRecords;
- else
- {
- MessageDialog msg = new MessageDialog("Sorry, 0 results found. Please try using another keyword", "No Results found :-(");
- await msg.ShowAsync();
- SearchInputBox.QueryText = string.Empty;
- }
- }
- }
- private void SearchBox_SuggestionsRequested(SearchBox sender, SearchBoxSuggestionsRequestedEventArgs e)
- {
- string queryText = e.QueryText;
- if (!string.IsNullOrEmpty(queryText))
- {
- Windows.ApplicationModel.Search.SearchSuggestionCollection suggestionCollection = e.Request.SearchSuggestionCollection;
- foreach (string suggestion in SuggestionList)
- {
- if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
- {
- suggestionCollection.AppendQuerySuggestion(suggestion);
- }
- }
- }
- }
- private bool DeleteNote(int noteid)
- {
- string result = string.Empty;
- bool success = false;
- using (var db = new SQLiteConnection(NoteDetails.DBPath()))
- {
- var notes = db.Table<NoteDetails>().Where(
- p => p.NoteID == noteid).FirstOrDefault();
- if (db.Delete(notes) > 0)
- success = true;
- }
- return success;
- }
- private void HideElements()
- {
- // Hide all the elements on the right side of the UI...
- BorderNoteTitle.Visibility = Visibility.Collapsed;
- BorderNoteContent.Visibility = Visibility.Collapsed;
- PreviewText.Visibility = Visibility.Collapsed;
- EditBtn.Visibility = Visibility.Collapsed;
- DeleteBtn.Visibility = Visibility.Collapsed;
- }
- void ShowRecords(string query)
- {
- HideElements();
- SuggestionList = new List<string>();
- SuggestionList.Clear();
- using (var db = new SQLiteConnection(NoteDetails.DBPath()))
- {
- List<NoteDetails> AllRecords = db.Query<NoteDetails>(string.Format("SELECT * FROM NoteDetails {0}", query));
- ShowAllNotes.ItemsSource = AllRecords;
- foreach (NoteDetails NotesSuggest in AllRecords)
- {
- SuggestionList.Add(NotesSuggest.NoteTitle);
- }
- }
- }
- public void CommandHandlersBeforeDelete(IUICommand commandLabel)
- {
- var Actions = commandLabel.Label;
- switch (Actions)
- {
- case "Yes":
- DeleteNote(this.TempNoteID);
- ShowRecords("ORDER BY NoteID DESC");
- break;
- }
- }
- private void FetchSelectedItem(int noteid)
- {
- var note = new NoteDetails();
- using (var db = new SQLiteConnection(NoteDetails.DBPath()))
- {
- var _note = (db.Table<NoteDetails>().Where(
- c => c.NoteID == noteid)).Single();
- TempNoteID = _note.NoteID;
- NoteTitlePreview.Text = _note.NoteTitle;
- NoteContentPreview.Text = _note.NoteContent;
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement