private async void save_Click(object sender, EventArgs e) { SetProgressIndicator(true); SystemTray.ProgressIndicator.Text = "Saving Task"; if (!string.IsNullOrEmpty(txtbxTitle.Text.Trim())) { //Disable buttons ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false; ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false; ((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = false; if (!_id.Equals("new")) { var s = from p in App.TaskViewModel.TaskItem where p.id == _id select p; //Set the variables var taskItem = s.First(); string oldNotes = null; string newNotes = null; if (taskItem.notes != null) { oldNotes = taskItem.notes; } if (txtNotes.Text.Trim() != null && txtNotes.Text.Trim() != "") { newNotes = txtNotes.Text.Trim(); } string oldDue = null; string newDue = null; if (taskItem.due != null) { oldDue = Convert.ToDateTime(Universal.ConvertToUniversalDate(taskItem.due)).ToString("yyyy-MM-dd'T'hh:mm:ss.00Z"); } if (txtDueDate.Value.ToString() != null) { newDue = GetReminderDateTime(); } //check for changes before submitting to google if (taskItem.title != txtbxTitle.Text.Trim() || oldNotes != newNotes || oldDue != newDue) { taskItem.title = txtbxTitle.Text.Trim(); taskItem.notes = newNotes; taskItem.due = newDue; //Update the task bool result = await taskItem.Update(GetResponse); // Update the task locally List <TaskListItem> lists = await TaskListHelper.GetTaskListFromApplicationStorage(); foreach (TaskListItem list in lists) { foreach (TaskItem task in list.taskList.Where(x => x.id == _id)) { task.title = taskItem.title; task.notes = taskItem.notes; task.due = taskItem.due; task.updated = DateTime.UtcNow.ToString("yyyy-MM-dd'T'hh:mm:ss.00Z"); //DateTime.UtcNow.ToString(); } } // Resubmit the list to local storage await TaskListHelper.SubmitToLocalStorage(lists); if (result) { if (GTaskSettings.MsgUpdateTask) { MessageBoxResult m = MessageBox.Show("Complete.", "Update Task", MessageBoxButton.OK); } } else { MessageBoxResult m = MessageBox.Show("This task was updated while offline, please sync once back online.", "Offline Mode", MessageBoxButton.OK); } } NavigationService.Navigate(new Uri("/Views/TaskView.xaml?Id=" + _listId, UriKind.RelativeOrAbsolute)); } else //if it is new { var t = new TaskItem { }; t.title = txtbxTitle.Text.Trim(); t.notes = txtNotes.Text.Trim(); if (!string.IsNullOrEmpty(txtDueDate.Value.ToString())) { t.due = GetReminderDateTime(); } else { t.due = null; } //Create the Task TaskItem result = await TaskHelper.AddTask(t, GetResponse, _listId); if (result != null) { // Refresh the data await GTaskSettings.RefreshData(false, _listId, true); if (GTaskSettings.MsgCreateTask) { MessageBoxResult n = MessageBox.Show("Complete.", "Create Task", MessageBoxButton.OK); } } else { // Create a random ID t.id = Guid.NewGuid().ToString(); t.status = "needsAction"; t.updated = DateTime.UtcNow.ToString("yyyy-MM-dd'T'hh:mm:ss.00Z"); t.kind = "tasks#task"; t.selfLink = "https://www.googleapis.com/tasks/v1/lists/" + _listId + "/tasks/" + t.id; // Add a setting to flag the list var settings = IsolatedStorageSettings.ApplicationSettings; settings.Add("Task_" + t.id + "_Action", "added"); settings.Add("Task_" + t.id + "_Timestamp", DateTime.UtcNow.ToString()); // Get local storage List <TaskListItem> list = await TaskListHelper.GetTaskListFromApplicationStorage(); // Set the position // Check if there are items in the current list if (list.Where(x => x.id == _listId).First().taskList.Count() > 0) { t.position = (double.Parse(list.Where(x => x.id == _listId).First().taskList.OrderBy(x => x.position).First().position) - 1).ToString(); } else { t.position = "10000"; } // Add the task to the list list.Where(x => x.id == _listId).First().taskList.Insert(0, t); // Resubmit the list to local storage await TaskListHelper.SubmitToLocalStorage(list); MessageBoxResult m = MessageBox.Show("This task was created while offline, please sync once back online.", "Offline Mode", MessageBoxButton.OK); } //Navigate back to TaskView if "New" //Given we don't know the TaskID of this item yet, if you don't return it will create multiple identical tasks NavigationService.Navigate(new Uri("/Views/TaskView.xaml?Id=" + _listId, UriKind.RelativeOrAbsolute)); } } else { MessageBoxResult o = MessageBox.Show("Well, hello there! Do you mind including a Title?", "Title?", MessageBoxButton.OK); } //re-enable buttons ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true; ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = true; ((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = true; SetProgressIndicator(false); }
/// <summary> /// Refresh the data on the page /// </summary> /// <param name="refreshLocalStorage">Refresh local storage</param> public async void refresh(bool refresh = false, bool hidden = false) { //Start Progress Indicator SetProgressIndicator(true); if (refresh) { SystemTray.ProgressIndicator.Text = "Syncing with Google"; } else { SystemTray.ProgressIndicator.Text = "Getting Data"; } //Check to see if the Id exists. Check_Id(Id); // Get the settings var settings = IsolatedStorageSettings.ApplicationSettings; // Check to see if the list currently being viewed was added if (settings.Contains("List_" + Id + "_Action") && settings["List_" + Id + "_Action"].ToString() == "added") { settings["GetNewListId"] = Id; } //If hidden = true then we need to reload all the data, else we don't if (hidden == false) { await GTaskSettings.RefreshData(true, Id, refresh); } else { await GTaskSettings.RefreshData(true, null, refresh); } if (refresh) { SystemTray.ProgressIndicator.Text = "Data Synced, Finishing up"; } // Update the ID value if (settings.Contains("GetNewListId")) { Id = settings["GetNewListId"].ToString(); // Remove the setting settings.Remove("GetNewListId"); } //Load the Tasks await App.TaskViewModel.LoadData(Id); //Set DataContext to TaskList(s) DataContext = App.TaskViewModel; //Update Title SetTitle(); //Stop Progress Indicator SetProgressIndicator(false); //Build menu bar BuildLocalizedApplicationBar(); }