iOS Development

ios – Logic Points on Recursive Alert Controller choices

Spread the love


On my iOS app, there is a tableView that shows names from a dictionary. That dictionary additionally accommodates ageGroup and gender for them. A person can faucet a number of names, after which click on Subsequent. This is what I WANT to occur if you click on subsequent. The app is for sorting the chosen names into lodge rooms, with a max of 4 per room. So, if there’s greater than 4 folks in a single gender, it is going to present an alert controller with the title room 1 for that gender, and an inventory of all that gender’s names. Tapping one title provides them to the room, removes them from the alert controller, and it’s repeated till the room is stuffed, and goes to the following room to complete up. If there are a number of genders, than it is going to go forward and transfer to the 2nd gender after ending up the primary. Then it shops all the information for what room what gender, and so forth.

The code under is usually wonderful. The largest subject now’s that it appears to be dealing with the whole lot primarily based off simply when 4 individuals are chosen, not relating to what number of rooms are wanted. If I’ve 4 males and 4 ladies chosen on the record it really works wonderful. I faucet first title for males and it removes that title and exhibits me 3 left, till I get achieved after which goes to the ladies. If I solely have 3 males, it throws the whole lot off.

- (void)nextButtonTapped {
    if (self.selectedItems.rely > 0) {
        // Separate men and women
        NSMutableArray<NSDictionary *> *males = [NSMutableArray array];
        NSMutableArray<NSDictionary *> *females = [NSMutableArray array];

        for (NSDictionary *selectedInfo in self.selectedItems) {
            if ([selectedInfo[@"gender"] isEqualToString:@"Male"]) {
                [males addObject:selectedInfo];
            } else if ([selectedInfo[@"gender"] isEqualToString:@"Feminine"]) {
                [females addObject:selectedInfo];
            }
        }

        // Deal with males
        [self handleGenderSelectionForGender:@"Males" genderArray:males completion:^{
            // Handle females
            [self handleGenderSelectionForGender:@"Females" genderArray:females completion:^{
                // Display the selected items in the desired format
                [self displaySelectedItems];
            }];
        }];
    }
}

- (void)handleGenderSelectionForGender:(NSString *)gender genderArray:(NSMutableArray<NSDictionary *> *)genderArray completion:(void (^)(void))completion {
    NSInteger numberOfRooms = (genderArray.rely + 3) / 4;
    
    // Verify if it is the final room for males
    if ([gender isEqualToString:@"Males"] && numberOfRooms > 0) {
        // Deal with males
        [self handleGenderSelectionRecursivelyForGender:gender genderArray:genderArray roomIndex:0 completion:^{
            // Move on to females after males are done
            [self handleGenderSelectionForGender:@"Females" genderArray:genderArray completion:completion];
        }];
    } else {
        // Deal with females
        [self handleGenderSelectionRecursivelyForGender:gender genderArray:genderArray roomIndex:0 completion:completion];
    }
}

- (void)handleGenderSelectionRecursivelyForGender:(NSString *)gender genderArray:(NSMutableArray<NSDictionary *> *)genderArray roomIndex:(NSInteger)roomIndex completion:(void (^)(void))completion {
    if (roomIndex < (genderArray.rely + 3) / 4) {
        [self presentRoomSelectionAlertForGender:gender genderArray:genderArray roomIndex:roomIndex completion:^{
            [self handleGenderSelectionRecursivelyForGender:gender genderArray:genderArray roomIndex:roomIndex + 1 completion:completion];
        }];
    } else {
        // All rooms are processed
        completion();
    }
}

- (void)displaySelectedItems {
    NSLog(@"Show %@", self.roomselectedItems);
    //IGNORING this technique till after the whole lot else is labored out
}
- (void)presentRoomSelectionAlertForGender:(NSString *)gender genderArray:(NSMutableArray<NSDictionary *> *)genderArray roomIndex:(NSInteger)roomIndex completion:(void (^)(void))completion {
    [self showNextAlertForGender:gender genderArray:genderArray roomIndex:roomIndex count:0 completion:completion];
}

- (void)showNextAlertForGender:(NSString *)gender genderArray:(NSMutableArray<NSDictionary *> *)genderArray roomIndex:(NSInteger)roomIndex rely:(NSInteger)rely completion:(void (^)(void))completion {
    NSString *title = [NSString stringWithFormat:@"%@: Room %@", gender, @(roomIndex + 1)];
    NSString *message = [NSString stringWithFormat:@"Choose up to 4 people for this room"];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                      preferredStyle:UIAlertControllerStyleAlert];

    __block NSInteger selectedCount = rely;  // Monitor the variety of chosen names for the room

    // Create a duplicate of genderArray to iterate over
    NSArray<NSDictionary *> *genderArrayCopy = [genderArray copy];

    // Add actions for choosing names
    for (NSDictionary *selectedInfo in genderArrayCopy) {
        NSString *title = selectedInfo[@"name"];

        UIAlertAction *nameAction = [UIAlertAction actionWithTitle:name
                                                             style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction * _Nonnull action) {
            // Handle the selection of a name for the room
            NSString *selectedName = action.title;
            NSLog(@"Selected Name: %@", selectedName);
            // Add logic to save the selected names for the room
            [self.roomselectedItems addObject:@{@"name": selectedName, @"gender": selectedInfo[@"gender"], @"ageGroup": selectedInfo[@"ageGroup"], @"roomIndex": @(roomIndex + 1)}];

            // Take away chosen particular person from the unique array
            [genderArray removeObject:selectedInfo];

            // Replace the chosen rely
            selectedCount++;

            // Verify if the required variety of folks for the room is reached
            if (selectedCount % 4 == 0 || selectedCount == genderArrayCopy.rely) {
                // Transfer on to the following room if accessible
                NSInteger nextRoomIndex = roomIndex + 1;
                
                if (nextRoomIndex < (genderArray.rely + 3) / 4) {
                    // Dismiss the present alert earlier than displaying the following one
                    [self dismissViewControllerAnimated:NO completion:^{
                        [self showNextAlertForGender:gender genderArray:genderArray roomIndex:nextRoomIndex count:count + 1 completion:completion];
                    }];
                } else {
                    // Dismiss the alert if the final room is reached
                    [self dismissViewControllerAnimated:YES completion:^{
                        completion(); // Call the completion handler when all rooms are processed
                    }];
                }
            } else {
                // Dismiss the present alert and present the following one
                [self dismissViewControllerAnimated:NO completion:^{
                    NSLog(@"Selected Items %@", self.roomselectedItems);

                    [self showNextAlertForGender:gender genderArray:genderArray roomIndex:roomIndex count:selectedCount completion:completion];
                }];
            }
        }];

        [alertController addAction:nameAction];
    }

    [self presentViewController:alertController animated:YES completion:nil];
}

Leave a Reply

Your email address will not be published. Required fields are marked *