MSMessage ComposeMessage(IceCream iceCream, string caption, MSSession session = null) { var components = new NSUrlComponents { QueryItems = iceCream.QueryItems }; var layout = new MSMessageTemplateLayout { Image = iceCream.RenderSticker(true), Caption = caption }; var message = new MSMessage(session ?? new MSSession()) { Url = components.Url, Layout = layout }; return(message); }
public void GetSticker(IceCream iceCream, Action <MSSticker> completion) { if (iceCream.Base == null || iceCream.Scoops == null || iceCream.Topping == null) { throw new Exception("Stickers can only be created for completed ice creams"); } // Determine the URL for the sticker. var fileName = $"{iceCream.Base.RawValue}{iceCream.Scoops.RawValue}{iceCream.Topping.RawValue}.png"; var url = Path.Combine(cacheURL, fileName); // Check if the sticker already exists at the URL if (!NSFileManager.DefaultManager.FileExists(url)) { // Create the sticker image and write it to disk. var image = iceCream.RenderSticker(false); var imageData = image?.AsPNG(); if (image == null || imageData == null) { throw new Exception("Unable to build image for ice cream"); } try { File.WriteAllBytes(url, imageData.ToArray()); } catch { throw new Exception("Failed to write sticker image to cach"); } } NSError error; var sticker = new MSSticker(new NSUrl($"file://{url}"), "Ice Cream", out error); if (error != null) { throw new Exception($"Failed to write image to cache, error: {error.LocalizedDescription}"); } completion?.Invoke(sticker); }