using InABox.Core; using Plugin.Media; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Comal.Classes; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI.Dialogs; using comal.timesheets.CustomControls; using InABox.Clients; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class NewReplyNotification : ContentPage { Guid notificationID = new Guid(); List newDocuments = new List(); Notification notification = new Notification(); public NewReplyNotification(Guid id = new Guid()) { InitializeComponent(); NavigationPage.SetHasBackButton(this, false); notificationID = id; notification.Sender.ID = App.Data.Me.ID; if (App.DeviceString == "i") { photosFrame.Margin = new Thickness(5, 5, 5, 15); } if (notificationID != Guid.Empty) { LoadExistingNotification(); } } private void ExitBtn_Clicked(object sender, EventArgs e) { Navigation.PopAsync(); } void LoadExistingNotification() { Task.Run(() => { CoreTable table = new Client().Query( new Filter(x => x.ID).IsEqualTo(notificationID), new Columns( x => x.ID, x => x.Title, x => x.Description, x => x.EntityID, x => x.EntityType, x => x.Sender.ID, x => x.Sender.Name) ); if (table.Rows.Any()) { notification = table.Rows.FirstOrDefault().ToObject(); Device.BeginInvokeOnMainThread(() => { try { titleEdt.Text = notification.Title; StringBuilder sb = new StringBuilder(); if (!string.IsNullOrWhiteSpace(notification.Sender.Name)) { titleLbl.Text = "Replying " + notification.Sender.Name; 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); notification.Employee.ID = notification.Sender.ID; notification.Employee.Name = notification.Sender.Name; recipientNameLbl.Text = notification.Employee.Name; } var origtext = CoreUtils.StripHTML(notification.Description); foreach (var line in origtext.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Where(x => !String.IsNullOrWhiteSpace(x))) { sb.AppendFormat(">> {0}\n", line.Trim()); } descriptionEdt.Text = sb.ToString(); LoadPhotos(); ForceLayout(); } catch { } }); } }); } void LoadPhotos() { Task.Run(() => { CoreTable table = new Client().Query( new Filter(x => x.EntityLink.ID).IsEqualTo(notificationID), new Columns(x => x.ID, x => x.DocumentLink.ID) ); if (table.Rows.Any()) { Device.BeginInvokeOnMainThread(() => { photosLbl.Text = "Loading " + table.Rows.Count + " Photos"; photosLbl.TextColor = Color.Orange; }); foreach (CoreRow row in table.Rows) { CoreTable table2 = new Client().Query(new Filter(x => x.ID).IsEqualTo(Guid.Parse(row.Values[1].ToString())), new Columns(x => x.Data)); DataToImage(table2.Rows.FirstOrDefault().ToObject().Data); } Device.BeginInvokeOnMainThread(() => { photosLbl.Text = "Photos"; photosLbl.TextColor = Color.Default; }); } }); } void SelectRecipientBtn_Clicked(object sender, EventArgs e) { Navigation.PushAsync(new EmployeeSelectionPage(employee => { recipientNameLbl.Text = employee.Name; notification.Employee.ID = employee.ID; notification.Employee.Name = employee.Name; })); } async void SendBtn_Clicked(object sender, EventArgs e) { if (notification.Employee.ID == Guid.Empty) { DisplayAlert("Unable to send", "Notification requires a recipient", "OK"); return; } if (string.IsNullOrWhiteSpace(notification.Title)) { DisplayAlert("Unable to send", "Notification requires a title", "OK"); return; } using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Sending")) { new Client().Save(notification, "Notification sent on mobile device"); if (newDocuments.Count > 0) { SavePhotos(); } DisplayAlert("Success", "Notification sent to " + notification.Employee.Name, "OK"); Navigation.PopAsync(); } } void SavePhotos() { List nDocsToSave = new List(); new Client().Save(newDocuments, "Saved from Notifications on Mobile"); foreach (Document doc in newDocuments) { NotificationDocument notificationDocument = new NotificationDocument(); notificationDocument.EntityLink.ID = notification.ID; notificationDocument.DocumentLink.ID = doc.ID; nDocsToSave.Add(notificationDocument); } Task.Run(() => { new Client().Save(nDocsToSave, "Saved from Notifications on Mobile"); }); } void TitleEdt_Changed(object sender, EventArgs e) { notification.Title = titleEdt.Text; } void DescriptionEdt_Changed(object sender, EventArgs e) { notification.Description = descriptionEdt.Text; } #region Photos void TakePhoto_Clicked(object sender, EventArgs e) { TakeAPhoto(); } void ChooseImage_Clicked(object sender, EventArgs e) { ChooseAPhoto(); } private async void TakeAPhoto() { await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await DisplayAlert("No Camera", ":( No camera available.", "OK"); return; } String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now); var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions { Name = filename, CompressionQuality = 10, PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full }); if (file == null) return; using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo")) { Image img = null; var memoryStream = new MemoryStream(); file.GetStream().CopyTo(memoryStream); var data = memoryStream.ToArray(); Document doc = new Document() { FileName = filename, Data = data, CRC = CoreUtils.CalculateCRC(data), TimeStamp = DateTime.Now }; ImageSource src = ImageSource.FromStream(() => new MemoryStream(data)); img = new Image(); img.HeightRequest = 150; img.WidthRequest = 150; img.Aspect = Aspect.AspectFit; img.Source = src; img.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(OnTap), CommandParameter = src, NumberOfTapsRequired = 1 }); newDocuments.Add(doc); file.Dispose(); if (img != null) { Device.BeginInvokeOnMainThread(() => { ImageScroller.IsVisible = true; images.Children.Add(img); }); } } } private async void ChooseAPhoto() { await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsPickPhotoSupported) { await DisplayAlert("No Library", ":( No Photo Library available.", "OK"); return; } var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions() { CompressionQuality = 10, PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full }); if (file == null) return; using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo")) { Image img = null; var memoryStream = new MemoryStream(); file.GetStream().CopyTo(memoryStream); var data = memoryStream.ToArray(); Document doc = new Document() { FileName = Path.GetFileName(file.Path), Data = data, CRC = CoreUtils.CalculateCRC(data), TimeStamp = DateTime.Now }; ImageSource src = ImageSource.FromStream(() => new MemoryStream(data)); img = new Image(); img.HeightRequest = 150; img.WidthRequest = 150; img.Aspect = Aspect.AspectFit; img.Source = src; img.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(OnTap), CommandParameter = src, NumberOfTapsRequired = 1 }); newDocuments.Add(doc); file.Dispose(); if (img != null) { Device.BeginInvokeOnMainThread(() => { ImageScroller.IsVisible = true; images.Children.Add(img); }); } } } private void OnTap(object obj) { ImageViewerEditor viewer = new ImageViewerEditor(obj as ImageSource, false); Navigation.PushAsync(viewer); viewer.OnSaveSelected += ImageViewEditor_OnSaveSelected; } private void ImageViewEditor_OnSaveSelected(byte[] array) { String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now); DataToImage(array, filename); } public void DataToImage(byte[] data, string filename = "") { try { ImageSource src = ImageSource.FromStream(() => new MemoryStream(data)); Image image = new Image(); image.HeightRequest = 200; image.WidthRequest = 200; image.Aspect = Aspect.AspectFit; image.VerticalOptions = LayoutOptions.FillAndExpand; image.HorizontalOptions = LayoutOptions.FillAndExpand; image.Source = src; image.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(OnTap), CommandParameter = src, NumberOfTapsRequired = 1 }); if (image != null) { Device.BeginInvokeOnMainThread(() => { images.Children.Add(image); }); } if (!string.IsNullOrWhiteSpace(filename)) { Document doc = new Document() { FileName = filename, Data = data, CRC = CoreUtils.CalculateCRC(data), TimeStamp = DateTime.Now }; newDocuments.Add(doc); } } catch { } } #endregion } }