diff --git a/VoidLink/Input/CustomOSC/OSCProfilesManager.h b/VoidLink/Input/CustomOSC/OSCProfilesManager.h index 7feb0711..aa187258 100644 --- a/VoidLink/Input/CustomOSC/OSCProfilesManager.h +++ b/VoidLink/Input/CustomOSC/OSCProfilesManager.h @@ -38,6 +38,8 @@ NS_ASSUME_NONNULL_BEGIN */ - (OSCProfile *) getSelectedProfile; +- (uint32_t) getIndexOfLastProfile; + /** * Returns the index of the 'selected' profile within the array it's in */ @@ -50,9 +52,9 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Setters /** - * Sets the profile object with the particular 'name' as the selected profile to be displayed on screen during game streaming + * Sets the profile object with the particular index as the selected profile to be displayed on screen during game streaming */ -- (void) setProfileToSelected:(NSString *)name; +- (void) setProfileToSelected:(uint32_t)index; /** * Saves a profile object with a particular 'name' and an array of button layers (the CALayer button layers are the objects currently visible on screen) to persistent storage diff --git a/VoidLink/Input/CustomOSC/OSCProfilesManager.m b/VoidLink/Input/CustomOSC/OSCProfilesManager.m index 75411a81..6ad2ff58 100644 --- a/VoidLink/Input/CustomOSC/OSCProfilesManager.m +++ b/VoidLink/Input/CustomOSC/OSCProfilesManager.m @@ -228,25 +228,23 @@ static CGRect layoutViewBounds; return 0; // if none of the profiles in the array have their 'isSelected' property set to YES (which should not be possible) return the 'Default' profile as the 'selected' profile } +- (uint32_t) getIndexOfLastProfile { + NSMutableArray *profiles = [self getAllProfiles]; + return (uint32_t)profiles.count-1; // if none of the profiles in the array have their 'isSelected' property set to YES (which should not be possible) return the 'Default' profile as the 'selected' profile +} + #pragma mark - Setters -- (void) setProfileToSelected:(NSString *)name { +- (void) setProfileToSelected:(uint32_t)tableIndex { NSMutableArray *profiles = [self getAllProfiles]; - - /* Iterate through each profile. If its name equals the value of the 'name' parameter passed into this method then set the profile's 'isSelected' property to YES, otherwise set the value to NO */ - for (OSCProfile *profile in profiles) { - - if ([profile.name isEqualToString:name]) { - profile.isSelected = YES; - } - else { - profile.isSelected = NO; - } - } + + [profiles enumerateObjectsUsingBlock:^(OSCProfile* profile, NSUInteger idx, BOOL *stop) { + profile.isSelected = idx == tableIndex; + }]; NSMutableArray *profilesEncoded = [self encodedProfilesFromArray:profiles]; // encode each 'profile' object in the array and add them to a new array - + /* Encode the array itself, NOT the objects inside the array, which have already been encoded by this point */ NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profilesEncoded requiringSecureCoding:YES error:nil]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"OSCProfiles"]; diff --git a/VoidLink/ViewControllers/CustomOSCViewControl/OSCProfilesTableViewController.m b/VoidLink/ViewControllers/CustomOSCViewControl/OSCProfilesTableViewController.m index ddbaa1af..7e9ece99 100644 --- a/VoidLink/ViewControllers/CustomOSCViewControl/OSCProfilesTableViewController.m +++ b/VoidLink/ViewControllers/CustomOSCViewControl/OSCProfilesTableViewController.m @@ -160,7 +160,7 @@ const double NAV_BAR_HEIGHT = 50; } else { // if user entered a valid name that doesn't already exist then save the profile to persistent storage [self->profilesManager saveProfileWithName: enteredProfileName andButtonLayers:self.currentOSCButtonLayers]; // the OSC layout here is passed from parent LayoutOSCViewController; - [self->profilesManager setProfileToSelected: enteredProfileName]; + [self->profilesManager setProfileToSelected: [self->profilesManager getIndexOfLastProfile]]; UIAlertController * savedAlertController = [UIAlertController alertControllerWithTitle: [NSString stringWithFormat:@""] message: [LocalizationHelper localizedStringForKey:@"Profile %@ duplicated from current layout", enteredProfileName] preferredStyle:UIAlertControllerStyleAlert]; // Let user know this profile has been duplicated & saved @@ -320,7 +320,7 @@ didPickDocumentsAtURLs:(NSArray *)urls { cell.contentView.backgroundColor = [UIColor clearColor]; // Configure the checkmark accessory - if ([profile.name isEqualToString:[profilesManager getSelectedProfile].name]) { + if (indexPath.row == [profilesManager getIndexOfSelectedProfile]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; @@ -343,9 +343,8 @@ didPickDocumentsAtURLs:(NSArray *)urls { [cell.contentView addSubview:separatorView]; [cell.contentView bringSubviewToFront:separatorView]; - // Replace the default checkmark with a UILabel displaying a checkmark character - if ([profile.name isEqualToString:[profilesManager getSelectedProfile].name]) { + if (indexPath.row == [profilesManager getIndexOfSelectedProfile]) { UILabel *checkmarkLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; // Adjust size as needed checkmarkLabel.text = @"✓"; // The checkmark character checkmarkLabel.font = [UIFont systemFontOfSize:25]; // Adjust font size as needed @@ -428,8 +427,8 @@ didPickDocumentsAtURLs:(NSArray *)urls { UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath: selectedIndexPath]; selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; // add checkmark to the cell the user tapped selectedCell.accessoryView.tintColor = [[UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:0.85] colorWithAlphaComponent:1.0]; - OSCProfile *profile = [[profilesManager getAllProfiles] objectAtIndex:indexPath.row]; - [profilesManager setProfileToSelected: profile.name]; // set the profile associated with this cell's 'isSelected' property to YES + + [profilesManager setProfileToSelected:(uint32_t)indexPath.row]; // set the profile associated with this cell's 'isSelected' property to YES /* Remove checkmark on the previously selected cell */ UITableViewCell *lastSelectedCell = [tableView cellForRowAtIndexPath: lastSelectedIndexPath];