/// <summary> /// Deletes the specified list. /// </summary> /// <param name="list"> /// The list to delete. /// </param> /// <returns> /// A <see cref="Task"/> that represents the operation. /// </returns> public async Task DeleteAsync(TodoList list) { // Validate if (list == null) throw new ArgumentNullException("list"); // Only proceed if lists are loaded if (cache != null) { // If found, remove and save if (cache.Contains(list)) { cache.Remove(list); await SaveAllAsync(); } } }
/// <summary> /// Loads all lists. /// </summary> /// <param name="useCache"> /// <c>true</c> if the cache should be used when available; otherwise false. /// </param> /// <returns> /// A <see cref="Task"/> that yields the result of the operation. /// </returns> public async Task<ObservableCollection<TodoList>> LoadListsAsync(bool useCache = true) { // If cache loaded and cache okay, just return it if ((useCache) && (cache != null)) { return cache; } // Either no cache or cache not allowed // Try to load from disk cache = await FileHelper.ReadFileAsync<ObservableCollection<TodoList>>(ListFileName, FileHelper.StorageStrategies.Roaming); // If still null, that means the file doesn't exist. Create a new list. if (cache == null) { var defaultList = new TodoList() { Title = "New List" }; var defaultItem = new TodoItem() { Title = "New Item" }; defaultList.Items.Add(defaultItem); cache = new ObservableCollection<TodoList>() { defaultList }; } /* cache = new ObservableCollection<TodoList>() { new TodoList() { Title = "Groceries", Items = new ObservableCollection<TodoItem>() { new TodoItem() { Title = "Item A" }, new TodoItem() { Title = "Item B" }, new TodoItem() { Title = "Item C" }, new TodoItem() { Title = "Item D" }, } }, new TodoList() { Title = "Home Supplies", Items = new ObservableCollection<TodoItem>() { new TodoItem() { Title = "Item A" }, new TodoItem() { Title = "Item B" }, new TodoItem() { Title = "Item C" }, new TodoItem() { Title = "Item D" }, } }, }; */ return cache; }
public Task SaveListAsync(TodoList list = null) { // If null, use current if (list == null) { list = currentList; } // Save list with exception handling if (list != null) { return RunWithErrorHandling(() => todoService.SaveAsync(list), TaskRunOptions.WithFailure(string.Format("Could not save {0} list", list.Title))); } else { return TaskHelper.CompletedTask; } }
/// <summary> /// Savs the specified list. /// </summary> /// <param name="list"> /// The list to save. /// </param> /// <returns> /// A <see cref="Task"/> that represents the operation. /// </returns> public async Task SaveAsync(TodoList list) { // Validate if (list == null) throw new ArgumentNullException("list"); // Get the lists (loading if necessary) var lists = await LoadListsAsync(); // If not in the lists, add if (!lists.Contains(list)) { lists.Add(list); } // Save all await SaveAllAsync(); }
public void AddList() { var list = new TodoList() { Title = "New List" }; var item = new TodoItem() { Title = "New Item" }; list.Items.Add(item); lists.Add(list); CurrentList = list; CurrentItem = item; }