Beispiel #1
0
        public async override void RowSelected(UITableView tableView, Foundation.NSIndexPath indexPath)
        {
            // Loading indicator
            var loadingOverlay = new LoadingOverlay(Container.View.Bounds);

            Container.View.Add(loadingOverlay);

            IList <Screen> screens = null;

            try
            {
                screens = (await LessonUtil.GetScreensByLessonAsync(Items[indexPath.Row].Id));
            }
            catch (Exception e)
            {
                var alert = UIAlertController.Create("Something goes wrong", String.Format("Please check your Internet connection and try again.{0} Details: {1}", Environment.NewLine, e.Message), UIAlertControllerStyle.Alert);
                alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                Container.PresentViewController(alert, true, null);
            }
            loadingOverlay.HideThenRemove();

            if ((screens != null) && (screens.Count > 0))
            {
                Container.NavigationController.PushViewController(new LessonScreenViewController(Items[indexPath.Row].Id, screens, 0, new List <ScreenAnswer>()), true);
            }
            else
            {
                var alert = UIAlertController.Create("Oops!", "There is nothing in this lesson.", UIAlertControllerStyle.Alert);
                alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                Container.PresentViewController(alert, true, null);
            }

            tableView.DeselectRow(indexPath, true);
        }
Beispiel #2
0
 private async Task getScreenFromServer()
 {
     try {
         screens = await LessonUtil.GetScreensByLessonAsync(lesson_id);
     } catch (Exception ex) {
         DialogFactory.ToastDialog(this, "Data Error", "Error Connecting to Server,please try again later!", Constants.TEXT_ERROR);
     }
     if (screens != null)
     {
         //make text and images
         screen = screens [0];
         string type = screen.Type;
         if (type.Contains("text"))
         {
             var texts = screen.Texts;
             Utils.makeTextViews(texts, this, ll_content, Color.Black);
         }
         if (type.Contains("image"))
         {
             var imageslist = screen.Images;
             var images     = new List <Image> (imageslist);
             Utils.makeTextImages(this, ll_content, images);
         }
         //make return button
         Utils.makeReturnButton(this, ll_content);
         ll_content.Visibility = ViewStates.Visible;
         ll_load.Visibility    = ViewStates.Invisible;
     }
 }
        public async Task getDataFromServer()
        {
            IList <Lesson> lessons = null;

            try {
                lessons = await LessonUtil.GetLessonsByModuleAsync(module_id);
            } catch (Exception ex) {
                DialogFactory.ToastDialog(this, "Data Error", "Error Connecting to Server,please try again later!", Constants.LESSON_ERROR);
            }
            if (lessons != null)
            {
                sourceDateList = new List <SortModel> ();
                foreach (var lesson in lessons)
                {
                    string name = lesson.Title;
                    if (name.Length > 0)
                    {
                        string    firstAlpha = name.Substring(0, 1).ToUpper();
                        SortModel temp       = new SortModel();
                        temp.SortLetters = firstAlpha;
                        temp.Name        = name;
                        temp.LessonId    = lesson.Id;
                        temp.ScreenCount = lesson.ScreenCount;
                        sourceDateList.Add(temp);
                    }
                    else
                    {
                        continue;
                    }
                }
                sourceDateList.Sort();
            }
        }
Beispiel #4
0
        public async override void RowSelected(UITableView tableView, Foundation.NSIndexPath indexPath)
        {
            // Loading indicator
            var loadingOverlay = new LoadingOverlay(Container.View.Bounds);

            Container.View.Add(loadingOverlay);

            IList <Lesson> lessons = null;

            try
            {
                lessons = await LessonUtil.GetLessonsByModuleAsync(Items[indexPath.Row].Id);
            }
            catch (Exception e)
            {
                var alert = UIAlertController.Create("Something goes wrong", e.Message, UIAlertControllerStyle.Alert);
                alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                Container.PresentViewController(alert, true, null);
            }
            loadingOverlay.HideThenRemove();

            if ((lessons != null) && (lessons.Count > 0))
            {
                Container.NavigationController.PushViewController(new LessonsViewController(lessons, Items[indexPath.Row].Name), true);
            }
            else
            {
                var alert = UIAlertController.Create("Oops!", "There is nothing in this module", UIAlertControllerStyle.Alert);
                alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                Container.PresentViewController(alert, true, null);
            }

            tableView.DeselectRow(indexPath, true);
        }
