Esempio n. 1
0
        /// <summary>
        /// Adds a quiz to the search stack given a QuizInfo
        /// </summary>
        /// <param name="quizzes"></param>
        private void AddQuizzes(List <QuizInfo> quizzes)
        {
            List <QuizInfo> currentlySubscribed = QuizRosterDatabase.GetRoster();

            foreach (QuizInfo quiz in quizzes)
            {// Only add quiz if the category is what user picked (we are asking the server for more then we need so this could be changed)
                if (this.category == "All" || quiz.Category == this.category)
                {
                    Frame quizFrame = new Frame
                    {
                        VerticalOptions   = LayoutOptions.Start,
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                        CornerRadius      = 10
                    };

                    StackLayout frameStack = new StackLayout
                    {
                        FlowDirection = FlowDirection.LeftToRight,
                        Orientation   = StackOrientation.Vertical
                    };

                    StackLayout topStack = new StackLayout
                    {
                        FlowDirection = FlowDirection.LeftToRight,
                        Orientation   = StackOrientation.Horizontal
                    };

                    Label quizName = new Label // 0
                    {
                        Text              = quiz.QuizName,
                        FontAttributes    = FontAttributes.Bold,
                        FontSize          = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                        HorizontalOptions = LayoutOptions.StartAndExpand
                    };
                    topStack.Children.Add(quizName);

                    ImageButton ImageButtonSubscribe = new ImageButton // 1
                    {
                        IsVisible         = false,
                        Source            = "ic_playlist_add_black_48dp.png",
                        StyleId           = quiz.DBId,
                        HeightRequest     = 30,
                        BackgroundColor   = Color.White,
                        HorizontalOptions = LayoutOptions.End
                    };
                    ImageButtonSubscribe.Clicked += this.ImageButtonSubscribe_Clicked;
                    topStack.Children.Add(ImageButtonSubscribe);

                    ImageButton ImageButtonUnsubscribe = new ImageButton // 2
                    {
                        IsVisible         = false,
                        Source            = "ic_playlist_add_check_black_48dp.png",
                        StyleId           = quiz.DBId,
                        HeightRequest     = 30,
                        BackgroundColor   = Color.White,
                        HorizontalOptions = LayoutOptions.End
                    };
                    ImageButtonUnsubscribe.Clicked += this.ImageButtonUnsubscribe_Clicked;
                    topStack.Children.Add(ImageButtonUnsubscribe);

                    ActivityIndicator Syncing = new ActivityIndicator // 3
                    {
                        IsVisible         = false,
                        Color             = Color.Accent,
                        HeightRequest     = 25,
                        WidthRequest      = 25,
                        VerticalOptions   = LayoutOptions.StartAndExpand,
                        HorizontalOptions = LayoutOptions.End,
                    };
                    topStack.Children.Add(Syncing);


                    if (quiz.AuthorName != CredentialManager.Username)
                    {
                        // If already subscribed
                        if (!(currentlySubscribed.Where(quizInfo => quizInfo.DBId == quiz.DBId && !quizInfo.IsDeletedLocally).Count() > 0))
                        {
                            ImageButtonSubscribe.IsVisible = true;
                        }
                        else
                        {
                            ImageButtonUnsubscribe.IsVisible = true;
                        }
                    }

                    frameStack.Children.Add(topStack);

                    Label quizAuthor = new Label
                    {
                        Text = "Created by: " + quiz.AuthorName,
                    };
                    frameStack.Children.Add(quizAuthor);

                    Label quizCategory = new Label
                    {
                        Text = "Category: " + quiz.Category,
                    };
                    frameStack.Children.Add(quizCategory);
                    quizFrame.Content = frameStack;
                    this.quizzesSearched.Add(quiz);
                    Device.BeginInvokeOnMainThread(() =>
                                                   this.SearchedStack.Children.Add(quizFrame));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Generate a frame (card-like button) for the UI based on a QuizInfo
        /// </summary>
        /// <param name="quizInfo">QuizInfo of the quiz to generate a frame for</param>
        /// <returns></returns>
        private Frame GenerateFrame(QuizInfo quizInfo)
        {
            Frame frame = new Frame()
            {
                VerticalOptions   = LayoutOptions.Start,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                CornerRadius      = 10
            };

            StackLayout frameStack = new StackLayout // 1st Child of frameLayout
            {
                FlowDirection = FlowDirection.LeftToRight,
                Orientation   = StackOrientation.Vertical,
                Padding       = 10
            };

            StackLayout topStack = new StackLayout
            {
                FlowDirection     = FlowDirection.LeftToRight,
                Orientation       = StackOrientation.Horizontal,
                VerticalOptions   = LayoutOptions.StartAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            Label title = new Label // 0
            {
                Text              = quizInfo.QuizName,
                FontSize          = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                FontAttributes    = FontAttributes.Bold,
                VerticalOptions   = LayoutOptions.StartAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand //,
                                                                //HeightRequest = 45
            };

            topStack.Children.Add(title);

            // Add the sync buttons, We create one for each sync action to keep correct formatting and fix a sizing bug.
            ImageButton SyncOffline = new ImageButton // 1
            {
                IsVisible         = false,
                Source            = "ic_cloud_off_black_48dp.png",
                HeightRequest     = 25,
                WidthRequest      = 25,
                BackgroundColor   = Color.White,
                VerticalOptions   = LayoutOptions.StartAndExpand,
                HorizontalOptions = LayoutOptions.End,
                ClassId           = quizInfo.DBId,
                StyleId           = quizInfo.Category
            };

            SyncOffline.Clicked += this.SyncOffline_Clicked;
            topStack.Children.Add(SyncOffline);

            ImageButton SyncUpload = new ImageButton // 2
            {
                IsVisible         = false,
                Source            = "ic_cloud_upload_black_48dp.png",
                HeightRequest     = 25,
                WidthRequest      = 25,
                BackgroundColor   = Color.White,
                VerticalOptions   = LayoutOptions.StartAndExpand,
                HorizontalOptions = LayoutOptions.End,
                ClassId           = quizInfo.DBId,
                StyleId           = quizInfo.Category
            };

            SyncUpload.Clicked += this.SyncUpload_Clicked;
            topStack.Children.Add(SyncUpload);

            ImageButton SyncDownload = new ImageButton // 3
            {
                IsVisible         = false,
                Source            = "ic_cloud_download_black_48dp.png",
                HeightRequest     = 25,
                WidthRequest      = 25,
                BackgroundColor   = Color.White,
                VerticalOptions   = LayoutOptions.StartAndExpand,
                HorizontalOptions = LayoutOptions.End,
                ClassId           = quizInfo.DBId,
                StyleId           = quizInfo.Category
            };

            SyncDownload.Clicked += this.SyncDownload_Clicked;
            topStack.Children.Add(SyncDownload);

            ImageButton SyncNoChange = new ImageButton // 4
            {
                IsVisible         = false,
                Source            = "ic_cloud_done_black_48dp.png",
                HeightRequest     = 25,
                WidthRequest      = 25,
                BackgroundColor   = Color.White,
                VerticalOptions   = LayoutOptions.StartAndExpand,
                HorizontalOptions = LayoutOptions.End,
                ClassId           = quizInfo.DBId,
                StyleId           = quizInfo.Category
            };

            SyncNoChange.Clicked += this.SyncNoChange_Clicked;
            topStack.Children.Add(SyncNoChange);

            ActivityIndicator Syncing = new ActivityIndicator // 5
            {
                IsVisible         = false,
                Color             = Color.Accent,
                HeightRequest     = 25,
                WidthRequest      = 25,
                VerticalOptions   = LayoutOptions.StartAndExpand,
                HorizontalOptions = LayoutOptions.End,
            };

            topStack.Children.Add(Syncing);

            ImageButton imageButtonMenu = new ImageButton // 6
            {
                Source            = "ic_more_vert_black_48dp.png",
                HeightRequest     = 35,
                WidthRequest      = 35,
                BackgroundColor   = Color.White,
                VerticalOptions   = LayoutOptions.StartAndExpand,
                HorizontalOptions = LayoutOptions.End,
                ClassId           = quizInfo.DBId
            };

            imageButtonMenu.Clicked += this.ImageButtonMenu_Clicked;
            topStack.Children.Add(imageButtonMenu);

            if (CredentialManager.Username == quizInfo.AuthorName)
            {
                imageButtonMenu.StyleId = "Delete";
            }
            else
            {
                imageButtonMenu.StyleId = "Unsubscribe";
            }

            frameStack.Children.Add(topStack);

            BoxView Seperator = new BoxView // 1
            {
                Color             = Color.LightGray,
                CornerRadius      = 1,
                HeightRequest     = 2,
                WidthRequest      = Application.Current.MainPage.Width - 75,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                VerticalOptions   = LayoutOptions.CenterAndExpand
            };

            frameStack.Children.Add(Seperator);

            Label Author = new Label // 2
            {
                Text              = "Created by: " + quizInfo.AuthorName,
                FontSize          = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
                VerticalOptions   = LayoutOptions.End,
                HorizontalOptions = LayoutOptions.StartAndExpand
            };

            frameStack.Children.Add(Author);

            // The sync button thats active in the current frame
            ImageButton activeSync;

            if (quizInfo.SyncStatus == (int)SyncStatusEnum.Offline) // SyncOffline
            {
                SyncOffline.IsVisible = true;
                activeSync            = SyncOffline;
            }
            else if (quizInfo.SyncStatus == (int)SyncStatusEnum.Synced) // SyncNoChange
            {
                SyncNoChange.IsVisible = true;
                activeSync             = SyncNoChange;
            }
            else if (quizInfo.SyncStatus == (int)SyncStatusEnum.NeedUpload && quizInfo.AuthorName == CredentialManager.Username) // SyncUpload
            {
                SyncUpload.IsVisible = true;
                activeSync           = SyncUpload;
            }
            else if (quizInfo.SyncStatus == (int)SyncStatusEnum.NeedDownload ||
                     quizInfo.SyncStatus == (int)SyncStatusEnum.NotDownloadedAndNeedDownload) // SyncDownload
            {
                SyncDownload.IsVisible = true;
                activeSync             = SyncDownload;
                if (quizInfo.SyncStatus == (int)SyncStatusEnum.NotDownloadedAndNeedDownload) // Sync Download & notLocal yet
                {
                    frame.StyleId = "notLocal";
                }
            }
            else
            {
                SyncOffline.IsVisible = true;
                activeSync            = SyncOffline;
            }

            TapGestureRecognizer recognizer = new TapGestureRecognizer();

            recognizer.Tapped += async(object sender, EventArgs e) =>
            {
                if (frame.StyleId != "notLocal")
                {
                    frame.GestureRecognizers.Remove(recognizer);
                    frame.BackgroundColor           = Color.LightGray;
                    Seperator.Color                 = Color.Gray;
                    imageButtonMenu.BackgroundColor = Color.LightGray;
                    activeSync.BackgroundColor      = Color.LightGray;

                    // Load the quiz associated with this DBId
                    Quiz newQuiz = new Quiz(quizInfo.DBId);
                    //await this.RemoveMenu(frameMenu);
                    await this.Navigation.PushAsync(new Game(newQuiz));

                    frame.BackgroundColor           = Color.Default;
                    Seperator.Color                 = Color.LightGray;
                    imageButtonMenu.BackgroundColor = Color.White;
                    activeSync.BackgroundColor      = Color.White;
                    frame.GestureRecognizers.Add(recognizer);
                }
                else
                {
                    await this.DisplayAlert("Hold on!", "In order to study with this quiz, you must download it first", "OK");
                }
            };

            frame.GestureRecognizers.Add(recognizer);
            frame.Content = frameStack;

            return(frame);
        }