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

Commit b9b0f21

Browse files
committed
🔥 Cut down on duplicated code
Tired of stuffing a bunch of ViewModel copies every time I do a new view. Every time you duplicate code, god kills a kitten (and every time you deduplicate code, god restores a kiten, so we should be striving for positive kitten numbers here)
1 parent b69935d commit b9b0f21

11 files changed

+70
-132
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
using System.Windows.Input;
88
using System.Windows.Navigation;
99
using ReactiveUI;
10+
using System.Windows;
11+
using GitHub.ViewModels;
12+
using NullGuard;
1013

1114
namespace GitHub.UI
1215
{
@@ -77,6 +80,37 @@ public void Dispose()
7780
Dispose(true);
7881
GC.SuppressFinalize(this);
7982
}
83+
}
84+
85+
public class SimpleViewUserControl<TViewModel, TImplementor> : SimpleViewUserControl, IViewFor<TViewModel>, IView
86+
where TViewModel : class, IViewModel where TImplementor : class
87+
{
88+
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
89+
"ViewModel", typeof(TViewModel), typeof(TImplementor), new PropertyMetadata(null));
90+
91+
object IViewFor.ViewModel
92+
{
93+
get { return ViewModel; }
94+
set { ViewModel = (TViewModel)value; }
95+
}
96+
97+
object IView.ViewModel
98+
{
99+
get { return ViewModel; }
100+
set { ViewModel = (TViewModel)value; }
101+
}
80102

103+
public TViewModel ViewModel
104+
{
105+
[return: AllowNull]
106+
get { return (TViewModel)GetValue(ViewModelProperty); }
107+
set { SetValue(ViewModelProperty, value); }
108+
}
109+
110+
TViewModel IViewFor<TViewModel>.ViewModel
111+
{
112+
get { return ViewModel; }
113+
set { ViewModel = (TViewModel)value; }
114+
}
81115
}
82116
}

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: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
using GitHub.ViewModels;
1010
using NullGuard;
1111
using ReactiveUI;
12+
using System.ComponentModel.Composition;
1213

1314
namespace GitHub.VisualStudio.UI.Views.Controls
1415
{
16+
public class GenericLoginControl : SimpleViewUserControl<ILoginControlViewModel, LoginControl>
17+
{ }
18+
1519
/// <summary>
1620
/// Interaction logic for LoginControl.xaml
1721
/// </summary>
1822
[ExportView(ViewType=UIViewType.Login)]
19-
public partial class LoginControl : SimpleViewUserControl, IViewFor<ILoginControlViewModel>, IView
23+
public partial class LoginControl : GenericLoginControl
2024
{
2125
public LoginControl()
2226
{
@@ -110,27 +114,5 @@ void SetupSelectedAndVisibleTabBindings(Action<IDisposable> d)
110114
.Where(x => x == true)
111115
.BindTo(this, v => v.enterpriseTab.IsSelected));
112116
}
113-
114-
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
115-
"ViewModel", typeof(ILoginControlViewModel), typeof(LoginControl), new PropertyMetadata(null));
116-
117-
object IViewFor.ViewModel
118-
{
119-
get { return ViewModel; }
120-
set { ViewModel = (ILoginControlViewModel)value; }
121-
}
122-
123-
object IView.ViewModel
124-
{
125-
get { return ViewModel; }
126-
set { ViewModel = (ILoginControlViewModel)value; }
127-
}
128-
129-
public ILoginControlViewModel ViewModel
130-
{
131-
[return: AllowNull]
132-
get { return (ILoginControlViewModel)GetValue(ViewModelProperty); }
133-
set { SetValue(ViewModelProperty, value); }
134-
}
135117
}
136118
}

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: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
using GitHub.ViewModels;
1616
using NullGuard;
1717
using ReactiveUI;
18+
using System.ComponentModel.Composition;
1819

