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); }
public ConnectionInfoManagementViewModel() { AddNewItemCommand = IsItemEditing.Inverse().ToReactiveCommand(); AddNewItemCommand.Subscribe(() => { SelectedItem.Value = null; EditingItem.Value = new T(); if (IsGroupSelected?.Value == true) { EditingItem.Value.GroupName = SelectedGroup.Value?.Name; } IsItemEditing.Value = true; }).AddTo(Disposable); ConnectCommand.Subscribe(async item => await ConfirmConnect(item)).AddTo(Disposable); IsItemSelected = SelectedItem.Select(x => x != null).ToReadOnlyReactiveProperty(); IsNotItemEditing = IsItemEditing.Inverse().ToReadOnlyReactiveProperty(); SelectedItem.Subscribe(x => { EditingItem.Value = SelectedItem.Value; IsItemEditing.Value = false; }).AddTo(Disposable); StartEditCommand = IsItemSelected .CombineLatest(IsItemEditing.Inverse(), (a, b) => a && b) .ToReactiveCommand(); StartEditCommand.Subscribe(() => { EditingItem.Value = SelectedItem.Value.CloneDeep(); IsItemEditing.Value = true; }).AddTo(Disposable); ReplicateCommand = IsItemSelected .CombineLatest(IsItemEditing.Inverse(), (a, b) => a && b) .ToReactiveCommand(); ReplicateCommand.Subscribe(() => { var replicated = SelectedItem.Value.CloneDeep(); replicated.Id = -1; SelectedItem.Value = null; EditingItem.Value = replicated; IsItemEditing.Value = true; }).AddTo(Disposable); RemoveCommand = IsItemSelected .CombineLatest(IsItemEditing.Inverse(), (a, b) => a && b) .ToAsyncReactiveCommand(); RemoveCommand.Subscribe(async() => { if (await Remove(SelectedItem.Value)) { Items.Remove(SelectedItem.Value); // Renew Windows JumpList JumpListHelper.RenewJumpList(await MainWindow.DbContext.EnumerateAllConnectionInfos()); } }).AddTo(Disposable); DiscardChangesCommand = IsItemEditing.ToReactiveCommand(); DiscardChangesCommand.Subscribe(() => { EditingItem.Value = SelectedItem.Value ?? new T(); IsItemEditing.Value = false; }).AddTo(Disposable); SaveChangesCommand = IsItemEditing.ToReactiveCommand(); SaveChangesCommand.Subscribe(async() => { var selectedItem = SelectedItem.Value; var item = EditingItem.Value; try { var(result, resultItem) = await Save(item); if (resultItem == null) { return; // FAILED } item = resultItem; // Replace with the saved item if (result) // ADDED { Items.Add(item); } else // UPDATED { var oldItem = Items.FirstOrDefault(x => x.Id == item.Id); if (oldItem != null) { var index = Items.IndexOf(oldItem); if (index >= 0) { Items.RemoveAt(index); Items.Insert(index, item); } } } // Renew Windows JumpList JumpListHelper.RenewJumpList(await MainWindow.DbContext.EnumerateAllConnectionInfos()); } catch (OperationCanceledException) // User manually canceled { return; } SelectedItem.Value = item; IsItemEditing.Value = false; }).AddTo(Disposable); // Connection info filterings FilterText .Throttle(TimeSpan.FromMilliseconds(500)) .ObserveOnDispatcher() .Subscribe(_ => RefreshCollectionView()) .AddTo(Disposable); SelectedGroup .ObserveOnDispatcher() .Subscribe(_ => RefreshCollectionView()) .AddTo(Disposable); // If any group is selected or not (except for "All") IsGroupSelected = SelectedGroup .Select(x => x?.Name != AllGroupName) .ToReadOnlyReactivePropertySlim() .AddTo(Disposable); // Group list extraction on connection info events Observable.CombineLatest( // When Add, Remove or Update Items.CollectionChangedAsObservable() .Select(_ => Unit.Default) .StartWith(Unit.Default), // When GroupName property in each element changed Items.ObserveElementPropertyChanged() .Where(x => x.EventArgs.PropertyName == nameof(ConnectionInfoBase.GroupName)) .Select(_ => Unit.Default) .StartWith(Unit.Default) ) .Throttle(TimeSpan.FromMilliseconds(500)) // Once 500 ms .ObserveOnDispatcher() .Subscribe(_ => { var selectedGroup = SelectedGroup.Value; // Reload group list Groups.Clear(); EnumerateGroups().ToList().ForEach(Groups.Add); // Reset selected group SelectedGroup.Value = (selectedGroup is null) ? Groups.FirstOrDefault() : selectedGroup; }) .AddTo(Disposable); }
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); }); }