Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 6061741

Browse files
committed
Merge pull request #116 from github/shana/kill-duplicated-code
🔥 Cut down on duplicated code
2 parents 9aaa286 + c83fa8a commit 6061741

11 files changed

+68
-139
lines changed

src/GitHub.UI.Reactive/Controls/SimpleViewUserControl.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Reactive.Linq;
43
using System.Reactive.Subjects;
4+
using System.Windows;
55
using System.Windows.Controls;
6-
using System.Windows.Documents;
76
using System.Windows.Input;
8-
using System.Windows.Navigation;
7+
using GitHub.ViewModels;
8+
using NullGuard;
99
using ReactiveUI;
1010

1111
namespace GitHub.UI
@@ -35,11 +35,11 @@ public SimpleViewUserControl()
3535
});
3636
}
3737

38-
public IObservable<object> Done { get { return close; } }
38+
public IObservable<object> Done => close;
3939

40-
public IObservable<object> Cancel { get { return cancel; } }
40+
public IObservable<object> Cancel => cancel;
4141

42-
public IObservable<bool> IsBusy{ get { return isBusy; } }
42+
public IObservable<bool> IsBusy => isBusy;
4343

4444
protected void NotifyDone()
4545
{
@@ -77,6 +77,37 @@ public void Dispose()
7777
Dispose(true);
7878
GC.SuppressFinalize(this);
7979
}
80+
}
81+
82+
public class SimpleViewUserControl<TViewModel, TImplementor> : SimpleViewUserControl, IViewFor<TViewModel>, IView
83+
where TViewModel : class, IViewModel where TImplementor : class
84+
{
85+
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
86+
"ViewModel", typeof(TViewModel), typeof(TImplementor), new PropertyMetadata(null));
87+
88+
object IViewFor.ViewModel
89+
{
90+
get { return ViewModel; }
91+
set { ViewModel = (TViewModel)value; }
92+
}
93+
94+
object IView.ViewModel
95+
{
96+
get { return ViewModel; }
97+
set { ViewModel = (TViewModel)value; }
98+
}
8099

100+
public TViewModel ViewModel
101+
{
102+
[return: AllowNull]
103+
get { return (TViewModel)GetValue(ViewModelProperty); }
104+
set { SetValue(ViewModelProperty, value); }
105+
}
106+
107+
TViewModel IViewFor<TViewModel>.ViewModel
108+
{
109+
get { return ViewModel; }
110+
set { ViewModel = value; }
111+
}
81112
}
82113
}

src/GitHub.VisualStudio/UI/Views/Controls/LoginControl.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<uirx:SimpleViewUserControl x:Class="GitHub.VisualStudio.UI.Views.Controls.LoginControl"
1+
<local:GenericLoginControl x:Class="GitHub.VisualStudio.UI.Views.Controls.LoginControl"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -8,6 +8,7 @@
88
xmlns:helpers="clr-namespace:GitHub.Helpers;assembly=GitHub.UI"
99
xmlns:cache="clr-namespace:GitHub.VisualStudio.Helpers"
1010
xmlns:prop="clr-namespace:GitHub.VisualStudio"
11+
xmlns:local="clr-namespace:GitHub.VisualStudio.UI.Views.Controls"
1112
mc:Ignorable="d"
1213
d:DesignWidth="414"
1314
d:DesignHeight="440"
@@ -179,4 +180,4 @@
179180
</TabItem>
180181
</TabControl>
181182
</DockPanel>
182-
</uirx:SimpleViewUserControl>
183+
</local:GenericLoginControl>

src/GitHub.VisualStudio/UI/Views/Controls/LoginControl.xaml.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
using GitHub.Extensions;
88
using GitHub.UI;
99
using GitHub.ViewModels;
10-
using NullGuard;
1110
using ReactiveUI;
1211
using System.ComponentModel.Composition;
1312

