| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading.Tasks;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Mobile;
- using Plugin.Media;
- using Syncfusion.XForms.PopupLayout;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- namespace PRS.Mobile
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class BasePage
- {
- public IList<View> AlternateMenu => _alternateMenu.Children;
-
- public IList<View> PrimaryMenu => _primaryMenu.Children;
-
- public IList<View> PageContent => _content.Children;
- public bool BackButtonEnabled
- {
- get => _backButton.IsVisible;
- set => _backButton.IsVisible = value;
- }
- public event EventHandler BackButtonClicked;
-
- private SfPopupLayout _popup = new SfPopupLayout();
-
- public BasePage()
- {
- InitializeComponent();
- _titleBar.BackgroundColor = XF.Material.Forms.Material.Color.Primary;
- _titleLabel.TextColor = XF.Material.Forms.Material.Color.OnPrimary;
- BackgroundColor = Color.WhiteSmoke;
- }
-
- protected override void OnAppearing()
- {
- XF.Material.Forms.Material.PlatformConfiguration.ChangeStatusBarColor(XF.Material.Forms.Material.Color.PrimaryVariant);
- App.Data.TransportConnected += TransportConnected;
- App.Data.TransportDisconnected += TransportDisconnected;
- UpdateTransportStatus();
- base.OnAppearing();
- }
- protected override void OnDisappearing()
- {
- App.Data.TransportConnected -= TransportConnected;
- App.Data.TransportDisconnected -= TransportDisconnected;
- base.OnDisappearing();
- }
- private void TransportDisconnected(TransportDisconnectedEventArgs args) => UpdateConnectionStatus();
- private void TransportConnected(TransportConnectedEventArgs args) => UpdateConnectionStatus();
-
- private void UpdateConnectionStatus()
- {
- Device.BeginInvokeOnMainThread(UpdateTransportStatus);
- }
-
- //private static Task<IMaterialModalPage> _snackbar = null;
-
- protected virtual void UpdateTransportStatus()
- {
- // ConnectionIndicator.Image = App.Data.IsConnected()
- // ? ImageSource.FromFile("transparent.png")
- // : ImageSource.FromFile("disconnected.png"); //ImageSource.FromFile("Images/nodata.png");
- _connectionIndicator.IsVisible = !App.Data.IsConnected();
-
- // if (!App.Data.IsConnected() && (_snackbar == null))
- // _snackbar = MaterialDialog.Instance.LoadingSnackbarAsync(message: "No Connection Available!");
- //
- // else if (App.Data.IsConnected() && (_snackbar != null) && (_snackbar.Result != null))
- // {
- // _snackbar.Result.DismissAsync();
- // _snackbar = null;
- // }
- }
- private void _backButton_OnClicked(object sender, EventArgs e)
- {
- if (OnBackButtonPressed())
- {
- BackButtonClicked?.Invoke(this, EventArgs.Empty);
- Navigation.PopAsync();
- }
- }
- protected override bool OnBackButtonPressed()
- {
- return true;
- }
- protected void ShowPopup(Func<View> view, int height = 500, int width = 300, int padding = 10)
- {
- _popup.PopupView.HeightRequest = height;
- _popup.PopupView.WidthRequest = width;
- _popup.PopupView.ShowHeader = false;
- _popup.PopupView.ShowFooter = false;
- _popup.PopupView.ContentTemplate = new DataTemplate(() =>
- {
- Grid grid = new Grid() { Margin = padding, Padding = padding};
- grid.Children.Add(view());
- return grid;
- });
- _popup.Show();
- }
- protected void DismissPopup()
- {
- _popup.Dismiss();
- }
- public bool ProgressVisible
- {
- get => activity_indicator.IsRunning;
- set => activity_indicator.IsRunning = value;
- }
-
- protected async Task<T> TakePhoto<T>(Func<T> shell) where T : IEntityDocumentShell
- {
- T result = default(T);
-
- await CrossMedia.Current.Initialize();
- if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
- {
- await DisplayAlert("No Camera", ":( No camera available.", "OK");
- return result;
- }
- String filename = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}.png";
- var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
- {
- Name = filename,
- CompressionQuality = 5,
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Large
- });
- if (file == null)
- return result;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- byte[] data;
- await using (var stream = file.GetStream())
- {
- BinaryReader br = new BinaryReader(stream);
- data = br.ReadBytes((int)stream.Length);
- }
-
- Document doc = new Document()
- {
- FileName = filename,
- Data = data,
- CRC = CoreUtils.CalculateCRC(data),
- TimeStamp = DateTime.Now
- };
- new Client<Document>().Save(doc, "Photo taken on mobile device");
-
- result = shell();
- result.DocumentID = doc.ID;
- result.FileName = doc.FileName;
- result.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(doc.Data, 128, 128);
- result.Save("Photo taken on mobile device");
- }
- return result;
- }
- protected async Task<T> ChoosePhoto<T>(Func<T> shell) where T : IEntityDocumentShell
- {
- var result = default(T);
- await CrossMedia.Current.Initialize();
-
- if (!CrossMedia.Current.IsPickPhotoSupported)
- {
- await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
- return result;
- }
-
- var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
- {
- CompressionQuality = 5,
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Large
- });
-
- if (file == null)
- return result;
-
-
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
-
- byte[] data;
- await using (var stream = file.GetStream())
- {
- BinaryReader br = new BinaryReader(stream);
- data = br.ReadBytes((int)stream.Length);
- }
-
- Document doc = new Document()
- {
- FileName = file.OriginalFilename,
- Data = data,
- CRC = CoreUtils.CalculateCRC(data),
- TimeStamp = DateTime.Now
- };
- new Client<Document>().Save(doc, "Photo chosen from mobile library");
-
- result = shell();
- result.DocumentID = doc.ID;
- result.FileName = doc.FileName;
- result.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(doc.Data, 128, 128);
- result.Save("Photo chosen from mobile library");
-
- }
- return result;
- }
-
- }
- }
|