1920
namespace GitHub.VisualStudio.UI.Views.Controls
2021
{
22+
public class GenericRepositoryCloneControl : SimpleViewUserControl<IRepositoryCloneViewModel, RepositoryCloneControl>
23+
{}
24+
2125
/// <summary>
2226
/// Interaction logic for CloneRepoControl.xaml
2327
/// </summary>
2428
[ExportView(ViewType=UIViewType.Clone)]
25-
public partial class RepositoryCloneControl : SimpleViewUserControl, IViewFor<IRepositoryCloneViewModel>, IView
29+
public partial class RepositoryCloneControl : GenericRepositoryCloneControl
2630
{
2731
public RepositoryCloneControl()
2832
{
@@ -54,28 +58,6 @@ public RepositoryCloneControl()
5458
};
5559
}
5660

57-
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
58-
"ViewModel", typeof(IRepositoryCloneViewModel), typeof(RepositoryCloneControl), new PropertyMetadata(null));
59-
60-
object IViewFor.ViewModel
61-
{
62-
get { return ViewModel; }
63-
set { ViewModel = (IRepositoryCloneViewModel)value; }
64-
}
65-
66-
object IView.ViewModel
67-
{
68-
get { return ViewModel; }
69-
set { ViewModel = (IRepositoryCloneViewModel)value; }
70-
}
71-
72-
public IRepositoryCloneViewModel ViewModel
73-
{
74-
[return: AllowNull]
75-
get { return (IRepositoryCloneViewModel)GetValue(ViewModelProperty); }
76-
set { SetValue(ViewModelProperty, value); }
77-
}
78-
7961
static ListCollectionView CreateRepositoryListCollectionView(IEnumerable<IRepositoryModel> repositories)
8062
{
8163
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: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
using GitHub.ViewModels;
1212
using NullGuard;
1313
using ReactiveUI;
14+
using System.ComponentModel.Composition;
1415

1516
namespace GitHub.VisualStudio.UI.Views.Controls
1617
{
18+
public class GenericRepositoryCreationControl : SimpleViewUserControl<IRepositoryCreationViewModel, RepositoryCreationControl>
19+
{ }
20+
1721
/// <summary>
1822
/// Interaction logic for CloneRepoControl.xaml
1923
/// </summary>
2024
[ExportView(ViewType=UIViewType.Create)]
21-
public partial class RepositoryCreationControl : SimpleViewUserControl, IViewFor<IRepositoryCreationViewModel>, IView
25+
public partial class RepositoryCreationControl : GenericRepositoryCreationControl
2226
{
2327
public RepositoryCreationControl()
2428
{
@@ -72,28 +76,5 @@ public RepositoryCreationControl()
7276
this.TryMoveFocus(FocusNavigationDirection.First).Subscribe();
7377
};
7478
}
75-
76-
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
77-
"ViewModel", typeof(IRepositoryCreationViewModel), typeof(RepositoryCreationControl), new PropertyMetadata(null));
78-
79-
object IViewFor.ViewModel
80-
{
81-
get { return ViewModel; }
82-
set { ViewModel = (IRepositoryCreationViewModel)value; }
83-
}
84-
85-
object IView.ViewModel
86-
{
87-
get { return ViewModel; }
88-
set { ViewModel = (IRepositoryCreationViewModel)value; }
89-
}
90-
91-
public IRepositoryCreationViewModel ViewModel
92-
{
93-
[return: AllowNull]
94-
get
95-
{ return (IRepositoryCreationViewModel)GetValue(ViewModelProperty); }
96-
set { SetValue(ViewModelProperty, value); }
97-
}
9879
}
9980
}

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: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
using GitHub.ViewModels;
99
using NullGuard;
1010
using ReactiveUI;
11+
using System.ComponentModel.Composition;
1112

1213
namespace GitHub.VisualStudio.UI.Views.Controls
1314
{
14-
/// <summary>
15-
/// Interaction logic for CloneRepoControl.xaml
16-
/// </summary>
15+
public class GenericRepositoryPublishControl : SimpleViewUserControl<IRepositoryPublishViewModel, RepositoryPublishControl>
16+
{ }
17+
1718
[ExportView(ViewType=UIViewType.Publish)]
18-
public partial class RepositoryPublishControl : SimpleViewUserControl, IViewFor<IRepositoryPublishViewModel>, IView
19+
public partial class RepositoryPublishControl : GenericRepositoryPublishControl
1920
{
2021
public RepositoryPublishControl()
2122
{
@@ -55,27 +56,5 @@ public RepositoryPublishControl()
5556
this.TryMoveFocus(FocusNavigationDirection.First).Subscribe();
5657
};
5758
}
58-
59-
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
60-
"ViewModel", typeof(IRepositoryPublishViewModel), typeof(RepositoryPublishControl), new PropertyMetadata(null));
61-
62-
object IViewFor.ViewModel
63-
{
64-
get { return ViewModel; }
65-
set { ViewModel = (IRepositoryPublishViewModel)value; }
66-
}
67-
68-
object IView.ViewModel
69-
{
70-
get { return ViewModel; }
71-
set { ViewModel = (IRepositoryPublishViewModel)value; }
72-
}
73-
74-
public IRepositoryPublishViewModel ViewModel
75-
{
76-
[return: AllowNull]
77-
get { return (IRepositoryPublishViewModel)GetValue(ViewModelProperty); }
78-
set { SetValue(ViewModelProperty, value); }
79-
}
8059
}
8160
}

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)