Bernie Habermeier
Bernie Habermeier

Reputation: 2598

Getting the filename/path from MvvmCross Plugins.DownloadCache

I'm currently using MvvmCross DownloadCache -- and it's working alright -- especially nice when I just need to drop in an Image URL and it automagically downloads / caches the image and serves up a UIImage.

I was hoping to leverage the code for one other use case -- which is I'd like to grab source images from URL's and cache the files on the local file system, but what I really want for this other use case is the image path on the local file system instead of the UIImage itself.

What would help me most if I could get an example of how I might accomplish that. Is it possible to make that happen in a PCL, or does it need to go into the platform specific code?

Upvotes: 1

Views: 579

Answers (2)

Bernie Habermeier
Bernie Habermeier

Reputation: 2598

Thanks -- that works, but just in case anyone else is following along, I wanted to document how I got the Mvx.Resolve<IMvxFileDownloadCache>() to work. In my setup.cs (in the touch project), I had:

protected override void InitializeLastChance ()
{
    Cirrious.MvvmCross.Plugins.DownloadCache.PluginLoader.Instance.EnsureLoaded();
    Cirrious.MvvmCross.Plugins.File.PluginLoader.Instance.EnsureLoaded();
    Cirrious.MvvmCross.Plugins.Json.PluginLoader.Instance.EnsureLoaded();
    ...
}

But that wasn't enough, because nothing actually registers IMvxFileDownloadCache inside the DownloadCache plugin (which I was expecting, but it's just not the case).

So then I tried adding this line here:

Mvx.LazyConstructAndRegisterSingleton<IMvxFileDownloadCache, MvxFileDownloadCache>();

But that failed because MvxFileDownloadCache constructor takes a few arguments. So I ended up with this:

protected override void InitializeLastChance ()
{

    ...

    var configuration = MvxDownloadCacheConfiguration.Default;

    var fileDownloadCache = new MvxFileDownloadCache(
        configuration.CacheName,
        configuration.CacheFolderPath,
        configuration.MaxFiles,
        configuration.MaxFileAge);

    Mvx.RegisterSingleton<IMvxFileDownloadCache>(fileDownloadCache);

    ...
}

And the resolve works okay now.

Question: I do wonder what happens if two MvxFileDownloadCache objects that are configured in exactly the same way will cause issues by stepping on each other. I could avoid that question by changing the cache name on the one I'm constructing by hand, but I do want it to be a single cache (the assets will be the same).

Upvotes: 2

Stuart
Stuart

Reputation: 66882

If you look at the source for the plugin, you'll find https://github.com/MvvmCross/MvvmCross/blob/3.2/Plugins/Cirrious/DownloadCache/Cirrious.MvvmCross.Plugins.DownloadCache/IMvxFileDownloadCache.cs - that will give you a local file path for a cached file:

public interface IMvxFileDownloadCache 
{ 
     void RequestLocalFilePath(string httpSource, Action<string> success, Action<Exception> error); 
} 

You can get hold of a service implementing this interface using Mvx.Resolve<IMvxFileDownloadCache>()

To then convert that into a system-wide file path, try NativePath in https://github.com/MvvmCross/MvvmCross/blob/3.2/Plugins/Cirrious/File/Cirrious.MvvmCross.Plugins.File/IMvxFileStore.cs#L27

Upvotes: 1

Related Questions