public ProfileViewModel(IParentService parentService, IUserDialogs userDialogs,
                                IMvxMessenger mvxMessenger, AppHelper appHelper) : base(userDialogs, mvxMessenger, appHelper)
        {
            _parentService = parentService;

            SaveChangesCommand = ReactiveCommand.CreateFromObservable <Unit, ParentEntity>((param) =>
            {
                return(NewProfileImage != null ?
                       _parentService.UploadProfileImage(NewProfileImage.GetStream())
                       .SelectMany((_) => _parentService.Update(SelfParent.FirstName, SelfParent.LastName, SelfParent.Birthdate, SelfParent.Email, string.Concat(SelfParent.PhonePrefix, SelfParent.PhoneNumber))) :

                       _parentService.Update(SelfParent.FirstName, SelfParent.LastName, SelfParent.Birthdate, SelfParent.Email, string.Concat(SelfParent.PhonePrefix, SelfParent.PhoneNumber)));
            });


            SaveChangesCommand.Subscribe(AccountUpdatedHandler);

            SaveChangesCommand.IsExecuting.Subscribe((IsLoading) => HandleIsExecuting(IsLoading, AppResources.Profile_Save_Changes));

            SaveChangesCommand.ThrownExceptions.Subscribe(HandleExceptions);

            RefreshCommand = ReactiveCommand.CreateFromObservable <Unit, ParentEntity>((param) => _parentService.GetProfileInformation());

            RefreshCommand.Subscribe((ParentEntity) => {
                SelfParent.HydrateWith(ParentEntity);
                ResetCommonProps();
                IsDirtyMonitoring = true;
            });

            RefreshCommand.IsExecuting.Subscribe((IsLoading) => HandleIsExecuting(IsLoading, AppResources.Profile_Loading_Data));

            RefreshCommand.ThrownExceptions.Subscribe(HandleExceptions);

            DeleteAccountCommand = ReactiveCommand
                                   .CreateFromObservable <Unit, string>((param) =>
            {
                return(Observable.FromAsync <bool>((_) => _userDialogs.ConfirmAsync(new ConfirmConfig()
                {
                    Title = AppResources.Profile_Confirm_Account_Deleting
                })).Where((confirmed) => confirmed).Do((_) => HandleIsExecuting(true, AppResources.Profile_Account_Deleting))
                       .SelectMany((_) => _parentService.DeleteAccount())
                       .Do((_) => HandleIsExecuting(false, AppResources.Profile_Account_Deleting)));
            });


            DeleteAccountCommand.Subscribe((_) =>
            {
                Bullytect.Core.Config.Settings.AccessToken = null;
                ShowViewModel <AuthenticationViewModel>(new AuthenticationViewModel.AuthenticationParameter()
                {
                    ReasonForAuthentication = AuthenticationViewModel.ACCOUNT_DELETED
                });
            });


            DeleteAccountCommand.ThrownExceptions.Subscribe(HandleExceptions);


            TakePhotoCommand = ReactiveCommand.CreateFromObservable <Unit, MediaFile>((_) => appHelper.PickPhotoStream());


            TakePhotoCommand.Subscribe((ImageStream) => {
                _userDialogs.HideLoading();
                NewProfileImage = ImageStream;
                OnNewSelectedImage(ImageStream);
            });

            TakePhotoCommand.ThrownExceptions.Subscribe(HandleExceptions);
        }
