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)); }); }
public MenuViewModel( IApplicationService applicationService = null, IAccountsService accountsService = null) { _applicationService = applicationService = applicationService ?? Locator.Current.GetService <IApplicationService>(); accountsService = accountsService ?? Locator.Current.GetService <IAccountsService>(); var account = applicationService.Account; Avatar = new Avatar(account.AvatarUrl); Username = account.Username; var username = Username; Title = username; var repos = new ReactiveList <PinnedRepository>(); PinnedRepositories = repos.CreateDerivedCollection(x => { var vm = new PinnedRepositoryItemViewModel(x.Name, new Avatar(x.ImageUri)); vm.DeleteCommand .Do(_ => account.PinnedRepositories.RemoveAll(y => y.Id == x.Id)) .Subscribe(_ => repos.Remove(x)); vm.GoToCommand .Select(_ => new RepositoryViewModel(x.Owner, x.Slug)) .Subscribe(NavigateTo); return(vm); }); RefreshCommand = ReactiveCommand.CreateFromTask(_ => { repos.Reset(applicationService.Account.PinnedRepositories); return(Task.FromResult(Unit.Default)); }); var teams = new ReactiveList <User>(); Teams = teams.CreateDerivedCollection(x => { var viewModel = new TeamItemViewModel(x.Username); viewModel.GoToCommand .Select(_ => new TeamViewModel(x)) .Subscribe(NavigateTo); return(viewModel); }); TeamEvents = teams.CreateDerivedCollection(x => { var viewModel = new TeamItemViewModel(x.Username); viewModel.GoToCommand .Select(_ => new UserEventsViewModel(x.Username)) .Subscribe(NavigateTo); return(viewModel); }); LoadCommand = ReactiveCommand.CreateFromTask(t => { applicationService.Client .AllItems(x => x.Teams.GetAll()) .ToBackground(teams.Reset); applicationService.Client.Groups .GetGroups(username) .ToBackground(groups => Groups.Reset(groups.Select(ToViewModel))); return(Task.FromResult(Unit.Default)); }); GoToProfileCommand .Select(_ => new UserViewModel(username)) .Subscribe(NavigateTo); GoToMyEvents .Select(_ => new UserEventsViewModel(username)) .Subscribe(NavigateTo); GoToStarredRepositoriesCommand .Select(_ => new RepositoriesStarredViewModel()) .Subscribe(NavigateTo); GoToOwnedRepositoriesCommand .Select(_ => new UserRepositoriesViewModel(username)) .Subscribe(NavigateTo); GoToSharedRepositoriesCommand .Select(_ => new RepositoriesSharedViewModel()) .Subscribe(NavigateTo); GoToTeamsCommand .Select(_ => new TeamsViewModel()) .Subscribe(NavigateTo); GoToSettingsCommand .Select(_ => new SettingsViewModel()) .Subscribe(NavigateTo); GoToFeedbackCommand .Select(_ => new IssuesViewModel("thedillonb", "codebucket")) .Subscribe(NavigateTo); GoToGroupsCommand .Select(_ => new GroupsViewModel(username)) .Subscribe(NavigateTo); GoToExploreRepositoriesCommand .Select(_ => new RepositoriesExploreViewModel()) .Subscribe(NavigateTo); GoToDefaultTopView.Subscribe(_ => { var startupViewName = applicationService.Account.DefaultStartupView; if (!string.IsNullOrEmpty(startupViewName)) { var props = from p in GetType().GetProperties() let attr = p.GetCustomAttributes(typeof(PotentialStartupViewAttribute), true) where attr.Length == 1 select new { Property = p, Attribute = attr[0] as PotentialStartupViewAttribute }; var match = props.FirstOrDefault(x => string.Equals(startupViewName, x.Attribute.Name)); var cmd = match?.Property.GetValue(this) as ReactiveCommand <Unit, Unit>; if (cmd != null) { cmd.ExecuteNow(); return; } } //Oh no... Look for the last resort DefaultStartupViewAttribute var deprop = (from p in GetType().GetProperties() let attr = p.GetCustomAttributes(typeof(DefaultStartupViewAttribute), true) where attr.Length == 1 select new { Property = p, Attribute = attr[0] as DefaultStartupViewAttribute }).FirstOrDefault(); //That shouldn't happen... var bCmd = deprop?.Property.GetValue(this) as ReactiveCommand <Unit, Unit>; if (bCmd != null) { bCmd.ExecuteNow(); } }); }