StaffStatusPage.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using System;
  2. using System.Collections.Generic;
  3. using Xamarin.Forms;
  4. using InABox.Core;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Syncfusion.XForms.PopupLayout;
  10. using Xamarin.Essentials;
  11. using System.Windows.Input;
  12. using PRSSecurity = InABox.Core.Security;
  13. namespace comal.timesheets
  14. {
  15. class StaffStatus
  16. {
  17. public Guid ID { get; set; }
  18. public String StaffName { get; set; }
  19. public String In { get; set; }
  20. public String Out { get; set; }
  21. public string Mobile { get; set; }
  22. public string StaffLocation { get; set; }
  23. public bool InTimeVisible { get; set; }
  24. public bool OutTimeVisible { get; set; }
  25. public bool InImageVisible { get; set; }
  26. public bool OutImageVisible { get; set; }
  27. public bool HiddenRowVisible { get; set; }
  28. public int HiddenRowHeight { get; set; }
  29. public StaffStatus()
  30. {
  31. ID = Guid.Empty;
  32. StaffName = "";
  33. In = "";
  34. Out = "";
  35. StaffLocation = "";
  36. InTimeVisible = true;
  37. OutTimeVisible = true;
  38. InImageVisible = false;
  39. OutImageVisible = false;
  40. HiddenRowVisible = false;
  41. HiddenRowHeight = 0;
  42. Mobile = "";
  43. }
  44. }
  45. public partial class StaffStatusPage : ContentPage
  46. {
  47. CoreTable employees = null;
  48. CoreTable timesheets = null;
  49. bool bManager = false;
  50. public StaffStatusPage()
  51. {
  52. InitializeComponent();
  53. if (PRSSecurity.IsAllowed<CanViewMobileInOutBoardDetails>())
  54. bManager = true;
  55. ConfigurePage();
  56. }
  57. private void ConfigurePage()
  58. {
  59. NavigationPage.SetHasBackButton(this, false);
  60. ToolbarItems.Clear();
  61. ToolbarItems.Add(new ToolbarItem("Back", "", () =>
  62. {
  63. Navigation.PopAsync();
  64. }));
  65. popupLayout.PopupView.AppearanceMode = AppearanceMode.OneButton;
  66. popupLayout.PopupView.HeaderTitle = "Contact Details";
  67. Title = "In/Out Board";
  68. }
  69. protected override void OnAppearing()
  70. {
  71. base.OnAppearing();
  72. LoadData();
  73. }
  74. private void StaffList_Tapped(object sender, EventArgs e)
  75. {
  76. CreatePopup((StaffList.SelectedItem as StaffStatus).Mobile);
  77. popupLayout.Show();
  78. }
  79. private void CreatePopup(string mobile)
  80. {
  81. popupLayout.PopupView.FooterTemplate = new DataTemplate(() =>
  82. {
  83. return CreateFrame(mobile);
  84. });
  85. popupLayout.PopupView.ContentTemplate = new DataTemplate(() =>
  86. {
  87. return CreatePopupContent(mobile);
  88. });
  89. }
  90. private Label CreatePopupContent(string mobile)
  91. {
  92. return new Label
  93. {
  94. Text = "Mobile: " + mobile,
  95. HorizontalOptions = LayoutOptions.Center,
  96. VerticalOptions = LayoutOptions.Center,
  97. HorizontalTextAlignment = TextAlignment.Center,
  98. VerticalTextAlignment = TextAlignment.Center,
  99. FontAttributes = FontAttributes.Bold,
  100. FontSize = 24
  101. };
  102. }
  103. private Grid CreateGrid(Image img, Label label)
  104. {
  105. var grid = new Grid { BackgroundColor = Color.FromHex("#15C7C1"), RowSpacing = 0, ColumnSpacing = 0, Padding = 0, Margin = 0 };
  106. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  107. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  108. Grid.SetColumn(img, 0);
  109. Grid.SetColumn(label, 1);
  110. grid.Children.Add(img);
  111. grid.Children.Add(label);
  112. return grid;
  113. }
  114. private Image CreateImage(string mobile)
  115. {
  116. Image img = new Image
  117. {
  118. Source = "call.png",
  119. HeightRequest = 40,
  120. WidthRequest = 40,
  121. HorizontalOptions = LayoutOptions.Center,
  122. VerticalOptions = LayoutOptions.Center,
  123. Margin = 0,
  124. };
  125. img.GestureRecognizers.Add(new TapGestureRecognizer
  126. {
  127. Command = CallPerson(mobile)
  128. });
  129. return img;
  130. }
  131. private Label CreateCallLabel(string mobile)
  132. {
  133. Label label = new Label
  134. {
  135. Text = "Call",
  136. FontAttributes = FontAttributes.Bold,
  137. FontSize = 24,
  138. HorizontalOptions = LayoutOptions.FillAndExpand,
  139. VerticalOptions = LayoutOptions.FillAndExpand,
  140. HorizontalTextAlignment = TextAlignment.Start,
  141. VerticalTextAlignment = TextAlignment.Center,
  142. BackgroundColor = Color.FromHex("#15C7C1"),
  143. TextColor = Color.White,
  144. Margin = 0
  145. };
  146. label.GestureRecognizers.Add(new TapGestureRecognizer
  147. {
  148. Command = CallPerson(mobile)
  149. });
  150. return label;
  151. }
  152. private Frame CreateFrame(string mobile)
  153. {
  154. var img = CreateImage(mobile);
  155. var label = CreateCallLabel(mobile);
  156. var grid = CreateGrid(img, label);
  157. Frame frame = new Frame
  158. {
  159. Content = grid,
  160. Padding = 0,
  161. Margin = 0,
  162. BackgroundColor = Color.FromHex("#15C7C1")
  163. };
  164. frame.GestureRecognizers.Add(new TapGestureRecognizer
  165. {
  166. Command = CallPerson(mobile)
  167. });
  168. return frame;
  169. }
  170. private ICommand CallPerson(string mobile)
  171. {
  172. return new Command(() =>
  173. {
  174. try
  175. {
  176. PhoneDialer.Open(mobile);
  177. }
  178. catch (Exception e2)
  179. {
  180. string s = e2.Message;
  181. }
  182. });
  183. }
  184. private void LoadData()
  185. {
  186. Task.Run(() =>
  187. {
  188. ShowLoading();
  189. CoreTable employees = QueryEmployees();
  190. CoreTable timesheets = QueryTimeSheets();
  191. List<StaffStatus> staff = new List<StaffStatus>();
  192. foreach (CoreRow row in employees.Rows)
  193. staff.Add(CreateStatus(row, timesheets));
  194. ShowList(staff);
  195. });
  196. }
  197. private void ShowList(List<StaffStatus> staff)
  198. {
  199. Device.BeginInvokeOnMainThread(() =>
  200. {
  201. loadingColumn.Width = 0;
  202. listViewColumn.Width = new GridLength(1, GridUnitType.Star);
  203. loadingLayout.IsVisible = false;
  204. StaffList.IsVisible = true;
  205. StaffList.ItemsSource = staff;
  206. });
  207. }
  208. private StaffStatus CreateStatus(CoreRow row, CoreTable timesheets)
  209. {
  210. Guid empid = row.Get<Employee, Guid>(x => x.ID);
  211. String name = row.Get<Employee, String>(x => x.Name);
  212. CoreRow startrow = timesheets.Rows.FirstOrDefault(r => r.Get<TimeSheet, Guid>(c => c.EmployeeLink.ID).Equals(empid));
  213. CoreRow finishrow = timesheets.Rows.LastOrDefault(r => r.Get<TimeSheet, Guid>(c => c.EmployeeLink.ID).Equals(empid));
  214. String sIn = "";
  215. String sOut = "";
  216. if (startrow != null)
  217. {
  218. TimeSpan start = startrow.Get<TimeSheet, TimeSpan>(c => c.Start);
  219. TimeSpan finish = finishrow.Get<TimeSheet, TimeSpan>(c => c.Finish);
  220. sIn = String.Format("{0:hh\\:mm} ", start);
  221. if (finish.Ticks > 0)
  222. sOut = String.Format("{0:hh\\:mm}", finish);
  223. }
  224. var status = new StaffStatus()
  225. {
  226. StaffName = row.Get<Employee, String>(x => x.Name),
  227. ID = empid,
  228. In = sIn,
  229. Out = sOut
  230. };
  231. if (!string.IsNullOrWhiteSpace(row.Get<Employee, string>(x => x.Mobile)))
  232. status.Mobile = row.Get<Employee, String>(x => x.Mobile);
  233. if (!bManager)
  234. status = ShowDots(status);
  235. else if (startrow != null)
  236. status = ShowManagerOptions(status, startrow);
  237. return status;
  238. }
  239. private StaffStatus ShowManagerOptions(StaffStatus status, CoreRow startrow)
  240. {
  241. status.StaffLocation = startrow.Get<string>("Address");
  242. if (!string.IsNullOrWhiteSpace(status.StaffLocation))
  243. {
  244. status.HiddenRowVisible = true;
  245. status.HiddenRowHeight = 30;
  246. }
  247. return status;
  248. }
  249. private StaffStatus ShowDots(StaffStatus status)
  250. {
  251. status.InTimeVisible = false;
  252. status.OutTimeVisible = false;
  253. if (!string.IsNullOrWhiteSpace(status.In))
  254. status.InImageVisible = true;
  255. if (!string.IsNullOrWhiteSpace(status.Out))
  256. status.OutImageVisible = true;
  257. return status;
  258. }
  259. private void ShowLoading()
  260. {
  261. Device.BeginInvokeOnMainThread(async () =>
  262. {
  263. for (int i = 0; i < 10; i++)
  264. {
  265. Random random = new Random();
  266. uint number = (uint)random.Next(500, 3000);
  267. await loadingLbl.TranslateTo(0, 15, 500);
  268. await loadingLbl.TranslateTo(0, 0, 500);
  269. loadingLbl.RotateTo(360, number);
  270. await loadingLbl.TranslateTo(0, 15, 500);
  271. await loadingLbl.TranslateTo(0, 0, 500);
  272. await loadingLbl.TranslateTo(0, 15, 500);
  273. await loadingLbl.TranslateTo(0, 0, 500);
  274. await loadingLbl.TranslateTo(0, 15, 500);
  275. number = (uint)random.Next(500, 3000);
  276. await loadingLbl.TranslateTo(0, 0, 500);
  277. loadingLbl.RotateTo(360, number);
  278. }
  279. });
  280. }
  281. private CoreTable QueryEmployees()
  282. {
  283. return new Client<Employee>().Query(
  284. LookupFactory.DefineFilter<Employee>().And
  285. (x => x.Name).IsNotEqualTo("Administrator"),
  286. new Columns<Employee>(x => x.ID, x => x.Name, x => x.Mobile),
  287. new SortOrder<Employee>(x => x.Name)
  288. );
  289. }
  290. private CoreTable QueryTimeSheets()
  291. {
  292. return new Client<TimeSheet>().Query(
  293. new Filter<TimeSheet>(x => x.Date).IsEqualTo(DateTime.Today),
  294. new Columns<TimeSheet>(x => x.EmployeeLink.ID, x => x.Start, x => x.Finish, x => x.Address),
  295. new SortOrder<TimeSheet>(x => x.EmployeeLink.ID).ThenBy(x => x.Start)
  296. );
  297. }
  298. }
  299. }