1313using System . Windows . Input ;
1414using GitHub . Validation ;
1515using GitHub . Extensions ;
16+ using NullGuard ;
17+ using System . Reactive . Disposables ;
1618
1719namespace GitHub . ViewModels
1820{
1921 [ ExportViewModel ( ViewType = UIViewType . PRCreation ) ]
2022 [ PartCreationPolicy ( CreationPolicy . NonShared ) ]
2123 public class PullRequestCreationViewModel : BaseViewModel , IPullRequestCreationViewModel
2224 {
25+ readonly IRepositoryHost repositoryHost ;
26+ readonly ISimpleRepositoryModel activeRepo ;
27+ readonly CompositeDisposable disposables = new CompositeDisposable ( ) ;
28+
2329 [ ImportingConstructor ]
2430 PullRequestCreationViewModel (
2531 IConnectionRepositoryHostMap connectionRepositoryHostMap , ITeamExplorerServiceHolder teservice ,
26- IPullRequestService service )
27- : this ( connectionRepositoryHostMap . CurrentRepositoryHost , teservice . ActiveRepo , service )
32+ IPullRequestService service , INotificationService notifications )
33+ : this ( connectionRepositoryHostMap . CurrentRepositoryHost , teservice . ActiveRepo , service , notifications )
2834 { }
2935
30- public PullRequestCreationViewModel ( IRepositoryHost repositoryHost , ISimpleRepositoryModel activeRepo , IPullRequestService service )
36+ public PullRequestCreationViewModel ( IRepositoryHost repositoryHost , ISimpleRepositoryModel activeRepo ,
37+ IPullRequestService service , INotificationService notifications )
3138 {
32- repositoryHost . ModelService . GetBranches ( activeRepo )
33- . ToReadOnlyList ( )
34- . ObserveOn ( RxApp . MainThreadScheduler )
35- . Subscribe ( x => Branches = x ) ;
39+ this . repositoryHost = repositoryHost ;
40+ this . activeRepo = activeRepo ;
3641
3742 var repo = GitService . GitServiceHelper . GetRepo ( activeRepo . LocalPath ) ;
38- SourceBranch = new BranchModel ( repo . Head ) ;
43+ this . WhenAny ( x => x . Branches , x => x . Value )
44+ . WhereNotNull ( )
45+ . Subscribe ( x =>
46+ {
47+ // what do we do if there's no master?
48+ TargetBranch = x . FirstOrDefault ( b => b . Name == "master" ) ;
49+ SourceBranch = x . FirstOrDefault ( b => b . Name == repo . Head . FriendlyName ) ;
50+ } ) ;
3951
40- // what do we do if there's no master?
41- TargetBranch = new BranchModel { Name = "master" } ;
42- var titleObs = this . WhenAny ( x => x . PRTitle , x => x . Value ) . WhereNotNull ( ) ;
52+ var titleObs = this . WhenAny ( x => x . PRTitle , x => x . Value ) ;
4353 TitleValidator = ReactivePropertyValidator . ForObservable ( titleObs )
4454 . IfNullOrEmpty ( "Please enter a title for the Pull Request" ) ;
55+ disposables . Add ( TitleValidator ) ;
4556
4657 var branchObs = this . WhenAny (
4758 x => x . SourceBranch ,
48- x => x . TargetBranch ,
49- ( source , target ) => source . Value . Name == target . Value . Name ) ;
59+ source => source . Value ) ;
5060
5161 BranchValidator = ReactivePropertyValidator . ForObservable ( branchObs )
52- . IfTrue ( x => x , "Source and target branch cannot be the same" ) ;
62+ . IfTrue ( x => x == null , "Source branch doesn't exist remotely, have you pushed it?" )
63+ . IfTrue ( x => x . Name == TargetBranch . Name , "Source and target branch cannot be the same" ) ;
64+ disposables . Add ( BranchValidator ) ;
5365
5466 var whenAnyValidationResultChanges = this . WhenAny (
55- x => x . TitleValidator . ValidationResult . IsValid ,
56- x => x . BranchValidator . ValidationResult . IsValid ,
57- ( x , y ) => x . Value && y . Value ) ;
67+ x => x . TitleValidator . ValidationResult ,
68+ x => x . BranchValidator . ValidationResult ,
69+ ( x , y ) => ( x . Value ? . IsValid ?? false ) && ( y . Value ? . IsValid ?? false ) ) ;
70+
71+ disposables . Add (
72+ this . WhenAny ( x => x . BranchValidator . ValidationResult , x => x . GetValue ( ) )
73+ . WhereNotNull ( )
74+ . Where ( x => ! x . IsValid && x . DisplayValidationError )
75+ . Subscribe ( x => notifications . ShowError ( BranchValidator . ValidationResult . Message ) ) ) ;
5876
5977 createPullRequest = ReactiveCommand . CreateAsyncObservable ( whenAnyValidationResultChanges ,
6078 _ => service . CreatePullRequest ( repositoryHost , activeRepo , PRTitle , SourceBranch , TargetBranch )
@@ -65,51 +83,76 @@ public PullRequestCreationViewModel(IRepositoryHost repositoryHost, ISimpleRepos
6583 {
6684 }
6785 } ) ;
86+ disposables . Add ( createPullRequest ) ;
6887 }
6988
70- IBranch targetBranch ;
71- public IBranch TargetBranch
89+ public override void Initialize ( [ AllowNull ] ViewWithData data )
7290 {
73- get { return targetBranch ; }
74- set { this . RaiseAndSetIfChanged ( ref targetBranch , value ) ; }
91+ base . Initialize ( data ) ;
92+
93+ repositoryHost . ModelService . GetBranches ( activeRepo )
94+ . ToReadOnlyList ( )
95+ . ObserveOn ( RxApp . MainThreadScheduler )
96+ . Subscribe ( x => Branches = x ) ;
7597 }
7698
7799 IBranch sourceBranch ;
100+ [ AllowNull ]
78101 public IBranch SourceBranch
79102 {
103+ [ return : AllowNull ]
80104 get { return sourceBranch ; }
81105 set { this . RaiseAndSetIfChanged ( ref sourceBranch , value ) ; }
82106 }
83107
108+ IBranch targetBranch ;
109+ [ AllowNull ]
110+ public IBranch TargetBranch
111+ {
112+ [ return : AllowNull ]
113+ get { return targetBranch ; }
114+ set { this . RaiseAndSetIfChanged ( ref targetBranch , value ) ; }
115+ }
116+
84117 IReadOnlyList < IBranch > branches ;
85118 public IReadOnlyList < IBranch > Branches
86119 {
120+ [ return : AllowNull ]
87121 get { return branches ; }
88122 set { this . RaiseAndSetIfChanged ( ref branches , value ) ; }
89123 }
90124
91- IReactiveCommand createPullRequest ;
92- public ICommand CreatePullRequest => createPullRequest ;
125+ IReactiveCommand < IPullRequestModel > createPullRequest ;
126+ public IReactiveCommand < IPullRequestModel > CreatePullRequest => createPullRequest ;
93127
94128 string title ;
95129 public string PRTitle
96130 {
131+ [ return : AllowNull ]
97132 get { return title ; }
98133 set { this . RaiseAndSetIfChanged ( ref title , value ) ; }
99134 }
100135
136+ string description ;
137+ public string Description
138+ {
139+ [ return : AllowNull ]
140+ get { return description ; }
141+ set { this . RaiseAndSetIfChanged ( ref description , value ) ; }
142+ }
143+
101144 ReactivePropertyValidator titleValidator ;
102145 public ReactivePropertyValidator TitleValidator
103146 {
104147 get { return titleValidator ; }
105- private set { this . RaiseAndSetIfChanged ( ref titleValidator , value ) ; }
148+ set { this . RaiseAndSetIfChanged ( ref titleValidator , value ) ; }
106149 }
107150
108151 ReactivePropertyValidator branchValidator ;
109- public ReactivePropertyValidator BranchValidator
152+ ReactivePropertyValidator BranchValidator
110153 {
111154 get { return branchValidator ; }
112- private set { this . RaiseAndSetIfChanged ( ref branchValidator , value ) ; }
155+ set { this . RaiseAndSetIfChanged ( ref branchValidator , value ) ; }
113156 }
114157 }
115158}
0 commit comments