123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using InABox.Clients;
- using InABox.Core;
- using Comal.Classes;
- using Xamarin.Forms;
- using System.Linq;
- using System.Threading.Tasks;
- using InABox.Configuration;
- using Syncfusion.SfSchedule.XForms;
- using XF.Material.Forms;
- using XF.Material.Forms.UI.Dialogs;
- using XF.Material.Forms.UI.Dialogs.Configurations;
- using PRSSecurity = InABox.Core.Security;
- namespace comal.timesheets
- {
- public enum AssignmentView
- {
- Day,
- TimeLine
- }
- public enum AssignmentLookupType
- {
- ActiveJobs,
- UnbookedJobs,
- Tasks
- }
- public partial class AssignmentList
- {
- public AssignmentModel DataModel { get; private set; }
-
- private AssignmentEdit _editor = null;
- private Guid[] _employeeids = new Guid[] { };
- private AssignmentModuleSettings _settings = null;
- private AssignmentView _view = AssignmentView.Day;
- private AssignmentLookupType _lookuptype = AssignmentLookupType.UnbookedJobs;
- private String _teamname = "";
- public AssignmentList()
- {
- InitializeComponent();
-
- DataModel = new AssignmentModel(App.Data, GetFilter, true);
- BindingContext = DataModel;
-
- _settings = new LocalConfiguration<AssignmentModuleSettings>().Load();
- DatePicker.Date = _settings.Date.IsEmpty() ? DateTime.Today : _settings.Date;
- _view = _settings.View;
- _employeeids = (_settings.Employees != null)
- ? _settings.Employees
- : new Guid[] { App.Data.Me.ID };
- _lookuptype = _settings.LookupType;
- LookupType.Text = CoreUtils.Neatify(_lookuptype.ToString());
- _teamname = String.IsNullOrWhiteSpace(_settings.TeamName) ? App.Data.Me.Name : _settings.TeamName;
-
- ShowHideSources.IsVisible = PRSSecurity.IsAllowed<CanViewMobileAssignmentsSidebar>();
- }
- protected override void OnAppearing()
- {
- base.OnAppearing();
- RefreshLookups();
- Reload();
- }
-
- private void RefreshLookups()
- {
- if (_lookuptype == AssignmentLookupType.Tasks)
- {
- App.Data.KanbanLookups.Refresh(false, () =>
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- Lookups.ItemsSource = new ObservableCollection<KanbanLookupShell>(App.Data.KanbanLookups);
- });
- });
- }
- else if (_lookuptype == AssignmentLookupType.ActiveJobs)
- {
- App.Data.JobLookups.Refresh(false, () =>
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- Lookups.ItemsSource = new ObservableCollection<JobLookupShell>(App.Data.JobLookups);
- });
- });
- }
- else if (_lookuptype == AssignmentLookupType.UnbookedJobs)
- {
- App.Data.JobLookups.Refresh(false, () =>
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- Lookups.ItemsSource = new ObservableCollection<JobLookupShell>(App.Data.JobLookups.Where(x=>x.OpenAssignments == 0));
- });
- });
- }
- }
- private void Reload()
- {
- DayView.DataSource = null;
- TimeLineView.DataSource = null;
- ScheduleType.Text = _teamname;
- if (_view == AssignmentView.Day)
- {
- TimelineFrame.IsVisible = false;
- DayFrame.IsVisible = true;
- DayView.DayViewSettings.DayLabelSettings.TimeFormat = "HH:mm";
- }
- else
- {
- DayFrame.IsVisible = false;
- TimelineFrame.IsVisible = true;
-
- TimeLineView.ResourceViewSettings.VisibleResourceCount = Math.Max(1, Math.Min(16, _employeeids.Length));
- TimeLineView.TimelineViewSettings.AppointmentHeight =
- (this.Height / TimeLineView.ResourceViewSettings.VisibleResourceCount) + 100;
- var resources = new ObservableCollection<object>();
- foreach (var empid in _employeeids)
- {
- var empname = App.Data.Employees.Items.FirstOrDefault(x => x.ID == empid)?.Name ?? empid.ToString();
- if (_employeeids.Length > 10)
- empname = String.Join("", empname.Split(' ').Select(x => x.Substring(0, 1)));
- else if (_employeeids.Length > 6)
- {
- var comps = empname.Split(' ').ToArray();
- empname = $"{comps.First()} {String.Join("", comps.Skip(1).Select(x => x.Substring(0, 1)))}";
- }
- resources.Add(
- new ScheduleResource()
- {
- Name = empname,
- Id = empid,
- Color = Color.Transparent,
- Image = ""
- }
- );
- }
- TimeLineView.ScheduleResources = resources;
- TimeLineView.ShowResourceView = true;
-
- }
- RefreshData();
- }
- private Filter<Assignment> GetFilter() =>
- new Filter<Assignment>(x => x.Date).IsEqualTo(DatePicker.Date)
- .And(x => x.EmployeeLink.ID).InList(_employeeids);
- private void RefreshData()
- {
- DataModel.Refresh(true, () => Device.BeginInvokeOnMainThread(UpdateScreen));
- }
- private void UpdateScreen()
- {
- Title = $"{DatePicker.Date:dd MMMM yyyy}";
- if (_view == AssignmentView.Day)
- {
- DayView.DataSource = new ObservableCollection<AssignmentShell>(DataModel.Items);
- DayView.MoveToDate = ShowRelevantTime();
- }
- else
- {
- TimeLineView.DataSource = new ObservableCollection<AssignmentShell>(DataModel.Items);
- TimeLineView.MoveToDate = ShowRelevantTime();
- }
- }
- private DateTime ShowRelevantTime()
- {
- return (DataModel.Items.Count > 0 ? (DataModel.Items.First() as AssignmentShell).StartTime : DateTime.Now).AddMinutes(-30);
- }
-
- private void Lookups_OnItemTapped(object sender, ItemTappedEventArgs e)
- {
- if (e.Item is ILookupShell lookup)
- {
- if (lookup.Selected)
- lookup.Selected = false;
- else
- {
- IList<ILookupShell> lookups = _lookuptype == AssignmentLookupType.Tasks
- ? App.Data.KanbanLookups.Items.ToArray<ILookupShell>()
- : App.Data.JobLookups.Items.ToArray<ILookupShell>();
- {
- foreach (var other in lookups.Where(x => x.Selected).ToArray())
- other.Selected = false;
- }
- lookup.Selected = true;
- }
- }
- }
- private void LookupsType_Tapped(object sender, EventArgs e)
- {
- _lookuptype = _lookuptype == AssignmentLookupType.Tasks
- ? AssignmentLookupType.ActiveJobs
- : _lookuptype == AssignmentLookupType.ActiveJobs
- ? AssignmentLookupType.UnbookedJobs
- : AssignmentLookupType.Tasks;
- _settings.LookupType = _lookuptype;
- LookupType.Text = CoreUtils.Neatify(_lookuptype.ToString());
- new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
- RefreshLookups();
- }
- private async void Schedule_OnCellTapped(object sender, CellTappedEventArgs e)
- {
- if (e.Appointment is AssignmentShell item)
- {
- if (item.DeliveryID != Guid.Empty)
- {
- var delivery = new DeliveryDetails() { DeliveryID = item.DeliveryID};
- Navigation.PushAsync(delivery);
- }
- else
- {
- var editor = new AssignmentEdit(item);
- Navigation.PushAsync(editor);
- }
- }
- }
- private async void Schedule_OnCellLongPressed(object sender, CellTappedEventArgs e)
- {
- if (e.Appointment == null)
- {
- if (InABox.Core.Security.CanEdit<Assignment>())
- {
- CreateAssignment(
- e.Datetime,
- e.Resource as ScheduleResource
- );
- }
- }
- else if (InABox.Core.Security.CanDelete<Assignment>()
- && e.Appointment is AssignmentShell assignment)
- {
- await DeleteAssignment(assignment.Id);
- }
- }
- private void CreateAssignment(DateTime date, ScheduleResource resource)
- {
- var assignment = new Assignment()
- {
- Date = date.Date,
- Title = "New Assignment",
- };
- assignment.Booked.Start = new TimeSpan(date.TimeOfDay.Hours, 0, 0);
- assignment.Booked.Finish = date.TimeOfDay.Add(new TimeSpan(1, 0, 0));
- assignment.Booked.Duration = new TimeSpan(1, 0, 0);
- assignment.EmployeeLink.ID = (resource is ScheduleResource sr)
- ? (Guid)sr.Id
- : App.Data.Me.ID;
- var job = (_lookuptype == AssignmentLookupType.ActiveJobs) || (_lookuptype == AssignmentLookupType.UnbookedJobs)
- ? App.Data.JobLookups.Items.FirstOrDefault(x => x.Selected)
- : null;
- if (job != null)
- {
- assignment.JobLink.ID = job.ID;
- assignment.JobLink.JobNumber = job.Code;
- assignment.JobLink.Name = job.Description;
- assignment.Description = job.Notes;
- assignment.Title = job.Description;
- job.Selected = false;
- }
- var task = _lookuptype == AssignmentLookupType.Tasks
- ? App.Data.KanbanLookups.Items.FirstOrDefault(x => x.Selected)
- : null;
- if (task != null)
- {
- assignment.Task.ID = task.ID;
- assignment.Task.Number = int.Parse(task.Code);
- assignment.Task.Title = task.Description;
- assignment.Title = task.Notes;
- assignment.Description = task.Description;
- task.Selected = false;
- }
- var editor = new AssignmentEdit(assignment);
- Navigation.PushAsync(editor);
- }
- private async Task DeleteAssignment(Guid id)
- {
- var confirm = await MaterialDialog.Instance.ConfirmAsync(
- "Are you sure you wish to delete this assignment?",
- "Confirm Deletion",
- "Yes, Delete",
- "Cancel",
- new MaterialAlertDialogConfiguration()
- {
- ButtonFontFamily = Material.FontFamily.Body2
- }
- );
- if (confirm == true)
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Deleting Assignment"))
- {
- var assignment = new Assignment() { ID = id };
- new Client<Assignment>().Delete(assignment, "Deleted on Mobile Device");
- }
- RefreshData();
- }
- }
- private void SelectDate_OnClicked(object sender, EventArgs e)
- {
- DatePicker.Focus();
- }
-
- private void DatePicker_OnDateSelected(object sender, DateChangedEventArgs e)
- {
- if (_employeeids.Any())
- {
- _settings.Date = DatePicker.Date;
- new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
- }
- DayView.MoveToDate = DatePicker.Date;
- TimeLineView.MoveToDate = DatePicker.Date;
- RefreshData();
- }
- private async void SelectEmployees_OnClicked(object sender, EventArgs e)
- {
- var actions = new List<string>() { "Only Me" };
- //actions.AddRange(App.Data.TeamEmployeeShells.Where(x=>x.ID == App.Data.Employee.ID).Select(x=>x.TeamName).Distinct());
- actions.AddRange(App.Data.EmployeeTeams.Select(x => x.TeamName).Distinct());
- var result = await MaterialDialog.Instance.SelectActionAsync(title: "Select a Team",
- actions: actions);
- if (result == 0)
- {
- _view = AssignmentView.Day;
- _employeeids = new Guid[] { App.Data.Me.ID };
- _teamname = App.Data.Me.Name;
- }
- else if (result > 0)
- {
- _view = AssignmentView.TimeLine;
- _employeeids = App.Data.EmployeeTeams.Where(x => String.Equals(x.TeamName, actions[result]))
- .Select(x => x.ID).Distinct().ToArray();
- _teamname = actions[result];
- }
- _settings.Employees = _employeeids;
- _settings.View = _view;
- _settings.TeamName = _teamname;
- new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
- Dispatcher.BeginInvokeOnMainThread(() =>
- {
- Reload();
- });
- }
- private bool _sidebarvisible = false;
-
- private void ShowHideSources_OnClicked(object sender, EventArgs e)
- {
- _sidebarvisible = !_sidebarvisible;
- ShowHideSources.Source = _sidebarvisible
- ? ImageSource.FromFile("rightarrowwhite.png")
- : ImageSource.FromFile("downarrowwhite.png");
- JobColumn.Width = _sidebarvisible
- ? new GridLength(150, GridUnitType.Absolute)
- : new GridLength(0, GridUnitType.Absolute);
- }
- }
- }
|