Exemple #1
0
        private void mediaState_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            MediaViewer.Model.Media.Base.State.MediaState state = (MediaViewer.Model.Media.Base.State.MediaState)sender;

            switch (e.PropertyName)
            {
            case "MediaStateInfo":
            {
                Dispatcher.BeginInvoke(new Action(() => setInfoText(state.MediaStateInfo)));
                break;
            }

            case "MediaStateDateTime":
            {
                Dispatcher.BeginInvoke(new Action(() => setDateTime(state.MediaStateDateTime)));
                break;
            }

            case "MediaStateType":
            {
                Dispatcher.BeginInvoke(new Action(() => setMediaStateType(state.MediaStateType)));
                break;
            }
            }
        }
        public ImageSearchViewModel()           
        {                   
            NrColumns = 4;

            SearchCommand = new AsyncCommand<int>(async (imageOffset) =>
            {
                try
                {
                    if (imageOffset == 0)
                    {
                        CurrentQuery = new ImageSearchQuery(this);
                    }

                    SearchCommand.IsExecutable = false;
                    await doSearch(CurrentQuery, imageOffset);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Image search error\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                finally
                {
                    SearchCommand.IsExecutable = true;
                }
            });

            ViewCommand = new Command<SelectableMediaItem>((selectableItem) =>
                {
                    ImageResultItem item = (ImageResultItem)selectableItem.Item;
                    
                    if(item.ImageInfo.ContentType.Equals("image/animatedgif")) {

                        Shell.ShellViewModel.navigateToVideoView(item);  

                    } else {

                        Shell.ShellViewModel.navigateToImageView(item);     
                    }
                });

            ViewSourceCommand = new Command<SelectableMediaItem>((selectableItem) =>
                {
                    ImageResultItem item = (ImageResultItem)selectableItem.Item;

                    Process.Start(item.ImageInfo.SourceUrl);
                });

            SelectAllCommand = new Command(() =>
            {
                MediaStateCollectionView.selectAll();
            }, false);

            DeselectAllCommand = new Command(() =>
            {
                MediaStateCollectionView.deselectAll();
            });

            CloseCommand = new Command(() =>
            {             
                OnClosingRequest();
            });

            DownloadCommand = new AsyncCommand<SelectableMediaItem>(async (selectableItem) =>
                {
                    List<MediaItem> items = MediaStateCollectionView.getSelectedItems();
                    if (items.Count == 0)
                    {
                        items.Add(selectableItem.Item);
                    }

                    String outputPath = null;

                    switch (ImageSearchPlugin.Properties.Settings.Default.ImageSaveMode)
                    {
                        case MediaViewer.Infrastructure.Constants.SaveLocation.Current:
                            {
                                outputPath = MediaFileWatcher.Instance.Path;
                                break;
                            }
                        case MediaViewer.Infrastructure.Constants.SaveLocation.Ask:
                            {
                                DirectoryPickerView directoryPicker = new DirectoryPickerView();
                                directoryPicker.DirectoryPickerViewModel.InfoString = "Select Output Directory";
                                directoryPicker.DirectoryPickerViewModel.SelectedPath = MediaFileWatcher.Instance.Path;

                                if (directoryPicker.ShowDialog() == false)
                                {
                                    return;
                                }

                                outputPath = directoryPicker.DirectoryPickerViewModel.SelectedPath;

                                break;
                            }
                        case MediaViewer.Infrastructure.Constants.SaveLocation.Fixed:
                            {
                                outputPath = ImageSearchPlugin.Properties.Settings.Default.FixedDownloadPath;
                                break;
                            }
                        default:
                            break;
                    }

                    CancellableOperationProgressView progressView = new CancellableOperationProgressView();
                    DownloadProgressViewModel vm = new DownloadProgressViewModel();
                    progressView.DataContext = vm;

                    progressView.Show();
                    vm.OkCommand.IsExecutable = false;
                    vm.CancelCommand.IsExecutable = true;

                    try
                    {
                        await Task.Factory.StartNew(() =>
                        {
                            vm.startDownload(outputPath, items);

                        }, vm.CancellationToken);

                    }
                    catch (Exception)
                    {

                    }

                    vm.OkCommand.IsExecutable = true;
                    vm.CancelCommand.IsExecutable = false;
                });

            SettingsViewModel = (ImageSearchSettingsViewModel)ServiceLocator.Current.GetInstance(typeof(ImageSearchSettingsViewModel));
           
            Size = new ListCollectionView(size);
            Size.MoveCurrentTo(Settings.Default.Size);
            SafeSearch = new ListCollectionView(safeSearch);
            SafeSearch.MoveCurrentTo(Settings.Default.SafeSearch);
            Layout = new ListCollectionView(layout);
            Layout.MoveCurrentTo(Settings.Default.Layout);
            Type = new ListCollectionView(type);
            Type.MoveCurrentTo(Settings.Default.Type);
            People = new ListCollectionView(people);
            People.MoveCurrentTo(Settings.Default.People);
            Color = new ListCollectionView(color);
            Color.MoveCurrentTo(Settings.Default.Color);

            GeoTag = new GeoTagCoordinatePair();

            MediaState = new MediaState();

            MediaStateCollectionView = new ImageResultCollectionView(MediaState);        
            MediaStateCollectionView.MediaState.MediaStateType = MediaStateType.SearchResult;

            WeakEventManager<MediaLockedCollection, EventArgs>.AddHandler(MediaStateCollectionView.MediaState.UIMediaCollection, "IsLoadingChanged", mediaCollection_IsLoadingChanged);
            
        }
        public YoutubeViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            TokenSource = new CancellationTokenSource();
            
            RegionManager = regionManager;
            EventAggregator = eventAggregator;
            NrColumns = 4;

            MediaState = new MediaState();
            MediaStateCollectionView = new YoutubeCollectionView(MediaState);
            MediaState.clearUIState("Empty", DateTime.Now, MediaStateType.SearchResult);

            MediaStateCollectionView.SelectionChanged += mediaStateCollectionView_SelectionChanged;
                        
            ViewCommand = new Command<SelectableMediaItem>((selectableItem) =>
            {               
                if(selectableItem.Item.Metadata == null) return;

                if (selectableItem.Item is YoutubeVideoItem)
                {
                    YoutubeVideoItem item = selectableItem.Item as YoutubeVideoItem;

                    if (item.IsEmbeddedOnly)
                    {
                        Process.Start("https://www.youtube.com/watch?v=" + item.VideoId);
                    }
                    else
                    {
                        YoutubeVideoStreamedItem video, audio;
                        item.getStreams(out video, out audio, (int)Properties.Settings.Default.MaxPlaybackResolution);

                        Shell.ShellViewModel.navigateToVideoView(video, null, audio);
                    }
                }
                               
            });

            ViewChannelCommand = new AsyncCommand<SelectableMediaItem>(async (selectableItem) =>
            {
                if (selectableItem.Item.Metadata == null) return;

                YoutubeItem item = selectableItem.Item as YoutubeItem;

                SearchResource.ListRequest searchListRequest = Youtube.Search.List("snippet");
                searchListRequest.ChannelId = item.ChannelId;
                searchListRequest.MaxResults = YoutubeSearchViewModel.maxResults;
                searchListRequest.Order = Google.Apis.YouTube.v3.SearchResource.ListRequest.OrderEnum.Date;

                MediaStateCollectionView.FilterModes.MoveCurrentToFirst();

                await searchAsync(searchListRequest, item.ChannelTitle, false);
                
            });

            ViewPlaylistCommand = new AsyncCommand<SelectableMediaItem>(async (selectableItem) =>
                {
                    if (selectableItem.Item.Metadata == null) return;

                    YoutubePlaylistItem item = selectableItem.Item as YoutubePlaylistItem;

                    PlaylistItemsResource.ListRequest searchListRequest = Youtube.PlaylistItems.List("snippet");
                    searchListRequest.PlaylistId = item.PlaylistId;
                    searchListRequest.MaxResults = YoutubeSearchViewModel.maxResults;

                    MediaStateCollectionView.FilterModes.MoveCurrentToFirst();

                    await searchAsync(searchListRequest, item.Name, false);
                                        
                });

            SubscribeCommand = new Command<SelectableMediaItem>((selectableItem) =>
                {
                    YoutubeChannelItem item = selectableItem.Item as YoutubeChannelItem;

                    EventAggregator.GetEvent<AddFavoriteChannelEvent>().Publish(item);
                });

            DownloadCommand = new AsyncCommand<SelectableMediaItem>(async (selectableItem) => {

                List<MediaItem> items = MediaStateCollectionView.getSelectedItems();
                if (items.Count == 0)
                {
                    items.Add(selectableItem.Item);
                }

                String outputPath = null;

                switch (YoutubePlugin.Properties.Settings.Default.VideoSaveMode)
                {
                    case MediaViewer.Infrastructure.Constants.SaveLocation.Current:
                        {
                            outputPath = MediaFileWatcher.Instance.Path;
                            break;
                        }
                    case MediaViewer.Infrastructure.Constants.SaveLocation.Ask:
                        {
                            DirectoryPickerView directoryPicker = new DirectoryPickerView();
                            directoryPicker.DirectoryPickerViewModel.InfoString = "Select Output Directory";
                            directoryPicker.DirectoryPickerViewModel.SelectedPath = MediaFileWatcher.Instance.Path;

                            if (directoryPicker.ShowDialog() == false)
                            {
                                return;
                            }

                            outputPath = directoryPicker.DirectoryPickerViewModel.SelectedPath;

                            break;
                        }
                    case MediaViewer.Infrastructure.Constants.SaveLocation.Fixed:
                        {
                            outputPath = YoutubePlugin.Properties.Settings.Default.FixedDownloadPath;
                            break;
                        }
                    default:
                        break;
                }

                CancellableOperationProgressView progressView = new CancellableOperationProgressView();
                DownloadProgressViewModel vm = new DownloadProgressViewModel();
                progressView.DataContext = vm;

                progressView.Show();
                vm.OkCommand.IsExecutable = false;
                vm.CancelCommand.IsExecutable = true;

                await Task.Factory.StartNew(() =>
                {
                    vm.startDownload(outputPath, items);
                });

                vm.OkCommand.IsExecutable = true;
                vm.CancelCommand.IsExecutable = false;
                        
            });

            LoadNextPageCommand = new AsyncCommand(async () =>
            {                        
                await searchAsync(CurrentQuery, "", true);       
            });

            SelectAllCommand = new Command(() =>
            {
                MediaStateCollectionView.selectAll();
            }, false);

            DeselectAllCommand = new Command(() =>
            {
                MediaStateCollectionView.deselectAll();
            });

            ShutdownCommand = new Command(() =>
                {
                    Properties.Settings.Default.Save();
                });

            MediaState.UIMediaCollection.IsLoadingChanged += UIMediaCollection_IsLoadingChanged;

            MediaViewer.Model.Global.Commands.GlobalCommands.ShutdownCommand.RegisterCommand(ShutdownCommand);

            setupViews();

            EventAggregator.GetEvent<SearchEvent>().Subscribe(searchEvent);

            SearchTask = null;
        }