client/src/Iri.Modernisation.Controls/ViewModel/ConsultationBookView/ConsultationBookViewVM.cs
changeset 42 594fdedecf7f
parent 35 43bb1b8ed555
child 46 ab3057b82260
--- a/client/src/Iri.Modernisation.Controls/ViewModel/ConsultationBookView/ConsultationBookViewVM.cs	Wed Feb 10 14:56:46 2010 +0100
+++ b/client/src/Iri.Modernisation.Controls/ViewModel/ConsultationBookView/ConsultationBookViewVM.cs	Thu Feb 11 17:29:23 2010 +0100
@@ -54,25 +54,41 @@
             }
             set
             {
-                _selectedIndex = value;
-                _selectedIndexTitle = ((SegmentIndex)value).Title;
-                _selectedIndexDescription = ((SegmentIndex)value).Description;
-                _selectedIndexTags = ((SegmentIndex)value).Tags;
-                _selectedBook = _selectedIndex.Chapter.Book;
-                _title = _selectedIndex.Chapter.Book.Title;
-                _author = _selectedIndex.Chapter.Book.Author;
-                _chapters = _selectedIndex.Chapter.Book.Chapters;
-                _selectedIndexAnnotation.Clear();
-                List<AnnotationViewerVM> _temp = new List<AnnotationViewerVM>();
-                foreach (Annotation An in ((SegmentIndex)value).Chapter.Annotations)
+                if (value != null)
                 {
-                    if (An.TimerIn <= ((SegmentIndex)value).TimerOut && An.TimerOut >= ((SegmentIndex)value).TimerIn)
+                    _selectedIndex = value;
+                    _selectedIndexTitle = ((SegmentIndex)value).Title;
+                    _selectedIndexDescription = ((SegmentIndex)value).Description;
+                    _selectedIndexTags = ((SegmentIndex)value).Tags;
+                    _selectedBook = _selectedIndex.Chapter.Book;
+                    _title = _selectedIndex.Chapter.Book.Title;
+                    _author = _selectedIndex.Chapter.Book.Author;
+                    _chapters = _selectedIndex.Chapter.Book.Chapters;
+                    _selectedIndexAnnotation.Clear();
+                    List<AnnotationViewerVM> _temp = new List<AnnotationViewerVM>();
+                    foreach (Annotation An in ((SegmentIndex)value).Chapter.Annotations)
                     {
-                          _temp.Add(new AnnotationViewerVM (An));
+                        if (An.TimerIn <= ((SegmentIndex)value).TimerOut && An.TimerOut >= ((SegmentIndex)value).TimerIn)
+                        {
+                            _temp.Add(new AnnotationViewerVM(An));
+                        }
                     }
+                    SelectedIndexAnnotation = _temp;
                 }
-                SelectedIndexAnnotation = _temp;
-
+                else
+                {
+                    _selectedIndex = null;
+                    _selectedIndexTitle = String.Empty;
+                    _selectedIndexDescription = String.Empty;
+                    _selectedIndexTags = new List<string>();
+                    _selectedBook = null;
+                    _title = null;
+                    _author = null;
+                    _chapters = null;
+                    _selectedIndexAnnotation.Clear();
+                  
+                    SelectedIndexAnnotation = new List<AnnotationViewerVM>();
+                }
                 OnPropertyChanged(null);
             
          
@@ -169,7 +185,7 @@
             private set
             {
                 _title = value;
-                _selectedBook.Title = value;
+           
                 OnPropertyChanged("Title");
             }
         }
@@ -187,7 +203,7 @@
             private set
             {
                 _author = value;
-                _selectedBook.Author = value;
+            
                 OnPropertyChanged("Author");
             }
 
@@ -206,7 +222,7 @@
             private set
             {
                 _chapters = value;
-                _selectedBook.Chapters = value;
+        
                 OnPropertyChanged("Chapters");
             }
         }
