NewReplyNotification.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using InABox.Core;
  2. using Plugin.Media;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Comal.Classes;
  10. using Xamarin.Forms;
  11. using Xamarin.Forms.Xaml;
  12. using XF.Material.Forms.UI.Dialogs;
  13. using comal.timesheets.CustomControls;
  14. using InABox.Clients;
  15. namespace comal.timesheets
  16. {
  17. [XamlCompilation(XamlCompilationOptions.Compile)]
  18. public partial class NewReplyNotification : ContentPage
  19. {
  20. Guid notificationID = new Guid();
  21. List<Document> newDocuments = new List<Document>();
  22. Notification notification = new Notification();
  23. public NewReplyNotification(Guid id = new Guid())
  24. {
  25. InitializeComponent();
  26. NavigationPage.SetHasBackButton(this, false);
  27. notificationID = id;
  28. notification.Sender.ID = GlobalVariables.EmpID;
  29. if (GlobalVariables.DeviceString == "i")
  30. {
  31. photosFrame.Margin = new Thickness(5, 5, 5, 15);
  32. }
  33. if (notificationID != Guid.Empty)
  34. {
  35. LoadExistingNotification();
  36. }
  37. }
  38. private void ExitBtn_Clicked(object sender, EventArgs e)
  39. {
  40. Navigation.PopAsync();
  41. }
  42. void LoadExistingNotification()
  43. {
  44. Task.Run(() =>
  45. {
  46. CoreTable table = new Client<Notification>().Query(
  47. new Filter<Notification>(x => x.ID).IsEqualTo(notificationID),
  48. new Columns<Notification>(
  49. x => x.ID,
  50. x => x.Title,
  51. x => x.Description,
  52. x => x.EntityID,
  53. x => x.EntityType,
  54. x => x.Sender.ID,
  55. x => x.Sender.Name)
  56. );
  57. if (table.Rows.Any())
  58. {
  59. notification = table.Rows.FirstOrDefault().ToObject<Notification>();
  60. Device.BeginInvokeOnMainThread(() =>
  61. {
  62. try
  63. {
  64. titleEdt.Text = notification.Title;
  65. StringBuilder sb = new StringBuilder();
  66. if (!string.IsNullOrWhiteSpace(notification.Sender.Name))
  67. {
  68. titleLbl.Text = "Replying " + notification.Sender.Name;
  69. sb.AppendFormat("Hi {0},\n\n\nAt {1:HH:mmtt} on {1:dd MMM yy}, {2} wrote:\n", notification.Sender.Name.Split(" ").FirstOrDefault(), notification.Created, notification.Sender.Name);
  70. notification.Employee.ID = notification.Sender.ID;
  71. notification.Employee.Name = notification.Sender.Name;
  72. recipientNameLbl.Text = notification.Employee.Name;
  73. }
  74. var origtext = CoreUtils.StripHTML(notification.Description);
  75. foreach (var line in origtext.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Where(x => !String.IsNullOrWhiteSpace(x)))
  76. {
  77. sb.AppendFormat(">> {0}\n", line.Trim());
  78. }
  79. descriptionEdt.Text = sb.ToString();
  80. LoadPhotos();
  81. ForceLayout();
  82. }
  83. catch { }
  84. });
  85. }
  86. });
  87. }
  88. void LoadPhotos()
  89. {
  90. Task.Run(() =>
  91. {
  92. CoreTable table = new Client<NotificationDocument>().Query(
  93. new Filter<NotificationDocument>(x => x.EntityLink.ID).IsEqualTo(notificationID),
  94. new Columns<NotificationDocument>(x => x.ID, x => x.DocumentLink.ID)
  95. );
  96. if (table.Rows.Any())
  97. {
  98. Device.BeginInvokeOnMainThread(() =>
  99. {
  100. photosLbl.Text = "Loading " + table.Rows.Count + " Photos";
  101. photosLbl.TextColor = Color.Orange;
  102. });
  103. foreach (CoreRow row in table.Rows)
  104. {
  105. CoreTable table2 = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(Guid.Parse(row.Values[1].ToString())), new Columns<Document>(x => x.Data));
  106. DataToImage(table2.Rows.FirstOrDefault().ToObject<Document>().Data);
  107. }
  108. Device.BeginInvokeOnMainThread(() =>
  109. {
  110. photosLbl.Text = "Photos";
  111. photosLbl.TextColor = Color.Default;
  112. });
  113. }
  114. });
  115. }
  116. void SelectRecipientBtn_Clicked(object sender, EventArgs e)
  117. {
  118. EmployeeSelectionPage employeeSelectionPage = new EmployeeSelectionPage();
  119. employeeSelectionPage.OnItemSelected += () =>
  120. {
  121. recipientNameLbl.Text = employeeSelectionPage.employee.Name;
  122. notification.Employee.ID = employeeSelectionPage.employee.ID;
  123. notification.Employee.Name = employeeSelectionPage.employee.Name;
  124. };
  125. Navigation.PushAsync(employeeSelectionPage);
  126. }
  127. async void SendBtn_Clicked(object sender, EventArgs e)
  128. {
  129. if (notification.Employee.ID == Guid.Empty)
  130. {
  131. DisplayAlert("Unable to send", "Notification requires a recipient", "OK");
  132. return;
  133. }
  134. if (string.IsNullOrWhiteSpace(notification.Title))
  135. {
  136. DisplayAlert("Unable to send", "Notification requires a title", "OK");
  137. return;
  138. }
  139. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Sending"))
  140. {
  141. new Client<Notification>().Save(notification, "Notification sent on mobile device");
  142. if (newDocuments.Count > 0)
  143. {
  144. SavePhotos();
  145. }
  146. DisplayAlert("Success", "Notification sent to " + notification.Employee.Name, "OK");
  147. Navigation.PopAsync();
  148. }
  149. }
  150. void SavePhotos()
  151. {
  152. List<NotificationDocument> nDocsToSave = new List<NotificationDocument>();
  153. new Client<Document>().Save(newDocuments, "Saved from Notifications on Mobile");
  154. foreach (Document doc in newDocuments)
  155. {
  156. NotificationDocument notificationDocument = new NotificationDocument();
  157. notificationDocument.EntityLink.ID = notification.ID;
  158. notificationDocument.DocumentLink.ID = doc.ID;
  159. nDocsToSave.Add(notificationDocument);
  160. }
  161. Task.Run(() => { new Client<NotificationDocument>().Save(nDocsToSave, "Saved from Notifications on Mobile"); });
  162. }
  163. void TitleEdt_Changed(object sender, EventArgs e)
  164. {
  165. notification.Title = titleEdt.Text;
  166. }
  167. void DescriptionEdt_Changed(object sender, EventArgs e)
  168. {
  169. notification.Description = descriptionEdt.Text;
  170. }
  171. #region Photos
  172. void TakePhoto_Clicked(object sender, EventArgs e)
  173. {
  174. TakeAPhoto();
  175. }
  176. void ChooseImage_Clicked(object sender, EventArgs e)
  177. {
  178. ChooseAPhoto();
  179. }
  180. private async void TakeAPhoto()
  181. {
  182. await CrossMedia.Current.Initialize();
  183. if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  184. {
  185. await DisplayAlert("No Camera", ":( No camera available.", "OK");
  186. return;
  187. }
  188. String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  189. var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  190. {
  191. Name = filename,
  192. CompressionQuality = 10,
  193. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  194. });
  195. if (file == null)
  196. return;
  197. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  198. {
  199. Image img = null;
  200. var memoryStream = new MemoryStream();
  201. file.GetStream().CopyTo(memoryStream);
  202. var data = memoryStream.ToArray();
  203. Document doc = new Document()
  204. {
  205. FileName = filename,
  206. Data = data,
  207. CRC = CoreUtils.CalculateCRC(data),
  208. TimeStamp = DateTime.Now
  209. };
  210. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  211. img = new Image();
  212. img.HeightRequest = 150;
  213. img.WidthRequest = 150;
  214. img.Aspect = Aspect.AspectFit;
  215. img.Source = src;
  216. img.GestureRecognizers.Add(new TapGestureRecognizer
  217. {
  218. Command = new Command(OnTap),
  219. CommandParameter = src,
  220. NumberOfTapsRequired = 1
  221. });
  222. newDocuments.Add(doc);
  223. file.Dispose();
  224. if (img != null)
  225. {
  226. Device.BeginInvokeOnMainThread(() =>
  227. {
  228. ImageScroller.IsVisible = true;
  229. images.Children.Add(img);
  230. });
  231. }
  232. }
  233. }
  234. private async void ChooseAPhoto()
  235. {
  236. await CrossMedia.Current.Initialize();
  237. if (!CrossMedia.Current.IsPickPhotoSupported)
  238. {
  239. await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
  240. return;
  241. }
  242. var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  243. {
  244. CompressionQuality = 10,
  245. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  246. });
  247. if (file == null)
  248. return;
  249. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  250. {
  251. Image img = null;
  252. var memoryStream = new MemoryStream();
  253. file.GetStream().CopyTo(memoryStream);
  254. var data = memoryStream.ToArray();
  255. Document doc = new Document()
  256. {
  257. FileName = Path.GetFileName(file.Path),
  258. Data = data,
  259. CRC = CoreUtils.CalculateCRC(data),
  260. TimeStamp = DateTime.Now
  261. };
  262. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  263. img = new Image();
  264. img.HeightRequest = 150;
  265. img.WidthRequest = 150;
  266. img.Aspect = Aspect.AspectFit;
  267. img.Source = src;
  268. img.GestureRecognizers.Add(new TapGestureRecognizer
  269. {
  270. Command = new Command(OnTap),
  271. CommandParameter = src,
  272. NumberOfTapsRequired = 1
  273. });
  274. newDocuments.Add(doc);
  275. file.Dispose();
  276. if (img != null)
  277. {
  278. Device.BeginInvokeOnMainThread(() =>
  279. {
  280. ImageScroller.IsVisible = true;
  281. images.Children.Add(img);
  282. });
  283. }
  284. }
  285. }
  286. private void OnTap(object obj)
  287. {
  288. ImageViewerEditor viewer = new ImageViewerEditor(obj as ImageSource, false);
  289. Navigation.PushAsync(viewer);
  290. viewer.OnSaveSelected += ImageViewEditor_OnSaveSelected;
  291. }
  292. private void ImageViewEditor_OnSaveSelected(byte[] array)
  293. {
  294. String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  295. DataToImage(array, filename);
  296. }
  297. public void DataToImage(byte[] data, string filename = "")
  298. {
  299. try
  300. {
  301. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  302. Image image = new Image();
  303. image.HeightRequest = 200;
  304. image.WidthRequest = 200;
  305. image.Aspect = Aspect.AspectFit;
  306. image.VerticalOptions = LayoutOptions.FillAndExpand;
  307. image.HorizontalOptions = LayoutOptions.FillAndExpand;
  308. image.Source = src;
  309. image.GestureRecognizers.Add(new TapGestureRecognizer
  310. {
  311. Command = new Command(OnTap),
  312. CommandParameter = src,
  313. NumberOfTapsRequired = 1
  314. });
  315. if (image != null)
  316. {
  317. Device.BeginInvokeOnMainThread(() =>
  318. {
  319. images.Children.Add(image);
  320. });
  321. }
  322. if (!string.IsNullOrWhiteSpace(filename))
  323. {
  324. Document doc = new Document()
  325. {
  326. FileName = filename,
  327. Data = data,
  328. CRC = CoreUtils.CalculateCRC(data),
  329. TimeStamp = DateTime.Now
  330. };
  331. newDocuments.Add(doc);
  332. }
  333. }
  334. catch
  335. { }
  336. }
  337. #endregion
  338. }
  339. }