//Takes a datetime and returns a date public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var formatString = value as string; //If user wants date hidden return empty if (GTaskSettings.HideDueDate) { return(String.Empty); } try { //Check if Null or Empty if (string.IsNullOrEmpty(formatString)) { return("No Due Date"); } //Try to convert it to ShortDateString if "." is not included (assuming US Version) formatString = Universal.ConvertToUniversalDate(formatString); //Try to remove 00:00:00 and 12:00:00 from string and just return it formatString = formatString.Replace(" 00:00:00", ""); formatString = formatString.Replace(" 12:00:00", ""); return(formatString); } catch { //Return if error //MessageBox.Show(message); return("Invalid Due Date"); } }
//Takes a datetime and returns a date public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null) { var id = value as string; var s = from p in App.TaskViewModel.TaskItem where p.id == id select p; var taskItem = s.SingleOrDefault(); if (taskItem != null) { var formatString = taskItem.due as string; if (!string.IsNullOrEmpty(formatString)) { formatString = DateTime.Parse(Universal.ConvertToUniversalDate(formatString)).Date.ToShortDateString(); if (taskItem.status == "completed") { return("Gray"); } else if (DateTime.Parse(formatString) < DateTime.Parse(DateTime.Now.ToShortDateString())) { return("Red"); } else if (formatString == DateTime.Now.ToShortDateString()) { return("Green"); } } else if (GTaskSettings.NoDueDateAtTop && GTaskSettings.TaskSort == 1) //No Due Date { return("Red"); } return(Application.Current.Resources["PhoneForegroundBrush"] as Brush);; } return(Application.Current.Resources["PhoneForegroundBrush"] as Brush);; } return(Application.Current.Resources["PhoneForegroundBrush"] as Brush);; }
public static async Task <bool> RefreshData(bool alertWhenNoConnection = false, string specificList = null, bool refresh = false) { try { // Create a list to hold the local data List <Model.TaskListItem> localStorageList = await TaskListHelper.GetTaskListFromApplicationStorage(false); if (localStorageList == null) { localStorageList = new List <Model.TaskListItem>(); } // Create a list to hold the data from the api // If no data locally then populate it, else if AutoRefreshLists or AutoRefreshTasks = true then get data // Goal is to only get Google data when needed // Create a final list to hold the results for local storage List <Model.TaskListItem> finalList = new List <Model.TaskListItem>(); // Get the settings var settings = IsolatedStorageSettings.ApplicationSettings; // Sync w/ google? bool googleSync = false; List <Model.TaskListItem> googleApiList = null; if (AutoRefreshLists || AutoRefreshTasks || localStorageList.Count == 0 || refresh || GTaskSettings.FirstRun == true) { googleApiList = await TaskListHelper.LoadTaskDataFromApi(alertWhenNoConnection, null);// specificList); } else { //if (specificList != null) //{ // localStorageList.RemoveAll(x => x.id != specificList); // finalList = localStorageList; // goto finish; //} //else //{ finalList = localStorageList; goto finish; //} } // If the google api list returns null then we must assume that we can't get a connection, so error out if (googleApiList == null) { return(false); } googleSync = true; // Get the last sync date DateTime lastSyncDate = DateTime.Parse("1/1/1901"); if (settings.Contains("LastSyncDate")) { DateTime.TryParse(settings["LastSyncDate"].ToString(), out lastSyncDate); } // Loop through the two lists and check if any changes need to be synced foreach (Model.TaskListItem lSList in localStorageList) { // Create a boolean to determine if we need to reorder tasks bool reOrderTasks = false; // Look for a corresponding list in the API if (googleApiList.Where(x => x.id == lSList.id).Count() > 0) { // Get the corresponding google api list Model.TaskListItem gAList = googleApiList.Where(x => x.id == lSList.id).First(); // Remove this list from the google api list to narrow things down googleApiList.RemoveAll(x => x.id == gAList.id); // Check if the titles are the same if (lSList.title != gAList.title) { // Check which one was more recently updated if (DateTime.Parse(lSList.updated) > DateTime.Parse(gAList.updated)) { // Set the new title for the ga list gAList.title = lSList.title; // Update the list in the api await TaskListHelper.RetryUpdateList(new List <object> { gAList }); } else if (DateTime.Parse(lSList.updated) <= DateTime.Parse(gAList.updated)) { // Set the new title for the ls list lSList.title = gAList.title; } } // Create a final TaskListtem Model.TaskListItem finalTaskListItem = new Model.TaskListItem(); //Convert to universal string lSListupdated = Universal.ConvertToUniversalDate(lSList.updated); string gAListupdated = Universal.ConvertToUniversalDate(gAList.updated); //string lSListupdatedFormat = (DateTime)lSListupdated.ToString("MM/dd/yyyy hh:mm:ss"); // Set the necessary properties finalTaskListItem.updated = (DateTime.Parse(lSListupdated) > DateTime.Parse(gAListupdated)) ? lSList.updated : gAList.updated; finalTaskListItem.title = lSList.title; finalTaskListItem.id = lSList.id; finalTaskListItem.selfLink = gAList.selfLink; finalTaskListItem.kind = gAList.kind; // The lists are the same now but lets compare the individual tasks #region Tasks foreach (Model.TaskItem lSItem in lSList.taskList) { if (lSItem == null) { continue; } // Check if there is a corresponding task in the gAList if (gAList.taskList.Where(x => x.id == lSItem.id).Count() > 0) { // Get the task from the gAList Model.TaskItem gAItem = gAList.taskList.Where(x => x.id == lSItem.id).First(); // If the positions do not match then we need to update them all if (lSItem.position != gAItem.position) { reOrderTasks = true; } // Remove the gAItem from the gAList to narrow things down gAList.taskList.RemoveAll(x => x.id == gAItem.id); //Get due dates and compare formatted correctly //local vars string lSItemdue = Universal.ConvertToUniversalDate(lSItem.due); string lSItemupdated = Universal.ConvertToUniversalDate(lSItem.updated); string lSItemcompleted = Universal.ConvertToUniversalDate(lSItem.completed); //string lSItemnotes = lSItem.notes; //string lSItemtitle = lSItem.title; //google vars string gAItemdue = Universal.ConvertToUniversalDate(gAItem.due); string gAItemupdated = Universal.ConvertToUniversalDate(gAItem.updated); string gAItemcompleted = Universal.ConvertToUniversalDate(gAItem.completed); //string gAItemnotes = gAItem.notes; //string gAItemtitles = gAItem.title; // Now we check the properties of the item, get the more recently updated one if (DateTime.Parse(lSItemupdated) > DateTime.Parse(gAItemupdated) && (lSItemcompleted != gAItemcompleted || lSItem.deleted != gAItem.deleted || lSItemdue != gAItemdue || lSItem.hidden != gAItem.hidden || lSItem.kind != gAItem.kind || lSItem.notes != gAItem.notes || lSItem.parent != gAItem.parent || lSItem.position != gAItem.position || lSItem.status != gAItem.status || lSItem.title != gAItem.title)) { // The local storage item was updated more recently, submit it to the api await TaskHelper.RetryUpdateTask(new List <object> { lSItem }); // Check if we need ot update the status specifically if (lSItem.status != gAItem.status) { string due = null; if (lSItemdue != null) { due = Convert.ToDateTime(Universal.ConvertToUniversalDate(lSItem.due)).ToString("yyyy-MM-dd'T'hh:mm:ss.00Z"); } bool isChecked = false; if (lSItem.status == "completed") { isChecked = true; } await TaskHelper.UpdateTaskStatus(lSList.id, lSItem.id, lSItemdue, isChecked); } // Add the lSItem to the final list finalTaskListItem.taskList.Add(lSItem); } else { // The item was updated more recently on a remote device, simply add it to the final list finalTaskListItem.taskList.Add(gAItem); } } else { // There was no corresponding item in the list from the google api // Check if the item was added while in offline mode if (settings.Contains("Task_" + lSItem.id + "_Action") && settings.Contains("Task_" + lSItem.id + "_Timestamp") && settings["Task_" + lSItem.id + "_Action"].ToString() == "added" && DateTime.Parse(settings["Task_" + lSItem.id + "_Timestamp"].ToString()) > lastSyncDate) { // Submit the item to the google api Model.TaskItem result = await TaskHelper.RetryAddTask(new List <object> { lSItem, null, lSList.id }); // Add the item to the final list finalTaskListItem.taskList.Add(result); // Continue to the next one continue; } // At this point we can assume the item was deleted on a remote device, no need to add it to the final list } // Remove any settings associated with the task settings.Remove("Task_" + lSItem.id + "_Action"); settings.Remove("Task_" + lSItem.id + "_Timestamp"); } // Loop through what is left in the google api tasks foreach (Model.TaskItem task in gAList.taskList) { // Check if this task was deleted locally if (settings.Contains("Task_" + task.id + "_Action") && settings["Task_" + task.id + "_Action"].ToString() == "deleted" && DateTime.Parse(settings["Task_" + task.id + "_Timestamp"].ToString()) >= DateTime.Parse(task.updated)) { // Delete the list in the api await TaskHelper.DeleteTask(gAList.id, task.id); } else { finalTaskListItem.taskList.Add(task); } // Remove any settings settings.Remove("Task_" + task.id + "_Action"); settings.Remove("Task_" + task.id + "_Timestamp"); } #endregion finalList.Add(finalTaskListItem); } else { // There was no corresponding list in the list from the google api // Check if the list was added while in offline mode if (settings.Contains("List_" + lSList.id + "_Action") && settings.Contains("List_" + lSList.id + "_Timestamp") && settings["List_" + lSList.id + "_Action"].ToString() == "added" && DateTime.Parse(settings["List_" + lSList.id + "_Timestamp"].ToString()) > lastSyncDate) { // Submit the list to the google api Model.TaskListItem results = await TaskListHelper.RetryAddTaskList(new List <object> { lSList }); // Add the list to the final list if (results != null) { // Check if we need to update an ID value if (settings.Contains("GetNewListId") && settings["GetNewListId"].ToString() == lSList.id) { settings["GetNewListId"] = results.id; } // Add the tasks #region Tasks foreach (Model.TaskItem lSItem in lSList.taskList) { // Submit the item to the google api Model.TaskItem task = await TaskHelper.RetryAddTask(new List <object> { lSItem, null, results.id }); // Add the item to the final list results.taskList.Add(task); // Remove any settings associated with the task settings.Remove("Task_" + lSItem.id + "_Action"); settings.Remove("Task_" + lSItem.id + "_Timestamp"); } #endregion finalList.Add(results); } else { MessageBox.Show("Uh oh! There was an error when syncing the tasks. Please try again soon."); return(false); } } // At this point we can assume the list was deleted on a remote device and we don't have to add it to the final list } // Get the final task list if (finalList.Count > 0) { Model.TaskListItem finalTaskList = finalList.Last(); finalTaskList.taskList = finalTaskList.taskList.OrderBy(x => double.Parse(x.position)).ToList(); // Loop through the tasks //if ((GTaskSettings.TaskSort == 0 && GTaskSettings.DisableDragDrop != true) //only move if settings are enabled if (reOrderTasks) //only move if reOrderTasks is detected { for (int i = 0; i < finalTaskList.taskList.Count; i++) { string position = ""; if (i == 0) { position = await TaskHelper.MoveTask(finalTaskList.id, finalTaskList.taskList[i].id, null); } else { position = await TaskHelper.MoveTask(finalTaskList.id, finalTaskList.taskList[i].id, finalTaskList.taskList[i - 1].id); } // Update the position finalList.Last().taskList.First(x => x.id == finalTaskList.taskList[i].id).position = position; } } } // Remove any settings settings.Remove("List_" + lSList.id + "_Action"); settings.Remove("List_" + lSList.id + "_Timestamp"); } // Loop through what is left in the google api list foreach (Model.TaskListItem list in googleApiList) { // Check if this list was deleted locally if (settings.Contains("List_" + list.id + "_Action") && settings["List_" + list.id + "_Action"].ToString() == "deleted" && DateTime.Parse(settings["List_" + list.id + "_Timestamp"].ToString()) >= DateTime.Parse(list.updated)) { // Delete the list in the api await TaskListHelper.DeleteList(list.id); } else { finalList.Add(list); } // Remove any settings settings.Remove("List_" + list.id + "_Action"); settings.Remove("List_" + list.id + "_Timestamp"); } // Create a temp list List <string> keyStrList = settings.Select(x => x.Key.ToString()).ToList <string>(); // Remove any remaining settings foreach (string keyStr in keyStrList) { if (keyStr.Contains("List_") || keyStr.Contains("Task_")) { settings.Remove(keyStr); } } // Save the final list to local storage //if (specificList == null) //only commit to storage if it isn't for a specific list //{ await TaskListHelper.SubmitToLocalStorage(finalList); //} finish: // Update any live tiles // Loop through the live tiles foreach (ShellTile shellTile in ShellTile.ActiveTiles) { try { if (shellTile.NavigationUri.ToString().Contains("/Views/TaskView.xaml?Id=")) { // Get the ID string id = shellTile.NavigationUri.ToString().Substring(shellTile.NavigationUri.ToString().IndexOf("?Id=")).Replace("?Id=", ""); if (id.Contains("&")) { id = id.Substring(0, id.IndexOf("&")); } // Check if there is a corresponding list if (finalList.Where(x => x.id == id).Count() > 0) { // Get the list Model.TaskListItem list = finalList.Where(x => x.id == id).First(); // Get the title string title = list.title; if (title.Length > 24) { title = title.Substring(0, 21) + "..."; } //Create a completed list var NCtasks = new List <TaskItem>(); NCtasks.Clear(); NCtasks.AddRange(list.taskList.Where(y => y.status != "completed").ToList()); // Update the tile accordingly int count = 0; if ((int)settings["LiveTileCount"] > 0) { if ((bool)settings["IncludeNoDueDate"]) { count = NCtasks.Where(y => (y.due != null ? DateTime.Parse(y.due).AddHours(-12) : DateTime.MinValue) <= DateTime.Now).Count(); settings["DueCount_" + list.id] = count; } else { count = NCtasks.Where(y => (y.due != null ? DateTime.Parse(y.due).AddHours(-12) : DateTime.MaxValue) <= DateTime.Now).Count(); settings["DueNDCount_" + list.id] = count; } } else { count = NCtasks.Count; settings["Count_" + list.id] = count; } //most displays cut off the 16th charactor, this reduces it to 13 + ... for the Edit page and LiveTile var tileData = new StandardTileData { Title = title, BackgroundImage = new Uri("/Assets/Icons/202.png", UriKind.Relative), Count = count }; shellTile.Update(tileData); } } } catch (Exception e) { // Do nothing Console.WriteLine(e.ToString()); } } // Set the last sync date if (googleSync) { settings["LastSyncDate"] = DateTime.UtcNow; } //If you've made it this far on your first attempt them you FirstRun is complete GTaskSettings.FirstRun = false; // Return true return(true); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine(e.StackTrace.ToString()); return(false); } }
private static async Task RetryGetTasksForList(ObservableCollection <object> obj) { string message = "Ugh.. I can't find the Tasks for this Task List, can you try again?"; try { var taskItem = new ObservableCollection <TaskItem>(); taskItem.CollectionChanged += OnCollectionChanged; // Create a list of tasks List <TaskItem> tasks = new List <TaskItem>(); // Load the task lists from local storage List <TaskListItem> TaskListList = await GetTaskListFromApplicationStorage(); // Get the appropriate task list if available if (TaskListList != null && TaskListList.Where(x => x.id == obj[0].ToString()).Count() > 0) { var taskListItem = (TaskListItem)TaskListList.First(x => x.id == obj[0].ToString()); if (taskListItem.taskList != null) { tasks.AddRange(taskListItem.taskList); } } taskItem.Clear(); //Sort tasks by completed first]\//Sort the original list by completed time where Not complete is at the top, complete is then sorted by completion date tasks = tasks.OrderBy(x => x.completed != null ? DateTime.Parse(Universal.ConvertToUniversalDate(x.completed)) : DateTime.MinValue).ToList(); //Conditional sorting based on Settings selection if (GTaskSettings.TaskSort == 1) { if (!GTaskSettings.NoDueDateAtTop) { foreach (var task in tasks.OrderBy(x => x.due != null ? DateTime.Parse(Universal.ConvertToUniversalDate(x.due)) : DateTime.MaxValue).ToList()) //Closest First, No Date at Bottom { //Add task to list taskItem.Add(task); } } else { foreach (var task in tasks.OrderBy(x => x.due != null ? DateTime.Parse(Universal.ConvertToUniversalDate(x.due)) : DateTime.MinValue).ToList()) //Closest First, No Date at Top { //Add task to list taskItem.Add(task); } } } else if (GTaskSettings.TaskSort == 2) { foreach (var task in tasks.OrderBy(x => x.title).ToList()) //Alphabetical { //Add task to list taskItem.Add(task); } } else { foreach (var task in tasks.OrderBy(x => double.Parse(x.position)).ToList()) //Default { //Add task to list taskItem.Add(task); } } // Remove any completed items if necessary //THIS IS REQUIRED TO HIDE COMPLETED WITH OFFLINE MODE if (GTaskSettings.HideCompleted) { // Remove any hidden/completed tasks List <TaskItem> tempList = taskItem.ToList(); tempList.RemoveAll(x => x.hidden == "True"); taskItem = new ObservableCollection <TaskItem>(tempList); } //Set the context to taskItem list var list = obj[1] as Action <ObservableCollection <TaskItem> >; if (list != null) { list(taskItem); } //Store the Count in Storage, if the liveTile exists then update it's count var settings = IsolatedStorageSettings.ApplicationSettings; string id = obj[0].ToString(); //Create a completed list var NCtasks = new List <TaskItem>(); NCtasks.Clear(); NCtasks.AddRange(tasks.Where(y => y.status != "completed").ToList()); //Count = Total Count //DueCount = Past Due w/ No Date Included //DueNDCount = Past Due w/ No Date Excluded //string minDT = GTaskSettings.convertDate(DateTime.MinValue.ToString()); //string maxDT = GTaskSettings.convertDate(DateTime.MaxValue.ToString()); //string nowDT = GTaskSettings.convertDate(DateTime.Now.ToString()); settings["Count_" + id] = NCtasks.Count(); settings["DueCount_" + id] = NCtasks.Where(y => (y.due != null ? DateTime.Parse(Universal.ConvertToUniversalDate(y.due)).AddHours(-12) : DateTime.MinValue) <= DateTime.Now).Count(); //Closest Last, No Date Included settings["DueNDCount_" + id] = NCtasks.Where(y => (y.due != null ? DateTime.Parse(Universal.ConvertToUniversalDate(y.due)).AddHours(-12) : DateTime.MaxValue) <= DateTime.Now).Count(); //Closest Last, No Date Excluded //Update the LiveTile counts if they exist var tile = ShellTile.ActiveTiles.FirstOrDefault(y => y.NavigationUri.ToString().Contains(id)); if (tile != null) { //most displays cut off the 16th charactor, this reduces it to 13 + ... for the Edit page and LiveTile string liveTitle = settings["Title_" + id].ToString(); if (liveTitle.Length > 24) { liveTitle = liveTitle.Substring(0, 21) + "..."; } //Conditional LiveTile Updates depending on the settings (past due w/ no due date, past due w/o no due date, total count int count = 0; if (GTaskSettings.LiveTileCount > 0) { if (GTaskSettings.IncludeNoDueDate) { count = (int)settings["DueCount_" + id]; } else { count = (int)settings["DueNDCount_" + id]; } } else { count = (int)settings["Count_" + id]; } var tileData = new StandardTileData { Title = liveTitle, BackgroundImage = new Uri("/Assets/Icons/202.png", UriKind.Relative), Count = count }; tile.Update(tileData); } } catch (Exception) //e) { if (GTaskSettings.MsgError) { MessageBox.Show(message); } } }