public async Task <IFavoriteItem> CreateFavorite(string title, Guid fileKey, string command, Guid key = default) { var item = new FavoriteItem(key == default(Guid) ? Guid.NewGuid() : key, title, fileKey, command); if ((await GetItem(item.Key)) != null) { throw new InvalidOperationException($"Favorite key {item.Key} already exists"); } return(await ProcessChanges(item, (itm, items) => items.AddItem(itm))); }
private async Task <FavoriteItem> ProcessChanges(FavoriteItem item, Func <FavoriteItem, List <FavoriteItem>, IEnumerable <FavoriteItem> > action) { await GetFavorites(); Items = action(item, Items).ToList(); var serializedFavorites = new FavoriteSerializer().Serialize(Items); await LocalStorageRepository.SetItem(LocalStorageFavoritesName, serializedFavorites); FavoritesChanged?.Invoke(this, EventArgs.Empty); return(item); }
public async Task <IFavoriteItem> EditFavorite(string title, Guid fileKey, string command, Guid key) { var item = (await GetItem(key)) as FavoriteItem; if (item == null) { throw new InvalidOperationException($"Favorite item {key} not found."); } var newItem = new FavoriteItem(key, title, fileKey, command); return(await ProcessChanges(item, (itm, items) => items.Where(f => f.Key != itm.Key).AddItem(newItem))); }