Beispiel #5
0
        public async void getLessonsFromServer()
        {
            try {
                var lessons = await LessonUtil.GetLessonsByModuleAsync(module_id);

                lessonList = new List <Lesson> (lessons);
            } catch (Exception ex) {
                DialogFactory.ToastDialog(this, "Server busy", "Server is busy,please drag down to refresh later", 0);
                tv_pull_list_header_title.Text = "Server busy!";
                tv_pull_list_header_title.SetTextColor(Color.Red);
            }
            if (lessonList != null)
            {
                lv_list.onRefreshComplete();
                lv_list.Adapter = new MyListViewAdapter(this, lessonList);
                lv_list.setOnRefreshListener(new MyRefreshListener(this));
                lv_list.Visibility = ViewStates.Visible;
                ll_load.Visibility = ViewStates.Invisible;
            }
        }
Beispiel #6
0
        public async override void ViewDidLoad()
        {
            base.ViewDidLoad();

            View.BackgroundColor = UIColor.White;

            var modulesTable = new UITableView();

            View.AddSubview(modulesTable);

            View.ConstrainLayout(() =>
                                 modulesTable.Frame.Top == View.Frame.Top &&
                                 modulesTable.Frame.Left == View.Frame.Left &&
                                 modulesTable.Frame.Right == View.Frame.Right &&
                                 modulesTable.Frame.Bottom == View.Frame.Bottom
                                 );

            // Loading indicator
            var loadingOverlay = new LoadingOverlay(View.Bounds);

            View.AddSubview(loadingOverlay);

            try
            {
                Modules = await LessonUtil.GetModulesAsync();
            }
            catch (Exception e)
            {
                var alert = UIAlertController.Create("Something goes wrong", e.Message, UIAlertControllerStyle.Alert);
                alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                PresentViewController(alert, true, null);
            }

            if (Modules != null)
            {
                modulesTable.Source = new ModulesTableSource(this, Modules);
                modulesTable.ReloadData();
            }

            loadingOverlay.HideThenRemove();
        }
Beispiel #7
0
        public async void populateChoices(View view)
        {
            //query to server
            int id = -1;

            options = new List <Option> (await LessonUtil.GetOptionsByScreenAsync(lesson_id, screen.Id + ""));
            var activity = Activity as ScreensActivity;

            if (activity.answer.ContainsKey(screen.Id))
            {
                id = int.Parse(activity.answer [screen.Id]);
            }
            for (int i = 0; i < options.Count; i++)
            {
                Option option = options [i];
                if (option != null)
                {
                    RadioButton rdBtn = new RadioButton(Application.Context);
                    rdBtn.Id = (i);
                    if (!option.Title.Contains("."))
                    {
                        rdBtn.Text = Utils.populateSelection(option.Order) + ". " + option.Title;
                    }
                    rdBtn.SetTextSize(ComplexUnitType.Sp, 18.0f);
                    if (i == id)
                    {
                        //set checked
                        rdBtn.Checked = true;
                    }
                    choicesRadioGroup.AddView(rdBtn);
                }
            }
            //remove default selected
            if (id == -1)
            {
                (choicesRadioGroup as RadioGroup).ClearCheck();
            }
            activity.validateBtns();
        }
