Пример #1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            HeaderView.SetImage(null, Images.Avatar);

            var detailsSection   = new Section();
            var diffButton       = new ButtonElement(AtlassianIcon.ListAdd.ToImage());
            var removedButton    = new ButtonElement(AtlassianIcon.ListRemove.ToImage());
            var modifiedButton   = new ButtonElement(AtlassianIcon.Edit.ToImage());
            var allChangesButton = new ButtonElement("All Changes", AtlassianIcon.Devtoolssidediff.ToImage());

            detailsSection.Add(new [] { diffButton, removedButton, modifiedButton, allChangesButton });

            var additions = ViewModel.WhenAnyValue(x => x.DiffAdditions);

            diffButton.BindClick(ViewModel.GoToAddedFiles);
            diffButton.BindDisclosure(additions.Select(x => x > 0));
            diffButton.BindCaption(additions.Select(x => $"{x} Added"));

            var deletions = ViewModel.WhenAnyValue(x => x.DiffDeletions);

            removedButton.BindClick(ViewModel.GoToRemovedFiles);
            removedButton.BindDisclosure(deletions.Select(x => x > 0));
            removedButton.BindCaption(deletions.Select(x => $"{x} Removed"));

            var modifications = ViewModel.WhenAnyValue(x => x.DiffModifications);

            modifiedButton.BindClick(ViewModel.GoToModifiedFiles);
            modifiedButton.BindDisclosure(modifications.Select(x => x > 0));
            modifiedButton.BindCaption(modifications.Select(x => $"{x} Modified"));

            allChangesButton.BindClick(ViewModel.GoToAllFiles);

            var split         = new SplitButtonElement();
            var commentCount  = split.AddButton("Comments", "-");
            var participants  = split.AddButton("Participants", "-");
            var approvals     = split.AddButton("Approvals", "-");
            var headerSection = new Section {
                split
            };

            var actionButton = new UIBarButtonItem(UIBarButtonSystemItem.Action);

            NavigationItem.RightBarButtonItem = actionButton;

            var detailSection  = new Section();
            var detailsElement = new MultilinedElement();

            detailSection.Add(detailsElement);

            var approvalSection = new Section("Approvals");
            var approveElement  = new LoaderButtonElement("Approve", AtlassianIcon.Approve.ToImage());

            approveElement.Accessory = UITableViewCellAccessory.None;
            approveElement.BindLoader(ViewModel.ToggleApproveButton);
            approveElement.BindCaption(ViewModel.WhenAnyValue(x => x.Approved).Select(x => x ? "Unapprove" : "Approve"));

            var commentsSection   = new Section("Comments");
            var addCommentElement = new ButtonElement("Add Comment", AtlassianIcon.Addcomment.ToImage());

            commentsSection.Add(addCommentElement);

            if (ViewModel.ShowRepository)
            {
                var repo = new ButtonElement(ViewModel.Repository)
                {
                    TextColor = StringElement.DefaultDetailColor,
                    Image     = AtlassianIcon.Devtoolsrepository.ToImage()
                };

                detailSection.Add(repo);
                repo.Clicked.SelectUnit().BindCommand(ViewModel.GoToRepositoryCommand);
            }

            ViewModel.WhenAnyValue(x => x.Commit).Where(x => x != null).Subscribe(x => {
                participants.Text = x?.Participants?.Count.ToString() ?? "-";
                approvals.Text    = x?.Participants?.Count(y => y.Approved).ToString() ?? "-";

                var titleMsg  = (ViewModel.Commit.Message ?? string.Empty).Split(new [] { '\n' }, 2).FirstOrDefault();
                var avatarUrl = ViewModel.Commit.Author?.User?.Links?.Avatar?.Href;

                HeaderView.SubText = titleMsg ?? "Commited " + (ViewModel.Commit.Date).Humanize();
                HeaderView.SetImage(new Avatar(avatarUrl).ToUrl(128), Images.Avatar);
                RefreshHeaderView();

                var user = x.Author?.User?.DisplayName ?? x.Author?.Raw ?? "Unknown";
                detailsElement.Caption = user;
                detailsElement.Details = x.Message;
            });

            this.WhenAnyValue(x => x.ViewModel.Approvals)
            .Select(x => x.Select(y => new UserElement(y)).OfType <Element>())
            .Subscribe(x => approvalSection.Reset(x.Concat(new[] { approveElement })));

            ViewModel.Comments
            .CountChanged
            .Select(x => x.ToString())
            .StartWith("-")
            .Subscribe(x => commentCount.Text = x);

            ViewModel
            .Comments
            .ChangedObservable()
            .Subscribe(x =>
            {
                if (x.Count > 0)
                {
                    var comments     = x.Select(y => new Comment(y.Avatar.ToUrl(), y.Name, y.Content, y.CreatedOn)).ToList();
                    var commentModel = new Views.CommentModel(comments, (int)UIFont.PreferredSubheadline.PointSize);
                    var content      = new CommentsView {
                        Model = commentModel
                    }.GenerateString();
                    _commentsElement.SetValue(content);
                    commentsSection.Insert(0, UITableViewRowAnimation.None, _commentsElement);
                }
                else
                {
                    commentsSection.Remove(_commentsElement);
                }
            });

            ViewModel.WhenAnyValue(x => x.Commit)
            .Where(x => x != null)
            .Take(1)
            .Subscribe(_ => Root.Reset(headerSection, detailsSection, approvalSection, commentsSection));

            ViewModel.GoToAddedFiles.Select(_ => CommitFileType.Added).Subscribe(GoToFiles);
            ViewModel.GoToRemovedFiles.Select(_ => CommitFileType.Removed).Subscribe(GoToFiles);
            ViewModel.GoToModifiedFiles.Select(_ => CommitFileType.Modified).Subscribe(GoToFiles);
            ViewModel.GoToAllFiles.Subscribe(_ => GoToAllFiles());

            OnActivation(d =>
            {
                actionButton
                .GetClickedObservable()
                .Select(x => (object)x)
                .BindCommand(ViewModel.ShowMenuCommand)
                .AddTo(d);

                addCommentElement
                .Clicked
                .SelectUnit()
                .BindCommand(ViewModel.AddCommentCommand)
                .AddTo(d);

                this.WhenAnyObservable(x => x.ViewModel.AddCommentCommand)
                .Subscribe(_ => NewCommentViewController.Present(this, ViewModel.AddComment))
                .AddTo(d);
            });
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            var split        = new SplitButtonElement();
            var commentCount = split.AddButton("Comments", "-");
            var participants = split.AddButton("Participants", "-");
            var approvals    = split.AddButton("Approvals", "-");

            this.WhenAnyValue(x => x.ViewModel.Title)
            .Subscribe(x => RefreshHeaderView(x));

            ViewModel.WhenAnyValue(x => x.PullRequest).Subscribe(x =>
            {
                if (x != null)
                {
                    var avatarUrl      = x?.Author?.Links?.Avatar?.Href;
                    HeaderView.SubText = "Updated " + ViewModel.PullRequest.UpdatedOn.Humanize();
                    HeaderView.SetImage(new Avatar(avatarUrl).ToUrl(128), Images.Avatar);
                }
                else
                {
                    HeaderView.SetImage(null, Images.Avatar);
                    HeaderView.SubText = null;
                }
            });

            var root = new List <Section>();

            root.Add(new Section {
                split
            });

            var secDetails = new Section();

            root.Add(secDetails);

            this.WhenAnyValue(x => x.ViewModel.Description)
            .Where(x => !string.IsNullOrWhiteSpace(x))
            .Select(x => new DescriptionModel(x, (int)UIFont.PreferredSubheadline.PointSize, true))
            .Select(x => new MarkdownView {
                Model = x
            }.GenerateString())
            .Subscribe(content =>
            {
                _descriptionElement.SetValue(content);
                secDetails.Insert(0, UITableViewRowAnimation.None, _descriptionElement);
            });

            var split1 = new SplitViewElement(AtlassianIcon.Calendar.ToImage(), AtlassianIcon.Devtoolsbranch.ToImage());

            secDetails.Add(split1);

            this.WhenAnyValue(x => x.ViewModel.PullRequest.State)
            .Select(x => x?.ToLower().Humanize(LetterCasing.Title))
            .Subscribe(x => split1.Button2.Text = x ?? "Open");

            this.WhenAnyValue(x => x.ViewModel.PullRequest)
            .Select(x => x?.CreatedOn.ToString("MM/dd/yy"))
            .Subscribe(x => split1.Button1.Text = x);

            var commitsElement = new ButtonElement("Commits", AtlassianIcon.Devtoolscommit.ToImage());

            commitsElement.Clicked.SelectUnit().BindCommand(ViewModel.GoToCommitsCommand);
            secDetails.Add(commitsElement);

            var mergeElement = new ButtonElement("Merge", AtlassianIcon.ListAdd.ToImage());

            mergeElement.Accessory = UITableViewCellAccessory.None;

            var rejectElement = new LoaderButtonElement("Reject", AtlassianIcon.ListRemove.ToImage());

            rejectElement.Accessory = UITableViewCellAccessory.None;
            rejectElement.BindLoader(ViewModel.RejectCommand);

            var mergeSection = new Section {
                mergeElement, rejectElement
            };

            var approvalSection = new Section("Approvals");
            var approveElement  = new LoaderButtonElement("Approve", AtlassianIcon.Approve.ToImage());

            approveElement.Accessory = UITableViewCellAccessory.None;
            approveElement.BindLoader(ViewModel.ToggleApproveButton);
            approveElement.BindCaption(this.WhenAnyValue(x => x.ViewModel.Approved).Select(x => x ? "Unapprove" : "Approve"));
            root.Add(approvalSection);

            this.WhenAnyValue(x => x.ViewModel.Approvals)
            .Select(x => x.Select(y => new UserElement(y)).OfType <Element>())
            .Subscribe(x => approvalSection.Reset(x.Concat(new[] { approveElement })));

            var commentsSection = new Section("Comments");

            root.Add(commentsSection);

            var addComment = new ButtonElement("Add Comment")
            {
                Image = AtlassianIcon.Addcomment.ToImage()
            };

            commentsSection.Reset(new[] { addComment });

            ViewModel
            .Comments
            .ChangedObservable()
            .Subscribe(x =>
            {
                if (x.Count > 0)
                {
                    var comments     = x.Select(y => new Comment(y.Avatar.ToUrl(), y.Name, y.Content, y.CreatedOn)).ToList();
                    var commentModel = new CommentModel(comments, (int)UIFont.PreferredSubheadline.PointSize);
                    var content      = new CommentsView {
                        Model = commentModel
                    }.GenerateString();
                    _commentsElement.SetValue(content);
                    commentsSection.Insert(0, UITableViewRowAnimation.None, _commentsElement);
                }
                else
                {
                    commentsSection.Remove(_commentsElement);
                }
            });

            Root.Reset(root);

            this.WhenAnyValue(x => x.ViewModel.IsOpen).Subscribe(x =>
            {
                if (x)
                {
                    Root.Insert(root.IndexOf(secDetails) + 1, mergeSection);
                }
                else
                {
                    Root.Remove(mergeSection);
                }
            });

            OnActivation(disposable =>
            {
                mergeElement
                .Clicked
                .SelectUnit()
                .BindCommand(this, x => x.ViewModel.MergeCommand)
                .AddTo(disposable);

                addComment
                .Clicked
                .Subscribe(_ => NewCommentViewController.Present(this, ViewModel.AddComment))
                .AddTo(disposable);

                this.WhenAnyValue(x => x.ViewModel.ParticipantCount)
                .Subscribe(x => participants.Text = x?.ToString() ?? "-")
                .AddTo(disposable);

                this.WhenAnyValue(x => x.ViewModel.ApprovalCount)
                .Subscribe(x => approvals.Text = x?.ToString() ?? "-")
                .AddTo(disposable);

                this.WhenAnyValue(x => x.ViewModel.CommentCount)
                .Subscribe(x => commentCount.Text = x?.ToString() ?? "-")
                .AddTo(disposable);

                this.WhenAnyObservable(x => x.ViewModel.MergeCommand)
                .Subscribe(_ =>
                {
                    var vc = new PullRequestApproveViewController(
                        ViewModel.Username, ViewModel.Repository, ViewModel.PullRequestId);
                    vc.DeleteSourceBranch = ViewModel.PullRequest.CloseSourceBranch;
                    vc.MergeCommand
                    .Do(x => ViewModel.PullRequest = x)
                    .Do(x => DismissViewController(true, null))
                    .Subscribe();
                    this.PresentModal(vc);
                })
                .AddTo(disposable);
            });
        }