Skip to content

Commit

Permalink
Allows deleting selected payment option (#1149)
Browse files Browse the repository at this point in the history
* Allows deleting selected payment option.
Will automatically end editing if the last deletable payment option is removed.

* Call end editing on next run loop.

* Adds missing semicolon
  • Loading branch information
csabol-stripe authored Mar 18, 2019
1 parent 94838df commit 6f34322
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions Stripe/STPPaymentOptionsInternalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ - (BOOL)isPaymentOptionDetachable:(id<STPPaymentOption>)paymentOption {
return NO;
}

if (paymentOption == self.selectedPaymentOption) {
// Cannot detach selected payment method
return NO;
}

if (![paymentOption conformsToProtocol:@protocol(STPSourceProtocol)]) {
// Cannot detach non-source payment method
return NO;
Expand Down Expand Up @@ -210,10 +205,14 @@ - (void)handleEditButtonTapped:(__unused id)sender {
}

- (void)handleDoneButtonTapped:(__unused id)sender {
[self.tableView setEditing:NO animated:YES];
[self _endTableViewEditing];
[self reloadRightBarButtonItemWithTableViewIsEditing:self.tableView.isEditing animated:YES];
}

- (void)_endTableViewEditing {
[self.tableView setEditing:NO animated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(__unused UITableView *)tableView {
Expand Down Expand Up @@ -301,8 +300,21 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEd
// Perform deletion animation for single row
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

BOOL tableViewIsEditing = tableView.isEditing;
if (![self isAnyPaymentOptionDetachable]) {
// we deleted the last available payment option, stop editing
// (but delay to next runloop because calling tableView setEditing:animated:
// in this function is not allowed)
dispatch_async(dispatch_get_main_queue(), ^{
[self _endTableViewEditing];
});
// manually set the value passed to reloadRightBarButtonItemWithTableViewIsEditing
// below
tableViewIsEditing = NO;
}

// Reload right bar button item text
[self reloadRightBarButtonItemWithTableViewIsEditing:tableView.isEditing animated:YES];
[self reloadRightBarButtonItemWithTableViewIsEditing:tableViewIsEditing animated:YES];

// Notify delegate
[self.delegate internalViewControllerDidDeletePaymentOption:paymentOptionToDelete];
Expand Down

0 comments on commit 6f34322

Please sign in to comment.