Пример #1
0
        public NotificationsViewModel()
        {
            _notifications = new FilterableCollectionViewModel <NotificationModel, NotificationsFilterModel>("Notifications");
            _notifications.GroupingFunction = (n) => n.GroupBy(x => x.Repository.FullName);
            _notifications.Bind(x => x.Filter, () => LoadCommand.Execute(false));
            this.Bind(x => x.ShownIndex, x => {
                if (x == 0)
                {
                    _notifications.Filter = NotificationsFilterModel.CreateUnreadFilter();
                }
                else if (x == 1)
                {
                    _notifications.Filter = NotificationsFilterModel.CreateParticipatingFilter();
                }
                else
                {
                    _notifications.Filter = NotificationsFilterModel.CreateAllFilter();
                }
                ((IMvxCommand)ReadAllCommand).RaiseCanExecuteChanged();
            });
            this.Bind(x => x.IsLoading, ((IMvxCommand)ReadAllCommand).RaiseCanExecuteChanged);

            if (_notifications.Filter.Equals(NotificationsFilterModel.CreateUnreadFilter()))
            {
                _shownIndex = 0;
            }
            else if (_notifications.Filter.Equals(NotificationsFilterModel.CreateParticipatingFilter()))
            {
                _shownIndex = 1;
            }
            else
            {
                _shownIndex = 2;
            }
        }
        public override void ViewWillAppear(bool animated)
        {
            if (ToolbarItems != null && !IsSearching)
            {
                NavigationController.SetToolbarHidden(false, animated);
            }
            base.ViewWillAppear(animated);

            //Before we select which one, make sure we detach the event handler or silly things will happen
            _viewSegment.ValueChanged -= SegmentValueChanged;

            //Select which one is currently selected
            if (ViewModel.Notifications.Filter.Equals(NotificationsFilterModel.CreateUnreadFilter()))
            {
                _viewSegment.SelectedSegment = 0;
            }
            else if (ViewModel.Notifications.Filter.Equals(NotificationsFilterModel.CreateParticipatingFilter()))
            {
                _viewSegment.SelectedSegment = 1;
            }
            else
            {
                _viewSegment.SelectedSegment = 2;
            }

            _viewSegment.ValueChanged += SegmentValueChanged;
        }
 void SegmentValueChanged(object sender, EventArgs e)
 {
     if (_viewSegment.SelectedSegment == 0)
     {
         ViewModel.Notifications.ApplyFilter(NotificationsFilterModel.CreateUnreadFilter(), true);
     }
     else if (_viewSegment.SelectedSegment == 1)
     {
         ViewModel.Notifications.ApplyFilter(NotificationsFilterModel.CreateParticipatingFilter(), true);
     }
     else if (_viewSegment.SelectedSegment == 2)
     {
         ViewModel.Notifications.ApplyFilter(NotificationsFilterModel.CreateAllFilter(), true);
     }
 }
Пример #4
0
        public NotificationsViewModel(
            IMessageService messageService         = null,
            IApplicationService applicationService = null)
        {
            _messageService                 = messageService ?? GetService <IMessageService>();
            _applicationService             = applicationService ?? GetService <IApplicationService>();
            _notifications                  = new FilterableCollectionViewModel <Octokit.Notification, NotificationsFilterModel>("Notifications");
            _notifications.GroupingFunction = (n) => n.GroupBy(x => x.Repository.FullName);
            _notifications.Bind(x => x.Filter).Subscribe(_ => LoadCommand.Execute(false));

            this.Bind(x => x.ShownIndex).Subscribe(x => {
                if (x == 0)
                {
                    _notifications.Filter = NotificationsFilterModel.CreateUnreadFilter();
                }
                else if (x == 1)
                {
                    _notifications.Filter = NotificationsFilterModel.CreateParticipatingFilter();
                }
                else
                {
                    _notifications.Filter = NotificationsFilterModel.CreateAllFilter();
                }
                ((IMvxCommand)ReadAllCommand).RaiseCanExecuteChanged();
            });
            this.Bind(x => x.IsLoading).Subscribe(_ => ((IMvxCommand)ReadAllCommand).RaiseCanExecuteChanged());

            if (_notifications.Filter.Equals(NotificationsFilterModel.CreateUnreadFilter()))
            {
                _shownIndex = 0;
            }
            else if (_notifications.Filter.Equals(NotificationsFilterModel.CreateParticipatingFilter()))
            {
                _shownIndex = 1;
            }
            else
            {
                _shownIndex = 2;
            }
        }
Пример #5
0
        public NotificationsViewModel(IApplicationService applicationService)
        {
            _applicationService = applicationService;

            Title = "Notifications";

            var whenNotificationsChange =
                _notifications.Changed.Select(_ => Unit.Default)
                .Merge(_notifications.ItemChanged.Select(_ => Unit.Default));

            _groupedNotifications = whenNotificationsChange.Select(_ =>
                                                                   _notifications.GroupBy(x => x.Repository.FullName)
                                                                   .Select(x => new NotificationGroupViewModel(x.Key, new ReactiveList <NotificationModel>(x), __ => { })))
                                    .ToProperty(this, t => t.GroupedNotifications);

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var req = applicationService.Client.Notifications.GetAll(all: Filter.All, participating: Filter.Participating);
                return(this.RequestModel(req, t as bool?, response => _notifications.Reset(response.Data)));
            });

            GoToNotificationCommand = ReactiveCommand.Create();
            GoToNotificationCommand.OfType <NotificationModel>().Subscribe(GoToNotification);


            var canReadAll = _notifications.CountChanged.Select(x => x > 0).CombineLatest(
                this.WhenAnyValue(x => x.ShownIndex).Select(x => x != 2), (x, y) => x & y);

            ReadAllCommand = ReactiveCommand.CreateAsyncTask(canReadAll, async t =>
            {
                try
                {
                    if (!_notifications.Any())
                    {
                        return;
                    }
                    await applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkAsRead());
                    _notifications.Clear();
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to mark all notifications as read. Please try again.", e);
                }
            });

            ReadRepositoriesCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                try
                {
                    var repo = t as string;
                    if (repo == null)
                    {
                        return;
                    }
                    var repoId = new RepositoryIdentifier(repo);
                    await applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkRepoAsRead(repoId.Owner, repoId.Name));
                    _notifications.RemoveAll(_notifications.Where(x => string.Equals(x.Repository.FullName, repo, StringComparison.OrdinalIgnoreCase)).ToList());
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to mark repositories' notifications as read. Please try again.", e);
                }
            });

            this.WhenAnyValue(x => x.ShownIndex).Subscribe(x =>
            {
                switch (x)
                {
                case 0:
                    Filter = NotificationsFilterModel.CreateUnreadFilter();
                    break;

                case 1:
                    Filter = NotificationsFilterModel.CreateParticipatingFilter();
                    break;

                default:
                    Filter = NotificationsFilterModel.CreateAllFilter();
                    break;
                }
            });

            this.WhenAnyValue(x => x.Filter).Skip(1).Subscribe(x => LoadCommand.ExecuteIfCan());
        }
Пример #6
0
        public NotificationsViewModel(IApplicationService applicationService)
        {
            _applicationService = applicationService;
            _notifications      = new ReactiveList <NotificationModel>();
            Notifications       = _notifications.CreateDerivedCollection(x => x);

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var req = applicationService.Client.Notifications.GetAll(all: Filter.All, participating: Filter.Participating);
                return(this.RequestModel(req, t as bool?, response => _notifications.Reset(response.Data)));
            });

            GoToNotificationCommand = ReactiveCommand.Create();
            GoToNotificationCommand.OfType <NotificationModel>().Subscribe(GoToNotification);

            ReadAllCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.ShownIndex).Select(x => x != 2), async t =>
            {
                try
                {
                    if (!Notifications.Any())
                    {
                        return;
                    }
                    await applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkAsRead());
                    _notifications.Clear();
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to mark all notifications as read. Please try again.", e);
                }
            });

            ReadRepositoriesCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                try
                {
                    var repo = t as string;
                    if (repo == null)
                    {
                        return;
                    }
                    var repoId = new RepositoryIdentifier(repo);
                    await applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkRepoAsRead(repoId.Owner, repoId.Name));
                    _notifications.RemoveAll(Notifications.Where(x => string.Equals(x.Repository.FullName, repo, StringComparison.OrdinalIgnoreCase)).ToList());
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to mark repositories' notifications as read. Please try again.", e);
                }
            });

            this.WhenAnyValue(x => x.ShownIndex).Subscribe(x =>
            {
                switch (x)
                {
                case 0:
                    Filter = NotificationsFilterModel.CreateUnreadFilter();
                    break;

                case 1:
                    Filter = NotificationsFilterModel.CreateParticipatingFilter();
                    break;

                default:
                    Filter = NotificationsFilterModel.CreateAllFilter();
                    break;
                }
            });

            this.WhenAnyValue(x => x.Filter).Skip(1).Subscribe(x => LoadCommand.ExecuteIfCan());
        }