[tasque/viewmodel: 60/78] Improve ViewModel class



commit 0eaef93cf0b605ae248872ff4a4989717390ee4c
Author: Antonius Riha <antoniusriha gmail com>
Date:   Wed Aug 8 18:32:53 2012 +0200

    Improve ViewModel class
    
    * Rename ViewModelBase to ViewModel
    * Remove IDisposable interface: don't make it part of the API, but process Disposing internally.
    * Add support for building a ViewModel tree: Children are now registered in their parent upon creation.
    * Implement Close command: disposes of the child ViewModel tree recursively. Setup OnClose and Closed hooks.

 src/libtasqueui/{ViewModelBase.cs => ViewModel.cs} |   48 ++++++++++++++++++--
 src/libtasqueui/libtasqueui.csproj                 |    2 +-
 2 files changed, 45 insertions(+), 5 deletions(-)
---
diff --git a/src/libtasqueui/ViewModelBase.cs b/src/libtasqueui/ViewModel.cs
similarity index 62%
rename from src/libtasqueui/ViewModelBase.cs
rename to src/libtasqueui/ViewModel.cs
index 1620d1c..90c14e9 100644
--- a/src/libtasqueui/ViewModelBase.cs
+++ b/src/libtasqueui/ViewModel.cs
@@ -1,5 +1,5 @@
 // 
-// ViewModelBase.cs
+// ViewModel.cs
 //  
 // Author:
 //       Antonius Riha <antoniusriha gmail com>
@@ -24,13 +24,43 @@
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 using System;
+using System.Collections.ObjectModel;
 using System.ComponentModel;
+using CrossCommand;
 
 namespace Tasque.UIModel
 {
-	public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
+	public abstract class ViewModel : INotifyPropertyChanged
 	{
-		public void Dispose ()
+		internal ViewModel (ViewModel parent)
+		{
+			this.parent = parent;
+			if (parent != null)
+				parent.Children.Add (this);
+		}
+		
+		public ICommand Close {
+			get {
+				if (close == null) {
+					close = new RelayCommand () {
+						ExecuteAction = delegate {
+							foreach (var child in Children)
+								child.Close.Execute (null);
+							
+							parent.Children.Remove (this);
+							Dispose ();
+							if (Closed != null)
+								Closed (this, EventArgs.Empty);
+						}
+					};
+				}
+				return close;
+			}
+		}
+		
+		protected virtual void OnClose () {}
+		
+		void Dispose ()
 		{
 			Dispose (true);
 			GC.SuppressFinalize (this);
@@ -38,7 +68,7 @@ namespace Tasque.UIModel
 		
 		protected virtual void Dispose (bool disposing) {}
 		
-		~ViewModelBase ()
+		~ViewModel ()
 		{
 			Dispose (false);
 		}
@@ -50,5 +80,15 @@ namespace Tasque.UIModel
 			if (PropertyChanged != null)
 				PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
 		}
+		
+		internal Collection<ViewModel> Children {
+			get { return children ?? (children = new Collection<ViewModel> ()); }
+		}
+		
+		internal event EventHandler Closed;
+		
+		Collection<ViewModel> children;
+		RelayCommand close;
+		ViewModel parent;
 	}
 }
diff --git a/src/libtasqueui/libtasqueui.csproj b/src/libtasqueui/libtasqueui.csproj
index 9d3159d..4489421 100644
--- a/src/libtasqueui/libtasqueui.csproj
+++ b/src/libtasqueui/libtasqueui.csproj
@@ -94,7 +94,6 @@
     <Compile Include="Legacy\PreferencesDialogModel.cs" />
     <Compile Include="Legacy\AboutDialogModel.cs" />
     <Compile Include="Legacy\NoteDialogModel.cs" />
-    <Compile Include="ViewModelBase.cs" />
     <Compile Include="Legacy\TrayModel.cs" />
     <Compile Include="Legacy\TaskComparer.cs" />
     <Compile Include="Legacy\CompletionDateRange.cs" />
@@ -118,6 +117,7 @@
     <Compile Include="ValueConverter.cs" />
     <Compile Include="Legacy\CompletionDateRangeConverter.cs" />
     <Compile Include="Legacy\TaskGroupNameConverter.cs" />
+    <Compile Include="ViewModel.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]