BasePage.xaml.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using InABox.Clients;
  6. using InABox.Core;
  7. using InABox.Mobile;
  8. using Plugin.Media;
  9. using Syncfusion.XForms.PopupLayout;
  10. using Xamarin.Forms;
  11. using Xamarin.Forms.Xaml;
  12. using XF.Material.Forms.UI.Dialogs;
  13. namespace PRS.Mobile
  14. {
  15. [XamlCompilation(XamlCompilationOptions.Compile)]
  16. public partial class BasePage
  17. {
  18. public IList<View> AlternateMenu => _alternateMenu.Children;
  19. public IList<View> PrimaryMenu => _primaryMenu.Children;
  20. public IList<View> PageContent => _content.Children;
  21. public bool BackButtonEnabled
  22. {
  23. get => _backButton.IsVisible;
  24. set => _backButton.IsVisible = value;
  25. }
  26. public event EventHandler BackButtonClicked;
  27. private SfPopupLayout _popup = new SfPopupLayout();
  28. public BasePage()
  29. {
  30. InitializeComponent();
  31. _titleBar.BackgroundColor = XF.Material.Forms.Material.Color.Primary;
  32. _titleLabel.TextColor = XF.Material.Forms.Material.Color.OnPrimary;
  33. BackgroundColor = Color.WhiteSmoke;
  34. }
  35. protected override void OnAppearing()
  36. {
  37. XF.Material.Forms.Material.PlatformConfiguration.ChangeStatusBarColor(XF.Material.Forms.Material.Color.PrimaryVariant);
  38. App.Data.TransportConnected += TransportConnected;
  39. App.Data.TransportDisconnected += TransportDisconnected;
  40. UpdateTransportStatus();
  41. base.OnAppearing();
  42. }
  43. protected override void OnDisappearing()
  44. {
  45. App.Data.TransportConnected -= TransportConnected;
  46. App.Data.TransportDisconnected -= TransportDisconnected;
  47. base.OnDisappearing();
  48. }
  49. private void TransportDisconnected(TransportDisconnectedEventArgs args) => UpdateConnectionStatus();
  50. private void TransportConnected(TransportConnectedEventArgs args) => UpdateConnectionStatus();
  51. private void UpdateConnectionStatus()
  52. {
  53. Device.BeginInvokeOnMainThread(UpdateTransportStatus);
  54. }
  55. //private static Task<IMaterialModalPage> _snackbar = null;
  56. protected virtual void UpdateTransportStatus()
  57. {
  58. // ConnectionIndicator.Image = App.Data.IsConnected()
  59. // ? ImageSource.FromFile("transparent.png")
  60. // : ImageSource.FromFile("disconnected.png"); //ImageSource.FromFile("Images/nodata.png");
  61. _connectionIndicator.IsVisible = !App.Data.IsConnected();
  62. // if (!App.Data.IsConnected() && (_snackbar == null))
  63. // _snackbar = MaterialDialog.Instance.LoadingSnackbarAsync(message: "No Connection Available!");
  64. //
  65. // else if (App.Data.IsConnected() && (_snackbar != null) && (_snackbar.Result != null))
  66. // {
  67. // _snackbar.Result.DismissAsync();
  68. // _snackbar = null;
  69. // }
  70. }
  71. private void _backButton_OnClicked(object sender, EventArgs e)
  72. {
  73. if (OnBackButtonPressed())
  74. {
  75. BackButtonClicked?.Invoke(this, EventArgs.Empty);
  76. Navigation.PopAsync();
  77. }
  78. }
  79. protected override bool OnBackButtonPressed()
  80. {
  81. return true;
  82. }
  83. protected void ShowPopup(Func<View> view, int height = 500, int width = 300, int padding = 10)
  84. {
  85. _popup.PopupView.HeightRequest = height;
  86. _popup.PopupView.WidthRequest = width;
  87. _popup.PopupView.ShowHeader = false;
  88. _popup.PopupView.ShowFooter = false;
  89. _popup.PopupView.ContentTemplate = new DataTemplate(() =>
  90. {
  91. Grid grid = new Grid() { Margin = padding, Padding = padding};
  92. grid.Children.Add(view());
  93. return grid;
  94. });
  95. _popup.Show();
  96. }
  97. protected void DismissPopup()
  98. {
  99. _popup.Dismiss();
  100. }
  101. public bool ProgressVisible
  102. {
  103. get => activity_indicator.IsRunning;
  104. set => activity_indicator.IsRunning = value;
  105. }
  106. protected async Task<T> TakePhoto<T>(Func<T> shell) where T : IEntityDocumentShell
  107. {
  108. T result = default(T);
  109. await CrossMedia.Current.Initialize();
  110. if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  111. {
  112. await DisplayAlert("No Camera", ":( No camera available.", "OK");
  113. return result;
  114. }
  115. String filename = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}.png";
  116. var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  117. {
  118. Name = filename,
  119. CompressionQuality = 5,
  120. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Large
  121. });
  122. if (file == null)
  123. return result;
  124. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  125. {
  126. byte[] data;
  127. await using (var stream = file.GetStream())
  128. {
  129. BinaryReader br = new BinaryReader(stream);
  130. data = br.ReadBytes((int)stream.Length);
  131. }
  132. Document doc = new Document()
  133. {
  134. FileName = filename,
  135. Data = data,
  136. CRC = CoreUtils.CalculateCRC(data),
  137. TimeStamp = DateTime.Now
  138. };
  139. new Client<Document>().Save(doc, "Photo taken on mobile device");
  140. result = shell();
  141. result.DocumentID = doc.ID;
  142. result.FileName = doc.FileName;
  143. result.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(doc.Data, 128, 128);
  144. result.Save("Photo taken on mobile device");
  145. }
  146. return result;
  147. }
  148. protected async Task<T> ChoosePhoto<T>(Func<T> shell) where T : IEntityDocumentShell
  149. {
  150. var result = default(T);
  151. await CrossMedia.Current.Initialize();
  152. if (!CrossMedia.Current.IsPickPhotoSupported)
  153. {
  154. await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
  155. return result;
  156. }
  157. var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  158. {
  159. CompressionQuality = 5,
  160. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Large
  161. });
  162. if (file == null)
  163. return result;
  164. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  165. {
  166. byte[] data;
  167. await using (var stream = file.GetStream())
  168. {
  169. BinaryReader br = new BinaryReader(stream);
  170. data = br.ReadBytes((int)stream.Length);
  171. }
  172. Document doc = new Document()
  173. {
  174. FileName = file.OriginalFilename,
  175. Data = data,
  176. CRC = CoreUtils.CalculateCRC(data),
  177. TimeStamp = DateTime.Now
  178. };
  179. new Client<Document>().Save(doc, "Photo chosen from mobile library");
  180. result = shell();
  181. result.DocumentID = doc.ID;
  182. result.FileName = doc.FileName;
  183. result.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(doc.Data, 128, 128);
  184. result.Save("Photo chosen from mobile library");
  185. }
  186. return result;
  187. }
  188. }
  189. }