Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Berlitz.App.Helpers;
- using Berlitz.App.Services.Authentication;
- using Berlitz.App.Services.Dialog;
- using Berlitz.App.Services.Navigation;
- using Berlitz.App.Services.Student;
- using Berlitz.App.ViewModels.Base;
- using Berlitz.App.ViewModels.Profile;
- using Berlitz.App.Views;
- using Berlitz.App.Views.Home;
- using Berlitz.App.Views.Profile;
- using Berlitz.Core.Models;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Input;
- using Xamarin.Forms;
- namespace Berlitz.App.ViewModels.Home
- {
- public class AppIntroViewModel : BaseViewModel, IHandleViewAppearing, IHandleViewDisappearing
- {
- private const int VIDEO_PAGE_POSITION = 0;
- private const int PROFILE_PAGE_POSITION = 1;
- private const string VIDEO_PAGE_MESSAGE = "Afirmo que assisti ao vídeo\ne desejo prosseguir";
- private const string PROFILE_PAGE_MESSAGE = "SALVAR";
- private readonly IAuthenticationService _authenticationService;
- private readonly INavigationService _navigationService;
- private readonly IDialogService _dialogService;
- private readonly IStudentService _studentService;
- public AppIntroViewModel(
- IAuthenticationService authenticationService,
- INavigationService navigationService,
- IDialogService dialogService,
- IStudentService studentService
- )
- {
- _authenticationService = authenticationService;
- _navigationService = navigationService;
- _dialogService = dialogService;
- _studentService = studentService;
- SetPage(position: VIDEO_PAGE_POSITION);
- }
- public Task OnViewAppearingAsync(VisualElement view)
- {
- return Task.FromResult(true);
- }
- public Task OnViewDisappearingAsync(VisualElement view)
- {
- return Task.FromResult(true);
- }
- public override async Task InitializeAsync(object navigationData)
- {
- //return base.InitializeAsync(navigationData);
- var homeVideoViewModel = Locator.Instance.Resolve(typeof(HomeVideoViewModel)) as HomeVideoViewModel;
- var profileFormViewModel = Locator.Instance.Resolve(typeof(ProfileFormViewModel)) as ProfileFormViewModel;
- profileFormViewModel.Student = await _studentService.GetStudentAsync(_authenticationService.AuthenticatedUser.Token);
- profileFormViewModel.Student.Profile = new ProfileDto()
- {
- Id = profileFormViewModel.Student.Id,
- Sons = new List<ProfileSonDto>()
- {
- new ProfileSonDto(),new ProfileSonDto(),new ProfileSonDto(),new ProfileSonDto(),new ProfileSonDto()
- }
- };
- Pages = new ObservableCollection<VisualElement>()
- {
- new HomeVideoView() { BindingContext = homeVideoViewModel },
- new ProfileFormView(){ BindingContext = profileFormViewModel }
- };
- }
- private int _position;
- public int Position
- {
- get { return _position; }
- set
- {
- _position = value;
- OnPropertyChanged(nameof(Position));
- }
- }
- private string _buttonLabelText;
- public string ButtonLabelText
- {
- get { return _buttonLabelText; }
- set
- {
- _buttonLabelText = value;
- OnPropertyChanged(nameof(ButtonLabelText));
- }
- }
- private bool _showBackButton;
- public bool ShowBackButton
- {
- get { return _showBackButton; }
- set
- {
- _showBackButton = value;
- OnPropertyChanged(nameof(ShowBackButton));
- }
- }
- private ObservableCollection<VisualElement> _pages;
- public ObservableCollection<VisualElement> Pages
- {
- get { return _pages; }
- set
- {
- _pages = value;
- OnPropertyChanged(nameof(Pages));
- }
- }
- public ICommand NextStepCommand => new Helpers.AsyncCommand(NextStepAsync);
- private async Task NextStepAsync()
- {
- SetPage(PROFILE_PAGE_POSITION);
- if (Position == PROFILE_PAGE_POSITION)
- {
- //Submit
- var profileFormView = Pages.Where(x => x.GetType() == typeof(ProfileFormView)).FirstOrDefault();//[1];
- if (profileFormView != null)
- {
- var viewModel = ((ProfileFormView)profileFormView).BindingContext as ProfileFormViewModel;
- //viewModel.SaveProfileCommand.Execute(null);
- var success = await viewModel.SaveProfileAsync();
- if (success)
- {
- _dialogService.ShowToast("Perfil salvo com sucesso! Bem-vindo ao aplicativo Berlitz!");
- await _navigationService.InitializeAsync();
- }
- }
- }
- }
- private void SetPage(int position)
- {
- Position = position;
- if (position == VIDEO_PAGE_POSITION)
- {
- ButtonLabelText = VIDEO_PAGE_MESSAGE;
- ShowBackButton = false;
- }
- else if (position == PROFILE_PAGE_POSITION)
- {
- ButtonLabelText = PROFILE_PAGE_MESSAGE;
- ShowBackButton = true;
- }
- }
- public ICommand BackCommand => new Helpers.AsyncCommand(GoBackAsync);
- private async Task GoBackAsync()
- {
- SetPage(VIDEO_PAGE_POSITION);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement