public static async Task RefreshObjectCacheAsync
        (
            PortableContextLogger contextLogger,
            Action <int> updateCount
        )
        {
            _objectLoadingTask.ThrowIfUnitialised(nameof(_objectLoadingTask),
                                                  nameof(RefreshObjectCacheAsync),
                                                  nameof(RegisterPlatformSpecific));
            await Task.Run
            (
                async() =>

            {
                Parallel.ForEach
                (
                    _cachedSources,

                    cachedSource => cachedSource.Value.ClearObjects()
                );

                _cachedObjects.Clear();

                await _objectLoadingTask(contextLogger,
                                         updateCount);

                HasUnreadSourceChanges = true;
            }
            );
        }
Esempio n. 2
0
        private static Task CreateObjectLoadingTask
        (
            PortableContextLogger contextLogger,
            Action <int> updateCount,
            string sourceFile,
            IReadOnlyDictionary <string, string> fileProviders,
            Func <PortableDirectorySource, string, PortableFileObject> createObject,
            params string[] fileExtensions
        )
        {
            PortableFileEnumerator fileEnumerator = new DefaultFileEnumerator(fileProviders,
                                                                              fileExtensions);

            PortableFileDescriber fileDescriber = new DefaultFileDescriber();

            Task loadingTask = PortableObjectRepository <Activity> .LoadFileObjectsAsync
                               (
                contextLogger,
                updateCount,
                sourceFile,
                fileEnumerator,
                fileDescriber,
                createObject
                               );

            return(loadingTask);
        }
Esempio n. 3
0
 public DefaultSourceReader
 (
     PortableContextLogger contextLogger,
     string fileName
 )
 {
     _contextLogger = contextLogger;
     _fileName      = fileName;
 }
Esempio n. 4
0
        public static PortableSourceReader CreateSourceReader
        (
            PortableContextLogger contextLogger
        )
        {
            string sourceFile = AndroidCrapApplication.GetSourceFilePath();

            PortableSourceReader sourceReader = new DefaultSourceReader(contextLogger,
                                                                        sourceFile);

            return(sourceReader);
        }
Esempio n. 5
0
        public static void RegisterPlatformSpecific
        (
            PortableContextLogger exceptionLogger,
            Action exceptionClear,
            Action <string> exceptionNotification
        )
        {
            _exceptionLogger = exceptionLogger
                               ?? throw new ArgumentNullException(nameof(exceptionLogger));

            _exceptionClear        = exceptionClear;
            _exceptionNotification = exceptionNotification;
        }
Esempio n. 6
0
        public static async Task SaveSourceDataAsync
        (
            IEnumerable <PortableBaseSource> objectSources,
            PortableContextLogger contextLogger
        )
        {
            await Task.Run
            (
                () =>

            {
                _sourceWriterFactory.ThrowIfUnitialised(nameof(_sourceWriterFactory),
                                                        nameof(SaveSourceDataAsync),
                                                        nameof(RegisterPlatformSpecific));

                IEnumerable <XElement> sourceElements = from objectSource
                                                        in objectSources
                                                        select new XElement
                                                        (
                    SourceElementName,

                    new XAttribute(SourceIdAttributeName,
                                   ((PortableCorrelatedEntity)objectSource).CorrelationTag),

                    new XAttribute(SourceEnabledAttributeName,
                                   objectSource.IsEnabled)
                                                        );

                var xmlDeclaration = new XDeclaration("1.0",
                                                      "utf-8",
                                                      "yes");
                var sourceXml = new XDocument
                                (
                    xmlDeclaration,
                    new XElement(SourcesElementName,
                                 sourceElements)
                                );

                PortableSourceWriter sourceWriter = _sourceWriterFactory(contextLogger);

                sourceWriter.WriteSourceXml(_sourceLock,
                                            sourceXml);
            }
            );
        }
