private async void SaveButton_Clicked(object sender, EventArgs e) { Goal goal; if (_goalId.HasValue) { goal = await _goalDb.GetGoalAsync(_goalId.Value); goal.Title = this.TitleEntry.Text; goal.Description = this.DescEditor.Text; goal.GoalHour = this.GoalTimeSelector.Time.Hours; goal.GoalMinute = this.GoalTimeSelector.Time.Minutes; goal = await _goalDb.UpdateGoal(goal); } else { goal = await _goalDb.AddGoal(this.TitleEntry.Text, this.DescEditor.Text, this.GoalTimeSelector.Time.Hours, this.GoalTimeSelector.Time.Minutes); } DependencyService.Get <INotificationManager>().ScheduleNotification(goal.Id, goal.Title, goal.Description, DateTime.Now.Date.AddHours(goal.GoalHour).AddMinutes(goal.GoalMinute)); await Navigation.PopAsync(); }
public MainPageViewModel(GoalDatabase goalDatabase, INavigationService navigationService) { _goalDatabase = goalDatabase; _navigationService = navigationService; this.Todo = new ObservableCollection <GroupedGoals>(); this.Completed = new ObservableCollection <Goal>(); this.NoCompleted = true; this.HasCompleted = false; _morningGoals = new GroupedGoals("Morning", "AM"); _afternoonGoals = new GroupedGoals("Afternoon", "PM"); _eveningGoals = new GroupedGoals("Evening", "PM"); this.Todo.Add(_morningGoals); this.Todo.Add(_afternoonGoals); this.Todo.Add(_eveningGoals); this.Completed.CollectionChanged += Completed_CollectionChanged; this.AddCommand = new Command(async() => { await _navigationService.NavigateToEditAsync(); }); this.EditCommand = new Command(async(obj) => { await _navigationService.NavigateToEditAsync((int)obj); }); this.DeleteCommand = new Command(async(obj) => { int goalId = (int)obj; await _goalDatabase.DeactivateGoal(goalId); Completed.Remove(Completed.FirstOrDefault(g => g.Id == goalId)); RemoveGoalFromToDoList(goalId); DependencyService.Get <INotificationManager>().Cancel(goalId); }); this.CompleteCommand = new Command(async(obj) => { int goalId = (int)obj; var goal = await _goalDatabase.GetGoalAsync(goalId); var updatedGoal = await _goalDatabase.AddGoalCompletion(goal, DateTime.Now); RemoveGoalFromToDoList(goalId); Completed.Add(updatedGoal); }); this.UndoCompleteCommand = new Command(async(obj) => { int goalId = (int)obj; var goal = await _goalDatabase.GetGoalAsync(goalId); var updatedGoal = await _goalDatabase.UndoGoalCompletion(goalId); Completed.Remove(Completed.FirstOrDefault(g => g.Id == goalId)); AddGoalToToDoList(new GoalViewModel(updatedGoal)); }); }