Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. using Berlitz.App.Helpers;
  2. using Berlitz.App.Services.Authentication;
  3. using Berlitz.App.Services.Dialog;
  4. using Berlitz.App.Services.Navigation;
  5. using Berlitz.App.Services.Student;
  6. using Berlitz.App.ViewModels.Base;
  7. using Berlitz.App.ViewModels.Profile;
  8. using Berlitz.App.Views;
  9. using Berlitz.App.Views.Home;
  10. using Berlitz.App.Views.Profile;
  11. using Berlitz.Core.Models;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows.Input;
  19. using Xamarin.Forms;
  20.  
  21. namespace Berlitz.App.ViewModels.Home
  22. {
  23. public class AppIntroViewModel : BaseViewModel, IHandleViewAppearing, IHandleViewDisappearing
  24. {
  25. private const int VIDEO_PAGE_POSITION = 0;
  26. private const int PROFILE_PAGE_POSITION = 1;
  27. private const string VIDEO_PAGE_MESSAGE = "Afirmo que assisti ao vídeo\ne desejo prosseguir";
  28. private const string PROFILE_PAGE_MESSAGE = "SALVAR";
  29.  
  30. private readonly IAuthenticationService _authenticationService;
  31. private readonly INavigationService _navigationService;
  32. private readonly IDialogService _dialogService;
  33. private readonly IStudentService _studentService;
  34. public AppIntroViewModel(
  35. IAuthenticationService authenticationService,
  36. INavigationService navigationService,
  37. IDialogService dialogService,
  38. IStudentService studentService
  39. )
  40. {
  41. _authenticationService = authenticationService;
  42. _navigationService = navigationService;
  43. _dialogService = dialogService;
  44. _studentService = studentService;
  45. SetPage(position: VIDEO_PAGE_POSITION);
  46. }
  47.  
  48. public Task OnViewAppearingAsync(VisualElement view)
  49. {
  50. return Task.FromResult(true);
  51. }
  52.  
  53. public Task OnViewDisappearingAsync(VisualElement view)
  54. {
  55. return Task.FromResult(true);
  56. }
  57.  
  58. public override async Task InitializeAsync(object navigationData)
  59. {
  60. //return base.InitializeAsync(navigationData);
  61. var homeVideoViewModel = Locator.Instance.Resolve(typeof(HomeVideoViewModel)) as HomeVideoViewModel;
  62.  
  63. var profileFormViewModel = Locator.Instance.Resolve(typeof(ProfileFormViewModel)) as ProfileFormViewModel;
  64. profileFormViewModel.Student = await _studentService.GetStudentAsync(_authenticationService.AuthenticatedUser.Token);
  65. profileFormViewModel.Student.Profile = new ProfileDto()
  66. {
  67. Id = profileFormViewModel.Student.Id,
  68. Sons = new List<ProfileSonDto>()
  69. {
  70. new ProfileSonDto(),new ProfileSonDto(),new ProfileSonDto(),new ProfileSonDto(),new ProfileSonDto()
  71. }
  72. };
  73. Pages = new ObservableCollection<VisualElement>()
  74. {
  75. new HomeVideoView() { BindingContext = homeVideoViewModel },
  76. new ProfileFormView(){ BindingContext = profileFormViewModel }
  77. };
  78.  
  79. }
  80.  
  81. private int _position;
  82. public int Position
  83. {
  84. get { return _position; }
  85. set
  86. {
  87. _position = value;
  88. OnPropertyChanged(nameof(Position));
  89. }
  90. }
  91.  
  92. private string _buttonLabelText;
  93. public string ButtonLabelText
  94. {
  95. get { return _buttonLabelText; }
  96. set
  97. {
  98. _buttonLabelText = value;
  99. OnPropertyChanged(nameof(ButtonLabelText));
  100. }
  101. }
  102.  
  103. private bool _showBackButton;
  104. public bool ShowBackButton
  105. {
  106. get { return _showBackButton; }
  107. set
  108. {
  109. _showBackButton = value;
  110. OnPropertyChanged(nameof(ShowBackButton));
  111. }
  112. }
  113.  
  114. private ObservableCollection<VisualElement> _pages;
  115. public ObservableCollection<VisualElement> Pages
  116. {
  117. get { return _pages; }
  118.  
  119. set
  120. {
  121. _pages = value;
  122. OnPropertyChanged(nameof(Pages));
  123. }
  124. }
  125.  
  126.  
  127. public ICommand NextStepCommand => new Helpers.AsyncCommand(NextStepAsync);
  128. private async Task NextStepAsync()
  129. {
  130.  
  131. SetPage(PROFILE_PAGE_POSITION);
  132.  
  133. if (Position == PROFILE_PAGE_POSITION)
  134. {
  135. //Submit
  136. var profileFormView = Pages.Where(x => x.GetType() == typeof(ProfileFormView)).FirstOrDefault();//[1];
  137. if (profileFormView != null)
  138. {
  139. var viewModel = ((ProfileFormView)profileFormView).BindingContext as ProfileFormViewModel;
  140. //viewModel.SaveProfileCommand.Execute(null);
  141. var success = await viewModel.SaveProfileAsync();
  142. if (success)
  143. {
  144. _dialogService.ShowToast("Perfil salvo com sucesso! Bem-vindo ao aplicativo Berlitz!");
  145. await _navigationService.InitializeAsync();
  146. }
  147. }
  148. }
  149. }
  150.  
  151. private void SetPage(int position)
  152. {
  153. Position = position;
  154. if (position == VIDEO_PAGE_POSITION)
  155. {
  156. ButtonLabelText = VIDEO_PAGE_MESSAGE;
  157. ShowBackButton = false;
  158. }
  159. else if (position == PROFILE_PAGE_POSITION)
  160. {
  161. ButtonLabelText = PROFILE_PAGE_MESSAGE;
  162. ShowBackButton = true;
  163. }
  164. }
  165.  
  166.  
  167. public ICommand BackCommand => new Helpers.AsyncCommand(GoBackAsync);
  168. private async Task GoBackAsync()
  169. {
  170. SetPage(VIDEO_PAGE_POSITION);
  171. }
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement