/// <summary>
        /// Downloads data from ArcGIS Portal and unzips it to the local data folder
        /// </summary>
        /// <param name="path">The path to put the data in - if the folder already exists, the data is assumed to have been downloaded and this immediately returns</param>
        /// <param name="progress">Progress reporr status callback</param>
        /// <returns></returns>
        public static async Task GetDataAsync(string path, Action <string> progress)
        {
            if (System.IO.Directory.Exists(path))
            {
                return;
            }
            DownloadManager.FileDownloadTask task = null;
            string tempFile = null;

            if (AppSettings.Contains("DataDownloadTask"))
            {
                var previousTask = DownloadManager.FileDownloadTask.FromJson(AppSettings.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 = Path.GetTempFileName();
                task     = await DownloadManager.FileDownloadTask.StartDownload(tempFile, item);

                string downloadTask = task.ToJson();
                AppSettings.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");
            progress?.Invoke("Unpacking data...");
            await UnpackData(tempFile, path);

            progress?.Invoke("Complete");
        }
Ejemplo n.º 2
0
        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);
        }