fixed: allow widget profiles with identical names and shows the checkmark at the correct profile

This commit is contained in:
TrueZhuanjia
2025-09-12 22:47:10 +08:00
parent f5d629c62c
commit 424fe2c2f0
3 changed files with 20 additions and 21 deletions
@@ -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
+11 -13
View File
@@ -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"];
@@ -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<NSURL *> *)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<NSURL *> *)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<NSURL *> *)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];