Esempio n. 1
0
        public SourceViewModel(IApplicationService applicationService, IAccountsService accountsService, IFilesystemService filesystemService)
            : base(accountsService)
        {
            GoToEditCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.SourceItem, x => x.TrueBranch).Select(x => x.Item1 != null && x.Item2));
            GoToEditCommand.Subscribe(_ =>
            {
                var vm        = CreateViewModel <EditSourceViewModel>();
                vm.Path       = Path;
                vm.Branch     = Branch;
                vm.Username   = Username;
                vm.Repository = Repository;
                ShowViewModel(vm);
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                string filepath;
                string mime;

                using (var stream = filesystemService.CreateTempFile(out filepath))
                {
                    mime = await applicationService.Client.DownloadRawResource2(GitUrl, stream) ?? string.Empty;
                }

                // We can force a binary representation if it was passed during init. In which case we don't care to figure out via the mime.
                var isBinary = !mime.Contains("charset");
                var fileUri  = new Uri(filepath);
                SourceItem   = new FileSourceItemViewModel(fileUri, ForceBinary || isBinary);
            });
        }
Esempio n. 2
0
        public ChangesetDiffViewModel(IApplicationService applicationService, IFilesystemService filesystemService)
        {
            Comments = new ReactiveList <CommentModel>();

            GoToCommentCommand = ReactiveCommand.Create();
            GoToCommentCommand.OfType <int?>().Subscribe(line =>
            {
                var vm = CreateViewModel <CommentViewModel>();
                ReactiveUI.Legacy.ReactiveCommandMixins.RegisterAsyncTask(vm.SaveCommand, async t =>
                {
                    var req      = applicationService.Client.Users[Username].Repositories[Repository].Commits[Branch].Comments.Create(vm.Comment, Filename, line);
                    var response = await applicationService.Client.ExecuteAsync(req);
                    Comments.Add(response.Data);
                    vm.DismissCommand.ExecuteIfCan();
                });
                ShowViewModel(vm);
            });

            this.WhenAnyValue(x => x.Filename).Subscribe(x =>
            {
                if (string.IsNullOrEmpty(x))
                {
                    Title = "Diff";
                }
                else
                {
                    _actualFilename = Path.GetFileName(Filename) ??
                                      Filename.Substring(Filename.LastIndexOf('/') + 1);
                    Title = _actualFilename;
                }
            });

            _loadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                var branch = applicationService.Client.Users[Username].Repositories[Repository].Commits[Branch];

                //Make sure we have this information. If not, go get it
                if (CommitFile == null)
                {
                    var data   = await applicationService.Client.ExecuteAsync(branch.Get());
                    CommitFile = data.Data.Files.First(x => string.Equals(x.Filename, Filename));
                }

                string path;
                using (var stream = filesystemService.CreateTempFile(out path))
                {
                    using (var fs = new StreamWriter(stream))
                    {
                        fs.Write(CommitFile.Patch);
                    }
                }

                SourceItem = new FileSourceItemViewModel {
                    FilePath = path
                };
                await Comments.SimpleCollectionLoad(branch.Comments.GetAll(), t as bool?);
            });
        }
Esempio n. 3
0
        public SourceViewModel(IApplicationService applicationService, IAccountsService accountsService, IFilesystemService filesystemService)
        {
            GoToEditCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.SourceItem, x => x.TrueBranch).Select(x => x.Item1 != null && x.Item2));
            GoToEditCommand.Subscribe(_ =>
            {
                var vm        = CreateViewModel <EditSourceViewModel>();
                vm.Path       = CurrentItem.Path;
                vm.Branch     = Branch;
                vm.Username   = Username;
                vm.Repository = Repository;
                ShowViewModel(vm);
            });

            Theme = applicationService.Account.CodeEditTheme;
            this.WhenAnyValue(x => x.Theme).Skip(1).Subscribe(x =>
            {
                applicationService.Account.CodeEditTheme = x;
                accountsService.Update(applicationService.Account);
            });

            this.WhenAnyValue(x => x.CurrentItem)
            .Skip(1)
            .Where(x => x != null)
            .Subscribe(_ => LoadCommand.ExecuteIfCan());

            _loadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                var fileName = System.IO.Path.GetFileName(CurrentItem.Name);
                if (fileName == null)
                {
                    return;
                }

                string filepath;
                string mime;

                using (var stream = filesystemService.CreateTempFile(out filepath))
                {
                    mime = await applicationService.Client.DownloadRawResource2(CurrentItem.GitUrl, stream) ?? string.Empty;
                }

                // We can force a binary representation if it was passed during init. In which case we don't care to figure out via the mime.
                var isBinary = !mime.Contains("charset");
                SourceItem   = new FileSourceItemViewModel {
                    FilePath = filepath, IsBinary = (CurrentItem.ForceBinary || isBinary)
                };
            });

            SetupRx();
        }
