private void _reloadFiles(MediaDirectoryViewmodel directory)
 {
     if (directory?.IsInitialized == true && (!directory.IsIngestDirectory || directory.IsImport))
     {
         UiServices.SetBusyState();
         if (_mediaItems != null)
         {
             foreach (var m in _mediaItems)
             {
                 m.Dispose();
             }
         }
         MediaItems = new ObservableCollection <MediaViewViewmodel>(directory.Directory.GetFiles().Select(f => new MediaViewViewmodel(f)));
         _mediaView = CollectionViewSource.GetDefaultView(_mediaItems);
         if (!directory.IsXdcam)
         {
             _mediaView.SortDescriptions.Add(new SortDescription(nameof(MediaViewViewmodel.MediaName), ListSortDirection.Ascending));
         }
         if (!directory.IsArchiveDirectory)
         {
             _mediaView.Filter = _filter;
         }
         if (directory.IsXdcam && !directory.IsWan)
         {
             ThreadPool.QueueUserWorkItem(o => _refreshMediaDirectory(directory.Directory));
         }
     }
     else
     {
         MediaItems = null;
     }
 }
        private void _setSelectdDirectory(MediaDirectoryViewmodel directory)
        {
            IMediaDirectory dir = _selectedDirectory?.Directory;

            if (dir != null)
            {
                dir.MediaAdded      -= _selectedDirectoryMediaAdded;
                dir.MediaRemoved    -= _selectedDirectoryMediaRemoved;
                dir.PropertyChanged -= _selectedDirectoryPropertyChanged;
            }
            dir = directory?.Directory;
            _selectedDirectory = directory;
            if (dir != null)
            {
                dir.MediaAdded      += _selectedDirectoryMediaAdded;
                dir.MediaRemoved    += _selectedDirectoryMediaRemoved;
                dir.PropertyChanged += _selectedDirectoryPropertyChanged;
                if (dir is IArchiveDirectory)
                {
                    if (!string.IsNullOrEmpty((dir as IArchiveDirectory).SearchString))
                    {
                        SearchText = (dir as IArchiveDirectory).SearchString;
                    }
                }
            }
            PreviewDisplay = PreviewViewmodel != null &&
                             dir != null &&
                             (!(dir is IIngestDirectory) || (dir as IIngestDirectory).AccessType == TDirectoryAccessType.Direct);
            if (PreviewViewmodel != null)
            {
                PreviewViewmodel.IsSegmentsVisible = dir is IServerDirectory || dir is IArchiveDirectory;
            }
            _reloadFiles(directory);
            SelectedMedia = null;
            NotifyPropertyChanged(nameof(SelectedDirectory));
            NotifyPropertyChanged(nameof(DisplayDirectoryInfo));
            NotifyPropertyChanged(nameof(IsDisplayFolder));
            NotifyPropertyChanged(nameof(IsServerDirectory));
            NotifyPropertyChanged(nameof(IsMediaCategoryVisible));
            NotifyPropertyChanged(nameof(IsIngestOrArchiveDirectory));
            NotifyPropertyChanged(nameof(IsAnimationDirectory));
            NotifyPropertyChanged(nameof(IsDisplayClipNr));
            InvalidateRequerySuggested();
            _notifyDirectoryPropertiesChanged();
        }
        public MediaManagerViewmodel(IMediaManager mediaManager, IPreview preview)
        {
            _mediaManager = mediaManager;
            _preview      = preview;
            if (preview != null)
            {
                PreviewViewmodel = new PreviewViewmodel(preview);
            }

            MediaDirectories = new List <MediaDirectoryViewmodel>();
            MediaDirectories.AddRange(mediaManager.IngestDirectories.Where(d => d.ContainsImport()).Select(d => new MediaDirectoryViewmodel(d, true)));
            IArchiveDirectory archiveDirectory = mediaManager.ArchiveDirectory;

            if (archiveDirectory != null)
            {
                MediaDirectories.Insert(0, new MediaDirectoryViewmodel(archiveDirectory));
            }

            IAnimationDirectory animationDirectoryPRI = mediaManager.AnimationDirectoryPRI;

            if (animationDirectoryPRI != null)
            {
                MediaDirectories.Insert(0, new MediaDirectoryViewmodel(animationDirectoryPRI));
            }
            IAnimationDirectory animationDirectorySEC = mediaManager.AnimationDirectorySEC;

            if (animationDirectorySEC != null)
            {
                MediaDirectories.Insert(0, new MediaDirectoryViewmodel(animationDirectorySEC));
            }

            IServerDirectory        serverDirectoryPRI   = mediaManager.MediaDirectoryPRI;
            MediaDirectoryViewmodel serverDirectoryPRIVm = new MediaDirectoryViewmodel(serverDirectoryPRI);

            if (serverDirectoryPRI != null)
            {
                MediaDirectories.Insert(0, serverDirectoryPRIVm);
            }
            IServerDirectory serverDirectorySEC = mediaManager.MediaDirectorySEC;

            if (serverDirectorySEC != null && serverDirectorySEC != serverDirectoryPRI)
            {
                MediaDirectories.Insert(1, new MediaDirectoryViewmodel(serverDirectorySEC));
            }

            _mediaCategory    = MediaCategories.FirstOrDefault();
            SelectedDirectory = serverDirectoryPRIVm;
            if (mediaManager.FileManager != null)
            {
                FileManagerViewmodel = new FileManagerViewmodel(mediaManager.FileManager);
            }
            RecordersViewmodel = new RecordersViewmodel(mediaManager.Recorders);
            RecordersViewmodel.PropertyChanged += _recordersViewmodel_PropertyChanged;
            _previewDisplay = true;
            ComposePlugins();
            VideoPreview?.SetSource(RecordersViewmodel.Channel?.PreviewUrl);

            CommandSearch = new UICommand {
                ExecuteDelegate = _search, CanExecuteDelegate = _canSearch
            };
            CommandClearFilters = new UICommand {
                ExecuteDelegate = _clearFilters, CanExecuteDelegate = _canClearFilters
            };
            CommandDeleteSelected = new UICommand {
                ExecuteDelegate = _deleteSelected, CanExecuteDelegate = _isSomethingSelected
            };
            CommandIngestSelectedToServer = new UICommand {
                ExecuteDelegate = _ingestSelectedToServer, CanExecuteDelegate = _canIngestSelectedToServer
            };
            CommandMoveSelectedToArchive = new UICommand {
                ExecuteDelegate = _moveSelectedToArchive, CanExecuteDelegate = o => _selectedDirectory != null && _selectedDirectory.IsServerDirectory && _isSomethingSelected(o)
            };
            CommandCopySelectedToArchive = new UICommand {
                ExecuteDelegate = _copySelectedToArchive, CanExecuteDelegate = o => _selectedDirectory != null && _selectedDirectory.IsServerDirectory && _isSomethingSelected(o)
            };
            CommandSweepStaleMedia = new UICommand {
                ExecuteDelegate = _sweepStaleMedia
            };
            CommandGetLoudness = new UICommand {
                ExecuteDelegate = _getLoudness, CanExecuteDelegate = _isSomethingSelected
            };
            CommandExport = new UICommand {
                ExecuteDelegate = _export, CanExecuteDelegate = _canExport
            };
            CommandRefresh = new UICommand {
                ExecuteDelegate = ob => _refreshMediaDirectory(_selectedDirectory?.Directory), CanExecuteDelegate = o => _selectedDirectory?.IsIngestDirectory == true
            };
            CommandSyncPriToSec = new UICommand {
                ExecuteDelegate = _syncSecToPri, CanExecuteDelegate = o => _selectedDirectory.IsServerDirectory
            };
            CommandCloneAnimation = new UICommand {
                ExecuteDelegate = _cloneAnimation, CanExecuteDelegate = _canCloneAnimation
            };
        }