示例#1
0
        public MenuViewModel(ISessionService sessionService, IAccountsRepository accountsService)
        {
            _sessionService = sessionService;

            this.WhenAnyValue(x => x.Account)
            .Select(x => new GitHubAvatar(x.AvatarUrl))
            .ToProperty(this, x => x.Avatar, out _avatar);

            GoToNotificationsCommand = ReactiveCommand.Create().WithSubscription(_ => {
                var vm = this.CreateViewModel <NotificationsViewModel>();
                vm.NotificationCount.Subscribe(x => Notifications = x);
                NavigateTo(vm);
            });

            GoToAccountsCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <AccountsViewModel>()));

            GoToProfileCommand = ReactiveCommand.Create();
            GoToProfileCommand
            .Select(_ => this.CreateViewModel <UserViewModel>())
            .Select(x => x.Init(Account.Username))
            .Subscribe(NavigateTo);

            GoToMyIssuesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <MyIssuesViewModel>()));

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <RepositoryIdentifier>()
            .Select(x => this.CreateViewModel <RepositoryViewModel>().Init(x.Owner, x.Name))
            .Subscribe(NavigateTo);

            GoToSettingsCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <SettingsViewModel>()));

            GoToNewsCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                        NavigateTo(this.CreateViewModel <NewsViewModel>()));

            GoToOrganizationsCommand = ReactiveCommand.Create();
            GoToOrganizationsCommand
            .Select(_ => this.CreateViewModel <OrganizationsViewModel>())
            .Select(x => x.Init(Account.Username))
            .Subscribe(NavigateTo);

            GoToTrendingRepositoriesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                                        NavigateTo(this.CreateViewModel <RepositoriesTrendingViewModel>()));

            GoToExploreRepositoriesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                                       NavigateTo(this.CreateViewModel <ExploreViewModel>()));

            GoToOrganizationEventsCommand = ReactiveCommand.Create();
            GoToOrganizationEventsCommand
            .OfType <Octokit.Organization>()
            .Select(x => this.CreateViewModel <UserEventsViewModel>().Init(x.Login))
            .Subscribe(NavigateTo);

            GoToOrganizationCommand = ReactiveCommand.Create();
            GoToOrganizationCommand
            .OfType <Octokit.Organization>()
            .Select(x => this.CreateViewModel <OrganizationViewModel>().Init(x.Login))
            .Subscribe(NavigateTo);

            GoToOwnedRepositoriesCommand = ReactiveCommand.Create();
            GoToOwnedRepositoriesCommand
            .Select(_ => this.CreateViewModel <UserRepositoriesViewModel>())
            .Select(x => x.Init(Account.Username))
            .Subscribe(NavigateTo);

            GoToStarredRepositoriesCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <RepositoriesStarredViewModel>()));

            GoToWatchedRepositoriesCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <RepositoriesWatchedViewModel>()));

            GoToPublicGistsCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <PublicGistsViewModel>()));

            GoToStarredGistsCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <StarredGistsViewModel>()));

            GoToMyGistsCommand = ReactiveCommand.Create();
            GoToMyGistsCommand
            .Select(_ => this.CreateViewModel <UserGistsViewModel>())
            .Select(x => x.Init(Account.Username))
            .Subscribe(NavigateTo);

            GoToMyEvents = ReactiveCommand.Create();
            GoToMyEvents
            .Select(_ => this.CreateViewModel <UserEventsViewModel>())
            .Select(x => x.Init(Account.Username))
            .Subscribe(NavigateTo);

            GoToFeedbackCommand = ReactiveCommand.Create();
            GoToFeedbackCommand.Subscribe(_ => {
                var vm = sessionService.Account.IsEnterprise
                    ? (IBaseViewModel)this.CreateViewModel <EnterpriseSupportViewModel>()
                    : this.CreateViewModel <SupportViewModel>();
                NavigateTo(vm);
            });

            DeletePinnedRepositoryCommand = ReactiveCommand.Create();

            DeletePinnedRepositoryCommand.OfType <PinnedRepository>()
            .Subscribe(x => {
                sessionService.Account.PinnnedRepositories.Remove(x);
                accountsService.Update(sessionService.Account);
            });

            ActivateCommand = ReactiveCommand.Create();
            ActivateCommand.Subscribe(x => {
                var startupViewModel            = sessionService.StartupViewModel;
                sessionService.StartupViewModel = null;
                if (startupViewModel != null)
                {
                    NavigateTo(startupViewModel);
                }
                else
                {
                    GoToDefaultTopView.ExecuteIfCan();
                }
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(_ => {
//                var notifications = sessionService.GitHubClient.Notification.GetAllForCurrent();
//                notifications.ToBackground(x => Notifications = x.Count);
////
//                var organizations = sessionService.GitHubClient.Organization.GetAllForCurrent();
//                organizations.ToBackground(x => Organizations = x);
//
//                return Task.WhenAll(notifications, organizations);

                return(Task.FromResult(Unit.Default));
            });
        }
示例#2
0
        public UserViewModel(ISessionService applicationService, IActionMenuFactory actionMenuService)
        {
            _applicationService = applicationService;

            ToggleFollowingCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsFollowing).Select(x => x.HasValue), t => ToggleFollowing());

            _hasBlog = this.WhenAnyValue(x => x.User.Blog)
                       .Select(x => !string.IsNullOrEmpty(x))
                       .ToProperty(this, x => x.HasBlog);

            _avatar = this.WhenAnyValue(x => x.User.AvatarUrl)
                      .Select(x => new GitHubAvatar(x))
                      .ToProperty(this, x => x.Avatar);

            GoToGistsCommand = ReactiveCommand.Create();
            GoToGistsCommand
            .Select(_ => this.CreateViewModel <UserGistsViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToRepositoriesCommand = ReactiveCommand.Create();
            GoToRepositoriesCommand
            .Select(_ => this.CreateViewModel <UserRepositoriesViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToOrganizationsCommand = ReactiveCommand.Create();
            GoToOrganizationsCommand
            .Select(_ => this.CreateViewModel <OrganizationsViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToEventsCommand = ReactiveCommand.Create();
            GoToEventsCommand
            .Select(_ => this.CreateViewModel <UserEventsViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToFollowingCommand = ReactiveCommand.Create();
            GoToFollowingCommand
            .Select(_ => this.CreateViewModel <UserFollowingsViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToFollowersCommand = ReactiveCommand.Create();
            GoToFollowersCommand
            .Select(_ => this.CreateViewModel <UserFollowersViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToWebsiteCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.HasBlog));
            GoToWebsiteCommand
            .Select(_ => this.CreateViewModel <WebBrowserViewModel>())
            .Select(x => x.Init(User.Blog))
            .Subscribe(NavigateTo);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsFollowing).Select(x => x.HasValue),
                sender => {
                var menu = actionMenuService.Create();
                menu.AddButton(IsFollowing.Value ? "Unfollow" : "Follow", ToggleFollowingCommand);
                return(menu.Show(sender));
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                if (!IsLoggedInUser)
                {
                    applicationService.GitHubClient.User.Followers.IsFollowingForCurrent(Username)
                    .ToBackground(x => IsFollowing = x);
                }

                User = await applicationService.GitHubClient.User.Get(Username);
            });
        }