Esempio n. 7
0
        public static async Task <IReadOnlyDictionary <string, PortableSourceData> > LoadSourceDataAsync
        (
            PortableContextLogger contextLogger
        )
        {
            return(await Task.Run
                   (
                       () =>

            {
                IReadOnlyDictionary <string, PortableSourceData> sourceData
                    = new Dictionary <string, PortableSourceData>();

                PortableSourceReader sourceReader = _sourceReaderFactory(contextLogger);

                XDocument sourceXml = sourceReader.ReadSourceXml(_sourceLock);

                if (sourceXml == null)
                {
                    return sourceData;
                }

                try
                {
                    XElement sourcesElement = sourceXml.Element(SourcesElementName);

                    if (sourcesElement == null)
                    {
                        throw new XmlException(SourcesElementName);
                    }

                    IEnumerable <XElement> sourceElements
                        = from sourceElement
                          in sourcesElement.Elements()
                          where (sourceElement.Name == SourceElementName)
                          select sourceElement;

                    sourceData = sourceElements.ToDictionary
                                 (
                        sourceElement =>
                    {
                        XAttribute idAttribute = sourceElement.Attribute(SourceIdAttributeName);

                        if (idAttribute == null)
                        {
                            throw new XmlException(SourceIdAttributeName);
                        }

                        string sourceKey = idAttribute.Value;

                        return sourceKey;
                    },

                        sourceElement =>
                    {
                        XAttribute enabledAttribute = sourceElement.Attribute(SourceEnabledAttributeName);

                        if (enabledAttribute == null)
                        {
                            throw new XmlException(SourceEnabledAttributeName);
                        }

                        bool isEnabled = bool.Parse(enabledAttribute.Value);

                        var sourceValue = new PortableSourceData(isEnabled);

                        return sourceValue;
                    }
                                 );
                }
                catch (Exception exception)
                {
                    contextLogger.LogError(exception);
#if (THROW)
                    throw;
#endif
                }

                return sourceData;
            }
                   ));
        }
        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);
                    }
                }
                );
            }
            );
        }
Esempio n. 9
0
        public static async Task LoadAndroidObjectsAsync
        (
            PortableContextLogger contextLogger,
            Action <int> updateCount
        )
        {
            IReadOnlyDictionary <string, string> fileProviders = AndroidCrapApplication.GetFileProviders
                                                                 (
                phoneProviderKey: "Phone",

                phoneProviderRoot: "/storage/emulated/0",  // TODO: how do I know it?

                cardProviderPrefix: "Card"
                                                                 );

            string sourceFile = AndroidCrapApplication.GetSourceFilePath();

            // https://developer.android.com/guide/topics/media/media-formats.html

            Task imageTask = CreateObjectLoadingTask
                             (
                contextLogger,
                updateCount,
                sourceFile,
                fileProviders,

                (
                    directorySource,
                    fileName
                )
                =>
            {
                return(new AndroidImageObject(directorySource,
                                              fileName));
            },

                "bmp",
                "gif",
                "jpeg",
                "jpg",
                "png",
                "tiff",
                "webp"
                             );

            Task videoTask = CreateObjectLoadingTask
                             (
                contextLogger,
                updateCount,
                sourceFile,
                fileProviders,

                (
                    directorySource,
                    fileName
                )
                =>
            {
                return(new AndroidVideoObject(directorySource,
                                              fileName));
            },

                "avi",
                "mp4",
                "mpeg",
                "webm"
                             );

            Task audioTask = CreateObjectLoadingTask
                             (
                contextLogger,
                updateCount,
                sourceFile,
                fileProviders,

                (
                    directorySource,
                    fileName
                )
                =>
            {
                return(new AndroidAudioObject(directorySource,
                                              fileName));
            },

                "aac",
                "flac",
                "imy",
                "m4a",
                "mid",
                "mkv",
                "mp3",
                // mp4 ?! and other possible overalaps between audio and video ^
                "mxmf",
                "ogg",
                "ota",
                "rtttl",
                "rtx",
                "ts",
                "wav",
                "wma",
                "xmf"
                             );

            await Task.WhenAll(imageTask,
                               videoTask,
                               audioTask);
        }