Ejemplo n.º 1
0
        /// <summary>
        /// Download the file and add it as a match
        /// </summary>
        /// <param name="File"></param>
        /// <returns></returns>
        private async Task DownloadFileAsync(IDictionary <string, object> File)
        {
            await OneDrive.DownloadAsync(File);

            vmMatch _vmMatch = new vmMatch();
            await _vmMatch.Load(File["name"].ToString());

            vmMatches.StoreMatch(_vmMatch);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Synchronize the files between the OneDrive and the local files
        /// </summary>
        /// <returns></returns>
        public async Task <List <String> > SyncFiles()
        {
            List <String> Result = new List <string>(0);

            try
            {
                //Check if network is available
                bool isConnected = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
                if (!isConnected)
                {
                    return(Result);
                }


                List <IDictionary <string, object> > Files = await OneDrive.FilesAsync("TennisData");

                IReadOnlyList <IStorageFile> LocalFiles = await LocalStorage.Files();

                List <Task> Queue = new List <Task>(0);


                if (Files != null) //When Files==null no network access
                {
                    //Retrieve last synchronization
                    DateTime LastSynchronization = DateTime.MinValue;
                    if (ApplicationData.Current.LocalSettings.Values.ContainsKey("LastSynchronization"))
                    {
                        try
                        {
                            LastSynchronization = Convert.ToDateTime(ApplicationData.Current.LocalSettings.Values["LastSynchronization"]);
                        }
                        catch (Exception e)
                        {
                            LastSynchronization = DateTime.MinValue;
                        }
                    }

                    //Download files from onedrive if not present on local storage
                    bool Download = false;

                    foreach (var File in Files)
                    {
                        DateTime parsedDateTime;
                        DateTime.TryParse(File["updated_time"].ToString(), null, System.Globalization.DateTimeStyles.None, out parsedDateTime);

                        if (File["type"].ToString() == "file")
                        {
                            Download = true;

                            //Check if file does not exist in the local files
                            foreach (IStorageFile LocalFile in LocalFiles)
                            {
                                if (File["name"].ToString() == LocalFile.Name)
                                {
                                    Download = false;

                                    //If it does, check if file is newer than last synchronization, download it
                                    var      UpdatedTimeString = File["updated_time"];
                                    DateTime UpdatedTime       = Convert.ToDateTime(UpdatedTimeString);

                                    if ((DateTime)UpdatedTime >= LastSynchronization)
                                    {
                                        //await DeleteFileAsync(LocalFile.Name);
                                        Download = true;
                                    }

                                    break;
                                }
                            }

                            //Download the file to the local files (if necessary)
                            if (Download)
                            {
                                Task newDownload = DownloadFileAsync(File);

                                Queue.Add(newDownload);
                                Result.Add(File["name"].ToString());
                            }
                        }
                    }

                    //Remove files from local storage if not available on external storage
                    bool Found = false;

                    foreach (IStorageFile LocalFile in LocalFiles)
                    {
                        Found = false;

                        foreach (var File in Files)
                        {
                            if (File["name"].ToString() == LocalFile.Name)
                            {
                                Found = true;
                                break;
                            }
                        }

                        if (!Found)
                        {
                            if (LocalFile.Name.StartsWith("match_"))
                            {
                                Task newDeletion = DeleteFileAsync(LocalFile.Name);

                                Queue.Add(newDeletion);
                            }
                        }
                    }

                    //Wait until all tasks in queue are finished
                    bool bBusy = true;

                    while (bBusy)
                    {
                        bBusy = false;

                        foreach (Task _task in Queue)
                        {
                            if (!_task.IsCompleted)
                            {
                                bBusy = true;
                            }
                        }

                        if (bBusy)
                        {
                            await Task.Delay(100);
                        }
                    }

                    //Save setting of last synchronization
                    var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
                    localSettings.Values["LastSynchronization"] = DateTime.Now.ToString();

                    LocalFiles = await LocalStorage.Files();

                    foreach (IStorageFile LocalFile in LocalFiles)
                    {
                        Result.Add(LocalFile.Name);
                    }
                }
            }
            catch
            {
            }

            return(Result);
        }