Ejemplo n.º 1
0
        public async Task <SyncResult> SyncCategories()
        {
            // Current set of categories in the database so we can check if any have been removed.
            List <String>       currentList = (await DataAccessLayer.Current.GetCategoryNamesAsync()).ToList();
            SyncResult          result      = SyncResult.Failed;
            HttpResponseMessage response;

            try
            {
                response = await WebAPI.GetCategories();

                if (response.IsSuccessStatusCode)
                {
                    String[] Categories = await response.Content.ReadAsAsync <String[]>();

                    foreach (String s in Categories)
                    {
                        // Add the category to the database
                        await DataAccessLayer.Current.UpdateOrAddCategoryAsync(new Category()
                        {
                            Name = s, IsActive = true
                        }, CategoryKeepActive.Keep);

                        //Remove from the current list.
                        currentList.Remove(s);
                    }

                    // If there are any categories remaining in currentList, then they've been removed
                    // from the backend and we need to delete those records from our database.
                    foreach (String s in currentList)
                    {
                        await DataAccessLayer.Current.DeleteCategoryAsync(s);
                    }

                    // Refresh the Configuration object
                    await Configuration.PopulateCategoriesFromDB();

                    result = SyncResult.Success;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("SyncCategories failed: " + ex.Message);
            }

            return(result);
        }