Exemplo n.º 1
0
        public MovieDetailsPageModel(IAppStoreContainer storeContainer,
                                     MovieDetailActionCreator movieDetailActionCreator,
                                     ReviewActionCreator reviewActionCreator)
        {
            MovieDetailActionCreator = movieDetailActionCreator;
            ReviewActionCreator      = reviewActionCreator;
            Store = storeContainer.Store;

            Store.Subscribe(s =>
            {
                IsAuthenticated  = !string.IsNullOrEmpty(s.AuthenticationState.SessionId);
                IsLoadingDetails = s.MovieDetailsState.IsLoading;

                if (s.MovieDetailsState.MovieId != Movie.Id || IsLoadingDetails)
                {
                    return;
                }

                if (s.MovieDetailsState.MovieAccountState == null)
                {
                    // an error occured, can be handled here
                    return;
                }
                var accountState = s.MovieDetailsState.MovieAccountState;

                IsFavorite = accountState.Favorite;

                if (!(accountState.Rated is Dictionary <string, object> rated))
                {
                    return;
                }

                // workaround, unboxing doesn't work
                Rate = int.Parse(rated["value"].ToString());
            });

            PositiveRateCommand = new Command(async() =>
            {
                await Store.Dispatch(MovieDetailActionCreator.RateMovieAction(new RateMovieArgs
                {
                    MovieId = Movie.Id,
                    Rate    = 10
                }));
            });

            NegativeRateCommand = new Command(async() =>
            {
                await Store.Dispatch(MovieDetailActionCreator.RateMovieAction(new RateMovieArgs
                {
                    MovieId = Movie.Id,
                    Rate    = 1
                }));
            });

            FavoriteCommand = new Command(async() =>
            {
                await Store.Dispatch(MovieDetailActionCreator.FavoriteMovieAction(new FavoriteMovieArgs
                {
                    IsFavorite = !IsFavorite,
                    MovieId    = Movie.Id
                }));
            });

            ReadReviewsCommand = new FreshAwaitCommand(async(parameter, tcs) =>
            {
                await CoreMethods.PushPageModel <ReviewsPageModel>(Movie, true, true);
                tcs.SetResult(true);

                await Store.Dispatch(ReviewActionCreator.LoadReviewsAction(Movie.Id));
            });
        }