public MessageSummaryViewModel(MessageSummary state,
                                       MessageListViewModel list,
                                       IProfileDataQueryFactory queryFactory)
        {
            _detail = new Lazy <MessageDetailViewModel>(() => new MessageDetailViewModel(state, queryFactory));
            Id      = state.Id;
            Refresh(state);

            Archive = ReactiveCommand.CreateFromObservable(() => list.Archive.Execute(new[] { Id }));

            Delete = ReactiveCommand.CreateFromObservable(() => list.Delete.Execute(new[] { Id }));

            ToggleFlag = ReactiveCommand.CreateFromObservable(() =>
            {
                var command = IsFlagged ? list.ClearFlag : list.SetFlag;
                IsFlagged   = !IsFlagged;
                return(command.Execute(new[] { Id }));
            });
            ToggleFlag.ThrownExceptions
            .ObserveOn(RxApp.MainThreadScheduler)
            .Do(_ => IsFlagged = !IsFlagged)
            .Subscribe()
            .DisposeWith(_disposables);

            ToggleRead = ReactiveCommand.CreateFromObservable(() =>
            {
                var command = IsRead ? list.MarkAsUnread : list.MarkAsRead;
                IsRead      = !IsRead;
                return(command.Execute(new[] { Id })
                       .Do(_ => _markingAsReadSubscription.Disposable = null));
            });
            ToggleRead.ThrownExceptions
            .ObserveOn(RxApp.MainThreadScheduler)
            .Do(_ => IsRead = !IsRead)
            .Subscribe()
            .DisposeWith(_disposables);

            Move = ReactiveCommand.CreateFromObservable(() => list.Move.Execute(new[] { Id }));

            MoveToJunk = ReactiveCommand.CreateFromObservable(() => list.Move.Execute(new[] { Id }));
        }
Esempio n. 2
0
        public MailFolderViewModel(Node <MailFolder, string> node,
                                   MailBoxViewModel mailBox,
                                   IProfileDataQueryFactory queryFactory,
                                   IMailService mailService)
        {
            Id         = node.Item.Id;
            ParentId   = node.Item.ParentId;
            Name       = node.Item.Name;
            Type       = node.Item.Type;
            IsFavorite = node.Item.IsFavorite;
            Messages   = new MessageListViewModel(node.Item.Id, node.Item.Type,
                                                  mailBox, queryFactory, mailService, Activator);

            Synchronize = ReactiveCommand.CreateFromObservable(() => Observable
                                                               .StartAsync((token) => mailService.SynchronizeMessagesAsync(node.Item.Id, token))
                                                               .TakeUntil(CancelSynchronization));
            Synchronize.IsExecuting
            .ToPropertyEx(this, x => x.IsSynchronizing)
            .DisposeWith(_disposables);
            Synchronize.ThrownExceptions
            .Subscribe(ex => this.Log().Error(ex))
            .DisposeWith(_disposables);
            CancelSynchronization = ReactiveCommand.Create(() => { }, Synchronize.IsExecuting);

            Move = ReactiveCommand.CreateFromTask(async() =>
            {
                var result = await mailBox.PromptUserToSelectFolder(
                    "Move a folder",
                    "Select another folder to move to:",
                    includeRoot: true,
                    CanMoveTo);
                this.Log().Debug(result);
            });

            node.Children.Connect()
            .Transform(n => new MailFolderViewModel(n, mailBox, queryFactory, mailService))
            .Sort(SortExpressionComparer <MailFolderViewModel> .Ascending(f => f.Name))
            .ObserveOn(RxApp.MainThreadScheduler)
            .Bind(out _childFolders)
            .DisposeMany()
            .Subscribe()
            .DisposeWith(_disposables);

            mailService.MessageChanges
            .Where(changes => changes.AffectsFolder(node.Item.Id))
            .SelectMany(_ => CountMessages(queryFactory, node.Item.Id))
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(x =>
            {
                UnreadCount = x.UnreadCount;
                TotalCount  = x.TotalCount;
            })
            .DisposeWith(_disposables);

            CountMessages(queryFactory, node.Item.Id)
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(x =>
            {
                UnreadCount = x.UnreadCount;
                TotalCount  = x.TotalCount;
            })
            .DisposeWith(_disposables);
        }