Beispiel #1
0
        public ReleaseViewModel(IApplicationService applicationService, IShareService shareService,
                                IUrlRouterService urlRouterService, IActionMenuService actionMenuService)
        {
            this.WhenAnyValue(x => x.ReleaseModel)
            .Select(x => x == null ? "Release" : x.Name).Subscribe(x => Title = x);

            ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null));
            ShareCommand.Subscribe(_ => shareService.ShareUrl(ReleaseModel.HtmlUrl));

            var gotoUrlCommand = this.CreateUrlCommand();

            GoToGitHubCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null));
            GoToGitHubCommand.Select(_ => ReleaseModel.HtmlUrl).Subscribe(gotoUrlCommand.ExecuteIfCan);

            GoToLinkCommand = ReactiveCommand.Create();
            GoToLinkCommand.OfType <string>().Subscribe(x =>
            {
                var handledViewModel = urlRouterService.Handle(x);
                if (handledViewModel != null && applicationService.Account.OpenUrlsInApp)
                {
                    ShowViewModel(handledViewModel);
                }
                else
                {
                    gotoUrlCommand.ExecuteIfCan(x);
                }
            });

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null),
                _ =>
            {
                var menu = actionMenuService.Create(Title);
                menu.AddButton("Share", ShowMenuCommand);
                menu.AddButton("Show in GitHub", GoToGitHubCommand);
                return(menu.Show());
            });

            _contentText = this.WhenAnyValue(x => x.ReleaseModel).IsNotNull()
                           .Select(x => x.BodyHtml).ToProperty(this, x => x.ContentText);

            LoadCommand = ReactiveCommand.CreateAsyncTask(x =>
                                                          this.RequestModel(applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetRelease(ReleaseId),
                                                                            x as bool?, r => ReleaseModel = r.Data));
        }
Beispiel #2
0
        public GistFileViewModel(IAccountsService accounts, IApplicationService applicationService,
                                 IFilesystemService filesystemService, IShareService shareService, IActionMenuService actionMenuService)
            : base(accounts)
        {
            this.WhenAnyValue(x => x.Filename).Subscribe(x =>
            {
                Title = x == null ? "Gist" : x.Substring(x.LastIndexOf('/') + 1);
            });

            GoToUrlCommand = this.CreateUrlCommand();

            OpenWithCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.SourceItem).Select(x => x != null));

            _isMarkdown = this.WhenAnyValue(x => x.GistFile).IsNotNull().Select(x =>
                                                                                string.Equals(x.Language, MarkdownLanguage, StringComparison.OrdinalIgnoreCase)).ToProperty(this, x => x.IsMarkdown);

            ShowThemePickerCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                var path = System.IO.Path.Combine("WebResources", "styles");
                if (!System.IO.Directory.Exists(path))
                {
                    return;
                }

                var themes = System.IO.Directory.GetFiles(path)
                             .Where(x => x.EndsWith(".css", StringComparison.Ordinal))
                             .Select(x => System.IO.Path.GetFileNameWithoutExtension(x))
                             .ToList();

                var picker = actionMenuService.CreatePicker();
                themes.ForEach(picker.Options.Add);
                picker.SelectedOption = themes.IndexOf(Theme ?? "idea");

                var selected = await picker.Show();

                if (selected >= 0 && selected <= themes.Count)
                {
                    Theme = themes[selected];
                }
            });

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.SourceItem).Select(x => x != null),
                _ =>
            {
                var menu = actionMenuService.Create(Title);
                menu.AddButton("Open With", OpenWithCommand);
                if (!IsMarkdown)
                {
                    menu.AddButton("Change Theme", ShowThemePickerCommand);
                }
                return(menu.Show());
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                if (GistFile == null)
                {
                    var data = await applicationService.Client.ExecuteAsync(applicationService.Client.Gists[_id].Get());
                    GistFile = data.Data.Files[_filename];
                }

                //Check to make sure...
                if (GistFile == null || GistFile.Content == null)
                {
                    throw new Exception("Unable to retreive gist!");
                }

                var content = GistFile.Content;
                if (MarkdownLanguage.Equals(GistFile.Language, StringComparison.OrdinalIgnoreCase))
                {
                    content = await applicationService.Client.Markdown.GetMarkdown(content);
                }

                var gistFileName = System.IO.Path.GetFileName(GistFile.Filename);
                string filePath;

                using (var stream = filesystemService.CreateTempFile(out filePath, gistFileName))
                {
                    using (var fs = new System.IO.StreamWriter(stream))
                    {
                        fs.Write(content);
                    }
                }

                var fileUri = new Uri(filePath);
                SourceItem  = new FileSourceItemViewModel(fileUri, false);
            });
        }
Beispiel #3
0
        public UserViewModel(IApplicationService applicationService, IActionMenuService actionMenuService)
        {
            _applicationService = applicationService;

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

            GoToGistsCommand = ReactiveCommand.Create();
            GoToGistsCommand.Subscribe(_ =>
            {
                var vm      = CreateViewModel <UserGistsViewModel>();
                vm.Username = Username;
                ShowViewModel(vm);
            });

            GoToRepositoriesCommand = ReactiveCommand.Create();
            GoToRepositoriesCommand.Subscribe(_ =>
            {
                var vm      = CreateViewModel <UserRepositoriesViewModel>();
                vm.Username = Username;
                ShowViewModel(vm);
            });

            GoToOrganizationsCommand = ReactiveCommand.Create();
            GoToOrganizationsCommand.Subscribe(_ =>
            {
                var vm      = CreateViewModel <OrganizationsViewModel>();
                vm.Username = Username;
                ShowViewModel(vm);
            });

            GoToEventsCommand = ReactiveCommand.Create();
            GoToEventsCommand.Subscribe(_ =>
            {
                var vm      = CreateViewModel <UserEventsViewModel>();
                vm.Username = Username;
                ShowViewModel(vm);
            });

            GoToFollowingCommand = ReactiveCommand.Create();
            GoToFollowingCommand.Subscribe(_ =>
            {
                var vm      = CreateViewModel <UserFollowingsViewModel>();
                vm.Username = Username;
                ShowViewModel(vm);
            });

            GoToFollowersCommand = ReactiveCommand.Create();
            GoToFollowersCommand.Subscribe(_ =>
            {
                var vm      = CreateViewModel <UserFollowersViewModel>();
                vm.Username = Username;
                ShowViewModel(vm);
            });

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

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                this.RequestModel(applicationService.Client.AuthenticatedUser.IsFollowing(Username), t as bool?, x => IsFollowing = x.Data).FireAndForget();
                return(this.RequestModel(applicationService.Client.Users[Username].Get(), t as bool?, response => User = response.Data));
            });
        }