Advertisement
Guest User

C# MainPage

a guest
Oct 21st, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.WindowsRuntime;
  7. using Windows.Foundation;
  8. using Windows.Foundation.Collections;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Controls;
  11. using Windows.UI.Xaml.Controls.Primitives;
  12. using Windows.UI.Xaml.Data;
  13. using Windows.UI.Xaml.Input;
  14. using Windows.UI.Xaml.Media;
  15. using Windows.UI.Xaml.Navigation;
  16. using HackathonOctober.Database;
  17. using SQLite;
  18. using System.Diagnostics;
  19. using Windows.UI.Popups;
  20.  
  21. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  22.  
  23. namespace HackathonOctober
  24. {
  25.     /// <summary>
  26.     /// An empty page that can be used on its own or navigated to within a Frame.
  27.     /// </summary>
  28.     public sealed partial class MainPage : Page
  29.     {
  30.         public int TempNoteID { get; set; }
  31.         List<string> SuggestionList { get; set; }
  32.         public ObservableCollection<NoteDetails> Notes = new ObservableCollection<NoteDetails>();
  33.         public MainPage()
  34.         {            
  35.             this.InitializeComponent();
  36.             ShowRecords("ORDER BY NoteID DESC");
  37.         }
  38.  
  39.         #region EventHandlers
  40.         private void AddNoteBtn_Tapped(object sender, TappedRoutedEventArgs e)
  41.         {
  42.             Frame.Navigate(typeof(AddNote));
  43.         }
  44.  
  45.         private void SortBox_DropDownClosed(object sender, object e)
  46.         {
  47.             if (SortBox.SelectedIndex == 0)
  48.                 ShowRecords("ORDER BY NoteTitle ASC");
  49.             else if (SortBox.SelectedIndex == 1)
  50.                 ShowRecords("ORDER BY NoteDate DESC");
  51.             else if (SortBox.SelectedIndex == 2)
  52.                 ShowRecords("ORDER BY NoteID DESC");
  53.             else if (SortBox.SelectedIndex == 3)
  54.                 ShowRecords("ORDER BY NoteID ASC");
  55.         }
  56.  
  57.         private void ShowAllNotes_SelectionChanged(object sender, SelectionChangedEventArgs e)
  58.         {
  59.             NoteDetails selectItem = (NoteDetails)ShowAllNotes.SelectedItem;
  60.             if (e.AddedItems.Count > 0)
  61.             {
  62.                 FetchSelectedItem(selectItem.NoteID);
  63.                 BorderNoteTitle.Visibility = Visibility.Visible;
  64.                 BorderNoteContent.Visibility = Visibility.Visible;
  65.                 PreviewText.Visibility = Visibility.Visible;
  66.                 EditBtn.Visibility = Visibility.Visible;
  67.                 DeleteBtn.Visibility = Visibility.Visible;
  68.             }
  69.         }
  70.  
  71.         private async void DeleteBtn_Tapped(object sender, TappedRoutedEventArgs e)
  72.         {
  73.             MessageDialog msg = new MessageDialog("This action cannot be undone. Delete note?", "Confirm Action");
  74.             //Commands
  75.             msg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(CommandHandlersBeforeDelete)));
  76.             msg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(CommandHandlersBeforeDelete)));
  77.             await msg.ShowAsync();
  78.         }
  79.  
  80.         private void EditBtn_Tapped(object sender, TappedRoutedEventArgs e)
  81.         {
  82.             Frame.Navigate(typeof(EditNote), this.TempNoteID);
  83.         }
  84.  
  85.         private void SearchBox_QuerySubmitted(SearchBox sender, SearchBoxQuerySubmittedEventArgs args)
  86.         {
  87.             SearchQuery(args.QueryText);
  88.         }
  89.         #endregion
  90.  
  91.         #region Functions
  92.         private async void SearchQuery(string query)
  93.         {
  94.             HideElements();
  95.             using (var db = new SQLiteConnection(NoteDetails.DBPath()))
  96.             {
  97.                 List<NoteDetails> AllRecords = db.Query<NoteDetails>(string.Format("SELECT * FROM NoteDetails WHERE UPPER(NoteTitle) LIKE UPPER({0})",
  98.                     "'%" + query + "%'"));
  99.                 if (AllRecords.Count() > 0)
  100.                     ShowAllNotes.ItemsSource = AllRecords;
  101.                 else
  102.                 {
  103.                     MessageDialog msg = new MessageDialog("Sorry, 0 results found. Please try using another keyword", "No Results found :-(");
  104.                     await msg.ShowAsync();
  105.                     SearchInputBox.QueryText = string.Empty;
  106.                 }
  107.             }
  108.         }
  109.  
  110.         private void SearchBox_SuggestionsRequested(SearchBox sender, SearchBoxSuggestionsRequestedEventArgs e)
  111.         {
  112.             string queryText = e.QueryText;
  113.             if (!string.IsNullOrEmpty(queryText))
  114.             {
  115.                 Windows.ApplicationModel.Search.SearchSuggestionCollection suggestionCollection = e.Request.SearchSuggestionCollection;
  116.                 foreach (string suggestion in SuggestionList)
  117.                 {
  118.                     if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
  119.                     {
  120.                         suggestionCollection.AppendQuerySuggestion(suggestion);
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.  
  126.         private bool DeleteNote(int noteid)
  127.         {
  128.             string result = string.Empty;
  129.             bool success = false;
  130.             using (var db = new SQLiteConnection(NoteDetails.DBPath()))
  131.             {
  132.                 var notes = db.Table<NoteDetails>().Where(
  133.                     p => p.NoteID == noteid).FirstOrDefault();
  134.  
  135.                 if (db.Delete(notes) > 0)
  136.                     success = true;
  137.             }
  138.  
  139.             return success;
  140.         }
  141.  
  142.         private void HideElements()
  143.         {
  144.             // Hide all the elements on the right side of the UI...
  145.             BorderNoteTitle.Visibility = Visibility.Collapsed;
  146.             BorderNoteContent.Visibility = Visibility.Collapsed;
  147.             PreviewText.Visibility = Visibility.Collapsed;
  148.             EditBtn.Visibility = Visibility.Collapsed;
  149.             DeleteBtn.Visibility = Visibility.Collapsed;
  150.         }
  151.  
  152.         void ShowRecords(string query)
  153.         {
  154.             HideElements();
  155.             SuggestionList = new List<string>();
  156.             SuggestionList.Clear();
  157.             using (var db = new SQLiteConnection(NoteDetails.DBPath()))
  158.             {
  159.                 List<NoteDetails> AllRecords = db.Query<NoteDetails>(string.Format("SELECT * FROM NoteDetails {0}", query));
  160.                 ShowAllNotes.ItemsSource = AllRecords;
  161.  
  162.                 foreach (NoteDetails NotesSuggest in AllRecords)
  163.                 {
  164.                     SuggestionList.Add(NotesSuggest.NoteTitle);
  165.                 }
  166.             }
  167.         }
  168.  
  169.         public void CommandHandlersBeforeDelete(IUICommand commandLabel)
  170.         {
  171.             var Actions = commandLabel.Label;
  172.             switch (Actions)
  173.             {
  174.                 case "Yes":
  175.                     DeleteNote(this.TempNoteID);
  176.                     ShowRecords("ORDER BY NoteID DESC");
  177.                     break;
  178.             }
  179.         }
  180.  
  181.         private void FetchSelectedItem(int noteid)
  182.         {
  183.             var note = new NoteDetails();
  184.             using (var db = new SQLiteConnection(NoteDetails.DBPath()))
  185.             {
  186.                 var _note = (db.Table<NoteDetails>().Where(
  187.                     c => c.NoteID == noteid)).Single();
  188.  
  189.                 TempNoteID = _note.NoteID;
  190.                 NoteTitlePreview.Text = _note.NoteTitle;
  191.                 NoteContentPreview.Text = _note.NoteContent;
  192.             }
  193.         }
  194.         #endregion
  195.  
  196.     }  
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement