NewReplyNotification.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Comal.Classes;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using XF.Material.Forms.UI.Dialogs;
  12. using InABox.Clients;
  13. using InABox.Mobile;
  14. using Xamarin.Essentials;
  15. namespace PRS.Mobile
  16. {
  17. [XamlCompilation(XamlCompilationOptions.Compile)]
  18. public partial class NewReplyNotification
  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 = App.Data.Me.ID;
  29. if (App.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>(ColumnTypeFlags.None).Add(
  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. Title = "Reply To: " + 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>(ColumnTypeFlags.None).Add(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(
  106. new Filter<Document>(x => x.ID).IsEqualTo(Guid.Parse(row.Values[1].ToString())),
  107. new Columns<Document>(ColumnTypeFlags.None).Add(x => x.Data));
  108. DataToImage(table2.Rows.FirstOrDefault().ToObject<Document>().Data);
  109. }
  110. Device.BeginInvokeOnMainThread(() =>
  111. {
  112. photosLbl.Text = "Photos";
  113. photosLbl.TextColor = Color.Default;
  114. });
  115. }
  116. });
  117. }
  118. void SelectRecipientBtn_Clicked(object sender, EventArgs e)
  119. {
  120. Navigation.PushAsync(new EmployeeSelectionPage(employee =>
  121. {
  122. recipientNameLbl.Text = employee.Name;
  123. notification.Employee.ID = employee.ID;
  124. notification.Employee.Name = employee.Name;
  125. }));
  126. }
  127. void SavePhotos()
  128. {
  129. List<NotificationDocument> nDocsToSave = new List<NotificationDocument>();
  130. new Client<Document>().Save(newDocuments, "Saved from Notifications on Mobile");
  131. foreach (Document doc in newDocuments)
  132. {
  133. NotificationDocument notificationDocument = new NotificationDocument();
  134. notificationDocument.EntityLink.ID = notification.ID;
  135. notificationDocument.DocumentLink.ID = doc.ID;
  136. nDocsToSave.Add(notificationDocument);
  137. }
  138. Task.Run(() => { new Client<NotificationDocument>().Save(nDocsToSave, "Saved from Notifications on Mobile"); });
  139. }
  140. void TitleEdt_Changed(object sender, EventArgs e)
  141. {
  142. notification.Title = titleEdt.Text;
  143. }
  144. void DescriptionEdt_Changed(object sender, EventArgs e)
  145. {
  146. notification.Description = descriptionEdt.Text;
  147. }
  148. #region Photos
  149. void TakePhoto_Clicked(object sender, EventArgs e)
  150. {
  151. TakeAPhoto();
  152. }
  153. void ChooseImage_Clicked(object sender, EventArgs e)
  154. {
  155. ChooseAPhoto();
  156. }
  157. private async void TakeAPhoto()
  158. {
  159. var file = await MediaPicker.CapturePhotoAsync();
  160. if (file == null)
  161. return;
  162. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  163. {
  164. Image img = null;
  165. var memoryStream = new MemoryStream();
  166. using (var stream = await file.OpenReadAsync())
  167. await stream.CopyToAsync(memoryStream);
  168. var data = memoryStream.ToArray();
  169. Document doc = new Document()
  170. {
  171. FileName = Path.GetFileName(file.FileName),
  172. Data = data,
  173. CRC = CoreUtils.CalculateCRC(data),
  174. TimeStamp = DateTime.Now
  175. };
  176. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  177. img = new Image();
  178. img.HeightRequest = 150;
  179. img.WidthRequest = 150;
  180. img.Aspect = Aspect.AspectFit;
  181. img.Source = src;
  182. img.GestureRecognizers.Add(new TapGestureRecognizer
  183. {
  184. Command = new Command(OnTap),
  185. CommandParameter = src,
  186. NumberOfTapsRequired = 1
  187. });
  188. newDocuments.Add(doc);
  189. if (img != null)
  190. {
  191. Device.BeginInvokeOnMainThread(() =>
  192. {
  193. ImageScroller.IsVisible = true;
  194. images.Children.Add(img);
  195. });
  196. }
  197. }
  198. }
  199. private async void ChooseAPhoto()
  200. {
  201. var file = await MediaPicker.PickPhotoAsync();
  202. if (file == null)
  203. return;
  204. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  205. {
  206. Image img = null;
  207. var memoryStream = new MemoryStream();
  208. using (var stream = await file.OpenReadAsync())
  209. await stream.CopyToAsync(memoryStream);
  210. var data = memoryStream.ToArray();
  211. Document doc = new Document()
  212. {
  213. FileName = Path.GetFileName(file.FileName),
  214. Data = data,
  215. CRC = CoreUtils.CalculateCRC(data),
  216. TimeStamp = DateTime.Now
  217. };
  218. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  219. img = new Image();
  220. img.HeightRequest = 150;
  221. img.WidthRequest = 150;
  222. img.Aspect = Aspect.AspectFit;
  223. img.Source = src;
  224. img.GestureRecognizers.Add(new TapGestureRecognizer
  225. {
  226. Command = new Command(OnTap),
  227. CommandParameter = src,
  228. NumberOfTapsRequired = 1
  229. });
  230. newDocuments.Add(doc);
  231. if (img != null)
  232. {
  233. Device.BeginInvokeOnMainThread(() =>
  234. {
  235. ImageScroller.IsVisible = true;
  236. images.Children.Add(img);
  237. });
  238. }
  239. }
  240. }
  241. private void OnTap(object obj)
  242. {
  243. ImageViewerEditor viewer = new ImageViewerEditor(
  244. obj as ImageSource,
  245. (data) =>
  246. {
  247. String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  248. DataToImage(data, filename);
  249. }
  250. );
  251. Navigation.PushAsync(viewer);
  252. }
  253. public void DataToImage(byte[] data, string filename = "")
  254. {
  255. try
  256. {
  257. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  258. Image image = new Image();
  259. image.HeightRequest = 200;
  260. image.WidthRequest = 200;
  261. image.Aspect = Aspect.AspectFit;
  262. image.VerticalOptions = LayoutOptions.FillAndExpand;
  263. image.HorizontalOptions = LayoutOptions.FillAndExpand;
  264. image.Source = src;
  265. image.GestureRecognizers.Add(new TapGestureRecognizer
  266. {
  267. Command = new Command(OnTap),
  268. CommandParameter = src,
  269. NumberOfTapsRequired = 1
  270. });
  271. if (image != null)
  272. {
  273. Device.BeginInvokeOnMainThread(() =>
  274. {
  275. images.Children.Add(image);
  276. });
  277. }
  278. if (!string.IsNullOrWhiteSpace(filename))
  279. {
  280. Document doc = new Document()
  281. {
  282. FileName = filename,
  283. Data = data,
  284. CRC = CoreUtils.CalculateCRC(data),
  285. TimeStamp = DateTime.Now
  286. };
  287. newDocuments.Add(doc);
  288. }
  289. }
  290. catch
  291. { }
  292. }
  293. #endregion
  294. private async void _send_OnClicked(object sender, MobileMenuButtonClickedEventArgs args)
  295. {
  296. if (notification.Employee.ID == Guid.Empty)
  297. {
  298. await DisplayAlert("Unable to send", "Notification requires a recipient", "OK");
  299. return;
  300. }
  301. if (string.IsNullOrWhiteSpace(notification.Title))
  302. {
  303. await DisplayAlert("Unable to send", "Notification requires a title", "OK");
  304. return;
  305. }
  306. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Sending"))
  307. {
  308. new Client<Notification>().Save(notification, "Notification sent on mobile device");
  309. if (newDocuments.Count > 0)
  310. {
  311. SavePhotos();
  312. }
  313. DisplayAlert("Success", "Notification sent to " + notification.Employee.Name, "OK");
  314. Navigation.PopAsync();
  315. }
  316. }
  317. }
  318. }