/// <summary> /// Navigates to channel listings. /// </summary> /// <param name="providerName">Name of the provider.</param> public void NavigateToChannelListings(ITVProvider provider) { // prepare list of menuItems for the new menu we're going to navigate to. List <MenuItemModel> menuItems = new List <MenuItemModel>(); // Show the "Scanning for Matches" loading modal ModalModel.GetInstance.ModalUserControl = new ScanningForMatchesModal(); _scanningForMatchesModal = ModalModel.GetInstance.ModalUserControl as ScanningForMatchesModal; _scanningForMatchesModal.progressBar1.Min = 0; _scanningForMatchesModal.progressBar1.Max = provider.Channels.Count; _scanningForMatchesModal.progressBar1.Value = 0; Task.Run(() => { // Create new list? if (this.TVProviderCacheEntities == null) { this.TVProviderCacheEntities = new List <TVProviderCacheEntity>(); } // Do we have cached result we can use? TVProviderCacheEntity providerCache = this.TVProviderCacheEntities.Where(m => m.providerName == provider.ProviderName).FirstOrDefault(); // Check if we have a cache and that the cache is not old (earlier than the TVSchedule object) if (providerCache == null || providerCache.LastScanTime < this.Schedule.ScheduleDate) { // Create a new cache for the provider providerCache = new TVProviderCacheEntity(); providerCache.providerName = provider.ProviderName; providerCache.MenuItemModels = ScanForMatches(provider); providerCache.LastScanTime = DateTime.Now; // store provider cache in the cache this.TVProviderCacheEntities.Add(providerCache); menuItems = providerCache.MenuItemModels; } else { // Cache is good so just navigate to the menuItemModels menuItems = providerCache.MenuItemModels; } this.Dispatcher.Invoke(() => { // navigate to the new menuEntity PageModel.GetInstance.NavigateForwards(menuItems); // remove the "Scanning for Matches" modal ModalModel.GetInstance.ModalUserControl = null; // update the bread crumbs label PageModel.GetInstance.DoBreadCrumbs(this._componentName); }); }); }
/// <summary> /// Scans for matches for a provider. /// </summary> /// <param name="provider">The provider.</param> /// <returns></returns> private List <MenuItemModel> ScanForMatches(ITVProvider provider) { // Cap the channel name lengths Parallel.ForEach <IChannel>(provider.Channels, channel => { // Cap the name length? if (channel.Name.Length > 19) { channel.Name = channel.Name.Substring(0, 18) + ".."; } }); // Get longest channel name for the padright int longestChannelName = provider.Channels .Select(m => m.Name) .OrderByDescending(m => m.Length).First().Length; int longestChannelNumber = provider.Channels .Select(m => m.ChannelNumber) .OrderByDescending(m => m.Length).First().Length; // prepare results List <MenuItemModel> result = new List <MenuItemModel>(); Parallel.ForEach <IChannel>(provider.Channels, channel => { Parallel.ForEach <IProgramme>(channel.Programmes, programme => { // Attempt to get files that match the TV show name. List <MenuItemModel> matches = GetMatchesForProgramme(programme); if (matches.Count > 0) { lock (obj) { result.Add(new MenuItemModel { DisplayText = channel.ChannelNumber.PadRight(longestChannelNumber + 2) + channel.Name.PadRight(longestChannelName + 2) + Convert.ToDateTime(programme.ShowingTime).ToShortTimeString().PadRight(8) + programme.ProgramName, Tag = channel.ChannelNumber, IsSelected = false, FilePath = null, ParentSelected = true, IsVisible = true, RelayCommand = new RelayCommand(() => { // navigate to the list of files PageModel.GetInstance.NavigateForwards(matches); // update the bread crumbs label PageModel.GetInstance.DoBreadCrumbs(this._componentName); }) }); } } }); this.Dispatcher.Invoke(() => { _scanningForMatchesModal.progressBar1.Value++; }); }); // Show 100% progress for a 10th of a second AutoResetEvent e = new AutoResetEvent(false); this.Dispatcher.Invoke(() => { _scanningForMatchesModal.progressBar1.Value = _scanningForMatchesModal.progressBar1.Max; e.Set(); }); e.WaitOne(); Thread.Sleep(100); // No matches found so add a blank button stating "No Matches found" if (result.Count == 0) { result.Add(new MenuItemModel() { DisplayText = "No matches found..", IsSelected = true, IsVisible = true, ParentSelected = true, RelayCommand = new RelayCommand(() => { }) }); return(result); } else { // return the results sorted by the channel number return(result.OrderBy(m => Convert.ToInt32(m.Tag)).ToList()); } }