Last active
December 21, 2015 02:39
-
-
Save chkn/6236833 to your computer and use it in GitHub Desktop.
Xamarin async gems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class LocalCache { | |
static HashSet<string> tempKeys = new HashSet<string> (); | |
public static Task<Stream> GetCachedOrDownload (string key, Uri uri, bool temporary = true) | |
{ | |
return GetCachedOrDownload (key, uri, CreateNativeHttpClient, temporary); | |
} | |
public static Task<Stream> GetCachedOrDownload (string key, Uri uri, Func<HttpClient> createHttpClient, bool temporary = true) | |
{ | |
return GetCachedOrCreate ( | |
key: key, | |
temporary: temporary, | |
create: async () => { | |
using (var client = createHttpClient ()) | |
return await client.GetStreamAsync (uri); | |
}); | |
} | |
public static async Task<Stream> GetCachedOrCreate (string key, Func<Task<Stream>> create, bool temporary = true) | |
{ | |
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication ()) { | |
await AddToCacheIfMissing (isoStore, key, create, temporary); | |
return isoStore.OpenFile (key, FileMode.Open, FileAccess.Read, FileShare.Read); | |
} | |
} | |
public static async Task AddToCacheIfMissing (string key, Func<Task<Stream>> create, bool temporary = true) | |
{ | |
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication ()) | |
await AddToCacheIfMissing (isoStore, key, create, temporary); | |
} | |
static async Task AddToCacheIfMissing (IsolatedStorageFile isoStore, string key, Func<Task<Stream>> create, bool temporary = true) | |
{ | |
if (!isoStore.FileExists (key)) { | |
using (var outStream = isoStore.CreateFile (key)) { | |
var inStream = new BufferedStream (await create ()); | |
await inStream.CopyToAsync (outStream); | |
} | |
if (temporary) | |
tempKeys.Add (key); | |
} | |
} | |
public static void CleanUpTemporaryItems () | |
{ | |
foreach (var key in tempKeys) | |
Invalidate (key); | |
} | |
public static void Invalidate (string key) | |
{ | |
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication ()) { | |
if (isoStore.FileExists (key)) | |
isoStore.DeleteFile (key); | |
} | |
} | |
static HttpClient CreateNativeHttpClient () | |
{ | |
#if __IOS__ | |
return new HttpClient (new CFNetworkHandler ()); | |
#else | |
return new HttpClient (); | |
#endif | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Displays a UIAlertView and returns the index of the button pressed. | |
public static Task<int> ShowAlert (string title, string message, params string [] buttons) | |
{ | |
var tcs = new TaskCompletionSource<int> (); | |
var alert = new UIAlertView { | |
Title = title, | |
Message = message | |
}; | |
foreach (var button in buttons) | |
alert.AddButton (button); | |
alert.Clicked += (s, e) => tcs.TrySetResult (e.ButtonIndex); | |
alert.Show (); | |
return tcs.Task; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment