Skip to content

Commit

Permalink
Configuration: prompt delete data on remove disk
Browse files Browse the repository at this point in the history
Resolves #103
  • Loading branch information
osy committed Mar 8, 2020
1 parent 5527175 commit 837d94f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ConfigurationViews/VMConfigDrivesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSAssert(indexPath.section == 0, @"Invalid section");
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *name = [self.configuration driveImagePathForIndex:indexPath.row];
[self.configuration removeDriveAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self promptDelete:name];
}
}

Expand Down Expand Up @@ -108,4 +110,38 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
}
}

#pragma mark - Delete Disk

- (void)showAlert:(NSString *)msg completion:(nullable void (^)(UIAlertAction *action))completion {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okay = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK button") style:UIAlertActionStyleDefault handler:completion];
[alert addAction:okay];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
}

- (void)promptDelete:(NSString *)name {
NSURL *path;
if (self.configuration.existingPath) {
path = [self.configuration.existingPath URLByAppendingPathComponent:[UTMConfiguration diskImagesDirectory] isDirectory:YES];
} else {
path = [[NSFileManager defaultManager].temporaryDirectory URLByAppendingPathComponent:[UTMConfiguration diskImagesDirectory] isDirectory:YES];
}
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Delete Data", @"VMConfigDrivesViewController") message:NSLocalizedString(@"Do you want to also delete the disk image data? If yes, the data will be lost. Otherwise, you can create a new drive with the existing data.", @"VMConfigDrivesViewController") preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *delete = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", @"Yes button") style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
NSError *err;
[[NSFileManager defaultManager] removeItemAtURL:[path URLByAppendingPathComponent:name] error:&err];
if (err) {
[self showAlert:err.localizedDescription completion:nil];
}
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", @"No button") style:UIAlertActionStyleCancel handler:nil];
[alert addAction:delete];
[alert addAction:cancel];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
}

@end

0 comments on commit 837d94f

Please sign in to comment.