TimeSheetNotePage.xaml.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using InABox.Core;
  4. using Xamarin.Forms;
  5. using InABox.Clients;
  6. using Comal.Classes;
  7. using System.Threading.Tasks;
  8. using System.Linq;
  9. namespace comal.timesheets
  10. {
  11. public partial class TimeSheetNotePage
  12. {
  13. TimeSheet timeSheet = new TimeSheet();
  14. protected void SaveTimeSheetCallback(TimeSheet entity, Exception e)
  15. {
  16. if (e != null)
  17. Device.BeginInvokeOnMainThread(() => { DisplayAlert("Error Adding Note", "Unable to Save Note\n\n" + e.Message, "OK"); });
  18. }
  19. public TimeSheetNotePage(TimeSheet _timeSheet)
  20. {
  21. InitializeComponent();
  22. timeSheet = _timeSheet;
  23. NavigationPage.SetBackButtonTitle(this, "Cancel");
  24. ToolbarItems.Clear();
  25. ToolbarItems.Add(new ToolbarItem("Save", "", () =>
  26. {
  27. SaveNewNotes();
  28. Navigation.PopAsync();
  29. }));
  30. LoadNewNotes();
  31. }
  32. private void LoadNewNotes()
  33. {
  34. Task.Run(() =>
  35. {
  36. try
  37. {
  38. CoreTable table =
  39. new Client<TimeSheet>().Query
  40. (
  41. new Filter<TimeSheet>(x => x.ID).IsEqualTo(timeSheet.ID),
  42. new Columns<TimeSheet>(
  43. x => x.Notes
  44. ));
  45. if (table.Rows.Any())
  46. {
  47. CoreRow row = table.Rows.FirstOrDefault();
  48. List<object> list = row.Values;
  49. if (list[0] == null) { list[0] = ""; }
  50. Device.BeginInvokeOnMainThread(() =>
  51. {
  52. notesLbl.Text = list[0].ToString();
  53. });
  54. }
  55. }
  56. catch { }
  57. });
  58. }
  59. private void SaveNewNotes()
  60. {
  61. try
  62. {
  63. string notesLableText = notesLbl.Text + "\n";
  64. if (string.IsNullOrWhiteSpace(notesLbl.Text))
  65. {
  66. notesLableText = "";
  67. }
  68. if (!string.IsNullOrWhiteSpace(notesEdt.Text))
  69. {
  70. timeSheet.Notes = notesLableText +
  71. notesEdt.Text + " (Added by " + App.Data.Me.Name + " at " + DateTime.Now + ")";
  72. new Client<TimeSheet>().Save(timeSheet, "Updated Note from Mobile");
  73. DisplayAlert("Success", "Note Saved", "OK");
  74. }
  75. }
  76. catch { }
  77. }
  78. }
  79. }