Beispiel #8
0
        private async void getVideoUrlFromServer()
        {
            var result = await LessonUtil.GetScreensByLessonAsync(lessonId);

            if (result != null && result.Count > 0)
            {
                Constants.screens = result;
                //jump to screenactivity
                Intent intent = new Intent(this, typeof(ScreensActivity));
                intent.PutExtra(Constants.LESSON_TITLE, currentLesson.Title);
                intent.PutExtra(Constants.LESSON_DES, currentLesson.Description);
                intent.PutExtra(Constants.LESSON_ID, currentLesson.Id);
                StartActivity(intent);
                Finish();
            }
            else
            {
                lv_list.Visibility = ViewStates.Visible;
                ll_load.Visibility = ViewStates.Invisible;
                DialogFactory.ToastDialog(this, "Empty lesson", "Oops,this lesson has not uploaded yet!", 0);
            }
        }
Beispiel #9
0
        public async void getModulesFromServer()
        {
            try {
                var modules = await LessonUtil.GetModulesAsync();

                Constants.currentUser = await UserUtil.GetCurrentUserAsync();

                moduleList = new List <Module> (modules);
            } catch (Exception ex) {
                DialogFactory.ToastDialog(activity, "Server busy", "Server is busy,please login later", Constants.LOGIN_TIMEOUT);
            }
            if (Constants.currentUser == null)
            {
                //illegal login
                DialogFactory.ToastDialog(activity, "Login timeout", "Timeout,please login again", Constants.LOGIN_TIMEOUT);
            }
            else
            {
                lv_list.Adapter    = new MyModuleListAdapter(moduleList, this.activity);
                lv_list.Visibility = ViewStates.Visible;
                ll_load.Visibility = ViewStates.Invisible;
            }
        }
