public async Task GetDataAsync(string path, Action <string> progress) { if (System.IO.File.Exists(path)) { return; } DownloadManager.FileDownloadTask task = null; string tempFile = null; if (Plugin.Settings.CrossSettings.Current.Contains("DataDownloadTask")) { var previousTask = DownloadManager.FileDownloadTask.FromJson(Plugin.Settings.CrossSettings.Current.GetValueOrDefault("DataDownloadTask", string.Empty)); if (previousTask.IsResumable) { task = previousTask; tempFile = task.Filename; } } if (task == null) { progress?.Invoke("Fetching offline data info..."); var portal = await Esri.ArcGISRuntime.Portal.ArcGISPortal.CreateAsync(new Uri("https://www.arcgis.com/sharing/rest")).ConfigureAwait(false); var item = await Esri.ArcGISRuntime.Portal.PortalItem.CreateAsync(portal, ItemId).ConfigureAwait(false); progress?.Invoke("Initiating download..."); tempFile = System.IO.Path.GetTempFileName(); task = await DownloadManager.FileDownloadTask.StartDownload(tempFile, item); string downloadTask = task.ToJson(); Plugin.Settings.CrossSettings.Current.AddOrUpdateValue("DataDownloadTask", task.ToJson()); } progress?.Invoke("Downloading data..."); if (progress != null) { string lastState = ""; task.Progress += (s, e) => { var state = "Downloading data " + (e.HasPercentage ? e.Percentage.ToString() + "%" : e.TotalBytes / 1024 + " kb..."); if (state != lastState) { progress?.Invoke(state); lastState = state; } }; } await task.DownloadAsync(); //AppSettings.Remove("DataDownloadTask"); //if(tempFile.EndsWith(".zip")) //{ progress?.Invoke("Unpacking data..."); // await UnpackData(tempFile, path); //If zipped //} //else //{ System.IO.File.Move(tempFile, path); //} progress?.Invoke("Complete"); }
private static async Task <FileDownloadTask> StartDownload(HttpRequestMessage request, string filename, HttpMessageHandler handler = null) { HttpClient client; if (handler == null) { handler = new HttpClientHandler(); } client = new HttpClient(handler); var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); var content = response.EnsureSuccessStatusCode(); FileDownloadTask result = new FileDownloadTask(filename, request.RequestUri, content, client); return(result); }