Exemplo n.º 1
0
        public SeasonalItemsViewModel()
        {
            Title                = "What's in Season?";
            Items                = new ObservableConcurrentDictionary <int, SeasonalItem>();
            DocsChangeIndexes    = new HashSet <int>();
            LoadItemsCommand     = new Command(() => ExecuteLoadItemsCommand());
            SaveDocumentsCommand = new Command(async() => await ExecuteSaveDocumentsCommand());

            //tag::LoadData[]
            var q = QueryBuilder.Select(SelectResult.All())
                    .From(DataSource.Database(_db))
                    .Where(Meta.ID.EqualTo(Expression.String(CoreApp.DocId)))
                    .AddChangeListener((sender, args) =>
            {
                var allResult = args.Results.AllResults();
                var result    = allResult[0];
                var dict      = result[CoreApp.DB.Name].Dictionary;
                var arr       = dict.GetArray(CoreApp.ArrKey);

                if (arr.Count < Items.Count)
                {
                    Items = new ObservableConcurrentDictionary <int, SeasonalItem>();
                }

                Parallel.For(0, arr.Count, i =>
                {
                    var item  = arr[i].Dictionary;
                    var name  = item.GetString("key");
                    var cnt   = item.GetInt("value");
                    var image = item.GetBlob("image");

                    if (_items.ContainsKey(i))
                    {
                        _items[i].Name           = name;
                        _items[i].Quantity       = cnt;
                        _items[i].ImageByteArray = image?.Content;
                    }
                    else
                    {
                        var seasonalItem = new SeasonalItem {
                            Index          = i,
                            Name           = name,
                            Quantity       = cnt,
                            ImageByteArray = image?.Content
                        };

                        _items.Add(i, seasonalItem);
                    }
                });
            });
            //end::LoadData[]
        }
Exemplo n.º 2
0
        public async Task <bool> UpdateItemAsync(SeasonalItem item)
        {
            using (var doc = _db.GetDocument(CoreApp.DocId))
                using (var mdoc = doc.ToMutable())
                    using (MutableArrayObject listItems = mdoc.GetArray(CoreApp.ArrKey)) {
                        var dictObj = listItems.GetDictionary(item.Index);
                        dictObj.SetString("key", item.Name);
                        dictObj.SetInt("value", item.Quantity);
                        var blob = new Blob("image/png", item.ImageByteArray);
                        dictObj.SetBlob("image", blob);

                        _db.Save(mdoc);
                    }

            return(await Task.FromResult(true));
        }