using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; using comal.timesheets.Tasks; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI; using XF.Material.Forms.UI.Dialogs; using System.Threading; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class TasksList { Color color = Color.FromHex("#15C7C1"); int currentCategory = 0; bool searching = false; bool firstLoad = true; bool tapping = false; bool doubleTap = false; public TasksList() { InitializeComponent(); LoadPreferences(); searchEnt.IsEnabled = false; App.Data.Kanbans.Refresh( false, () => Device.BeginInvokeOnMainThread(DisplayList) ); } private void LoadPreferences() { try { if (!App.Current.Properties.ContainsKey("ObserverPreference")) { App.Current.Properties.Add("ObserverPreference", "True"); observerSwitch.IsToggled = true; } else { observerSwitch.IsToggled = Convert.ToBoolean(App.Current.Properties["ObserverPreference"]); } if (!App.Current.Properties.ContainsKey("PromptedOnUsage")) { App.Current.Properties.Add("PromptedOnUsage", "True"); DisplayAlert("One-time Prompt:", "Single Tap to Open Task," + System.Environment.NewLine + "Double Tap to Complete Task", "OK"); } } catch { } } private void DisplayList() { App.Data.Kanbans.Search((shell) => { bool bResult = true; bResult = bResult && (App.Current.Properties["ObserverPreference"].Equals("True") || !shell.IsObserver); bResult = bResult && currentCategory switch { 0 => shell.Category == "To Do", 1 => shell.Category == "Current", 2 => shell.Category == "Waiting", _ => false }; bResult = bResult && ( String.IsNullOrWhiteSpace(searchEnt.Text) || String.Equals(shell.Number.ToString(), searchEnt.Text) || shell.ManagerName.ToUpper().Contains(searchEnt.Text.ToUpper()) || shell.Title.ToUpper().Contains(searchEnt.Text.ToUpper()) || shell.Summary.ToUpper().Contains(searchEnt.Text.ToUpper()) || shell.DueDate.ToString().Contains(searchEnt.Text) ); return bResult; }); searchEnt.IsEnabled = true; observerSwitch.IsEnabled = true; buttonToDo.Text = "To Do (" + App.Data.Kanbans.Items.Count(x => x.Category == "To Do") + ")"; buttonCurrent.Text = "Current (" + App.Data.Kanbans.Items.Count(x => x.Category == "Current") + ")"; buttonWaiting.Text = "Waiting (" + App.Data.Kanbans.Items.Count(x => x.Category == "Waiting") + ")"; } private string ChooseCategory(string _category) { switch (_category) { case "Open": _category = "To Do"; break; case "In Progress": _category = "Current"; break; default: break; } return _category; } private void AddTask_Clicked(object sender, EventArgs e) { try { AddEditTask addEditTask = new AddEditTask(); Navigation.PushAsync(addEditTask); } catch { } } async void KanbanList_Tapped(object sender, EventArgs e) { try { if (tapping) { doubleTap = true; var selectedTask = taskListView.SelectedItem as KanbanShell; if (selectedTask.Locked) { doubleTap = false; DisplayAlert("Alert", "Unable to complete locked task", "Cancel"); return; } string chosenOption = await DisplayActionSheet("Complete Task?", "Cancel", null, "Yes", "No"); switch (chosenOption) { case "Cancel": break; case "No": break; case "Yes": CompleteTask(selectedTask); break; default: break; } doubleTap = false; } else { tapping = true; Timer t = new Timer(TimerCallBack, null, 750, Timeout.Infinite); } } catch { } } private async void CompleteTask(KanbanShell shell) { // try // { // kanbanSubscriberShells.Remove(shell); // RefreshButtons(kanbanSubscriberShells); // DisplayList(kanbanSubscriberShells); // await Task.Run(() => // { // CoreTable table = new Client().Query // ( // new Filter(x => x.ID).IsEqualTo(shell.ID), // new Columns(x => x.ID, x => x.Category) // ); // Kanban kanban = table.Rows.First().ToObject(); // kanban.Category = "Complete"; // new Client().Save(kanban, "Completed from mobile device"); // }); // } // catch { } } private async void TimerCallBack(object o) //for single tap { try { tapping = false; if (!doubleTap) { var selectedTask = taskListView.SelectedItem as KanbanShell; string loadingMessage = "Loading Task " + selectedTask.Number; if (selectedTask.Attachments != 0) { loadingMessage = loadingMessage + " with " + selectedTask.Attachments + " attached document(s). Please wait for photos to appear."; } Device.BeginInvokeOnMainThread(async () => { using (await MaterialDialog.Instance.LoadingDialogAsync(message: loadingMessage)) { Guid ID = selectedTask.ID; var form = new AddEditTask(ID); await Navigation.PushAsync(form); } }); } } catch { } } private async void SearchEnt_Changed(object sender, EventArgs e) { DisplayList(); } private void ButtonToDo_Clicked(object sender, EventArgs e) { currentCategory = 0; DisplayList(); ChangeButtonColour(); } private void ButtonCurrent_Clicked(object sender, EventArgs e) { currentCategory = 1; DisplayList(); ChangeButtonColour(); } private void ButtonWaiting_Clicked(object sender, EventArgs e) { currentCategory = 2; DisplayList(); ChangeButtonColour(); } private void ChangeButtonColour() { buttonToDo.BackgroundColor = currentCategory == 0 ? color : Color.Default; buttonCurrent.BackgroundColor = currentCategory == 1 ? color : Color.Default; buttonWaiting.BackgroundColor = currentCategory == 2 ? color : Color.Default; } private void ObserverSwitch_Toggled(object sender, EventArgs e) { if (firstLoad) return; if (observerSwitch.IsToggled) { if (App.Current.Properties.ContainsKey("ObserverPreference")) { App.Current.Properties["ObserverPreference"] = "True"; } else { App.Current.Properties.Add("ObserverPreference", "True"); } } else { if (App.Current.Properties.ContainsKey("ObserverPreference")) { App.Current.Properties["ObserverPreference"] = "False"; } else { App.Current.Properties.Add("ObserverPreference", "False"); } } DisplayList(); } } }