DeliveryList.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Xamarin.Forms;
  5. using InABox.Core;
  6. using InABox.Clients;
  7. using Comal.Classes;
  8. using XF.Material.Forms.UI;
  9. using XF.Material.Forms.UI.Dialogs;
  10. using System.Linq;
  11. using comal.timesheets.Deliveries;
  12. using System.Threading;
  13. using System.IO;
  14. using System.Linq.Expressions;
  15. using Xamarin.Essentials;
  16. namespace comal.timesheets
  17. {
  18. public partial class DeliveryList : ContentPage
  19. {
  20. List<DeliveryShell> deliveries = new List<DeliveryShell>();
  21. DateTime Today = DateTime.Today.Date;
  22. bool includeUnbooked = false;
  23. bool firstLoad = true;
  24. public DeliveryList()
  25. {
  26. includeUnbooked = false;
  27. InitializeComponent();
  28. Load();
  29. }
  30. private async void Load()
  31. {
  32. Title = "Refreshing";
  33. await Task.Run(() =>
  34. {
  35. LoadData();
  36. Device.BeginInvokeOnMainThread(async () =>
  37. {
  38. AdjustOpenList();
  39. multiButton.SetFirstButton();
  40. multiButton.ButtonChangedEvent += ButtonChanged;
  41. Title = "Deliveries";
  42. if (firstLoad)
  43. {
  44. await Task.Run(() => { Thread.Sleep(1500); firstLoad = false; });
  45. }
  46. });
  47. });
  48. }
  49. protected override async void OnAppearing()
  50. {
  51. if (firstLoad)
  52. return;
  53. Title = "Refreshing";
  54. await Task.Run(() =>
  55. {
  56. LoadData();
  57. Device.BeginInvokeOnMainThread(() =>
  58. {
  59. ListSelectFromButton();
  60. Title = "Deliveries";
  61. });
  62. });
  63. base.OnAppearing();
  64. }
  65. private void LoadData()
  66. {
  67. deliveries.Clear();
  68. CoreTable table = new Client<Delivery>().Query(
  69. new Filter<Delivery>(x => x.Completed).IsEqualTo(DateTime.MinValue).Or(x => x.Completed).IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)),
  70. new Columns<Delivery>(
  71. x => x.ID, //0
  72. x => x.Number, //1
  73. x => x.Job.Name, //2
  74. x => x.Assignment.Date, //3
  75. x => x.DeliveredBy.Name, //4
  76. x => x.Completed, //5
  77. x => x.Documents, //6
  78. x => x.Location.Latitude, //7
  79. x => x.Location.Longitude, //8
  80. x => x.Due, //9
  81. x => x.Delivered, //10
  82. x => x.Contact.Name, //11
  83. x => x.Location.Address //12
  84. ),
  85. new SortOrder<Delivery>(x => x.Completed, SortDirection.Descending)
  86. );
  87. string emptyString = "";
  88. foreach (var row in table.Rows)
  89. {
  90. List<object> list = row.Values;
  91. if (list[0] == null) { list[0] = Guid.Empty; }
  92. if (list[1] == null) { list[1] = 0; }
  93. if (list[2] == null) { list[2] = emptyString; }
  94. if (list[3] == null) { list[3] = DateTime.MinValue; }
  95. if (list[4] == null) { list[4] = emptyString; }
  96. if (list[5] == null) { list[5] = DateTime.MinValue; }
  97. if (list[6] == null) { list[6] = 0; }
  98. if (list[7] == null) { list[7] = 0; }
  99. if (list[8] == null) { list[8] = 0; }
  100. if (list[9] == null) { list[9] = DateTime.MinValue; }
  101. if (list[10] == null) { list[10] = DateTime.MinValue; }
  102. if (list[11] == null) { list[11] = emptyString; }
  103. if (list[12] == null) { list[12] = emptyString; }
  104. DeliveryShell deliveryShell = new DeliveryShell();
  105. deliveryShell.ID = Guid.Parse(list[0].ToString());
  106. deliveryShell.Number = int.Parse(list[1].ToString());
  107. deliveryShell.JobName = list[2].ToString();
  108. if (list[3].Equals(DateTime.MinValue))
  109. {
  110. deliveryShell.AssignmentDate = "";
  111. }
  112. else
  113. {
  114. deliveryShell.AssignmentDate = DateTime.Parse(list[3].ToString()).ToString("dd MMM yy");
  115. }
  116. deliveryShell.EmployeeName = list[4].ToString();
  117. deliveryShell.Completed = DateTime.Parse(list[5].ToString());
  118. deliveryShell.Documents = int.Parse(list[6].ToString());
  119. deliveryShell.Due = DateTime.Parse(list[9].ToString());
  120. deliveryShell.Delivered = DateTime.Parse(list[10].ToString());
  121. if (deliveryShell.Documents != 0)
  122. {
  123. if (Device.RuntimePlatform.Equals(Device.iOS))
  124. {
  125. deliveryShell.ClipImagePath = "attachments";
  126. }
  127. else deliveryShell.ClipImagePath = "paperclip.png";
  128. }
  129. if (double.Parse(list[7].ToString()) != 0 && double.Parse(list[8].ToString()) != 0)
  130. {
  131. if (Device.RuntimePlatform.Equals(Device.iOS))
  132. {
  133. deliveryShell.LocationImagePath = "locationmarker";
  134. }
  135. else deliveryShell.LocationImagePath = "locationpointer.png";
  136. }
  137. deliveryShell.Latitude = double.Parse(list[7].ToString());
  138. deliveryShell.Longitude = double.Parse(list[8].ToString());
  139. deliveryShell.Contact = list[11].ToString();
  140. if (deliveryShell.Delivered != DateTime.MinValue)
  141. deliveryShell.DisplayDelivered = deliveryShell.Delivered.ToString("dd MMM yy");
  142. deliveryShell.DeliveredAddress = list[12].ToString();
  143. deliveries.Add(deliveryShell);
  144. }
  145. }
  146. private async void Delivery_Clicked(object sender, EventArgs e)
  147. {
  148. var delivery = deliveriesListView.SelectedItem as DeliveryShell;
  149. string loadingMessage = "Loading Delivery " + delivery.Number;
  150. if (delivery.Documents != 0)
  151. {
  152. loadingMessage = loadingMessage + " with " + delivery.Documents + " attached document(s). Please wait for photos to appear.";
  153. }
  154. using (await MaterialDialog.Instance.LoadingDialogAsync(message: loadingMessage))
  155. {
  156. var details = new DeliveryDetails(delivery.ID);
  157. Navigation.PushAsync(details);
  158. }
  159. }
  160. private void AddDelivery_Clicked(object sender, EventArgs e)
  161. {
  162. Delivery delivery = new Delivery();
  163. var details = new DeliveryDetails(delivery.ID);
  164. Navigation.PushAsync(details);
  165. }
  166. private void ButtonChanged(object sender, EventArgs e)
  167. {
  168. ListSelectFromButton();
  169. }
  170. private void ListSelectFromButton()
  171. {
  172. if (multiButton.ButtonSelected != null)
  173. {
  174. if (multiButton.ButtonSelected.Equals(multiButton.Button1Text))
  175. {
  176. AdjustOpenList();
  177. includUnbookedBtn.IsEnabled = true;
  178. }
  179. if (multiButton.ButtonSelected.Equals(multiButton.Button2Text))
  180. {
  181. deliveriesListView.ItemsSource = deliveries.Where(x => x.Delivered.Date.Equals(Today));
  182. includUnbookedBtn.IsEnabled = false;
  183. }
  184. if (multiButton.ButtonSelected.Equals(multiButton.Button3Text))
  185. {
  186. deliveriesListView.ItemsSource = deliveries.Where(x => x.Delivered >= (DateTime.Today.AddDays(-7)) || x.Completed >= (DateTime.Today.AddDays(-7)));
  187. includUnbookedBtn.IsEnabled = false;
  188. }
  189. }
  190. }
  191. private void Image_Tapped(object sender, EventArgs e)
  192. {
  193. var shell = ((TappedEventArgs)e).Parameter as DeliveryShell;
  194. if (shell == null)
  195. return;
  196. if (!string.IsNullOrWhiteSpace(shell.DeliveredAddress))
  197. {
  198. popupLayout.PopupView.ShowHeader = false;
  199. popupLayout.PopupView.ShowFooter = false;
  200. popupLayout.PopupView.ContentTemplate = new DataTemplate(() =>
  201. {
  202. Label lbl = new Label();
  203. lbl.Margin = new Thickness(10);
  204. lbl.FontSize = Device.GetNamedSize(NamedSize.Medium, lbl);
  205. lbl.FontAttributes = FontAttributes.Bold;
  206. lbl.HorizontalOptions = LayoutOptions.Center;
  207. lbl.VerticalOptions = LayoutOptions.Center;
  208. lbl.HorizontalTextAlignment = TextAlignment.Center;
  209. lbl.LineBreakMode = LineBreakMode.WordWrap;
  210. lbl.Text = shell.DeliveredAddress + " at " + shell.Delivered.ToString("hh:mm:ss tt dd MMM yy") + System.Environment.NewLine
  211. + "by: " + shell.EmployeeName;
  212. Button btn = new Button
  213. {
  214. BackgroundColor = Color.FromHex("#15C7C1"),
  215. Text = "View in Maps",
  216. FontAttributes = FontAttributes.Bold,
  217. TextColor = Color.White,
  218. CornerRadius = 10,
  219. HorizontalOptions = LayoutOptions.Center,
  220. };
  221. btn.FontSize = Device.GetNamedSize(NamedSize.Medium, btn);
  222. btn.Clicked += async (object sender, EventArgs e) =>
  223. {
  224. var location = new Xamarin.Essentials.Location(shell.Latitude, shell.Longitude);
  225. var options = new MapLaunchOptions { };
  226. await Map.OpenAsync(location, options);
  227. };
  228. Image img = new Image
  229. {
  230. HorizontalOptions = LayoutOptions.Center,
  231. HeightRequest = 40,
  232. WidthRequest = 40
  233. };
  234. if (Device.RuntimePlatform.Equals(Device.iOS))
  235. {
  236. img.Source = "locationmarker.png";
  237. }
  238. else img.Source = "locationpointer.png";
  239. img.Margin = new Thickness(10, 30, 10, 10);
  240. StackLayout stack = new StackLayout();
  241. stack.Margin = new Thickness(20, 50, 20, 50);
  242. stack.Children.Add(img);
  243. stack.Children.Add(lbl);
  244. stack.Children.Add(btn);
  245. return stack;
  246. });
  247. popupLayout.Show();
  248. }
  249. }
  250. private void AdjustOpenList()
  251. {
  252. if (includeUnbooked)
  253. {
  254. deliveriesListView.ItemsSource = deliveries.Where(x => x.Delivered.Equals(DateTime.MinValue));
  255. }
  256. else if (!includeUnbooked)
  257. {
  258. var list = deliveries.Where(x => x.Delivered.Equals(DateTime.MinValue));
  259. List<DeliveryShell> excludeUnbooked = new List<DeliveryShell>();
  260. foreach (DeliveryShell shell in list)
  261. {
  262. if (!string.IsNullOrWhiteSpace(shell.AssignmentDate))
  263. {
  264. excludeUnbooked.Add(shell);
  265. }
  266. }
  267. deliveriesListView.ItemsSource = excludeUnbooked;
  268. }
  269. }
  270. private void IncludeUnbookedBtn_Clicked(object sender, EventArgs e)
  271. {
  272. if (includeUnbooked)
  273. {
  274. includeUnbooked = false;
  275. includUnbookedBtn.BackgroundColor = Color.FromHex("#15C7C1");
  276. includUnbookedBtn.Text = "Include Unbooked (Off)";
  277. }
  278. else if (!includeUnbooked)
  279. {
  280. includeUnbooked = true;
  281. includUnbookedBtn.BackgroundColor = Color.Gray;
  282. includUnbookedBtn.Text = "Include Unbooked (On)";
  283. }
  284. AdjustOpenList();
  285. }
  286. }
  287. }