Exemplo n.º 1
0
        public UserViewModel(IScreen screen, User user, IUserRepository userRepository)
        {
            _userRepository = userRepository;
            HostScreen      = screen;
            // Commands
            var canSubmit = this.WhenAny(e => e.Code, code => code.Value.IsValid());

            Submit = ReactiveCommand.CreateFromTask(_ => userRepository.Submit(Model), canSubmit);
            Submit.Subscribe(result => MessageBox.Show(result ? "Success" : "Failure"));
            // Observe on UI thread
            Submit.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
            .Select(ex => new UserError("It will fail again, try anyway?", ex.Message))
            .SelectMany(UserError.Throw)
            .Subscribe(resolution =>
            {
                if (resolution == RecoveryOptionResult.RetryOperation)
                {
                    Submit.Execute();
                }
            });
            // Model subscription
            this.WhenAnyValue(e => e.Model).Where(e => e != null).Subscribe(model =>
            {
                Code  = model.Code;
                Group = model.Group;
            });
            Model = user;
            // Properties subscriptions
            this.WhenAnyValue(e => e.Group).Subscribe(group => Model.Group = group);
            this.WhenAnyValue(e => e.Code).Subscribe(code => Model.Code    = code);
            // Subscribe to error handle
            UserError.RegisterHandler(async error =>
            {
                // This shouldn't be a messagebox because is blocking the application, you must provide context and offer a resolution to the user not just showing "error"
                await Task.Delay(1);
                var message             = new StringBuilder();
                bool hasRecoveryOptions = error.ErrorCauseOrResolution.IsValid();
                if (hasRecoveryOptions)
                {
                    message.AppendLine(error.ErrorCauseOrResolution);
                }
                message.AppendLine(error.ErrorMessage);
                var result = MessageBox.Show(message.ToString(), "Alert!",
                                             hasRecoveryOptions ? MessageBoxButton.YesNo : MessageBoxButton.OK);

                return(hasRecoveryOptions && result == MessageBoxResult.Yes ?  RecoveryOptionResult.RetryOperation : RecoveryOptionResult.CancelOperation);
            });
        }