public async Task<List<Favorite>> AddAsync(Favorite toAdd) { var favorites = await LoadAsync().ConfigureAwait(false); if (null != favorites) { if (null != FindFavorite(favorites, toAdd)) return favorites; // Do not add twice to the list } else { favorites = new List<Favorite>(); } favorites.Add(toAdd); await SaveFavoritesAsync(favorites).ConfigureAwait(false); _loadedFavorites = favorites; return _loadedFavorites; }
public async Task<List<Favorite>> RemoveAsync(Favorite toRemove) { var favorites = await LoadAsync().ConfigureAwait(false); if (null == favorites) return null; // that should never happen actually var found = FindFavorite(favorites, toRemove); if (null == found) return favorites; // not in the list? nothing to do favorites.Remove(found); await SaveFavoritesAsync(favorites).ConfigureAwait(false); _loadedFavorites = favorites; return _loadedFavorites; }
// A very simple search that looks for name only private Favorite FindFavorite(List<Favorite> favorites, Favorite lookingFor) { return favorites .FirstOrDefault(f => f.LocationName == lookingFor.LocationName); }
public async Task RemoveFavorite(Favorite toRemove) { var favorites = await _favoritesRepository.RemoveAsync(toRemove); Favorites = new ObservableCollection<Favorite>(favorites); }