public static void RegisterPlatformSpecific ( Func <T> getUiContext, Action <T, Exception> showExceptionAndExit, PortableFileDescriber fileObjectDescriber, Func <PortableContextLogger, Action <int>, Task> objectLoadingTask ) { _getUiContext = getUiContext; _showExceptionAndExit = showExceptionAndExit; _fileObjectDescriber = fileObjectDescriber; _objectLoadingTask = objectLoadingTask; }
public PortableFileObject ( PortableDirectorySource directorySource, string fileName, PortableFileDescriber fileDescriber ) : base ( directorySource ) { DirectorySource = directorySource; FileExtension = Path.GetExtension(fileName).TrimStart('.'); FileName = Path.GetFileNameWithoutExtension(fileName); FilePath = Path.Combine(directorySource.DirectoryPath, fileName); FileTime = fileDescriber.GetFileCreationTime(FilePath); }
public static async Task LoadFileObjectsAsync ( PortableContextLogger contextLogger, Action <int> updateCount, string sourceFile, PortableFileEnumerator fileEnumerator, PortableFileDescriber fileDescriber, Func <PortableDirectorySource, string, PortableFileObject> createObject ) { await Task.Run ( async() => { // http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html Task <IEnumerable <KeyValuePair <string, IEnumerable <string> > > > filesByProviders = fileEnumerator.GetFilesByProvidersAsync(); Task <IReadOnlyDictionary <string, PortableSourceData> > sourceData = PortableSourceRepository.LoadSourceDataAsync(contextLogger); // http://stackoverflow.com/questions/6123406/waitall-vs-whenall await Task.WhenAll(filesByProviders, sourceData); var objectsLock = new ReaderWriterLockSlim(); Parallel.ForEach // TODO partitioning? ( filesByProviders.Result, filesByProvider => { string providerName = filesByProvider.Key; foreach (string filePath in filesByProvider.Value) { string fileDirectory = Path.GetDirectoryName(filePath); PortableDirectorySource directorySource; PortableBaseSource cachedSource; if (_cachedSources.TryGetValue(fileDirectory, out cachedSource)) { directorySource = cachedSource as PortableDirectorySource; if (directorySource == null) { throw new InvalidCastException ( string.Format("'{0}' should be '{1}' but it is of type '{2}'.", nameof(cachedSource), typeof(PortableDirectorySource).FullName, cachedSource.GetType().FullName) ); } } else { directorySource = new PortableDirectorySource(providerName, fileDirectory); PortableSourceData sourceDatum; if (sourceData.Result.TryGetValue(fileDirectory, out sourceDatum)) { directorySource.IsEnabled = sourceDatum.IsEnabled; } else { directorySource.IsEnabled = (directorySource.DirectoryPath.Contains(".thumbnail") == false) && (directorySource.DirectoryPath.Contains(".imagecache") == false); } _cachedSources.TryAdd(fileDirectory, directorySource); } AddFileObject(filePath, directorySource, objectsLock, createObject); updateCount?.Invoke(_cachedObjects.Count); } } ); } ); }