Exemple #2
0
        public EditSonViewModel(IUserDialogs userDialogs, IMvxMessenger mvxMessenger, IParentService parentService,
                                ISocialMediaService socialMediaService, AppHelper appHelper, IOAuthService oauthService, ISchoolService schoolService) : base(userDialogs, mvxMessenger, appHelper)
        {
            _parentService      = parentService;
            _socialMediaService = socialMediaService;
            _schoolService      = schoolService;
            _oauthService       = oauthService;


            RefreshCommand = ReactiveCommand.CreateFromObservable <Unit, PageModel>((param) => !PageLoaded ? LoadPageModel(): Observable.Empty <PageModel>());

            RefreshCommand.Subscribe(OnPageModelLoaded);

            RefreshCommand.IsExecuting.Subscribe((isExecuting) => HandleIsExecuting(isExecuting, AppResources.Common_Loading));

            RefreshCommand.ThrownExceptions.Subscribe(HandleExceptions);

            ForceRefreshCommand = ReactiveCommand.CreateFromObservable <Unit, PageModel>((param) => LoadPageModel());

            ForceRefreshCommand.Subscribe(OnPageModelLoaded);

            ForceRefreshCommand.IsExecuting.Subscribe((isExecuting) => HandleIsExecuting(isExecuting, AppResources.Common_Loading));

            ForceRefreshCommand.ThrownExceptions.Subscribe(HandleExceptions);

            TakePhotoCommand = ReactiveCommand.CreateFromObservable <Unit, MediaFile>((param) => _appHelper.PickPhotoStream());

            TakePhotoCommand.Subscribe((ImageFile) =>
            {
                _userDialogs.HideLoading();
                NewProfileImage = ImageFile;
                OnNewSelectedImage(ImageFile);
            });

            TakePhotoCommand.ThrownExceptions.Subscribe(HandleExceptions);


            SaveChangesCommand = ReactiveCommand
                                 .CreateFromObservable <Unit, bool>((param) =>
            {
                return((CurrentSon.Identity == null ?
                        _parentService.AddSonToSelfParent(CurrentSon.FirstName, CurrentSon.LastName, CurrentSon.Birthdate, CurrentSon.School.Identity) :
                        _parentService.UpdateSonInformation(CurrentSon.Identity, CurrentSon.FirstName, CurrentSon.LastName, CurrentSon.Birthdate, CurrentSon.School.Identity))
                       .Do((SonEntity) => CurrentSon.HydrateWith(SonEntity))
                       .SelectMany((SonEntity) => _socialMediaService
                                   .SaveAllSocialMedia(
                                       CurrentSon.Identity,
                                       CurrentSocialMedia.Select(s => { s.Son = CurrentSon.Identity; return s; }).ToList()
                                       ).Catch <IList <SocialMediaEntity>, NoSocialMediaFoundException>(ex => Observable.Return(new List <SocialMediaEntity>()))
                                   )
                       .Do((SocialMediaEntities) => CurrentSocialMedia.ReplaceRange(SocialMediaEntities))

                       .SelectMany((_) =>
                {
                    return NewProfileImage != null ?
                    _parentService.UploadSonProfileImage(CurrentSon.Identity, NewProfileImage.GetStream()) :
                    Observable.Empty <ImageEntity>();
                })
                       .Select((_) => true)
                       .DefaultIfEmpty(true));
            });

            SaveChangesCommand.Subscribe((_) =>
            {
                NewProfileImage = null;
                OnSonUpdated(CurrentSon);
                _userDialogs.ShowSuccess(AppResources.EditSon_Saved_Changes_Successfully);
                IsDirtyMonitoring = true;
            });

            SaveChangesCommand.IsExecuting.Subscribe((isLoading) => HandleIsExecuting(isLoading, AppResources.EditSon_Saving_Changes));

            SaveChangesCommand.ThrownExceptions.Subscribe(HandleExceptions);

            SaveSchoolCommand = ReactiveCommand.CreateFromObservable <Unit, SchoolEntity>((param) => _schoolService.CreateSchool(NewSchool.Name, NewSchool.Residence, NewSchool.Latitude, NewSchool.Longitude, NewSchool.Province, NewSchool.Tfno, NewSchool.Email));

            SaveSchoolCommand.Subscribe((SchoolAdded) =>
            {
                NewSchool = new SchoolEntity();
                CurrentSon.School.HydrateWith(SchoolAdded);
                _appHelper.ShowAlert(AppResources.EditSon_School_Saved);
                OnSchoolAdded(SchoolAdded);
            });


            SaveSchoolCommand.IsExecuting.Subscribe((isLoading) => HandleIsExecuting(isLoading, AppResources.EditSon_Saving_School));

            SaveSchoolCommand.ThrownExceptions.Subscribe(HandleExceptions);


            FindSchoolsCommand = ReactiveCommand.CreateFromObservable <string, IList <SchoolEntity> >(
                (name) => _schoolService.FindSchools(name));

            FindSchoolsCommand.IsExecuting.Subscribe((isLoading) => HandleIsExecuting(isLoading, AppResources.EditSon_Find_School));

            FindSchoolsCommand.ThrownExceptions.Subscribe(HandleExceptions);

            FindSchoolsCommand.Subscribe((SchoolsFounded) => {
                SearchPerformed = true;
                Schools.ReplaceRange(SchoolsFounded);
            });
        }