Пример #1
0
        /// <summary>
        /// Sets up stuff that needs storage/network permissions
        /// </summary>
        protected override async void OnStart()
        {
            string tempPicturesDir = App.Path + "/" + "Pictures";

            if (Directory.Exists(tempPicturesDir))
            {
                Directory.Delete(App.Path + "/" + "Pictures", true);
            }

            CredentialManager.Username = "******";
            Directory.CreateDirectory(App.Path + $"dflt");

            if (!App.Current.Properties.ContainsKey("Tutorial"))
            {
                App.Current.Properties.Add("Tutorial", "Yes");
                await this.SavePropertiesAsync();
            }

            BugReportHandler.Setup();
            await ThreadTimer.RunServerChecksAsync();

            if (Directory.GetDirectories(App.UserPath).Length < 5)
            {
                await DependencyService.Get <IGetStorage>().SetupDefaultQuizzesAsync(App.UserPath);
            }
            BugReportHandler.ProcessCrashLog();
        }
Пример #2
0
        /// <summary>
        /// Triggered when the scrollsearch is moved, checks if more levels need to be loaded as the user scrolls down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void ScrollSearch_Scrolled(object sender, ScrolledEventArgs e)
        {
            ScrollView scrollView     = sender as ScrollView;
            double     scrollingSpace = scrollView.ContentSize.Height - scrollView.Height;

            if (scrollingSpace <= e.ScrollY)
            {
                try
                {
                    if (this.PickerCategory.SelectedIndex >= 0)
                    {
                        this.currentChunk++;

                        string category = this.PickerCategory.Items[this.PickerCategory.SelectedIndex];

                        await Task.Run(() =>
                                       this.SetupNetworkQuizzes(category));
                    }
                }
                catch (Exception ex)
                {
                    BugReportHandler.SaveReport(ex);
                    await this.DisplayAlert("Oops, network failure", "Try again later", "Ok");
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Refreshes the page's quiz content
        /// </summary>
        private async Task RefreshAsync()
        {
            this.LabelNoQuiz.IsVisible = false;
            this.SearchedStack.Children.Clear();
            this.quizzesFeatured.Clear();
            try
            {
                if (CredentialManager.IsLoggedIn)
                {
                    this.PickerCategory.IsVisible          = true;
                    this.ToolbarItemSearchButton.IsEnabled = true;
                    Device.BeginInvokeOnMainThread(() => {
                        this.SearchedStack.Children.Clear();
                        this.ActivityIndicator.IsVisible = true;
                        this.ActivityIndicator.IsRunning = true;
                    });
                    await Task.Run(() => this.AddQuizzes(SearchUtils.GetQuizzesByAuthorChunked("BizQuiz", 1)));

                    Device.BeginInvokeOnMainThread(() =>
                    {
                        this.ActivityIndicator.IsVisible = false;
                        this.ActivityIndicator.IsRunning = false;
                    });
                }
                else
                {
                    this.PickerCategory.IsVisible          = false;
                    this.ToolbarItemSearchButton.IsEnabled = false;
                    this.SearchedStack.Children.Clear();
                    this.SearchedStack.Children.Add(
                        new Frame()
                    {
                        CornerRadius      = 10,
                        HorizontalOptions = LayoutOptions.CenterAndExpand,
                        Content           = new Label
                        {
                            Text = "Create an account to create, search for, and use quizzes " +
                                   "created by the BizQuiz community!",
                            HorizontalTextAlignment = TextAlignment.Center,
                            FontSize = 38
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                BugReportHandler.SaveReport(ex);
                await this.DisplayAlert("Error", "Couldn't get quizzes", "Ok");
            }
        }
Пример #4
0
 /// <summary>
 /// Gets a quizinfo based on a dbid
 /// </summary>
 /// <param name="dbId">id of the quiz to fetch</param>
 /// <returns>the quiz matching the DBID (null if there isn't one)</returns>
 public static QuizInfo GetQuizInfo(string dbId)
 {
     try
     {
         RealmConfiguration threadConfig = App.realmConfiguration(RosterPath);
         Realm realmDB = Realm.GetInstance(threadConfig);
         return(realmDB.All <QuizInfo>().Where
                    (quizInfo => quizInfo.DBId == dbId).First());
     }
     catch (Exception ex)
     {
         BugReportHandler.SaveReport(ex);
         return(null);
     }
 }
Пример #5
0
        /// <summary>
        /// Read byte array of what the server returns.
        /// </summary>
        /// <param name="size">Size of the file to read</param>
        /// <returns>Byte array of data that the server returns</returns>
        private static byte[] ReadByteArray(int size)
        {
            try
            {
                if (CrossConnectivity.Current.IsConnected)
                {
                    byte[] buffer = new byte[1024];

                    List <byte> data      = new List <byte>();
                    int         bytes     = -1;
                    int         bytesRead = 0;
                    do
                    {
                        int toRead = 0;
                        if ((size - bytesRead) > buffer.Length)
                        {
                            toRead = buffer.Length;
                        }
                        else
                        {
                            toRead = size - bytesRead;
                        }

                        bytes      = ssl.Read(buffer, 0, toRead);
                        bytesRead += bytes;

                        if (bytes > 0)
                        {
                            for (int i = 0; i < bytes; i++)
                            {
                                data.Add(buffer[i]);
                            }
                        }
                    } while (data.Count < size);
                    data.RemoveRange(size, data.Count - size);
                    return(data.ToArray());
                }
                else
                {
                    return(new byte[0]);
                }
            }
            catch (Exception ex)
            {
                BugReportHandler.SaveReport(ex);
                return(new byte[0]);
            }
        }
Пример #6
0
        private async void Submit_Clicked(object sender, EventArgs e)
        {
            if (this.BugBodyEntry.Text == "" || String.IsNullOrWhiteSpace((string)this.CategoryPicker.SelectedItem) || this.BugTitleEntry.Text == "")
            {
                await this.DisplayAlert("Cannot Submit", "Please fill all required fields before submitting a bug report.", "Keep editing");
            }
            else
            {
                BugReport report = new BugReport(this.BugTitleEntry.Text, this.CategoryPicker.SelectedItem as string, this.BugBodyEntry.Text, this.ImagePath);
                BugReportHandler.SaveReport(report);
                Task task = Task.Run(() => BugReportHandler.SubmitSavedReports());
                await this.DisplayAlert("Report Saved", "We'll send your report as soon as we can, and our team will take a look at your issue. Thanks for letting us know!", "OK");

                await task;
                await this.Navigation.PopAsync();
            }
        }
Пример #7
0
 /// <summary>
 /// Given a dbid, deletes it from the roster
 /// </summary>
 /// <param name="dbId">ID of quiz to delete</param>
 /// <returns>indicated whether or not the quizz was successfully deleted</returns>
 public static bool DeleteQuizInfo(string dbId)
 {
     try
     {
         RealmConfiguration threadConfig = App.realmConfiguration(RosterPath);
         Realm realmDB = Realm.GetInstance(threadConfig);
         realmDB.Write(() =>
         {
             realmDB.Remove(realmDB.All <QuizInfo>().Where(quizInfo => quizInfo.DBId == dbId).First());
         });
         return(true);
     }
     catch (Exception ex)
     {
         BugReportHandler.SaveReport(ex);
         return(false);
     }
 }
Пример #8
0
 /// <summary>
 /// Conducts a search of the online database
 /// </summary>
 /// <returns></returns>
 private async Task SearchAsync()
 {
     try
     {
         this.isLoading = true;
         Device.BeginInvokeOnMainThread(() =>
         {
             this.SearchedStack.Children.Clear();
             this.ActivityIndicator.IsRunning = true;
         });
         List <QuizInfo> chunk = new List <QuizInfo>();
         if (this.searchType == SearchType.Title)
         {
             chunk = SearchUtils.GetQuizzesByQuizNameChunked(this.SearchBar.Text, this.currentChunk);
         }
         else
         {
             chunk = SearchUtils.GetQuizzesByAuthorChunked(this.SearchBar.Text, this.currentChunk);
         }
         if (chunk.Count < 20)
         {
             await Task.Run(() => this.AddQuizzes(chunk));
         }
         Device.BeginInvokeOnMainThread(() =>
         {
             if (this.SearchedStack.Children.Count() == 0)
             {
                 this.LableNone.IsVisible = true;
             }
             else
             {
                 this.LableNone.IsVisible = false;
             }
             this.ActivityIndicator.IsRunning = false;
         });
         this.isLoading = false;
     }
     catch (Exception ex)
     {
         BugReportHandler.SaveReport(ex);
         await this.DisplayAlert("Search Failed", "Try again later", "Ok");
     }
 }
Пример #9
0
 public Task SetupDefaultQuizzesAsync(string userpath)
 {
     try
     {
         var assembly = IntrospectionExtensions.GetTypeInfo(typeof(AppDelegate)).Assembly;
         using (Stream dbAssetStream = assembly.GetManifestResourceStream("appFBLA2019.dflt.zip"))
         {
             MemoryStream memStream = new MemoryStream();
             dbAssetStream.CopyTo(memStream);
             memStream.Position = 0;
             File.WriteAllBytes(userpath + "dflt.zip", memStream.ToArray());
             ZipFile.ExtractToDirectory(userpath + "dflt.zip", userpath, true);
             File.Delete(userpath + "dflt.zip");
         }
     }
     catch (Exception ex)
     {
         BugReportHandler.SaveReport(ex, "SetupDefaultLevels");
     }
     return(Task.CompletedTask);
 }
Пример #10
0
 /// <summary>
 /// Copies the default levels from the assets to the user folder
 /// </summary>
 /// <param name="userpath"></param>
 /// <returns></returns>
 public async Task SetupDefaultQuizzesAsync(string userpath)
 {
     if (await CrossPermissions.Current.CheckPermissionStatusAsync(Plugin.Permissions.Abstractions.Permission.Storage) == PermissionStatus.Granted)
     {
         try
         {
             using (Stream dbAssetStream = Android.App.Application.Context.Assets.Open("dflt.zip"))
             {
                 MemoryStream memStream = new MemoryStream();
                 dbAssetStream.CopyTo(memStream);
                 memStream.Position = 0;
                 File.WriteAllBytes(userpath + "dflt.zip", memStream.ToArray());
                 ZipFile.ExtractToDirectory(userpath + "dflt.zip", userpath, true);
                 File.Delete(userpath + "dflt.zip");
             }
         }
         catch (Exception ex)
         {
             BugReportHandler.SaveReport(ex, "SetupDefaultLevels");
         }
     }
 }
Пример #11
0
        /// <summary>
        /// Update local data through synchronzing data with server.
        /// Runs on a timed background task and repeats a synchronization every two minutes.
        /// </summary>
        /// <returns></returns>
        public static async Task RunServerChecksAsync()
        {
            await CredentialManager.CheckLoginStatusAsync();

            await Task.Run(async() => await QuizRosterDatabase.UpdateLocalDatabaseAsync());

            var minutes = TimeSpan.FromMinutes(2);

            Device.StartTimer(minutes, () =>
            {
                Task task = Task.Run(async() =>
                {
                    if (CredentialManager.IsLoggedIn)
                    {
                        await CredentialManager.CheckLoginStatusAsync();
                    }

                    await QuizRosterDatabase.UpdateLocalDatabaseAsync();
                    BugReportHandler.SubmitSavedReports();
                });
                // Return true to continue the timer
                return(true);
            });
        }