123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- 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<Document> newDocuments = new List<Document>();
- Notification notification = new Notification();
- public NewReplyNotification(Guid id = new Guid())
- {
- InitializeComponent();
- NavigationPage.SetHasBackButton(this, false);
- notificationID = id;
- notification.Sender.ID = GlobalVariables.EmpID;
- if (GlobalVariables.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<Notification>().Query(
- new Filter<Notification>(x => x.ID).IsEqualTo(notificationID),
- new Columns<Notification>(
- 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<Notification>();
- 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<NotificationDocument>().Query(
- new Filter<NotificationDocument>(x => x.EntityLink.ID).IsEqualTo(notificationID),
- new Columns<NotificationDocument>(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<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(Guid.Parse(row.Values[1].ToString())), new Columns<Document>(x => x.Data));
- DataToImage(table2.Rows.FirstOrDefault().ToObject<Document>().Data);
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- photosLbl.Text = "Photos";
- photosLbl.TextColor = Color.Default;
- });
- }
- });
- }
- void SelectRecipientBtn_Clicked(object sender, EventArgs e)
- {
- EmployeeSelectionPage employeeSelectionPage = new EmployeeSelectionPage();
- employeeSelectionPage.OnItemSelected += () =>
- {
- recipientNameLbl.Text = employeeSelectionPage.employee.Name;
- notification.Employee.ID = employeeSelectionPage.employee.ID;
- notification.Employee.Name = employeeSelectionPage.employee.Name;
- };
- Navigation.PushAsync(employeeSelectionPage);
- }
- 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<Notification>().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<NotificationDocument> nDocsToSave = new List<NotificationDocument>();
- new Client<Document>().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<NotificationDocument>().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
- }
- }
|