Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation Image Loading + Caching W/ Relative and Absolute images #1773

Merged

Conversation

WilliamABradley
Copy link
Contributor

-GetDocumentationPathAsync now returns the Path of the documentation as well.

-DocumentationTextBlock image resolving now solves relative links, loads images, and caches them (Caching only on Release build).

-More than just .md are included into the AppX from docs.

-Fixed InitialContent.md using a white image on white background for example.

PR Type

What kind of change does this PR introduce?

  • Sample App Changes

What is the current behavior?

Current

What is the new behavior?

New

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features) (if applicable)

Does this PR introduce a breaking change?

[ ] Yes
[x] No

…as well.

-DocumentationTextBlock image resolving now solves relative links, loads images, and caches them (Caching only on Release build).

-More than just .md are included into the AppX from docs.

-Fixed InitialContent.md using a white image on white background for example.
@WilliamABradley
Copy link
Contributor Author

WilliamABradley commented Jan 26, 2018

Important question, do we clear the Cache on version update?
We don't want stale docs.

@WilliamABradley WilliamABradley added this to the v2.2 milestone Jan 26, 2018
@skendrot
Copy link
Contributor

Any reason we don't just have a webview to the docs website?

@WilliamABradley
Copy link
Contributor Author

WilliamABradley commented Jan 26, 2018

You mean instead of the MarkdownTextBlock?

A WebView isn't native, takes up memory, doesn't adapt to theming, slow to load, steals input, might have issues with touch, etc.

@skendrot
Copy link
Contributor

Yes, instead of the MarkdownTextBlock. The documentation on the docs site looks better than markdown does.

@WilliamABradley
Copy link
Contributor Author

WilliamABradley commented Jan 28, 2018

Right now it does, but with #1650 and #1755 that changes.

@nmetulev
Copy link
Contributor

It also works offline this way too. However, this seems to also package the documentation images with the app, effectively increasing the size of the package by quite a bit. I would rather the app retrieve the images remotely and cache them in run time.

@WilliamABradley
Copy link
Contributor Author

Hmm, ok. I have an idea, I will include all files and debug, and .md only in release. That way we can still view the Images in debug, if we are altering the documentation in a PR.

…nly collect the .md files.

-Pragma disabled SA1009 for Value Tuple, because following it will cause SA1015.
@WilliamABradley
Copy link
Contributor Author

@nmetulev Done.

{
try
{
using (var client = new HttpClient())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep a single static instance of HttpClient rather than creating a new one each time

private async void SaveImageToCache(string localpath, Stream imageStream)
{
// Close stream when finished.
using (imageStream)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the using outside of the method. Methods usually will not dispose of objects passed to them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is inside the method, because we want to save the file asynchronously without interrupting the UI thread, so it takes a second copy of the image stream and saves it in this void method, this way it can display the image before the file is saved to improve performance.

{
if (response.IsSuccessStatusCode)
{
var imageCopy = await CopyStream(response.Content);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a note that we cannot dispose of imageCopy because we need it for the image source

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not disposed of, that is why the stream is copied.

@@ -147,22 +147,26 @@ public bool IsSupported
}
}

public async Task<string> GetDocumentationAsync()
#pragma warning disable SA1009 // Doesn't like ValueTuples.
public async Task<(string contents, string Path)> GetDocumentationAsync()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both return variables should be cased the same. Either capitalize both, or lowercase both

… it can also share the HttpClient with the rest of the Sample.

-Fixed casing of return variable for GetDocumentationAsync.

-Use dispose instead of using for SaveImageToCache.
@WilliamABradley
Copy link
Contributor Author

@skendrot Changes have been made.

@WilliamABradley WilliamABradley removed the request for review from michael-hawker February 7, 2018 00:24
var deferral = e.GetDeferral();
BitmapImage image = null;

var absolute = Uri.TryCreate(e.Url, UriKind.Absolute, out Uri Url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lower case the Url variable

var deferral = e.GetDeferral();
BitmapImage image = null;

var absolute = Uri.TryCreate(e.Url, UriKind.Absolute, out Uri Url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If absolute is not going to be used again, just put the TryCreate in the if
if(Uri.TryCreate(e.Url, UriKind.Absolute, out Uri url) == false)

return imageStream;
}

private async void SaveImageToCache(string localpath, Stream imageStream)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async void methods should be avoided. I understand we want to get the image back to the user as fast as possible, but this only happens once. Every time after it should grab from the local file system.
Either

  1. Make this a Task returning method and await it
  2. Change CopyToAsync to CopyTo. I rarely notice a performance hit when coping from one stream to the next.

-Made Url lowercase.

-Simplified relative check.
@WilliamABradley
Copy link
Contributor Author

@skendrot Changes have been made again.

@WilliamABradley
Copy link
Contributor Author

ping @skendrot

@skendrot
Copy link
Contributor

Looks like there are conflicts now

@WilliamABradley
Copy link
Contributor Author

Fixed conflicts.

@WilliamABradley WilliamABradley merged commit 19fb708 into CommunityToolkit:master Feb 12, 2018
@WilliamABradley WilliamABradley deleted the DocumentationImages branch February 12, 2018 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants