Ejemplo n.º 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();
            });
        }
Ejemplo n.º 2
0
		public async Task UpdateNotifications(bool update)
		{
			nothingToLoad.IsVisible = false;
			//TODO add correct servercall
			if (update) { notiList = await _dataManager.MessageApiManager.GetNotifications(); }
			if (notiList == null || notiList.Count == 0)
			{
				nothingToLoad.IsVisible = true;
				updateList.IsRefreshing = false;
				return;
			}
			unseenNotifications.Clear();

			notiList = notiList.OrderByDescending(c => c.SendTime).ToList();

			int n = 0;
			List<Notification> commentNoti = new List<Notification>();
			foreach (Notification c in notiList)
			{
				if (!c.Seen && (c.ModelType == NotificationModelType.ProfileConversation || c.ModelType == NotificationModelType.EventConversation || 
				                c.ModelType == NotificationModelType.GroupConversation))
				{
					commentNoti.Add(c);
				}
				else if (!c.Seen)
				{
					n++;
				}
				unseenNotifications.Add(c);
			}

			foreach (Notification c in commentNoti)
			{
				notiList.Remove(c);
			}
			//notiList.RemoveAll(noti => noti.ModelType == NotificationModelType.ProfileConversation);
			App.coreView.setHowlsNoti(n);

			ObservableCollection<GroupedNotifications> groupedNotifications = new ObservableCollection<GroupedNotifications>();
			if (notiList.Count > 0)
			{
				GroupedNotifications monthGroup = null;
				int month = notiList[0].SendTime.Month;
				for (int d = 0; d < notiList.Count; d++)
				{
					if (d == 0) { monthGroup = new GroupedNotifications() { Date = (notiList[d].SendTime.ToString("MMMMM")) }; }
					if (month != notiList[d].SendTime.Month)
					{
						month = notiList[d].SendTime.Month;
						groupedNotifications.Add(monthGroup);
						monthGroup = new GroupedNotifications() { Date = (notiList[d].SendTime.ToString("MMMMM")) };
					}
					monthGroup.Add(notiList[d]);
					if (d == notiList.Count - 1) { groupedNotifications.Add(monthGroup); }
				}
			}
			updateList.ItemsSource = groupedNotifications;
			updateList.IsRefreshing = false;
		}
Ejemplo n.º 3
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();
            });
        }