@@ -257,21 +273,35 @@
             }
             set
             {
-                _selectedBookVM = value;
-             
-                _selectedBook = value.SelectedBook;
-                _title = value.SelectedBook.Title;
-                _author = value.SelectedBook.Author;
-                _chapters = value.SelectedBook.Chapters;
-                ActualVideoSourceVM.Source = value.SelectedBook.MediaPath;
+                if (value != null)
+                {
+                    _selectedBookVM = value;
+
+                    _selectedBook = value.SelectedBook;
+                    _title = value.SelectedBook.Title;
+                    _author = value.SelectedBook.Author;
+                    _chapters = value.SelectedBook.Chapters;
+                    ActualVideoSourceVM.Source = value.SelectedBook.MediaPath;
                     ActualVideoSourceVM.Position = TimeSpan.FromMilliseconds(value.Position);
-               
-               
-                
-                SelectedIndex = value.SelectedBook.Chapters[0].Index[0];
-             
-                OnPropertyChanged(null);
-                Commands.GoToTime.Execute(value.Position);
+
+
+
+                    SelectedIndex = value.SelectedBook.Chapters[0].Index[0];
+                    Commands.GoToTime.Execute(value.Position);
+                }
+                else
+                {
+                    _selectedBookVM = null;
+                    _selectedBook = null;
+                    _title = null;
+                    _author = null;
+                    _chapters = null;
+                    ActualVideoSourceVM.Source = null;
+                    ActualVideoSourceVM.Position = TimeSpan.Zero;
+                    SelectedIndex = null;
+                }
+                OnPropertyChanged(String.Empty);
+              
             }
         }
         private VideoViewerVM _actualVideoSourceVM;
@@ -313,19 +343,55 @@
             SelectedIndex = param.SelectedBook.Chapters[0].Index[0];
             
             InitializeCommands();
+           
+          
 
+        }
 
+        void ActualVideoSourceVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
+        {
+         
+            if (e.PropertyName == "Position")
+            {
+                if (SelectedBook != null)
+                {
+                    TimeSpan Pos = TimeSpan.FromMilliseconds(SelectedBookVM.Position);
+                    SelectedBookVM.Position = Pos.TotalMilliseconds;
+                    if (SelectedIndex.TimerOut < Pos || SelectedIndex.TimerIn > Pos)
+                    {
+                        foreach (SegmentIndex Index in SelectedIndex.Chapter.Index)
+                        {
+                            if (Index.TimerIn <= Pos && Index.TimerOut >= Pos)
+                            {
+                                SelectedIndex = Index;
+                            }
+
+                        }
+                    }
+                }
+            }
         }
         private void InitializeCommands()
         {
             ActualVideoSourceVM.Tick += new EventHandler<VideoViewerVMEventArgs>(ActualVideoSourceVM_Tick);
-              }
+            Commands.TimeChange.Executed += new EventHandler<SLExtensions.Input.ExecutedEventArgs>(TimeChange_Executed);
+        }
 
-        private void ActualVideoSourceVM_Tick(object sender, VideoViewerVMEventArgs e)
+        void TimeChange_Executed(object sender, SLExtensions.Input.ExecutedEventArgs e)
         {
-            if(SelectedBook!=null)
+            BookTimeLineVM BookTimeLineVM = (BookTimeLineVM)e.Source;
+
+            if (BookTimeLineVM == this.SelectedBookVM)
             {
-                TimeSpan Pos = e.Position; 
+                UpdateAnnotation();
+            }
+        }
+
+        private void UpdateAnnotation()
+        {
+            if (SelectedBook != null)
+            {
+                TimeSpan Pos = TimeSpan.FromMilliseconds(SelectedBookVM.Position);
                 SelectedBookVM.Position = Pos.TotalMilliseconds;
                 if (SelectedIndex.TimerOut < Pos || SelectedIndex.TimerIn > Pos)
                 {
@@ -340,6 +406,12 @@
                 }
             }
         }
+
+        private void ActualVideoSourceVM_Tick(object sender, VideoViewerVMEventArgs e)
+        {
+            SelectedBookVM.Position =  e.Position.TotalMilliseconds;
+            UpdateAnnotation();
+        }
        
     }
 }