Permalink
Browse files

Refactor stuff out of Window, move to separate ViewModel

  • Loading branch information...
1 parent 1ea9459 commit 01c625f331c5c2b205a98e99c67fb741c00f72f7 @paulcbetts committed Dec 28, 2009
Showing with 105 additions and 25 deletions.
  1. +99 −1 App.xaml.cs
  2. +2 −1 MainWindow.xaml
  3. +4 −23 MainWindow.xaml.cs
View
@@ -3,16 +3,114 @@
using System.Configuration;
using System.Data;
using System.Linq;
+using System.IO;
using System.Windows;
+using System.ComponentModel;
+using System.Collections.ObjectModel;
+using System.ComponentModel.Composition;
namespace InstallQueuer
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
- public partial class App : Application
+ public partial class App : Application
{
}
+
+ class AppViewModel : INotifyPropertyChanged
+ {
+ public AppViewModel(IInputElement view) { currentWindow = view; }
+
+ [ImportMany]
+ public IEnumerable<IPackageInstallerFactory> PackageInstallers { get; set; }
+
+ readonly ObservableCollection<InstallableItem> InstallQueue = new ObservableCollection<InstallableItem>();
+
+ readonly IInputElement currentWindow;
+
+ BackgroundWorker _CurrentRunningJob = null;
+ public BackgroundWorker CurrentRunningJob {
+ get { return _CurrentRunningJob; }
+ set {
+ if (_CurrentRunningJob == value)
+ return;
+ _CurrentRunningJob = value;
+ notifyPropertyChanged("CurrentRunningJob");
+ notifyPropertyChanged("IsRunning");
+ }
+ }
+
+ public bool IsRunning {
+ get { return (CurrentRunningJob != null); }
+ }
+
+
+ //
+ // INotifyPropertyChanged stuff
+ //
+
+ void notifyPropertyChanged(String property)
+ {
+ if (PropertyChanged != null) {
+ PropertyChanged(this, new PropertyChangedEventArgs(property));
+ }
+ }
+ public event PropertyChangedEventHandler PropertyChanged;
+ }
+
+ enum InstallableItemState {
+ Initializing,
+ Queued,
+ Running,
+ Succeeded,
+ Failed,
+ }
+
+ class InstallableItem : INotifyPropertyChanged
+ {
+ public InstallableItem(IPackageInstaller installer) { PackageInstaller = installer; }
+
+ InstallableItemState _State;
+ public InstallableItemState State {
+ get { return _State; }
+ set {
+ if (_State == value)
+ return;
+ _State = value;
+ notifyPropertyChanged("State");
+ }
+ }
+
+ public string FullPath {
+ get { return PackageInstaller.FilePath; }
+ }
+
+ public string FileName {
+ get { return Path.GetFileName(FullPath); }
+ }
+
+ public readonly IPackageInstaller PackageInstaller;
+
+ public BackgroundWorker DoInstall()
+ {
+ if (State != InstallableItemState.Queued)
+ throw new Exception("Only queued items can be started");
+ }
+
+
+ //
+ // INotifyPropertyChanged stuff
+ //
+
+ void notifyPropertyChanged(String property)
+ {
+ if (PropertyChanged != null) {
+ PropertyChanged(this, new PropertyChangedEventArgs(property));
+ }
+ }
+ public event PropertyChangedEventHandler PropertyChanged;
+ }
}
// vim: ts=4:sts=4:sw=4:et
View
@@ -2,7 +2,8 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="350" Height="525">
- <Grid>
+
+ <Grid DataContext="{Binding Path=ViewModel, ElementName=MainWindow}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
View
@@ -22,33 +22,14 @@ namespace InstallQueuer.Ui
/// </summary>
public partial class MainWindow : Window
{
- [ImportMany]
- public IEnumerable<IPackageInstallerFactory> PackageInstallers { get; set; }
-
- readonly ObservableCollection<InstallableItem> InstallQueue = new ObservableCollection<InstallableItem>();
+ readonly AppViewModel ViewModel;
public MainWindow()
{
- InitializeComponent();
-
- var v = new MsiPackageInstaller("Foo");
- }
- }
-
- class InstallableItem : INotifyPropertyChanged
- {
- //
- // INotifyPropertyChanged stuff
- //
-
- private void NotifyPropertyChanged(String property)
- {
- if (PropertyChanged != null) {
- PropertyChanged(this, new PropertyChangedEventArgs(property));
- }
+ ViewModel = new AppViewModel(this);
+ InitializeComponent();
}
- public event PropertyChangedEventHandler PropertyChanged;
}
}
-// vim: ts=4:sts=4:sw=4:et
+// vim: ts=4:sts=4:sw=4:et

0 comments on commit 01c625f

Please sign in to comment.