Ejemplo n.º 1
0
        public async override void ViewAppeared()
        {
            Products = ProductService.GetAllProducts();
            Events   = EventService.GetAllEvents();

            ProductsLoaded = (ProductCount > 0);
            EventsLoaded   = (EventCount > 0);

            //if we're connected lets check for data
            if (IsConnected)
            {
                ProgressBarTitle = "Syncing...";
                await Task.Delay(100);

                //if either the products or events aren't loaded, lets go get them.
                if (!ProductsLoaded || !EventsLoaded)
                {
                    //something's not loaded, lets go get it.
                    if (!ProductsLoaded)
                    {
                        ProgressBarTitle = "Retrieving Products...";
                        try
                        {
                            if (Products == null)
                            {
                                Products = new List <Product>();
                            }
                            var productData = await DataRetrievalService.GetProductData();

                            ProgressBarProgress = 10;
                            await Task.Delay(100);

                            //uncomment to pitch error here
                            //int i = 0;
                            //int text = 15 / i;

                            ProgressBarTitle = "Saving Products...";

                            int progress    = 10;
                            var numProducts = 0;
                            foreach (Product p in productData.data)
                            {
                                numProducts = numProducts + ProductService.SaveProduct(p);
                                if (numProducts % 25 == 0)
                                {
                                    progress++;
                                    ProgressBarProgress = progress;
                                    await Task.Delay(10);
                                }
                            }

                            await Task.Delay(100);

                            Products       = ProductService.GetAllProducts();
                            ProductsLoaded = true;
                        }
                        catch
                        {
                            //if we're in error, set the message, isnoterror and bail, we can't do any more.
                            ErrorMessage = "We couldn't load that.";
                            IsNotError   = false;
                            return;
                        }
                    }
                    ProgressBarTitle = "Products Loaded!";
                    await Task.Delay(250);

                    if (!EventsLoaded)
                    {
                        ProgressBarTitle    = "Retrieving Events...";
                        ProgressBarProgress = 50;
                        try
                        {
                            if (Events == null)
                            {
                                Events = new List <Event>();
                            }
                            var eventData = await DataRetrievalService.GetEventData();

                            ProgressBarTitle = "Saving Events...";

                            int progress  = 50;
                            var numEvents = 0;
                            foreach (Event e in eventData.data)
                            {
                                numEvents = numEvents + EventService.SaveEvent(e);
                                if (numEvents % 25 == 0)
                                {
                                    progress++;
                                    ProgressBarProgress = progress;
                                    await Task.Delay(10);
                                }
                            }
                            Events              = EventService.GetAllEvents();
                            EventsLoaded        = true;
                            ProgressBarTitle    = "Events Loaded.";
                            ProgressBarProgress = 100;
                            await Task.Delay(100);
                        }
                        catch
                        {
                            //if we're in error, set the message, isnoterror and bail, we can't do any more.
                            //ErrorMessage = "We couldn't load that.";
                            IsNotError = false;
                            return;
                        }
                    }
                    ProgressBarTitle    = "Data Loaded!";
                    ProgressBarProgress = 100;

                    await Task.Delay(1000);

                    if (ProductsLoaded && EventsLoaded)
                    {
                        await _navigationService.Navigate <ProductsViewModel>();
                    }
                }
                else if (ProductsLoaded && EventsLoaded)
                {
                    //everything's loaded, lets ski-daddle!
                    ProgressBarProgress = 100;
                    ProgressBarTitle    = "Data Loaded!";
                    await Task.Delay(2000);

                    await _navigationService.Navigate <ProductsViewModel>();
                }
            }

            base.ViewAppeared();
        }