示例#1
0
        public NotificationsViewModel(IApplicationService applicationService)
        {
            _applicationService = applicationService;
            Title = "Notifications";

            ReadSelectedCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                if (GroupedNotifications.SelectMany(x => x.Notifications).All(x => !x.IsSelected))
                {
                    applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkAsRead()).ToBackground();
                    _notifications.Clear();
                }
                else
                {
                    var selected = GroupedNotifications.SelectMany(x => x.Notifications)
                                   .Where(x => x.IsSelected && x.Notification.Unread).ToList();

                    var tasks = selected
                                .Select(t => _applicationService.GitHubClient.Notification.MarkAsRead(int.Parse(t.Id)));

                    Task.WhenAll(tasks).ToBackground();

                    foreach (var s in selected)
                    {
                        _notifications.Remove(s.Notification);
                    }
                }
            });

            _notifications.Changed.Select(_ => Unit.Default)
            .Merge(_notifications.ItemChanged.Select(_ => Unit.Default))
            .Subscribe(_ =>
            {
                GroupedNotifications = _notifications.GroupBy(x => x.Repository.FullName).Select(x =>
                {
                    var items         = x.Select(y => new NotificationItemViewModel(y, GoToNotification));
                    var notifications = new ReactiveList <NotificationItemViewModel>(items);
                    return(new NotificationGroupViewModel(x.Key, notifications));
                }).ToList();
            });


            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                var all           = ActiveFilter == AllFilter;
                var participating = ActiveFilter == ParticipatingFilter;
                var req           = new Octokit.NotificationsRequest {
                    All = all, Participating = participating, Since = DateTimeOffset.MinValue
                };
                var notifictions = await applicationService.GitHubClient.Notification.GetAllForCurrent(req);
                _notifications.Reset(notifictions);
            });

            this.WhenAnyValue(x => x.ActiveFilter).Subscribe(x =>
            {
                _notifications.Clear();
                LoadCommand.ExecuteIfCan();
            });
        }
示例#2
0
        public NotificationsViewModel(ISessionService applicationService)
        {
            _applicationService = applicationService;
            Title = "Notifications";

            _showEditButton = this.WhenAnyValue(x => x.ActiveFilter)
                              .Select(x => x != AllFilter)
                              .ToProperty(this, x => x.ShowEditButton);

            var groupedNotifications = new ReactiveList <NotificationGroupViewModel>();

            GroupedNotifications = groupedNotifications;

            Notifications = _notifications.CreateDerivedCollection(y => new NotificationItemViewModel(y, GoToNotification, DeleteNotification));
            Notifications.Changed.Where(x => x.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
            .Select(_ => Notifications)
            .Subscribe(notifications => groupedNotifications.Reset(notifications.GroupBy(x => x.Notification.Repository.FullName).Select(x => {
                var items = notifications.CreateDerivedCollection(y => y, filter: y => y.Notification.Repository.FullName == x.Key);
                return(new NotificationGroupViewModel(x.Key, items));
            })));

            _anyItemsSelected = Notifications.Changed
                                .SelectMany(x => Notifications)
                                .Select(x => x.WhenAnyValue(y => y.IsSelected))
                                .Merge()
                                .Select(x => Notifications.Select(y => y.IsSelected).Any(y => y))
                                .ToProperty(this, x => x.IsAnyItemsSelected);

            ReadSelectedCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                if (GroupedNotifications.SelectMany(x => x.Notifications).All(x => !x.IsSelected))
                {
                    var request = new Octokit.MarkAsReadRequest {
                        LastReadAt = DateTimeOffset.Now
                    };
                    applicationService.GitHubClient.Notification.MarkAsRead(request).ToBackground();
                    _notifications.Clear();
                }
                else
                {
                    var selected = GroupedNotifications.SelectMany(x => x.Notifications)
                                   .Where(x => x.IsSelected && x.Notification.Unread).ToList();

                    var tasks = selected
                                .Select(t => _applicationService.GitHubClient.Notification.MarkAsRead(int.Parse(t.Id)));

                    Task.WhenAll(tasks).ToBackground();

                    _notifications.RemoveAll(selected.Select(y => y.Notification));
                }
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => {
                var all           = ActiveFilter == AllFilter;
                var participating = ActiveFilter == ParticipatingFilter;
                var req           = new Octokit.NotificationsRequest {
                    All = all, Participating = participating, Since = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(365))
                };
                _notifications.Reset(await applicationService.GitHubClient.Notification.GetAllForCurrent(req));
            });

            _notifications.CountChanged
            .Where(_ => ActiveFilter == UnreadFilter)
            .Subscribe(_notificationCount.OnNext);

            this.WhenAnyValue(x => x.ActiveFilter).Skip(1).Subscribe(x =>
            {
                _notifications.Clear();
                LoadCommand.ExecuteIfCan();
            });
        }