Beispiel #10
0
        private async void getCorrectAnswerFromServer()
        {
            //get correct answer
            foreach (var screen in Constants.screens)
            {
                string type = screen.Type;
                if (type != null)
                {
                    if (type.Equals("question") || type.Equals("audio_question") || type.Equals("question_audio"))
                    {
                        IList <Option> list = await LessonUtil.GetOptionsByScreenAsync(Intent.GetIntExtra(Constants.LESSON_ID, 0), screen.Id + "");

                        options = new List <Option> (list);
                        foreach (var option in options)
                        {
                            if (option.Detail == true)
                            {
                                correctAnswers.Add(screen.Id, option.Id);
                                switch (option.Order)
                                {
                                case 0:
                                    standardAnswer.Add("A");
                                    break;

                                case 1:
                                    standardAnswer.Add("B");
                                    break;

                                case 2:
                                    standardAnswer.Add("C");
                                    break;

                                case 3:
                                    standardAnswer.Add("D");
                                    break;

                                case 4:
                                    standardAnswer.Add("E");
                                    break;

                                case 5:
                                    standardAnswer.Add("F");
                                    break;

                                case 6:
                                    standardAnswer.Add("G");
                                    break;

                                default:
                                    standardAnswer.Add("H");
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            //send answer to server
            await SendLessonAnswers(Intent.GetIntExtra(Constants.LESSON_ID, 0), Constants.screenAnswers);

            //judge how many right
            foreach (var answer in Constants.screenAnswers)
            {
                if (correctAnswers.ContainsKey(answer.ScreenId))
                {
                    if (correctAnswers [answer.ScreenId] == answer.OptionId)
                    {
                        //correct
                        correct++;
                        right.Add(true);
                    }
                    else
                    {
                        right.Add(false);
                    }
                }
            }
            //complement
            for (int i = right.Count; i < standardAnswer.Count; i++)
            {
                right.Add(false);
            }
            //set adapter
            adapter          = new ResultListViewAdapter(this);
            lv_score.Adapter = adapter;
            pb_anti.Max      = standardAnswer.Count;
            pb_anti.Progress = correct;
            int score = (int)((correct / (float)standardAnswer.Count) * 100);

            if (score <= 50)
            {
                tv_initiate.SetTextColor(Color.Red);
            }
            else
            {
                tv_initiate.SetTextColor(Color.Green);
            }
            tv_initiate.Text = "Score:" + score;
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            #region Navigation Bar Buttons
            if (Index < Screens.Count - 1)
            {
                NavigationItem.SetRightBarButtonItem(new UIBarButtonItem("Next", UIBarButtonItemStyle.Plain, async(sender, e) =>
                {
                    if (SelectedOptionId != Constants.DefaultOptionId)
                    {
                        // Workaround for not having current user in database
                        User currentUser = null;
                        try
                        {
                            currentUser = await UserUtil.GetCurrentUserAsync();
                        }
                        catch
                        {
                            var alert = UIAlertController.Create("Something goes wrong", "Please check your Internet connection and try again.", UIAlertControllerStyle.Alert);
                            alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                            PresentViewController(alert, true, null);
                        }

                        if (currentUser == null)
                        {
                            var alert = UIAlertController.Create("Something goes wrong", "Please check your Internet connection, sign out and try again.", UIAlertControllerStyle.Alert);
                            alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                            PresentViewController(alert, true, null);
                        }
                        else
                        {
                            Answers.RemoveAll(x => x.ScreenId == Screens[Index].Id);
                            Answers.Add(new ScreenAnswer
                            {
//                                        UserId = UserUtil.CurrentUser.Id.GetValueOrDefault(),
                                UserId   = currentUser.Id.GetValueOrDefault(),
                                ScreenId = Screens[Index].Id,
                                OptionId = SelectedOptionId,
                            });
                        }
                    }

                    PushNextScreen();
                }), true);
            }
            else
            {
                NavigationItem.SetRightBarButtonItem(new UIBarButtonItem("Submit", UIBarButtonItemStyle.Plain, async(sender, e) =>
                {
                    #region Add Answer
                    if (SelectedOptionId != Constants.DefaultOptionId)
                    {
                        // Workaround for not having current user in database
                        User currentUser = null;
                        try
                        {
                            currentUser = await UserUtil.GetCurrentUserAsync();
                        }
                        catch
                        {
                            var alert = UIAlertController.Create("Something goes wrong", String.Format("Please check your Internet connection and try again."), UIAlertControllerStyle.Alert);
                            alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                            PresentViewController(alert, true, null);
                        }

                        if (currentUser == null)
                        {
                            var alert = UIAlertController.Create("Something goes wrong", String.Format("Please check your Internet connection and try again."), UIAlertControllerStyle.Alert);
                            alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                            PresentViewController(alert, true, null);
                        }
                        else
                        {
                            Answers.RemoveAll(x => x.ScreenId == Screens[Index].Id);
                            Answers.Add(new ScreenAnswer
                            {
//                                        UserId = UserUtil.CurrentUser.Id.GetValueOrDefault(),
                                UserId   = currentUser.Id.GetValueOrDefault(),
                                ScreenId = Screens[Index].Id,
                                OptionId = SelectedOptionId,
                            });
                        }
                    }
                    #endregion

                    #region Send Answers to server
                    if ((Answers != null) && (Answers.Count > 0))
                    {
                        var loadingOverlay = new LoadingOverlay(View.Bounds);
                        Tuple <string, int, int> response = null;
                        try
                        {
                            View.Add(loadingOverlay);
                            response = await LessonUtil.SendLessonAnswers(LessonId, Answers);
                        }
                        catch (Exception ex)
                        {
                            var alert = UIAlertController.Create("Something goes wrong", ex.Message, UIAlertControllerStyle.Alert);
                            alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                            PresentViewController(alert, true, null);
                        }
                        finally
                        {
                            loadingOverlay.HideThenRemove();
                        }

                        if (response != null)
                        {
                            var alertTitle   = response.Item1;
                            var alertMessage = String.Format("Your result: {0}/{1}", response.Item3, response.Item2);

                            var submissionAlert = UIAlertController.Create(alertTitle, alertMessage, UIAlertControllerStyle.Alert);
                            submissionAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, (UIAlertAction obj) =>
                            {
                                NavigationController.PopToRootViewController(true);
                            }));
                            PresentViewController(submissionAlert, true, null);
                        }
                    }
                    else
                    {
                        var alert = UIAlertController.Create("Nothing to submit", "You haven't selected any answer", UIAlertControllerStyle.Alert);
                        alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                        PresentViewController(alert, true, null);
                    }
                    #endregion
                }), true);
            }
            #endregion

            View.BackgroundColor = UIColor.White;

            var scrollView = new UIScrollView();
            View.AddSubview(scrollView);
            View.ConstrainLayout(() =>
                                 scrollView.Frame.Top == View.Frame.Top &&
                                 scrollView.Frame.Left == View.Frame.Left &&
                                 scrollView.Frame.Right == View.Frame.Right &&
                                 scrollView.Frame.Bottom == View.Frame.Bottom
                                 );

            var stackView = new UIStackView
            {
                Axis         = UILayoutConstraintAxis.Vertical,
                Alignment    = UIStackViewAlignment.Leading,
                Distribution = UIStackViewDistribution.EqualSpacing,
                Spacing      = Constants.BigGap,
            };
            scrollView.AddSubview(stackView);

            var twiceHorizontalPad = Constants.HorizontalPad * 2;
            View.ConstrainLayout(() =>
                                 stackView.Frame.Top == scrollView.Frame.Top + Constants.VerticalPad &&
                                 stackView.Frame.Bottom == scrollView.Frame.Bottom - Constants.VerticalPad &&
                                 stackView.Frame.Left == scrollView.Frame.Left + Constants.HorizontalPad &&
                                 stackView.Frame.Right == scrollView.Frame.Right + Constants.HorizontalPad &&
                                 stackView.Frame.Width == scrollView.Frame.Width - twiceHorizontalPad // required!
                                 );
            scrollView.ContentSize = stackView.Frame.Size;

            var mediaPlayerUrl = !String.IsNullOrWhiteSpace(Screens[Index].VideoUrl) ? Screens[Index].VideoUrl : Screens[Index].AudioUrl;
            if (!String.IsNullOrWhiteSpace(mediaPlayerUrl))
            {
                try
                {
                    MediaPlayer = new AVPlayer(NSUrl.FromString(mediaPlayerUrl));
                }
                catch (Exception ex)
                {
                    // Display error or skip this part !!
//                    throw ex;
                    var alert = UIAlertController.Create("Something goes wrong", "Invalid format for video or audio data", UIAlertControllerStyle.Alert);
                    alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                    PresentViewController(alert, true, null);
                }
                var playerViewController = new AVPlayerViewController
                {
                    Player = MediaPlayer,
                };
                AddChildViewController(playerViewController);
                stackView.AddArrangedSubview(playerViewController.View);
                View.ConstrainLayout(() =>
                                     playerViewController.View.Frame.Width == stackView.Frame.Width
                                     );

                if (String.IsNullOrWhiteSpace(Screens[Index].VideoUrl))
                {
                    View.ConstrainLayout(() =>
                                         playerViewController.View.Frame.Height == Constants.AudioPlayerHeight
                                         );
                }
            }

            if (Screens[Index].Images != null)
            {
                foreach (var image in Screens[Index].Images)
                {
                    var imageView = new UIImageView();
                    imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
                    try
                    {
                        using (var url = new NSUrl(image.Url))
                        {
                            using (var data = NSData.FromUrl(url))
                            {
                                imageView.Image = UIImage.LoadFromData(data);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // Display error!!
//                        throw ex;
                        var alert = UIAlertController.Create("Something goes wrong", "Invalid format for image data", UIAlertControllerStyle.Alert);
                        alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                        PresentViewController(alert, true, null);
                    }
                    var imageRatio     = imageView.IntrinsicContentSize.Width / imageView.IntrinsicContentSize.Height;
                    var stackViewWidth = View.Frame.Width - twiceHorizontalPad;
                    var resizedHeight  = stackViewWidth / imageRatio; // Pretty hacky!! Maybe can achieve the same thing by setting vertical hugging priority?
                    stackView.AddArrangedSubview(imageView);
                    View.ConstrainLayout(() =>
                                         imageView.Frame.Height == resizedHeight
                                         );
                }
            }

            if (Screens[Index].Texts != null)
            {
                foreach (var text in Screens[Index].Texts)
                {
                    var textLabel = new UILabel
                    {
                        Text          = text.TextValue,
                        Lines         = 0,
                        LineBreakMode = UILineBreakMode.WordWrap,
                        Font          = UIFont.SystemFontOfSize(Constants.LargeFontSize),
                    };
                    stackView.AddArrangedSubview(textLabel);
                    View.ConstrainLayout(() =>
                                         textLabel.Frame.Width == stackView.Frame.Width
                                         );
                }
            }

            if (Screens[Index].Options != null)
            {
                // Question
                var questionLabel = new UILabel
                {
                    Text          = Screens[Index].Question,
                    Lines         = 0,
                    LineBreakMode = UILineBreakMode.WordWrap,
                    Font          = UIFont.SystemFontOfSize(Constants.LargeFontSize),
                };
                stackView.AddArrangedSubview(questionLabel);
                View.ConstrainLayout(() =>
                                     questionLabel.Frame.Width == stackView.Frame.Width
                                     );

                // Options
                var optionsUIs = new List <Tuple <UIButton, UIButton, Option> >();

                var i = 1;
                foreach (var option in Screens[Index].Options)
                {
                    var transparentGap = new UIView();
                    stackView.AddArrangedSubview(transparentGap);
                    View.ConstrainLayout(() =>
                                         transparentGap.Frame.Height == 5f
                                         );

                    var optionStackView = new UIStackView
                    {
                        Axis         = UILayoutConstraintAxis.Horizontal,
                        Alignment    = UIStackViewAlignment.Center,
                        Distribution = UIStackViewDistribution.EqualSpacing,
                        Spacing      = Constants.SmallGap,
                    };
                    stackView.AddArrangedSubview(optionStackView);

                    var optionRadioButton = new UIButton(UIButtonType.RoundedRect);
                    optionStackView.AddArrangedSubview(optionRadioButton);
                    optionRadioButton.SetImage(UIImage.FromBundle("checkmark.png"), UIControlState.Normal);
                    optionRadioButton.SetImage(UIImage.FromBundle("blank.png"), UIControlState.Disabled);
                    optionRadioButton.Enabled = false;
                    View.ConstrainLayout(() =>
                                         optionRadioButton.Frame.Height == 20f &&
                                         optionRadioButton.Frame.Width == 20f
                                         );

                    var optionTextButton = new UIButton(UIButtonType.System)
                    {
                        LineBreakMode       = UILineBreakMode.WordWrap,
                        HorizontalAlignment = UIControlContentHorizontalAlignment.Left,
                        VerticalAlignment   = UIControlContentVerticalAlignment.Center,
                        Font = UIFont.SystemFontOfSize(Constants.LargeFontSize),
                    };
                    optionTextButton.SetTitle(option.Title, UIControlState.Normal);
                    optionStackView.AddArrangedSubview(optionTextButton);

                    var maxWidth   = View.Frame.Width - Constants.HorizontalPad * 2 - optionRadioButton.Frame.Width;
                    var textSize   = UIHelper.GetTextSize(option.Title, optionTextButton.Font, maxWidth, float.MaxValue);
                    var textHeight = textSize.Height;
                    View.ConstrainLayout(() =>
                                         optionTextButton.Frame.Height == textHeight
                                         );

                    optionsUIs.Add(new Tuple <UIButton, UIButton, Option>(optionRadioButton, optionTextButton, option));
                    i++;
                }

                foreach (var tuple in optionsUIs)
                {
                    tuple.Item2.TouchUpInside += (sender, e) =>
                    {
                        SelectedOptionId    = tuple.Item3.Id;
                        tuple.Item1.Enabled = true;

                        foreach (var otherTuple in optionsUIs)
                        {
                            if (!Object.ReferenceEquals(sender, otherTuple.Item2))
                            {
                                otherTuple.Item1.Enabled = false;
                            }
                        }
                    };
                }
            }

            // Start media automatically
            if ((MediaPlayer != null) && (!String.IsNullOrWhiteSpace(mediaPlayerUrl)))
            {
                MediaPlayer.Play();
            }
        }