/// <summary>
        /// Sets up the execution logic for all the DelegateCommands in the ViewModel
        /// </summary>
        private void SetupCommands()
        {
            Dismiss userSearchDismiss = null;
            Dismiss viewInvitesDismiss = null;
            Dismiss showNewListDismiss = null;
            Dismiss showChooseListDismiss = null;

            RemoveItemsCommand = new DelegateCommand(async () =>
            {
                foreach (Item item in _selectedItems)
                {
                    try
                    {
                        await _itemsTable.DeleteAsync(item);
                    }
                    catch (MobileServiceInvalidOperationException exc)
                    {
                        // a 404 is an expected scenario here, another user 
                        // has most likely deleted the item whilst we've been
                        // viewing. 
                        if (exc.Response.StatusCode != 404)
                        {
                            throw;
                        }
                    }
                    Items.Remove(item);
                }
            }, false);

            ShowUserSearchCommand = new DelegateCommand(() =>
            {
                var inviteUserViewModel = new InviteUserViewModel(this, _profilesTable, () => userSearchDismiss.DismissFlyout());
                userSearchDismiss = _popupService.ShowInviteUserFlyout(inviteUserViewModel);
            }, false);

            ViewInvitesCommand = new DelegateCommand(() =>
            {
                var viewInvitesViewModel = new ViewInvitesViewModel(this, _invitesTable, () => viewInvitesDismiss.DismissFlyout());
                viewInvitesDismiss = _popupService.ShowViewInvitesFlyout(viewInvitesViewModel);
            }, false);

            ShowNewListCommand = new DelegateCommand(() =>
            {
                showNewListDismiss = _popupService.ShowNewListFlyout();
            }, false);

            ShowChooseListCommand = new DelegateCommand(() =>
            {
                showChooseListDismiss = _popupService.ShowChooseListFlyout();
            });

            ShowAddItemCommand = new DelegateCommand(() =>
            {
                _popupService.ShowAddItemFlyout();
            }, false);

            RefreshCommand = new DelegateCommand(() =>
            {
                LoadSettings();
                LoadInvites();
                if (CurrentList != null)
                {
                    LoadLists();
                    LoadItems();
                }
            }, false);

            LeaveListCommand = new DelegateCommand(() =>
            {
                _popupService.ShowDialogAsync(
                    "Leave list?",
                    "Are you sure you want to leave this list? If you're the only member, all data will be lost forever.",
                    new SimpleCommand("OK", async () =>
                    {
                        await _listMembersTable.DeleteAsync(CurrentList);
                        Lists.Remove(CurrentList);
                        ShowList(0);
                    }),
                    new SimpleCommand("Cancel", () => { }));
            }, false);

            RegisterCommand = new DelegateCommand(async () =>
            {
                if (string.IsNullOrWhiteSpace(User.Name) ||
                    string.IsNullOrWhiteSpace(User.City) ||
                    string.IsNullOrWhiteSpace(User.State))
                {
                    await _popupService.ShowDialogAsync("All fields required", "A valid name, city and state/county must be provided");
                    return;
                }
                await _profilesTable.InsertAsync(User);
                DisplayRegistrationForm = false;
                RegisterDevice();
                LoadLists(true);
                LoadInvites();
            }, true);

            AddItemCommand = new DelegateCommand(async () =>
            {
                if (String.IsNullOrWhiteSpace(NewItemText))
                {
                    await _popupService.ShowDialogAsync("Description is required", "You must enter a valid description for your item");
                    return;
                }
                var item = new Item
                {
                    Text = NewItemText,
                    ListId = CurrentList.ListId,
                    CreatedBy = User.Name,
                };
                NewItemText = String.Empty;
                await _itemsTable.InsertAsync(item);
                Items.Add(item);
            });

            SelectListCommand = new DelegateCommand<ListMembership>(lm =>
            {
                ShowList(Lists.IndexOf(lm));
                showChooseListDismiss.DismissFlyout();
            });

            NewListCommand = new DelegateCommand(async () =>
            {
                if (String.IsNullOrWhiteSpace(NewListName))
                {
                    await _popupService.ShowDialogAsync("Name is required", "You must enter a valid name for your list");
                    return;
                }
                ListMembership membership = new ListMembership
                {
                    Name = NewListName,
                    UserId = _mobileServiceClient.CurrentUser.UserId,
                };
                NewListName = String.Empty;
                await _listMembersTable.InsertAsync(membership);
                Lists.Add(membership);
                ShowList(Lists.IndexOf(membership));
                showNewListDismiss.DismissFlyout();
            });
        }
 public void ShowInviteUserFlyout(InviteUserViewModel viewModel)
 {
     var control = new InviteUser();
     control.DataContext = viewModel;
     ShowDotoFlyout(buttonInviteUser, FlyoutPlacementMode.Top, String.Empty, control);
 }
        /// <summary>
        /// Sets up the execution logic for all the DelegateCommands in the ViewModel
        /// </summary>
        private void SetupCommands()
        {
            // Register user when they login the very first time. 
            RegisterCommand = new DelegateCommand(async () =>
            {
                // Show validation fail flyout
                if (string.IsNullOrWhiteSpace(User.Name) ||
                    string.IsNullOrWhiteSpace(User.City) ||
                    string.IsNullOrWhiteSpace(User.State))
                {
                    flyoutProvider.ShowRegisterValidationFlyout();
                    return;
                }
                await profilesTable.InsertAsync(User);
                DisplayRegistrationForm = false;
                RegisterDeviceAsync();
                LoadListsAsync(true);
                LoadInvitesAsync();
            }, true);

            // Shows the flyout for user to create a new list the first time 
            ShowNewUserNewListCommand = new DelegateCommand(() =>
            {
                flyoutProvider.ShowNewUserNewListFlyout();
            }, false);

            // Shows the flyout for user to enter the new list name
            ShowNewListCommand = new DelegateCommand(() =>
            {
                flyoutProvider.ShowNewListFlyout();
            }, false);

            // Creates a new list in the backend with the user as the owner
            NewListCommand = new DelegateCommand(async () =>
            {
                if (String.IsNullOrWhiteSpace(NewListName))
                {
                    flyoutProvider.ShowNameRequiredFlyout();
                    return;
                }
                ListMember membership = new ListMember
                {
                    Name = NewListName,
                    UserId = App.MobileService.CurrentUser.UserId,
                };
                NewListName = String.Empty;
                await listMembersTable.InsertAsync(membership);
                Lists.Add(membership);
                ShowList(Lists.IndexOf(membership));
                flyoutProvider.HideNewListFlyout();
            });

            // Shows the flyout with all the lists having user as the owner
            ShowChooseListCommand = new DelegateCommand(() =>
            {
                flyoutProvider.ShowChooseListFlyout();
            });

            // Opens the list as selected by the user
            SelectListCommand = new DelegateCommand<ListMember>(listMember =>
            {
                ShowList(Lists.IndexOf(listMember));
            });

            // Asks the user for confirmation before leaving list
            ConfirmLeaveListCommand = new DelegateCommand(() =>
            {
                flyoutProvider.ShowLeaveListConfirmationFlyout();
            });

            // Removes the user as the owner of the list
            LeaveListCommand = new DelegateCommand(async () =>
            {
                await listMembersTable.DeleteAsync(CurrentList);
                Lists.Remove(CurrentList);
                ShowList(0);
                flyoutProvider.HideLeaveListConfirmationFlyout();
            });

            // Shows the floyout where the user can add the name of an item in a list 
            ShowAddItemCommand = new DelegateCommand(() =>
            {
                flyoutProvider.ShowAddItemFlyout();
            }, false);

            // Adds an item in the list in the backend 
            AddItemCommand = new DelegateCommand(async () =>
            {
                if (String.IsNullOrWhiteSpace(NewItemText))
                {
                    flyoutProvider.ShowDescriptionRequiredFlyout();
                    return;
                }
                var item = new Item
                {
                    Text = NewItemText,
                    ListId = CurrentList.ListId,
                    CreatedBy = User.Name,
                };
                NewItemText = String.Empty;
                await itemsTable.InsertAsync(item);
                Items.Add(item);
            });

            // Removes an item from a list when Remove Items button is pressed
            RemoveItemsCommand = new DelegateCommand(async () =>
            {
                foreach (Item item in selectedItems)
                {
                    try
                    {
                        await itemsTable.DeleteAsync(item);
                    }
                    catch (MobileServiceInvalidOperationException exc)
                    {
                        // a 404 is an expected scenario here, another user 
                        // has most likely deleted the item whilst we've been
                        // viewing. 
                        if (exc.Response.StatusCode != HttpStatusCode.NotFound)
                        {
                            ShowError("Another user deleted the item already.");
                        }
                    }
                    Items.Remove(item);
                }
            }, false);

            // Refreshes a list when Refresh Items button is pressed
            // this will populate any items added by any other list owner elsewhere
            RefreshCommand = new DelegateCommand(() =>
            {
                LoadSettingsAsync();
                LoadInvitesAsync();
                if (CurrentList != null)
                {
                    LoadListsAsync();
                    LoadItemsAsync();
                }
            }, false);

            // Searches for a user in the backend
            ShowUserSearchCommand = new DelegateCommand(() =>
            {
                var inviteUserViewModel = new InviteUserViewModel(this, profilesTable);
                flyoutProvider.ShowInviteUserFlyout(inviteUserViewModel);
            }, false);

            // Shows all the available invites for the user
            ViewInvitesCommand = new DelegateCommand(() =>
            {
                var viewInvitesViewModel = new ViewInvitesViewModel(this, invitesTable);
                flyoutProvider.ShowViewInvitesFlyout(viewInvitesViewModel);
            }, false);
        }
 public Dismiss ShowInviteUserFlyout(InviteUserViewModel viewModel)
 {
     Flyout flyout = CreateFlyout();
     var control = new InviteUser();
     control.DataContext = viewModel;
     flyout.Content = control;
     flyout.Placement = PlacementMode.Top;
     flyout.PlacementTarget = buttonInviteUser;
     flyout.IsOpen = true;
     return new Dismiss(() => flyout.IsOpen = false);
 }