Exemplo n.º 1
0
        private async void downloadItemList()
        {
            var serverDb = new ServerDatabaseService();
            List <Models.Item> itemList = new List <Models.Item>();

            if (ItemCatalogue == null)
            {
                ItemCatalogue = new ObservableCollection <SalesItemSelection>();
            }
            Models.Item temp;
            foreach (SalesItemSelection item in ItemCatalogue)
            {
                temp = new Models.Item(item.ItemId, item.Name, item.Price, item.UnitMeasurement, item.Barcode);
                itemList.Add(temp);
            }
            var fetchedItem = await serverDb.getAllItems();

            if (fetchedItem != null)
            {
                //compare with local
                List <Models.Item> fetchedData = new List <Models.Item>(fetchedItem);
                List <Models.Item> newData     = fetchedData.Except(itemList).ToList();
                List <Models.Item> deletedData = itemList.Except(fetchedData).ToList();
                if (newData != null)
                {
                    //insert new data to local db
                    await itemDb.InsertItems(new ObservableCollection <Models.Item>(newData));
                }
                if (deletedData != null)
                {
                    //delete deleted data in local db
                    foreach (Models.Item data in deletedData)
                    {
                        await itemDb.DeleteItem(data);
                    }
                }
                loadItemCatalogue();
            }
        }