private async Task LoadTodoList() { var todos = await _todoService.GetAll(); var pendingGroup = TodoItems.FirstOrDefault(t => t.State == TodoItemCompletedState.Pending); if (pendingGroup is null) { pendingGroup = new TodoItemsGroup(TodoItemCompletedState.Pending); } var completedGroup = TodoItems.FirstOrDefault(t => t.State == TodoItemCompletedState.Completed); if (completedGroup is null) { completedGroup = new TodoItemsGroup(TodoItemCompletedState.Completed); } var pending = todos.Where(t => !t.Completed).ToList(); pendingGroup.TodoItems.ReplaceRange(pending); var completed = todos.Where(t => t.Completed).ToList(); completedGroup.TodoItems.ReplaceRange(completed); TodoItems.Clear(); TodoItems.Add(pendingGroup); TodoItems.Add(completedGroup); }
public bool Delete(int id) { var item = TodoItems.FirstOrDefault(t => t.Id == id); if (item != null) { TodoItems.Remove(item); return(true); } return(false); }
public MainPageViewModel(INavigationService navigationService) : base(navigationService) { Title = "Todo List"; var query = CrossCloudFirestore.Current .Instance .GetCollection(TodoItem.CollectionPath) .OrderBy(nameof(TodoItem.CreatedAt), true); query.ObserveAdded() .Select(change => (Object: change.Document.ToObject <TodoItem>(), Index: change.NewIndex)) .Select(t => (ViewModel: new TodoItemViewModel(t.Object), Index: t.Index)) .Subscribe(t => TodoItems.InsertOnScheduler(t.Index, t.ViewModel)) .AddTo(_disposables); query.ObserveModified() .Select(change => change.Document.ToObject <TodoItem>()) .Select(todoItem => (TodoItem: todoItem, ViewModel: TodoItems.FirstOrDefault(x => x.Id == todoItem.Id))) .Where(t => t.ViewModel != null) .Subscribe(t => t.ViewModel.Update(t.TodoItem.Name, t.TodoItem.Notes)) .AddTo(_disposables); query.ObserveRemoved() .Select(change => TodoItems.FirstOrDefault(x => x.Id == change.Document.Id)) .Where(viewModel => viewModel != null) .Subscribe(viewModel => TodoItems.RemoveOnScheduler(viewModel)) .AddTo(_disposables); AddCommand = new ReactiveCommand(); AddCommand.Subscribe(() => { navigationService.NavigateAsync <NewTodoItemPageViewModel>(useModalNavigation: true, wrapInNavigationPage: true); }); SelectItemCommand = new ReactiveCommand <TodoItemViewModel>(); SelectItemCommand.Subscribe(item => { navigationService.NavigateAsync <TodoItemDetailPageViewModel, string>(item.Id); }); }
public TodoItem GetTodoItem(Guid id) => TodoItems.FirstOrDefault(i => i.Identifier == id);