| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using Plugin.Media;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- namespace comal.timesheets
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class StockTakeCompletionPage : ContentPage
- {
- public delegate void StockTakeCompleted();
- public event StockTakeCompleted OnStockTakeCompleted;
- List<string> favourites = new List<string>
- { "Stocktake complete" };
- List<StockMovement> _movements = new List<StockMovement>();
- Dictionary<Image, Document> imagesDocuments = new Dictionary<Image, Document>();
- public StockTakeCompletionPage(List<StockMovement> movements)
- {
- InitializeComponent();
- _movements = movements;
- AddFavButtons();
- }
- private void AddFavButtons()
- {
- foreach (string s in favourites)
- {
- Button button = new Button
- {
- Text = s,
- TextColor = Color.White,
- BackgroundColor = Color.FromHex("#15C7C1"),
- CornerRadius = 10,
- Margin = 2,
- FontAttributes = FontAttributes.Bold,
- VerticalOptions = LayoutOptions.Center,
- HorizontalOptions = LayoutOptions.Center,
- Padding = new Thickness(6, 3, 6, 3)
- };
- button.Clicked += FavButton_Clicked;
- optionsFlexLayout.Children.Add(button);
- }
- }
- private void FavButton_Clicked(object sender, EventArgs e)
- {
- Button button = sender as Button;
- if (string.IsNullOrWhiteSpace(notesEdt.Text))
- {
- notesEdt.Text = button.Text;
- }
- else
- {
- notesEdt.Text = notesEdt.Text + " " + button.Text;
- }
- }
- private async void SaveBatch_Clicked(object sender, EventArgs e)
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
- {
- StockMovementBatch batch = new StockMovementBatch() { Notes = notesEdt.Text };
- batch.Type = StockMovementBatchType.Stocktake;
- new Client<StockMovementBatch>().Save(batch, "Created on mobile");
- SavePhotos(batch.ID);
- foreach (StockMovement movement in _movements)
- {
- movement.Batch.ID = batch.ID;
- movement.Date = batch.Created;
- }
- Task.Run(() =>
- {
- new Client<StockMovement>().Save(_movements, "Created on mobile device");
- });
- Device.BeginInvokeOnMainThread(async () =>
- {
- saveBatchBtn.IsVisible = false;
- await DisplayAlert("Success", "Batch Saved", "OK");
- OnStockTakeCompleted?.Invoke();
- Navigation.PopAsync();
- });
- }
- }
- #region Photos
- private async void SavePhotos(Guid batchID)
- {
- await Task.Run(() =>
- {
- new Client<Document>().Save(imagesDocuments.Values, "Photo taken on mobile device for stocktake");
- // Link the photos to the batch
- List<StockMovementBatchDocument> stockMovementBatchDocuments = new List<StockMovementBatchDocument>();
- foreach (var doc in imagesDocuments.Values)
- {
- var smd = new StockMovementBatchDocument();
- smd.EntityLink.ID = batchID;
- smd.DocumentLink.ID = doc.ID;
- smd.DocumentLink.FileName = doc.FileName;
- stockMovementBatchDocuments.Add(smd);
- }
- new Client<StockMovementBatchDocument>().Save(stockMovementBatchDocuments, "Photo taken on mobile device for stocktake");
- });
- }
- async void TakePhoto_Clicked(object sender, EventArgs e)
- {
- TakeAPhoto();
- }
- async 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
- });
- imagesDocuments.Add(img, doc);
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- UpdateColours();
- });
- }
- }
- }
- 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
- });
- imagesDocuments.Add(img, doc);
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- UpdateColours();
- });
- }
- }
- }
- private void OnTap(object obj)
- {
- ImageViewer viewer = new ImageViewer(obj as ImageSource);
- Navigation.PushAsync(viewer);
- viewer.ChooseDelete();
- viewer.OnDeleteSelected += () =>
- {
- Image img = imagesDocuments.Keys.First(x => x.Source.Equals(obj as ImageSource));
- imagesDocuments.Remove(img);
- Device.BeginInvokeOnMainThread(() =>
- {
- images.Children.Clear();
- if (imagesDocuments.Count > 0)
- {
- foreach (Image image in imagesDocuments.Keys)
- {
- images.Children.Add(image);
- }
- }
- UpdateColours();
- });
- };
- }
- #endregion
- #region Updating Screen
- private void NotesEdt_Changed(object sender, EventArgs e)
- {
- UpdateColours();
- }
- private void UpdateColours()
- {
- if (imagesDocuments.Values.Count > 0)
- {
- photosFrame.BorderColor = Color.Gray;
- }
- else
- {
- photosFrame.BorderColor = Color.Red;
- }
- if (!string.IsNullOrWhiteSpace(notesEdt.Text))
- {
- if (notesFrame.BorderColor == Color.Red)
- {
- notesFrame.BorderColor = Color.Gray;
- }
- }
- else
- {
- notesFrame.BorderColor = Color.Red;
- }
- ShowSave();
- }
- private void ShowSave()
- {
- if (notesFrame.BorderColor == Color.Gray && photosFrame.BorderColor == Color.Gray)
- {
- saveBatchBtn.IsVisible = true;
- }
- else
- {
- saveBatchBtn.IsVisible = false;
- }
- }
- #endregion
- }
- }
|