Ejemplo n.º 1
0
        public MainVM()
        {
            Openings  = new ObservableCollection <TruckOpeningVM>();
            Favorites = new ObservableCollection <FavoriteTruckVM>();

            HomeCommand = new Command(() =>
            {
                Repository.GetObject <INavigationService>().GotoPage("HomePage");
            });
            MapCommand = new Command(() =>
            {
                Repository.GetObject <INavigationService>().GotoPage("MapPage");
            });
            FavoritesCommand = new Command(() =>
            {
                Repository.GetObject <INavigationService>().GotoPage("FavoritesPage");
            });
            SettingsCommand = new Command(() =>
            {
                Repository.GetObject <INavigationService>().GotoPage("SettingsPage");
            });

            RefreshCommand = new Command(async() =>
            {
                await LoadTruckOpeningsAsync();
            });

            LoadTruckExecutor = new TaskExecutor <string>(async(TruckId) =>
            {
                var truck = await KnownException.Wrap <Truck>(() => TruckService.GetTruck(TruckId), "Cannot get truck information");

                LoadTruckVM(truck);
            });

            SelectTruckCommand = new Command(async(o) =>
            {
                var TruckId   = o as string;
                SelectedTruck = null;
                Repository.GetObject <INavigationService>().GotoPage("TruckPage");
                await LoadTruckExecutor.ExecuteAsync(TruckId);
            });

            SendTruckRatingExecutor = new TaskExecutor <TruckVM>(async(o) =>
            {
                var VM = o as TruckVM;

                await KnownException.Wrap(() => TruckService.SendRating(VM.Id, (int)VM.Rating), "There was an issue sending the rating");
                var truck = await KnownException.Wrap(() => TruckService.GetTruck(VM.Id), "Cannot get the latest truck information");
                VM.Rating = truck.Rating;
            });

            ToggleFavoriteCommand = new Command(async(o) =>
            {
                var VM        = o as TruckVM;
                var Value     = VM.IsFavorite;
                VM.IsFavorite = !VM.IsFavorite;
                var FoundFav  = Favorites.FirstOrDefault(f => f.Id.Equals(VM.Id));
                if (Value == false)
                {
                    if (FoundFav == null)
                    {
                        Favorites.Add(new FavoriteTruckVM()
                        {
                            Id       = VM.Id,
                            Title    = VM.Title,
                            ImageUrl = VM.ImageUrl,
                        });
                    }

                    Repository.GetObject <IToastService>().DoBasicToast(VM.Id, "Added Favorite", $"{VM.Title} was added to favorites", VM.ImageUrl);
                }
                else
                {
                    if (FoundFav != null)
                    {
                        Favorites.Remove(FoundFav);
                    }

                    Repository.GetObject <IToastService>().DoBasicToast(VM.Id, "Removed Favorite", $"{VM.Title} was removed from favorites", VM.ImageUrl);
                }
                var Favs = Favorites.Select(f => new Favorite()
                {
                    Id       = f.Id,
                    Title    = f.Title,
                    ImageUrl = f.ImageUrl,
                });
                await FavoritesService.Save(Favs);
            });

            ToggleTilePinCommand = new Command(async(o) =>
            {
                var VM    = o as TruckVM;
                var Value = VM.IsPinned;

                var tps             = Repository.GetObject <ITilePinService>();
                var IsPinnedAlready = tps.IsPinned(VM.Id);
                if (Value == false)
                {
                    if (IsPinnedAlready == false)
                    {
                        VM.IsPinned = await tps.PinTile(VM.Id, VM.Title, VM.ImageUrl);
                    }
                    else
                    {
                        VM.IsPinned = true;
                    }
                }
                else
                {
                    if (IsPinnedAlready == true)
                    {
                        await tps.UnPinTile(VM.Id);
                    }
                    VM.IsPinned = false;
                }
            });

            ClearFavoritesCommand = new Command(async() =>
            {
                await FavoritesService.Clear();
                Favorites.Clear();
            });
        }