From 1381dd8d9494ac5e302d02d5687fa8620385c547 Mon Sep 17 00:00:00 2001 From: Zack Allen Date: Wed, 27 Jul 2022 11:45:13 -0700 Subject: [PATCH] Updated data manager to handle no internet connection (#1065) --- .../Managers/DataManager.cs | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/ArcGISRuntime.Samples.Shared/Managers/DataManager.cs b/src/ArcGISRuntime.Samples.Shared/Managers/DataManager.cs index 45871311a5..d22343ebc0 100644 --- a/src/ArcGISRuntime.Samples.Shared/Managers/DataManager.cs +++ b/src/ArcGISRuntime.Samples.Shared/Managers/DataManager.cs @@ -87,17 +87,29 @@ private static void CancelDownload(FileDownloadTask downloadTask) /// /// The portal item to check. /// true if data is available and up-to-date, false otherwise. - private static bool IsDataPresent(PortalItem item) + private static async Task IsDataPresent(string itemId) { // Look for __sample.config file. Return false if not present. - string configPath = Path.Combine(GetDataFolder(item.ItemId), "__sample.config"); + string configPath = Path.Combine(GetDataFolder(itemId), "__sample.config"); if (!File.Exists(configPath)) { return false; } // Get the last write date from the __sample.config file metadata. DateTime downloadDate = File.GetLastWriteTime(configPath); - // Return true if the item was downloaded after it was last modified. - return downloadDate >= item.Modified; + try + { + // Create ArcGIS portal item + var portal = await ArcGISPortal.CreateAsync().ConfigureAwait(false); + var item = await PortalItem.CreateAsync(portal, itemId).ConfigureAwait(false); + // Return true if the item was downloaded after it was last modified. + return downloadDate >= item.Modified; + } + // Catch exception when data manager cant access the internet. + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine(ex.Message); + return true; + } } public static async Task EnsureSampleDataPresent(SampleInfo info, Action onProgress = null) @@ -137,12 +149,14 @@ public static async Task EnsureSampleDataPresent(IEnumerable itemIds, Ca int id = 0; foreach (string itemId in itemIds) { - // Create ArcGIS portal item - var portal = await ArcGISPortal.CreateAsync(token).ConfigureAwait(false); - var item = await PortalItem.CreateAsync(portal, itemId, token).ConfigureAwait(false); + bool isDownloaded = await IsDataPresent(itemId); // Download item if not already present - if (!IsDataPresent(item)) + if (!isDownloaded) { + // Create ArcGIS portal item + var portal = await ArcGISPortal.CreateAsync(token).ConfigureAwait(false); + var item = await PortalItem.CreateAsync(portal, itemId, token).ConfigureAwait(false); + var index = id; Action action = (info) => combinedProgress(info, index); Task downloadTask = DownloadItem(item, token, combinedProgress is null ? null : action); @@ -161,12 +175,14 @@ public static Task DownloadDataItem(string itemId) public static async Task DownloadDataItem(string itemId, CancellationToken cancellationToken, Action onProgress = null) { - // Create ArcGIS portal item - var portal = await ArcGISPortal.CreateAsync(cancellationToken).ConfigureAwait(false); - var item = await PortalItem.CreateAsync(portal, itemId, cancellationToken).ConfigureAwait(false); + bool isDownloaded = await IsDataPresent(itemId); // Download item if not already present - if (!IsDataPresent(item)) + if (!isDownloaded) { + // Create ArcGIS portal item + var portal = await ArcGISPortal.CreateAsync(cancellationToken).ConfigureAwait(false); + var item = await PortalItem.CreateAsync(portal, itemId, cancellationToken).ConfigureAwait(false); + await DownloadItem(item, cancellationToken, onProgress); } }