public ExerciseListViewModel(IMasterDetailNavigation navigationService, IMessagingService messagingService, IExercisesRepository exercisesRepository) { _navigationService = navigationService; _messagingService = messagingService; _exercisesRepository = exercisesRepository; Title = AppResources.ExercisesTitle; SelectItemCommand = new Command(async(item) => { await OnItemSelected(item); }); AddExerciseCommand = new Command(async() => { await _navigationService.NavigateToHierarchical <ExerciseDetailsViewModel>(); }); _messagingService.Subscribe <ExerciseDetailsViewModel, Exercise>(this, Messages.ItemAdded, async(sender, e) => { var item = new ExerciseItem { Exercise = e, TrainingDays = await _exercisesRepository.GetTrainingDays(e) }; Exercises.Add(item); UpdateVisible(); }); _messagingService.Subscribe <ExerciseDetailsViewModel, Exercise>(this, Messages.ItemChanged, async(sender, e) => { var item = Exercises.FirstOrDefault(x => x.Exercise.ExerciseId == e.ExerciseId); if (item == null) { return; } item.Exercise = null; item.Exercise = e; item.TrainingDays = await _exercisesRepository.GetTrainingDays(e); }); _messagingService.Subscribe <ExerciseDetailsViewModel>(this, Messages.ItemDeleted, sender => { var item = Exercises.FirstOrDefault(x => x.Exercise.ExerciseId == sender.ExerciseId); Exercises.Remove(item); UpdateVisible(); }); }
private async Task <ObservableCollection <ExerciseItem> > GetExercises() { var collection = new ObservableCollection <ExerciseItem>(); var list = await _exercisesRepository.AllAsync(); foreach (var item in list) { var vm = new ExerciseItem { Exercise = item, TrainingDays = await _exercisesRepository.GetTrainingDays(item) }; collection.Add(vm); } return(collection); }