示例#1
0
        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)));
        }
示例#2
0
        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);
        }
示例#3
0
        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)));
        }