async Task <bool> DeleteInvoice()
 {
     try{
         BigTed.BTProgressHUD.ShowContinuousProgress();
         bool success = false;
         if (Invoice.RecordId > 0)
         {
             success = await WebService.Main.DeleteWorkingInvoice(Invoice);
         }
         Invoice.DeleteLocal();
         success = true;
         return(success);
     }
     catch (Exception ex) {
         new SimpleAlertView("Error deleting invoice", "").Show();
     }
     finally{
         BigTed.BTProgressHUD.Dismiss();
     }
     return(false);
 }
 void NewInvoice()
 {
     Invoice.DeleteLocal();
     Invoice = new Invoice();
 }
        public InvoiceViewController()
        {
            SetTitle();
            Settings.Shared.SubscribeToProperty("TestMode", SetTitle);
            var searchBar = new ItemSearchView {
                Frame = new System.Drawing.RectangleF(0, 0, 200, 30)
            };

            searchBar.ItemSelected += (Item obj) => {
                Invoice.AddItem(obj);
            };
            NavigationItem.RightBarButtonItem = new UIBarButtonItem(searchBar);
            NavigationItem.LeftBarButtonItem  = new UIBarButtonItem(UIImage.FromBundle("menu").ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), UIBarButtonItemStyle.Plain, (s, e) => {
                //Show simple actionsheet
                if (sheet != null)
                {
                    sheet.DismissWithClickedButtonIndex(-1, true);
                    return;
                }
                sheet = new SimpleActionSheet {
                    { "New Invoice", async() => {
                          if (await AskSave())
                          {
                              NewInvoice();
                          }
                      } },
                    { "Load Invoice", () => {
                          if (popover != null)
                          {
                              popover.Dismiss(true);
                          }
                          popover = new UIPopoverController(new UINavigationController(new LoadInvoiceViewController()
                            {
                                InvoiceSelected = async(i) => {
                                    popover.Dismiss(true);
                                    try{
                                        BigTed.BTProgressHUD.ShowContinuousProgress();
                                        if (Invoice != null && Invoice.Id != i.Id)
                                        {
                                            if (!await AskSave())
                                            {
                                                return;
                                            }
                                        }
                                        Invoice.DeleteLocal();
                                        Invoice = await WebService.Main.GetInvoice(i.Id);
                                        Invoice.Save(true);
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }
                                    finally{
                                        BigTed.BTProgressHUD.Dismiss();
                                    }
                                },
                            }));
                          popover.DidDismiss += (sender, evt) => {
                              popover.Dispose();
                          };
                          popover.PresentFromBarButtonItem(NavigationItem.LeftBarButtonItem, UIPopoverArrowDirection.Any, true);
                      } },
                    { "Payout Buy", () => {
                          if (popover != null)
                          {
                              popover.Dismiss(true);
                          }
                          popover = new UIPopoverController(new UINavigationController(new LoadBuyPayoutViewController()
                            {
                                InvoiceSelected = async(i) => {
                                    popover.Dismiss(true);
                                    try{
                                        BigTed.BTProgressHUD.ShowContinuousProgress();
                                        if (Invoice != null && Invoice.Id != i.Id)
                                        {
                                            if (!await AskSave())
                                            {
                                                return;
                                            }
                                        }
                                        Invoice.DeleteLocal();
                                        Invoice = await WebService.Main.GetInvoice(i.Id);
                                        //Setup payments
                                        Database.Main.Table <PaymentType> ().Where(x => x.IsActive)
                                        .OrderBy(X => X.SortOrder).ToList().ForEach(x => Invoice.Payments.Add(new Payment {
                                            PaymentType = x
                                        }));
                                        Invoice.Save(true);
                                        if ((i as BuyInvoice).IsOnAccount)
                                        {
                                            Invoice.OnAccountPayment.Amount = Invoice.Total;
                                        }
                                        else
                                        {
                                            Invoice.CashPayment.Amount = Invoice.Total;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }
                                    finally{
                                        BigTed.BTProgressHUD.Dismiss();
                                    }
                                },
                            }));
                          popover.DidDismiss += (sender, evt) => {
                              popover.Dispose();
                          };
                          popover.PresentFromBarButtonItem(NavigationItem.LeftBarButtonItem, UIPopoverArrowDirection.Any, true);
                      } },
                    { "Settings", () => this.PresentViewControllerAsync(new UINavigationController(new SettingsViewController()), true) },
                };
                sheet.Dismissed += (object sender, UIButtonEventArgs e2) => {
                    sheet.Dispose();
                    sheet = null;
                };
                sheet.ShowFrom(s as UIBarButtonItem, true);
            });

            //this.AutomaticallyAdjustsScrollViewInsets = false;
        }