Esempio n. 4
0
        public SourceViewModel(IApplicationService applicationService, IAccountsService accountsService, IFilesystemService filesystemService)
            : base(accountsService)
        {
            GoToEditCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.SourceItem, x => x.TrueBranch).Select(x => x.Item1 != null && x.Item2));
            GoToEditCommand.Subscribe(_ =>
            {
                var vm        = CreateViewModel <EditSourceViewModel>();
                vm.Path       = Path;
                vm.Branch     = Branch;
                vm.Username   = RepositoryOwner;
                vm.Repository = RepositoryName;
                ShowViewModel(vm);
            });

            this.WhenAnyValue(x => x.Name).Subscribe(x => Title = x ?? string.Empty);

            _isMarkdown = this.WhenAnyValue(x => x.Path).IsNotNull().Select(x =>
                                                                            MarkdownExtensions.Any(Path.EndsWith)).ToProperty(this, x => x.IsMarkdown);

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                string filepath;
                bool isBinary = false;

                using (var stream = filesystemService.CreateTempFile(out filepath))
                {
                    if (MarkdownExtensions.Any(Path.EndsWith))
                    {
                        var renderedContent = await applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContentFileRendered(Path, Branch);
                        using (var stringWriter = new StreamWriter(stream))
                        {
                            await stringWriter.WriteAsync(renderedContent);
                        }
                    }
                    else
                    {
                        var mime = await applicationService.Client.DownloadRawResource2(GitUrl, stream) ?? string.Empty;
                        isBinary = !(mime ?? string.Empty).Contains("charset");
                    }
                }

                // We can force a binary representation if it was passed during init. In which case we don't care to figure out via the mime.
                var fileUri = new Uri(filepath);
                SourceItem  = new FileSourceItemViewModel(fileUri, ForceBinary || isBinary);
            });
        }
Esempio n. 5
0
        public SourceViewModel(ISessionService sessionService, IActionMenuFactory actionMenuFactory,
                               IFilesystemService filesystemService)
            : base(sessionService)
        {
            var canEdit = this.WhenAnyValue(x => x.SourceItem, x => x.TrueBranch, x => x.PushAccess)
                          .Select(x => x.Item1 != null && x.Item2 && !x.Item1.IsBinary && x.Item3.HasValue && x.Item3.Value);

            GoToEditCommand = ReactiveCommand.Create(canEdit);
            GoToEditCommand.Subscribe(_ => {
                var vm = this.CreateViewModel <EditFileViewModel>();
                vm.Init(RepositoryOwner, RepositoryName, Path, null, Branch);
                vm.SaveCommand.Subscribe(x => {
                    GitUrl = x.Content.GitUrl.AbsoluteUri;
                    LoadCommand.ExecuteIfCan();
                });
                NavigateTo(vm);
            });

            this.WhenAnyValue(x => x.Name).Subscribe(x => Title = x ?? string.Empty);

            _isMarkdown = this.WhenAnyValue(x => x.Path).IsNotNull().Select(x =>
                                                                            MarkdownExtensions.Any(Path.EndsWith)).ToProperty(this, x => x.IsMarkdown);

            OpenInGitHubCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.HtmlUrl).Select(x => !string.IsNullOrEmpty(x)));
            OpenInGitHubCommand
            .Select(_ => this.CreateViewModel <WebBrowserViewModel>())
            .Select(x => x.Init(this.HtmlUrl))
            .Subscribe(NavigateTo);

            var canShowMenu = this.WhenAnyValue(x => x.SourceItem).Select(x => x != null);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(canShowMenu, sender => {
                var menu = actionMenuFactory.Create();
                if (GoToEditCommand.CanExecute(null))
                {
                    menu.AddButton("Edit", GoToEditCommand);
                }
                menu.AddButton("Open With", OpenWithCommand);
                if (OpenInGitHubCommand.CanExecute(null))
                {
                    menu.AddButton("Open in GitHub", OpenInGitHubCommand);
                }
                return(menu.Show(sender));
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                string filepath;
                bool isBinary = false;

                if (!PushAccess.HasValue)
                {
                    sessionService.GitHubClient.Repository.Get(RepositoryOwner, RepositoryName)
                    .ToBackground(x => PushAccess = x.Permissions.Push);
                }

                using (var stream = filesystemService.CreateTempFile(out filepath, Name))
                {
                    if (MarkdownExtensions.Any(Path.EndsWith))
                    {
                        var renderedContent = await sessionService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContentFileRendered(Path, Branch);
                        using (var stringWriter = new StreamWriter(stream))
                        {
                            await stringWriter.WriteAsync(renderedContent);
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(GitUrl) || string.IsNullOrEmpty(HtmlUrl))
                        {
                            var req  = sessionService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContentFile(Path, Branch);
                            var data = (await sessionService.Client.ExecuteAsync(req)).Data;
                            GitUrl   = data.GitUrl;
                            HtmlUrl  = data.HtmlUrl;
                        }

                        var mime = await sessionService.Client.DownloadRawResource2(GitUrl, stream) ?? string.Empty;
                        isBinary = !(mime ?? string.Empty).Contains("charset");
                    }
                }

                // We can force a binary representation if it was passed during init. In which case we don't care to figure out via the mime.
                var fileUri = new Uri(filepath);
                SourceItem  = new FileSourceItemViewModel(fileUri, ForceBinary || isBinary);
            });
        }
