Beispiel #1
0
        private async Task LoadJson()
        {
            if (Connectivity.NetworkAccess != NetworkAccess.Internet)
            {
                await Application.Current.MainPage.DisplayAlert("Benötige Internetverbindung", "Zum Laden der Dateien wird eine Internetverbindung benötigt.", "Okay");
            }
            else
            {
                try
                {
                    DataStatus = "Lade Artinformationen …";
                    //load version file from internet
                    //json / bodentier_app_json
                    var versions = await _fileHelper.DownloadFileAsync(ServiceUrl + VersionsFile);

                    //get last time updated
                    var     versionJsonString = System.Text.Encoding.UTF8.GetString(versions, 0, versions.Length);
                    JObject versionJson       = JObject.Parse(versionJsonString);
                    var     versionDate       = versionJson["Versions.json"].Value <string>();
                    if (_fileHelper.FileExists(VersionsFile))
                    {
                        //if internet files not already loaded (== _oldVersionDate not set) and more up to date
                        if (!String.IsNullOrEmpty(_oldVersionDate) && versionDate != _oldVersionDate)
                        {
                            //update all files out of date
                            foreach (var file in versionJson)
                            {
                                var fileName = file.Key;
                                if (versionJson[fileName].Value <string>() != _oldVersionJson[fileName].Value <string>())
                                {
                                    var jsonFile = await _fileHelper.DownloadFileAsync(ServiceUrl + fileName);

                                    _fileHelper.CopyFileToLocal(jsonFile, fileName);
                                }
                            }
                        }
                    }
                    //load all files for the first time
                    else
                    {
                        foreach (var firstFile in versionJson)
                        {
                            var firstFileName = firstFile.Key;
                            var firstJsonFile = await _fileHelper.DownloadFileAsync(ServiceUrl + firstFileName);

                            _fileHelper.CopyFileToLocal(firstJsonFile, firstFileName);
                        }
                    }

                    //reload the Data in App
                    ((App)App.Current).LoadData();
                }
                catch (Exception e)
                {
                    var msg = e.InnerException;
                    Result = "Beim Laden der Artinformationen ist ein Problem aufgetreten.";
                }
            }
        }