Skip to content

Commit d84a004

Browse files
Fix Remote Doc/Image Caching in Sample App
Create Remote_DOCS compiler directive to make it easier to debug, now Debug mode only uses local packages assets, and can add compiler directive to test remote doc scenarios and caching while in Debug mode still. Fixes issue where images wouldn't be pulled from packaged assets in offline mode.
1 parent 112ce73 commit d84a004

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</PropertyGroup>
3838
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
3939
<OutputPath>bin\x86\Release\</OutputPath>
40-
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
40+
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP;REMOTE_DOCS</DefineConstants>
4141
<Optimize>true</Optimize>
4242
<NoWarn>;2008</NoWarn>
4343
<DebugType>pdbonly</DebugType>
@@ -63,7 +63,7 @@
6363
</PropertyGroup>
6464
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
6565
<OutputPath>bin\ARM\Release\</OutputPath>
66-
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
66+
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP;REMOTE_DOCS</DefineConstants>
6767
<Optimize>true</Optimize>
6868
<NoWarn>;2008</NoWarn>
6969
<DebugType>pdbonly</DebugType>
@@ -89,7 +89,7 @@
8989
</PropertyGroup>
9090
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
9191
<OutputPath>bin\x64\Release\</OutputPath>
92-
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
92+
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP;REMOTE_DOCS</DefineConstants>
9393
<Optimize>true</Optimize>
9494
<NoWarn>;2008</NoWarn>
9595
<DebugType>pdbonly</DebugType>
@@ -1484,7 +1484,7 @@
14841484
</PropertyGroup>
14851485
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM64'">
14861486
<OutputPath>bin\ARM64\Release\</OutputPath>
1487-
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
1487+
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP;REMOTE_DOCS</DefineConstants>
14881488
<Optimize>true</Optimize>
14891489
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
14901490
<NoWarn>;2008</NoWarn>

Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public string CodeUrl
115115

116116
set
117117
{
118-
#if DEBUG
118+
#if !REMOTE_DOCS
119119
_codeUrl = value;
120120
#else
121121
var regex = new Regex("^https://github.com/windows-toolkit/WindowsCommunityToolkit/(tree|blob)/(?<branch>.+?)/(?<path>.*)");
@@ -221,12 +221,13 @@ public async Task<string> GetDocumentationAsync()
221221
if (docMatch.Success)
222222
{
223223
filepath = docMatch.Groups["file"].Value;
224-
filename = Path.GetFileName(RemoteDocumentationPath);
224+
filename = Path.GetFileName(filepath);
225+
225226
RemoteDocumentationPath = Path.GetDirectoryName(filepath);
226-
LocalDocumentationFilePath = $"ms-appx:///docs/{RemoteDocumentationPath}/";
227+
LocalDocumentationFilePath = $"ms-appx:///docs/{filepath}/";
227228
}
228229

229-
#if !DEBUG // use the docs repo in release mode
230+
#if REMOTE_DOCS // use the docs repo in release mode
230231
string modifiedDocumentationUrl = $"{_docsOnlineRoot}live/docs/{filepath}";
231232

232233
// Read from Cache if available.
@@ -285,6 +286,11 @@ public async Task<string> GetDocumentationAsync()
285286
return _cachedDocumentation;
286287
}
287288

289+
public Uri GetOnlineResourcePath(string relativePath)
290+
{
291+
return new Uri($"{_docsOnlineRoot}live/docs/{RemoteDocumentationPath}/{relativePath}");
292+
}
293+
288294
/// <summary>
289295
/// Gets the image data from a Uri, with Caching.
290296
/// </summary>
@@ -301,45 +307,61 @@ async Task<Stream> CopyStream(HttpContent source)
301307
}
302308

303309
IRandomAccessStream imageStream = null;
304-
var localPath = $"{uri.Host}/{uri.LocalPath}";
310+
var localPath = $"{uri.Host}/{uri.LocalPath}".Replace("//", "/");
311+
312+
if (localPath.StartsWith(_docsOnlineRoot.Substring(8)))
313+
{
314+
// If we're looking for docs we should look in our local area first.
315+
localPath = localPath.Substring(_docsOnlineRoot.Length - 3); // want to chop "live/" but missing https:// as well.
316+
}
305317

306-
// Cache only in Release
307-
#if !DEBUG
318+
// Try cache only in Release (using remote docs)
319+
#if REMOTE_DOCS
308320
try
309321
{
310322
imageStream = await StreamHelper.GetLocalCacheFileStreamAsync(localPath, Windows.Storage.FileAccessMode.Read);
311323
}
312324
catch
313325
{
314326
}
315-
#endif
316327

317328
if (imageStream == null)
318329
{
319330
try
320331
{
332+
// Our docs don't reference any external images, this should only be for getting latest image from repo.
321333
using (var response = await client.GetAsync(uri))
322334
{
323335
if (response.IsSuccessStatusCode)
324336
{
325337
var imageCopy = await CopyStream(response.Content);
326338
imageStream = imageCopy.AsRandomAccessStream();
327339

328-
// Cache only in Release
329-
#if !DEBUG
330340
// Takes a second copy of the image stream, so that is can save the image data to cache.
331341
using (var saveStream = await CopyStream(response.Content))
332342
{
333343
await SaveImageToCache(localPath, saveStream);
334344
}
335-
#endif
336345
}
337346
}
338347
}
339348
catch
340349
{
341350
}
342351
}
352+
#endif
353+
354+
// If we don't have internet, then try to see if we have a packaged copy
355+
if (imageStream == null)
356+
{
357+
try
358+
{
359+
imageStream = await StreamHelper.GetPackagedFileStreamAsync(localPath);
360+
}
361+
catch
362+
{
363+
}
364+
}
343365

344366
return imageStream;
345367
}

Microsoft.Toolkit.Uwp.SampleApp/Pages/SampleController.xaml.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,21 @@ private async void DocumentationTextBlock_ImageResolving(object sender, ImageRes
453453
// Determine if the link is not absolute, meaning it is relative.
454454
if (!Uri.TryCreate(e.Url, UriKind.Absolute, out Uri url))
455455
{
456-
url = new Uri(CurrentSample.LocalDocumentationFilePath + e.Url);
457-
}
456+
var imageStream = await CurrentSample.GetImageStream(CurrentSample.GetOnlineResourcePath(e.Url));
458457

459-
if (url.Scheme == "ms-appx")
458+
if (imageStream != null)
459+
{
460+
image = new BitmapImage();
461+
await image.SetSourceAsync(imageStream);
462+
}
463+
}
464+
else if (url.Scheme == "ms-appx")
460465
{
461466
image = new BitmapImage(url);
462467
}
463468
else
464469
{
470+
// Cache a remote image from the internet.
465471
var imageStream = await CurrentSample.GetImageStream(url);
466472

467473
if (imageStream != null)

0 commit comments

Comments
 (0)