Esempio n. 6
0
        public GistFileViewModel(ISessionService sessionService, ISessionService applicationService, 
            IFilesystemService filesystemService, IActionMenuFactory actionMenuService)
            : base(sessionService)
	    {
	        this.WhenAnyValue(x => x.Filename)
                .Select(x => x == null ? "Gist" : x.Substring(x.LastIndexOf('/') + 1))
                .Subscribe(x => Title = x);
                
            _isMarkdown = this.WhenAnyValue(x => x.GistFile)
                .IsNotNull()
                .Select(x => string.Equals(x.Language, MarkdownLanguage, StringComparison.OrdinalIgnoreCase))
                .ToProperty(this, x => x.IsMarkdown);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.SourceItem).Select(x => x != null),
                sender => {
                    var menu = actionMenuService.Create();
                    menu.AddButton("Open With", OpenWithCommand);
                    return menu.Show(sender);
                });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t => {
                if (GistFile == null)
			    {
                    var data = await applicationService.GitHubClient.Gist.Get(_id);
                    GistFile = 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.GitHubClient.Miscellaneous.RenderRawMarkdown(content);

                var gistFileName = System.IO.Path.GetFileName(GistFile.Filename).Trim();
                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);
            });
	    }
        public SourceViewModel(ISessionService sessionService, IActionMenuFactory actionMenuFactory,
            IFilesystemService filesystemService)
            : base(sessionService)
	    {
            var canEdit = this.WhenAnyValue(x => x.SourceItem, x => x.TrueBranch, x => x.PushAccess)
                .Select(x => x.Item1 != null && x.Item2 && !x.Item1.IsBinary && x.Item3.HasValue && x.Item3.Value);
            GoToEditCommand = ReactiveCommand.Create(canEdit);
	        GoToEditCommand.Subscribe(_ => {
	            var vm = this.CreateViewModel<EditFileViewModel>();
                vm.Path = Path;
                vm.Branch = Branch;
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName = RepositoryName;
                vm.SaveCommand.Subscribe(x => {
                    GitUrl = x.Content.GitUrl.AbsoluteUri;
                    LoadCommand.ExecuteIfCan();
                });
	            NavigateTo(vm);
            });

            this.WhenAnyValue(x => x.Name).Subscribe(x => Title = x ?? string.Empty);

            _isMarkdown = this.WhenAnyValue(x => x.Path).IsNotNull().Select(x => 
                MarkdownExtensions.Any(Path.EndsWith)).ToProperty(this, x => x.IsMarkdown);

            OpenInGitHubCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.HtmlUrl).Select(x => !string.IsNullOrEmpty(x)));
            OpenInGitHubCommand
                .Select(_ => this.CreateViewModel<WebBrowserViewModel>())
                .Select(x => x.Init(this.HtmlUrl))
                .Subscribe(NavigateTo);

            var canShowMenu = this.WhenAnyValue(x => x.SourceItem).Select(x => x != null);
            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(canShowMenu, sender => {
                var menu = actionMenuFactory.Create();
                if (GoToEditCommand.CanExecute(null))
                    menu.AddButton("Edit", GoToEditCommand);
                menu.AddButton("Open With", OpenWithCommand);
                if (OpenInGitHubCommand.CanExecute(null))
                    menu.AddButton("Open in GitHub", OpenInGitHubCommand);
                return menu.Show(sender);
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
	        {
	            string filepath;
                bool isBinary = false;

                if (!PushAccess.HasValue)
                {
                    sessionService.GitHubClient.Repository.Get(RepositoryOwner, RepositoryName)
                        .ToBackground(x => PushAccess = x.Permissions.Push);
                }

                using (var stream = filesystemService.CreateTempFile(out filepath, Name))
                {
                    if (MarkdownExtensions.Any(Path.EndsWith))
                    {
                        var renderedContent = await sessionService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContentFileRendered(Path, Branch);
                        using (var stringWriter = new StreamWriter(stream))
                        {
                            await stringWriter.WriteAsync(renderedContent);
                        }
                    } 
                    else
                    {
                        if (string.IsNullOrEmpty(GitUrl) || string.IsNullOrEmpty(HtmlUrl))
                        {
                            var req = sessionService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContentFile(Path, Branch);
                            var data = (await sessionService.Client.ExecuteAsync(req)).Data;
                            GitUrl = data.GitUrl;
                            HtmlUrl = data.HtmlUrl;
                        }

                        var mime = await sessionService.Client.DownloadRawResource2(GitUrl, stream) ?? string.Empty;
                        isBinary = !(mime ?? string.Empty).Contains("charset");
                    }
                }

                // We can force a binary representation if it was passed during init. In which case we don't care to figure out via the mime.
                var fileUri = new Uri(filepath);
                SourceItem = new FileSourceItemViewModel(fileUri, ForceBinary || isBinary);
	        });
	    }