Ejemplo n.º 1
0
        public MainPageViewModel(DocumentManager documentManager, IAppCenterLogger appCenterLogger, IEventAggregator eventAggregator)
        {
            this.DocumentManager = documentManager;
            this.AppCenterLogger = appCenterLogger;
            this.EventAggregator = eventAggregator;

            Documents = DocumentManager.Documents
                .ToReadOnlyReactiveCollectionForCurrentContext();

            NewDocumentTitle = new ReactivePropertySlim<string>();
            AddDocumentCommand = NewDocumentTitle
                .Select(x => !string.IsNullOrEmpty(x))
                .ToReactiveCommand()
                .WithSubscribe(() =>
                {
                    if (NewDocumentTitle.Value == "野菜")
                    {
                        throw new InvalidOperationException("野菜 is invalid");
                    }

                    DocumentManager.AddDocument(NewDocumentTitle.Value);
                    AppCenterLogger.TrackEvent("Document created", ("name", NewDocumentTitle.Value));
                    NewDocumentTitle.Value = "";
                });

            SelectedDocument = new ReactivePropertySlim<Document>();
            IsEditorBladeOpen = SelectedDocument
                .Select(x => x != null)
                .ToReadOnlyReactivePropertySlim();

            BladeMode = DocumentManager.DocumentEditMode
                .Select(x => x == DocumentEditMode.Normal ? Microsoft.Toolkit.Uwp.UI.Controls.BladeMode.Normal : Microsoft.Toolkit.Uwp.UI.Controls.BladeMode.Fullscreen)
                .ToReadOnlyReactivePropertySlim();

            PreviewMarkdownText = SelectedDocument
                .SelectMany(x => x?.Content ?? Observable.Return(""))
                .Throttle(TimeSpan.FromSeconds(1))
                .ObserveOnUIDispatcher()
                .ToReadOnlyReactivePropertySlim();

            IsPreviewOpen = IsPreviewOpenNotifier.ToReadOnlyReactivePropertySlim();

            SwitchPreviewCommand = new ReactiveCommand()
                .WithSubscribe(() => IsPreviewOpenNotifier.SwitchValue());

            RemoveDocumentCommand = new ReactiveCommand<Document>()
                .WithSubscribe(x =>
                {
                    DocumentManager.RemoveDocument(x.Id.Value);
                    AppCenterLogger.TrackEvent("Document removed", ("name", x.Title.Value), ("content", x.Content.Value));
                    IsPreviewOpenNotifier.TurnOff();

                    EventAggregator.GetEvent<NotifyEvent>()
                        .Publish($"{x.Title.Value} を削除しました。");
                });

        }