Exemplo n.º 1
0
        private async void FillFriendsList(Grid grid)
        {
            try
            {
                var user = await ReaderWriter.GetPerson(email);

                var counter = 3;
                if (user == null)
                {
                    return;
                }
                foreach (var friend in user.friends)
                {
                    Image img = new Image
                    {
                        Source            = "profile.jpg", //profilna uporabnika
                        HeightRequest     = 25,
                        VerticalOptions   = LayoutOptions.End,
                        HorizontalOptions = LayoutOptions.Center
                    };
                    Label lbl = new Label
                    {
                        Text              = friend, //spremeni pol v username
                        FontSize          = 18,
                        TextColor         = Color.Black,
                        HorizontalOptions = LayoutOptions.Start,
                        VerticalOptions   = LayoutOptions.Start,
                        Padding           = new Thickness(0, -3, 0, 0)
                    };
                    var eventOnTap = new TapGestureRecognizer();
                    eventOnTap.Tapped += async(s, e) =>
                    {
                        string action = await DisplayActionSheet("What would you like to do with your friend " + friend + "?", "Cancel", null, "Display movies you both like", "Remove friend");

                        //bool decision = await DisplayAlert(friend, "What would you like to do?", "Display movies you both like", "Remove friend");
                        if (action == "Display movies you both like")
                        {
                            await PopupNavigation.Instance.PushAsync(new BusyPopUp());

                            try
                            {
                                var friendMovies = FillTheQueueWithMovies(await ReaderWriter.GetUserMoviesByUsername(friend)).ToList();

                                // Two lists of movie rec. (friendMovies, UserMovies) what now?

                                var commonList = friendMovies.Concat(UserMovies).ToList();
                                var avgOfList  = commonList.Select(x => x.Vote_Average).DefaultIfEmpty(0).Average();
                                var finalList  = commonList.Where(x => x.Vote_Average > avgOfList).ToList();
                                MovieGrid.Children.Clear();

                                RefreshGrid(MovieGrid, finalList);
                            }
                            finally
                            {
                                await PopupNavigation.Instance.PopAsync();
                            }
                        }
                        else if (action == "Remove friend")
                        {
                            await PopupNavigation.Instance.PushAsync(new BusyPopUp());

                            try
                            {
                                user.friends.Remove(friend);
                                await ReaderWriter.UpdatePerson(user.username, user.email, user.friends, user.movies);

                                // To remove from the list once its removed from the databse
                                var button  = (BindableObject)s;
                                var row     = Grid.GetRow(button);
                                var column  = Grid.GetColumn(button);
                                var getgrid = button as Grid;
                                //assuming the image is in column 1
                                var result    = grid.Children.Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == column);
                                var resultImg = grid.Children.Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == column - 1);
                                foreach (var label in result)
                                {
                                    grid.Children.Remove(label);
                                    break;
                                }
                                foreach (var image in resultImg)
                                {
                                    grid.Children.Remove(image);
                                    break;
                                }
                                await DisplayAlert("Success", "User successfully removed from your friends list", "Close");
                            }
                            finally
                            {
                                await PopupNavigation.Instance.PopAsync();
                            }
                        }
                        else
                        {
                            return;
                        }
                    };
                    lbl.GestureRecognizers.Add(eventOnTap);

                    grid.Children.Add(img, 0, counter); //column, row
                    grid.Children.Add(lbl, 1, counter);
                    Grid.SetColumnSpan(lbl, 2);
                    counter++;
                }
            }
            catch (Exception e)
            {
                await DisplayAlert("Exception", e.Message, "Ok");
            }
        }