[tasque/viewmodel: 64/78] Implement task notes on viewmodel layer



commit 64c3412ac707850d5150d25ed9ceb75e98b4ffe3
Author: Antonius Riha <antoniusriha gmail com>
Date:   Wed Aug 8 18:42:10 2012 +0200

    Implement task notes on viewmodel layer
    
    * Renamed NoteDialogModel.cs to NotesDialogModel.cs
    * Implementation reflects current behavior of Tasque.

 src/libtasqueui/Legacy/NoteDialogModel.cs  |   36 --------
 src/libtasqueui/Legacy/NoteModel.cs        |  126 ++++++++++++++++++++++++++++
 src/libtasqueui/Legacy/NotesDialogModel.cs |   98 +++++++++++++++++++++
 src/libtasqueui/libtasqueui.csproj         |    3 +-
 4 files changed, 226 insertions(+), 37 deletions(-)
---
diff --git a/src/libtasqueui/Legacy/NoteModel.cs b/src/libtasqueui/Legacy/NoteModel.cs
new file mode 100644
index 0000000..889280d
--- /dev/null
+++ b/src/libtasqueui/Legacy/NoteModel.cs
@@ -0,0 +1,126 @@
+// 
+// NoteModel.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using CrossCommand;
+
+namespace Tasque.UIModel.Legacy
+{
+	public class NoteModel : ViewModel
+	{
+		internal NoteModel (TaskNote note, ViewModel parent) : base (parent)
+		{
+			if (note == null)
+				throw new ArgumentNullException ("note");
+			Note = note;
+			Text = note.Text;
+		}
+		
+		public bool IsEditable { get; private set; }
+		
+		public string Text {
+			get { return text; }
+			set {
+				if (value != text) {
+					text = value;
+					OnPropertyChanged ("Text");
+				}
+			}
+		}
+		
+		public ICommand Edit {
+			get {
+				if (edit == null) {
+					edit = new RelayCommand () {
+						CanExecuteAction = delegate { return !IsEditable; },
+						ExecuteAction = delegate {
+							IsEditable = true;
+							OnPropertyChanged ("IsEditable");
+						}
+					};
+				}
+				return edit;
+			}
+		}
+		
+		public ICommand Remove {
+			get {
+				if (remove == null) {
+					remove = new RelayCommand () {
+						CanExecuteAction = delegate { return !IsEditable; },
+						ExecuteAction = delegate { OnClose (); }
+					};
+				}
+				return remove;
+			}
+		}
+		
+		public ICommand Cancel {
+			get {
+				if (cancel == null) {
+					cancel = new RelayCommand () {
+						CanExecuteAction = delegate { return IsEditable; },
+						ExecuteAction = delegate {
+							Text = Note.Text;
+							IsEditable = false;
+							OnPropertyChanged ("IsEditable");
+						}
+					};
+				}
+				return cancel;
+			}
+		}
+		
+		public ICommand Save {
+			get {
+				if (save == null) {
+					save = new RelayCommand () {
+						CanExecuteAction = delegate { return IsEditable; },
+						ExecuteAction = delegate {
+							Note.Text = Text;
+							IsEditable = false;
+							OnPropertyChanged ("IsEditable");
+						}
+					};
+				}
+				return save;
+			}
+		}
+		
+		protected override void OnClose ()
+		{
+			if (Removed != null)
+				Removed (this, EventArgs.Empty);
+			base.OnClose ();
+		}
+		
+		internal TaskNote Note { get; private set; }
+		
+		internal event EventHandler Removed;
+		
+		string text;
+		RelayCommand cancel, edit, remove, save;
+	}
+}
diff --git a/src/libtasqueui/Legacy/NotesDialogModel.cs b/src/libtasqueui/Legacy/NotesDialogModel.cs
new file mode 100644
index 0000000..64a255f
--- /dev/null
+++ b/src/libtasqueui/Legacy/NotesDialogModel.cs
@@ -0,0 +1,98 @@
+// 
+// NotesDialogModel.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// 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.Collections.Specialized;
+using System.Linq;
+using CrossCommand;
+
+namespace Tasque.UIModel.Legacy
+{
+	public class NotesDialogModel : ViewModel
+	{
+		public NotesDialogModel (Task task, ViewModel parent) : base (parent)
+		{
+			if (task == null)
+				throw new ArgumentNullException ("task");
+			this.task = task;
+			
+			Title = "Notes for: " + task.Name;
+			
+			notes = new ObservableCollection<NoteModel> ();
+			foreach (var note in task.Notes)
+				AddNote (note);
+			Notes = new ReadOnlyObservableCollection<NoteModel> (notes);
+			
+			((INotifyCollectionChanged)task.Notes).CollectionChanged += HandleNoteCollectionChanged;
+		}
+
+		public string Title { get; private set; }
+		
+		public ReadOnlyObservableCollection<NoteModel> Notes { get; private set; }
+		
+		public ICommand Add {
+			get {
+				if (add == null) {
+					add = new RelayCommand () {
+						ExecuteAction = delegate { task.AddNote (new TaskNote ()); }
+					};
+				}
+				return add;
+			}
+		}
+		
+		protected override void Dispose (bool disposing)
+		{
+			if (disposing)
+				((INotifyCollectionChanged)task.Notes).CollectionChanged -= HandleNoteCollectionChanged;
+			base.Dispose (disposing);
+		}
+		
+		void AddNote (TaskNote note)
+		{
+			var noteModel = new NoteModel (note, this);
+			noteModel.Removed += (sender, e) => task.RemoveNote (((NoteModel)sender).Note);
+			notes.Add (noteModel);
+		}
+		
+		void HandleNoteCollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
+		{
+			switch (e.Action) {
+			case NotifyCollectionChangedAction.Add:
+				AddNote ((TaskNote)e.NewItems [0]);
+				break;
+			case NotifyCollectionChangedAction.Remove:
+				notes.Remove (notes.Single (n => n.Note == (TaskNote)e.OldItems [0]));
+				break;
+			}
+		}
+		
+		ObservableCollection<NoteModel> notes;
+		Task task;
+		
+		RelayCommand add, close;
+	}
+}
diff --git a/src/libtasqueui/libtasqueui.csproj b/src/libtasqueui/libtasqueui.csproj
index c8d59cd..eec36be 100644
--- a/src/libtasqueui/libtasqueui.csproj
+++ b/src/libtasqueui/libtasqueui.csproj
@@ -93,7 +93,6 @@
     <Compile Include="Legacy\MainWindowModel.cs" />
     <Compile Include="Legacy\PreferencesDialogModel.cs" />
     <Compile Include="Legacy\AboutDialogModel.cs" />
-    <Compile Include="Legacy\NoteDialogModel.cs" />
     <Compile Include="Legacy\TrayModel.cs" />
     <Compile Include="Legacy\TaskComparer.cs" />
     <Compile Include="Legacy\CompletionDateRange.cs" />
@@ -120,6 +119,8 @@
     <Compile Include="Legacy\TaskContextMenuModel.cs" />
     <Compile Include="Legacy\SettingGroupModel.cs" />
     <Compile Include="ViewModel.cs" />
+    <Compile Include="Legacy\NotesDialogModel.cs" />
+    <Compile Include="Legacy\NoteModel.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>



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