/// <summary> /// Sets up the page with quizzes the user has subscribed to from the category of the page /// </summary> public void Setup() { if (!this.IsLoading) { this.IsLoading = true; this.StackLayoutButtonStack.Children.Clear(); List <QuizInfo> quizzes = QuizRosterDatabase.GetRoster(this.category); if (quizzes.Count == 0) { Device.BeginInvokeOnMainThread(() => { StackLayout stack = new StackLayout(); stack.Children.Add(new Label { Text = "You don't have any quizzes in this category yet!", HorizontalTextAlignment = TextAlignment.Center, FontSize = 38 }); if (CredentialManager.IsLoggedIn) { stack.Children.Add(new Button { Text = "Make a quiz now", CornerRadius = 25, BackgroundColor = Color.Accent, TextColor = Color.White, FontSize = 26 }); (stack.Children[1] as Button).Clicked += (object sender, EventArgs e) => this.Navigation.PushAsync(new CreateNewQuizPage()); stack.Children.Add(new Button { Text = "Search for quizzes", CornerRadius = 25, BackgroundColor = Color.Accent, TextColor = Color.White, FontSize = 26 }); (stack.Children[2] as Button).Clicked += (object sender, EventArgs e) => this.Navigation.PushAsync(new SearchPage()); } Frame frame = new Frame() { CornerRadius = 10, HorizontalOptions = LayoutOptions.CenterAndExpand, Content = stack }; this.StackLayoutButtonStack.Children.Add(frame); }); } foreach (QuizInfo quiz in quizzes) { Frame frame = GenerateFrame(quiz); this.StackLayoutButtonStack.Children.Add(frame); } this.IsLoading = false; } }
/// <summary> /// Sets up the quizzes owned by this user as cards /// </summary> private async Task SetupQuizzes(string category) { await QuizRosterDatabase.UpdateLocalDatabaseAsync(); // A single LINQ statement to get all quizzes by the current // user that is logged in, and of the specified category, // taking the top 20 of the current chunk ordered by // the subscriber count of the quizzes List <QuizInfo> rosterCopy = QuizRosterDatabase.GetRoster(); List <QuizInfo> quizzes = rosterCopy. Where( quizInfo => (quizInfo.AuthorName == CredentialManager.Username && (quizInfo.Category == category || category == "All"))). OrderBy(quizInfo => quizInfo.SubscriberCount). Take(20 * this.currentChunk).ToList(); this.AddQuizzes(quizzes, false); }
/// <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)); } } }
/// <summary> /// Loads the profile content if the user is logged in. /// </summary> /// <returns> </returns> private async Task UpdateProfileContentAsync(string category) { if (!this.IsContentLoading) { this.IsContentLoading = true; this.QuizNumber.IsVisible = true; this.PickerCategory.IsVisible = false; this.PickerCategory.IsEnabled = false; this.StackLayoutQuizStack.Children.Clear(); this.StackLayoutQuizStack.IsEnabled = false; this.ActivityIndicator.IsVisible = true; this.ActivityIndicator.IsRunning = true; await Task.Run(async() => { await this.SetupQuizzes(category); int createdCountFromServer = ServerOperations.GetNumberOfQuizzesByAuthorName(CredentialManager.Username); string frameMessage = ""; if (createdCountFromServer >= 0) // Returns -1 if can't connect to server { if (createdCountFromServer == 0 && !QuizRosterDatabase.GetRoster() .Any(quizInfo => quizInfo.AuthorName == CredentialManager.Username)) { frameMessage = "You haven't made any quizzes yet!"; Device.BeginInvokeOnMainThread(() => { this.PickerCategory.IsVisible = false; this.QuizNumber.IsVisible = false; }); } else { Device.BeginInvokeOnMainThread(() => { this.QuizNumber.Text = "You have published a total of " + createdCountFromServer + " quizzes!"; this.PickerCategory.IsVisible = true; this.PickerCategory.IsEnabled = true; }); } } else { frameMessage = "Could not connect to BizQuiz servers. Please try again later."; Device.BeginInvokeOnMainThread(() => { this.PickerCategory.IsVisible = true; this.PickerCategory.IsEnabled = true; }); } if (frameMessage != "") { Frame frame = new Frame() { CornerRadius = 10, HorizontalOptions = LayoutOptions.CenterAndExpand, Content = new Label { Text = frameMessage, HorizontalTextAlignment = TextAlignment.Center, FontSize = 38 } }; Device.BeginInvokeOnMainThread(() => this.StackLayoutQuizStack.Children.Add(frame)); } }); if (this.ToolbarItems.Count <= 0) { ToolbarItem accountSettingsButton = new ToolbarItem(); accountSettingsButton.Clicked += this.ToolbarItemAccountSettings_Clicked; accountSettingsButton.Icon = ImageSource.FromFile("ic_settings_white_48dp.png") as FileImageSource; this.ToolbarItems.Add(accountSettingsButton); } this.IsOnLoginPage = false; this.ActivityIndicator.IsRunning = false; this.ActivityIndicator.IsVisible = false; this.StackLayoutQuizStack.IsEnabled = true; this.IsContentLoading = false; } }