public override void HandleOrderSoup(OrderSoupIntent intent, Action <OrderSoupIntentResponse> completion) { var soup = intent.Soup; if (soup is null) { completion(new OrderSoupIntentResponse(OrderSoupIntentResponseCode.Failure, null)); return; } var order = Order.FromOrderSoupIntent(intent); if (order is null) { completion(new OrderSoupIntentResponse(OrderSoupIntentResponseCode.Failure, null)); return; } // The handle method is also an appropriate place to handle payment // via Apple Pay. A declined payment is another example of a // failure case that could take advantage of a custom response. // Place the soup order via the order manager. var orderManager = new SoupOrderDataManager(); orderManager.PlaceOrder(order); // For the success case, we want to indicate a wait time to the // user so that they know when their soup order will be ready. // This sample uses a hardcoded value, but your implementation could // use a time interval returned by your server. completion(OrderSoupIntentResponse.SuccessIntentResponseWithSoup(soup, 10)); }
public static Order FromOrderSoupIntent(OrderSoupIntent intent) { var menuManager = new SoupMenuManager(); var soupID = intent.Soup?.Identifier; if (soupID is null) { return(null); } var menuItem = menuManager.FindItem(soupID); if (menuItem is null) { return(null); } var quantity = intent.Quantity; if (menuItem is null) { return(null); } MenuItemOption[] rawOptions; if (intent.Options is null) { rawOptions = new MenuItemOption[0]; } else { // For the equivalent code in Apple's Swift sample, compactMap // is used. This eliminates nil values from the final result. // Here, LINQ's Where method is used to filter out the null // values. rawOptions = intent.Options.Select <INObject, MenuItemOption>((option) => { var optionID = option.Identifier; return((optionID is null) ? null : new MenuItemOption(optionID)); }).Where((option) => !(option is null)).ToArray <MenuItemOption>(); } var order = new Order(quantity.Int32Value, menuItem, new NSMutableSet <MenuItemOption>(rawOptions)); return(order); }
override public void ConfirmOrderSoup(OrderSoupIntent intent, Action <OrderSoupIntentResponse> completion) { // The confirm phase provides an opportunity for you to perform any // final validation of the intent parameters and to verify that any // needed services are available. You might confirm that you can // communicate with your company’s server. var soupMenuManager = new SoupMenuManager(); var soup = intent.Soup; if (soup is null) { completion(new OrderSoupIntentResponse(OrderSoupIntentResponseCode.Failure, null)); return; } var identifier = soup.Identifier; if (identifier is null) { completion(new OrderSoupIntentResponse(OrderSoupIntentResponseCode.Failure, null)); return; } var menuItem = soupMenuManager.FindItem(identifier); if (menuItem is null) { completion(new OrderSoupIntentResponse(OrderSoupIntentResponseCode.Failure, null)); return; } if (!menuItem.IsAvailable) { // Here's an example of how to use a custom response for a // failure case when a particular soup item is unavailable. completion(OrderSoupIntentResponse.FailureSoupUnavailableIntentResponseWithSoup(soup)); return; } // Once the intent is validated, indicate that the intent is ready // to handle. completion(new OrderSoupIntentResponse(OrderSoupIntentResponseCode.Ready, null)); }
CGSize DisplayOrderConfirmation(Order order, OrderSoupIntent intent, OrderSoupIntentResponse response) { confirmationView.ItemNameLabel.Text = order.MenuItem.LocalizedString; confirmationView.TotalPriceLabel.Text = order.LocalizedCurrencyValue; confirmationView.ImageView.Layer.CornerRadius = 8; var waitTime = response.WaitTime; if (!(waitTime is null)) { confirmationView.TimeLabel.Text = $"{waitTime} Minutes"; } var intentImage = intent.GetImage("soup"); if (!(intentImage is null)) { var weakThis = new WeakReference <IntentViewController>(this); intentImage.FetchImage((image) => { DispatchQueue.MainQueue.DispatchAsync(() => { if (weakThis.TryGetTarget(out var intentViewController)) { // NOTE: In the original Swift-based sample, // the original code sets this image on the // invoice view, which seems incorrect. intentViewController.confirmationView.ImageView.Image = image; } }); }); } View.AddSubview(confirmationView); var width = this.ExtensionContext?.GetHostedViewMaximumAllowedSize().Width ?? 320; var frame = new CGRect(0, 0, width, 170); confirmationView.Frame = frame; return(frame.Size); }
CGSize DisplayInvoice(Order order, OrderSoupIntent intent) { invoiceView.ItemNameLabel.Text = order.MenuItem.LocalizedString; invoiceView.TotalPriceLabel.Text = order.LocalizedCurrencyValue; invoiceView.UnitPriceLabel.Text = $"{order.Quantity} @ {order.MenuItem.LocalizedCurrencyValue}"; var intentImage = intent.GetImage("soup"); if (!(intentImage is null)) { var weakThis = new WeakReference <IntentViewController>(this); intentImage.FetchImage((image) => { DispatchQueue.MainQueue.DispatchAsync(() => { if (weakThis.TryGetTarget(out var intentViewController)) { intentViewController.invoiceView.ImageView.Image = image; } }); }); } var optionText = (!(intent.Options is null)) ? order.LocalizedOptionString : ""; invoiceView.OptionsLabel.Text = optionText; View.AddSubview(invoiceView); var width = this.ExtensionContext?.GetHostedViewMaximumAllowedSize().Width ?? 320; var frame = new CGRect(0, 0, width, 170); invoiceView.Frame = frame; return(frame.Size); }