Extra functionality to show/hide preference

Issue #37 open
Peter Greis
created an issue

Greetings

In my application (LiveTrack 24 for iOS), I have recently added an option for the Live Map to pivot and follow the compass setting. All fine and well. What would be nice is to add an extra node to the xml, which would read a BOOL NSUserDefaults setting. As an example:

Before setting up InAppSettings, I would set the NSUserDefaults key "shouldShowMapPivot" based on whether or not the device has a compass built in. While parsing the plist, InAppSettings would then hide this settings entry if "NO", and show it if "YES".

The only other way that I can handle this is to make multiple plists, but then things start getting messy....

Thoughts ?

regards

Peter

Comments (1)

  1. Peter Greis reporter
    • changed status to open

    So here's my version of the fix (of which David is free to integrate or leave...). File is InAppSettingsReader.m

    - (id)initWithFile:(NSString *)inputFile{
        self = [super init];
        if(self != nil){
            self.file = inputFile;
    
            //load plist
            NSDictionary *settingsDictionary = [[NSDictionary alloc] initWithContentsOfFile:InAppSettingsFullPlistPath(self.file)];
            NSArray *preferenceSpecifiers = [settingsDictionary objectForKey:InAppSettingsPreferenceSpecifiers];
            NSString *stringsTable = [settingsDictionary objectForKey:InAppSettingsStringsTable];
            
            //initialize the arrays
            self.headers = [[NSMutableArray alloc] init];
            self.settings = [[NSMutableArray alloc] init];
            
            //PG
            /*
             ** The idea here is to be able to eliminate settings that are dependent upon (most likely)
             ** something in hardware. For example, does the device have a compass ? So in the plist
             ** section for the compass, there would be an extra string value pair "shouldShowSettingKey"
             ** this is the key, then a value (for sake of argument "deviceHasCompass"). So, before the 
             ** InAppSettings are setup, there needs to be an NSUserDefaults boolean object that set
             ** "deviceHasCompass". 
             */
            
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSString *booleanKey;
            BOOL isSet;
            NSMutableArray *keepers = [NSMutableArray arrayWithCapacity:[preferenceSpecifiers count]];
            
            for (NSDictionary *eachSetting in preferenceSpecifiers) 
            {
                booleanKey = [eachSetting valueForKey:@"shouldShowSettingKey"]; //look for it
                if (nil != booleanKey) //There is a toggle value found, now check it...
                {
                    isSet = [defaults boolForKey:booleanKey];
                    if (isSet == YES)
                        [keepers addObject:eachSetting];
                }
                else //default is to keep the setting...
                    [keepers addObject:eachSetting];
            }
            
            //load the data
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
            //for(NSDictionary *eachSetting in preferenceSpecifiers){
            for (NSDictionary *eachSetting in keepers) {
                InAppSettingsSpecifier *setting = [[InAppSettingsSpecifier alloc] initWithDictionary:eachSetting andStringsTable:stringsTable];
                if([setting isValid]){
                    if([setting isType:InAppSettingsPSGroupSpecifier]){
                        [self.headers addObject:[setting localizedTitle]];
                        [self.settings addObject:[NSMutableArray array]];
                    }else{
                        //if there are no settings make an initial container
                        if([self.settings count] < 1){
                            [self.headers addObject:@""];
                            [self.settings addObject:[NSMutableArray array]];
                        }
                        [[self.settings lastObject] addObject:setting];
                    }
                }
                [setting release];
            }
            [pool drain];
            [settingsDictionary release];
    
        }
        return self;
    }
    
  2. Log in to comment