Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Reactive.Linq;
- using ReactiveUI;
- using XmlAuthority.Common;
- using XmlAuthority.Common.Extensions;
- using XmlAuthority.DocumentManagement.Contracts.Commands.Presentation;
- using XmlAuthority.DocumentManagement.Desktop.Properties;
- using XmlAuthority.DocumentManagement.Desktop.Services;
- using XmlAuthority.Services.Application.Contracts;
- namespace XmlAuthority.DocumentManagement.Desktop.Commands
- {
- internal sealed class SaveDocumentCommand : PresentationCommandBase, ISaveDocumentCommand
- {
- private readonly ICurrentDocumentIdMonitor _currentDocumentIdMonitor;
- public SaveDocumentCommand(
- ISaveFileService saveFileService,
- IMessageService messageService,
- ICurrentDocumentIdMonitor currentDocumentIdMonitor)
- {
- _currentDocumentIdMonitor = currentDocumentIdMonitor;
- var command = ReactiveCommand.Create(() => { }, CanExecuteWhenDocumentOpen);
- Instance = command;
- Instance.ThrownExceptions.Subscribe(ex => messageService.ShowWarning(string.Format(Resources.DocumentSavingErrorMessage, ex.Message)));
- command.WithLatestFrom(_currentDocumentIdMonitor.CurrentDocumentIdStream, (_, tgt) => tgt)
- .Subscribe(async tgt => await saveFileService.SaveAsync(tgt.Value));
- }
- private IObservable<bool> CanExecuteWhenDocumentOpen => _currentDocumentIdMonitor.CurrentDocumentIdStream.Select(x => x.HasValue);
- }
- }
- public static IObservable<TResult> WithLatestFrom<TLeft, TRight, TResult>(
- this IObservable<TLeft> source,
- IObservable<TRight> other,
- Func<TLeft, TRight, TResult> resultSelector)
- {
- return source.Publish(os =>
- other.Select(a => os
- .Select(b => resultSelector(b, a)))
- .Switch());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement