Exemplo n.º 1
0
        UICollectionViewCell DequeueIceCreamCell(IceCream iceCream, NSIndexPath indexPath)
        {
            var cell = CollectionView.DequeueReusableCell(IceCreamCell.ReuseIdentifier, indexPath) as IceCreamCell;

            if (cell == null)
            {
                throw new Exception("Unable to dequeue am IceCreamCell");
            }

            cell.RepresentedIceCream = iceCream;

            // Use a placeholder sticker while we fetch the real one from the cache.
            var cache = IceCreamStickerCache.Cache;

            cell.StickerView.Sticker = cache.PlaceholderSticker;

            // Fetch the sticker for the ice cream from the cache.
            cache.GetSticker(iceCream, (sticker) => {
                // If the cell is still showing the same ice cream, update its sticker view.
                if (cell.RepresentedIceCream == iceCream)
                {
                    cell.StickerView.Sticker = sticker;
                }
            });

            return(cell);
        }
        public UIViewController InstantiateCompletedIceCreamController(IceCream iceCream)
        {
            // Instantiate a `BuildIceCreamViewController` and present it.
            var controller = Storyboard.InstantiateViewController(CompletedIceCreamViewController.StoryboardIdentifier) as CompletedIceCreamViewController;

            if (controller == null)
            {
                throw new Exception("Unable to instantiate an CompletedIceCreamViewController from the storyboard");
            }

            controller.IceCream = iceCream;
            return(controller);
        }
        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);
        }
Exemplo n.º 4
0
        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);
        }