Example #1
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);
            });
        }