1413
namespace GitHub.VisualStudio.UI.Views.Controls
1514
{
15+
public class GenericLoginControl : SimpleViewUserControl<ILoginControlViewModel, LoginControl>
16+
{ }
17+
1618
/// <summary>
1719
/// Interaction logic for LoginControl.xaml
1820
/// </summary>
1921
[ExportView(ViewType=UIViewType.Login)]
2022
[PartCreationPolicy(CreationPolicy.NonShared)]
21-
public partial class LoginControl : SimpleViewUserControl, IViewFor<ILoginControlViewModel>, IView
23+
public partial class LoginControl : GenericLoginControl
2224
{
2325
public LoginControl()
2426
{
@@ -112,27 +114,5 @@ void SetupSelectedAndVisibleTabBindings(Action<IDisposable> d)
112114
.Where(x => x == true)
113115
.BindTo(this, v => v.enterpriseTab.IsSelected));
114116
}
115-
116-
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
117-
"ViewModel", typeof(ILoginControlViewModel), typeof(LoginControl), new PropertyMetadata(null));
118-
119-
object IViewFor.ViewModel
120-
{
121-
get { return ViewModel; }
122-
set { ViewModel = (ILoginControlViewModel)value; }
123-
}
124-
125-
object IView.ViewModel
126-
{
127-
get { return ViewModel; }
128-
set { ViewModel = (ILoginControlViewModel)value; }
129-
}
130-
131-
public ILoginControlViewModel ViewModel
132-
{
133-
[return: AllowNull]
134-
get { return (ILoginControlViewModel)GetValue(ViewModelProperty); }
135-
set { SetValue(ViewModelProperty, value); }
136-
}
137117
}
138118
}

src/GitHub.VisualStudio/UI/Views/Controls/RepositoryCloneControl.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<uirx:SimpleViewUserControl x:Class="GitHub.VisualStudio.UI.Views.Controls.RepositoryCloneControl"
1+
<local:GenericRepositoryCloneControl x:Class="GitHub.VisualStudio.UI.Views.Controls.RepositoryCloneControl"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -231,4 +231,4 @@
231231
</Grid>
232232
</Border>
233233
</DockPanel>
234-
</uirx:SimpleViewUserControl>
234+
</local:GenericRepositoryCloneControl>

src/GitHub.VisualStudio/UI/Views/Controls/RepositoryCloneControl.xaml.cs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919

2020
namespace GitHub.VisualStudio.UI.Views.Controls
2121
{
22+
public class GenericRepositoryCloneControl : SimpleViewUserControl<IRepositoryCloneViewModel, RepositoryCloneControl>
23+
{}
24+
2225
/// <summary>
2326
/// Interaction logic for CloneRepoControl.xaml
2427
/// </summary>
2528
[ExportView(ViewType=UIViewType.Clone)]
2629
[PartCreationPolicy(CreationPolicy.NonShared)]
27-
public partial class RepositoryCloneControl : SimpleViewUserControl, IViewFor<IRepositoryCloneViewModel>, IView
30+
public partial class RepositoryCloneControl : GenericRepositoryCloneControl
2831
{
2932
public RepositoryCloneControl()
3033
{
@@ -56,28 +59,6 @@ public RepositoryCloneControl()
5659
};
5760
}
5861

59-
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
60-
"ViewModel", typeof(IRepositoryCloneViewModel), typeof(RepositoryCloneControl), new PropertyMetadata(null));
61-
62-
object IViewFor.ViewModel
63-
{
64-
get { return ViewModel; }
65-
set { ViewModel = (IRepositoryCloneViewModel)value; }
66-
}
67-
68-
object IView.ViewModel
69-
{
70-
get { return ViewModel; }
71-
set { ViewModel = (IRepositoryCloneViewModel)value; }
72-
}
73-
74-
public IRepositoryCloneViewModel ViewModel
75-
{
76-
[return: AllowNull]
77-
get { return (IRepositoryCloneViewModel)GetValue(ViewModelProperty); }
78-
set { SetValue(ViewModelProperty, value); }
79-
}
80-
8162
static ListCollectionView CreateRepositoryListCollectionView(IEnumerable<IRepositoryModel> repositories)
8263
{
8364
var view = new ListCollectionView((IList)repositories);

src/GitHub.VisualStudio/UI/Views/Controls/RepositoryCreationControl.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<uirx:SimpleViewUserControl x:Class="GitHub.VisualStudio.UI.Views.Controls.RepositoryCreationControl"
1+
<local:GenericRepositoryCreationControl x:Class="GitHub.VisualStudio.UI.Views.Controls.RepositoryCreationControl"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -231,4 +231,4 @@
231231
</StackPanel>
232232

233233
</DockPanel>
234-
</uirx:SimpleViewUserControl>
234+
</local:GenericRepositoryCreationControl>

src/GitHub.VisualStudio/UI/Views/Controls/RepositoryCreationControl.xaml.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515

1616
namespace GitHub.VisualStudio.UI.Views.Controls
1717
{
18+
public class GenericRepositoryCreationControl : SimpleViewUserControl<IRepositoryCreationViewModel, RepositoryCreationControl>
19+
{ }
20+
1821
/// <summary>
1922
/// Interaction logic for CloneRepoControl.xaml
2023
/// </summary>
2124
[ExportView(ViewType=UIViewType.Create)]
2225
[PartCreationPolicy(CreationPolicy.NonShared)]
23-
public partial class RepositoryCreationControl : SimpleViewUserControl, IViewFor<IRepositoryCreationViewModel>, IView
26+
public partial class RepositoryCreationControl : GenericRepositoryCreationControl
2427
{
2528
public RepositoryCreationControl()
2629
{
@@ -74,28 +77,5 @@ public RepositoryCreationControl()
7477
this.TryMoveFocus(FocusNavigationDirection.First).Subscribe();
7578
};
7679
}
77-
78-
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
79-
"ViewModel", typeof(IRepositoryCreationViewModel), typeof(RepositoryCreationControl), new PropertyMetadata(null));
80-
81-
object IViewFor.ViewModel
82-
{
83-
get { return ViewModel; }
84-
set { ViewModel = (IRepositoryCreationViewModel)value; }
85-
}
86-
87-
object IView.ViewModel
88-
{
89-
get { return ViewModel; }
90-
set { ViewModel = (IRepositoryCreationViewModel)value; }
91-
}
92-
93-
public IRepositoryCreationViewModel ViewModel
94-
{
95-
[return: AllowNull]
96-
get
97-
{ return (IRepositoryCreationViewModel)GetValue(ViewModelProperty); }
98-
set { SetValue(ViewModelProperty, value); }
99-
}
10080
}
10181
}

