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); }
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); }
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(); }
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(); } }