src/GitHub.VisualStudio/UI/Views/Controls/RepositoryPublishControl.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<uirx:SimpleViewUserControl x:Class="GitHub.VisualStudio.UI.Views.Controls.RepositoryPublishControl"
1+
<local:GenericRepositoryPublishControl x:Class="GitHub.VisualStudio.UI.Views.Controls.RepositoryPublishControl"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -154,4 +154,4 @@
154154
</Button>
155155

156156
</StackPanel>
157-
</uirx:SimpleViewUserControl>
157+
</local:GenericRepositoryPublishControl>

src/GitHub.VisualStudio/UI/Views/Controls/RepositoryPublishControl.xaml.cs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
namespace GitHub.VisualStudio.UI.Views.Controls
1414
{
15-
/// <summary>
16-
/// Interaction logic for CloneRepoControl.xaml
17-
/// </summary>
15+
public class GenericRepositoryPublishControl : SimpleViewUserControl<IRepositoryPublishViewModel, RepositoryPublishControl>
16+
{ }
17+
1818
[ExportView(ViewType=UIViewType.Publish)]
1919
[PartCreationPolicy(CreationPolicy.NonShared)]
20-
public partial class RepositoryPublishControl : SimpleViewUserControl, IViewFor<IRepositoryPublishViewModel>, IView
20+
public partial class RepositoryPublishControl : GenericRepositoryPublishControl
2121
{
2222
public RepositoryPublishControl()
2323
{
@@ -57,27 +57,5 @@ public RepositoryPublishControl()
5757
this.TryMoveFocus(FocusNavigationDirection.First).Subscribe();
5858
};
5959
}
60-
61-
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
62-
"ViewModel", typeof(IRepositoryPublishViewModel), typeof(RepositoryPublishControl), new PropertyMetadata(null));
63-
64-
object IViewFor.ViewModel
65-
{
66-
get { return ViewModel; }
67-
set { ViewModel = (IRepositoryPublishViewModel)value; }
68-
}
69-
70-
object IView.ViewModel
71-
{
72-
get { return ViewModel; }
73-
set { ViewModel = (IRepositoryPublishViewModel)value; }
74-
}
75-
76-
public IRepositoryPublishViewModel ViewModel
77-
{
78-
[return: AllowNull]
79-
get { return (IRepositoryPublishViewModel)GetValue(ViewModelProperty); }
80-
set { SetValue(ViewModelProperty, value); }
81-
}
8260
}
8361
}

src/GitHub.VisualStudio/UI/Views/Controls/TwoFactorControl.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<uirx:SimpleViewUserControl x:Class="GitHub.VisualStudio.UI.Views.Controls.TwoFactorControl"
1+
<local:GenericTwoFactorControl x:Class="GitHub.VisualStudio.UI.Views.Controls.TwoFactorControl"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -82,4 +82,4 @@
8282
Content="{x:Static prop:Resources.resendCodeButtonContent}" />
8383
</StackPanel>
8484
</StackPanel>
85-
</uirx:SimpleViewUserControl>
85+
</local:GenericTwoFactorControl>

0 commit comments

Comments
 (0)