3lvis / Form

The most flexible and powerful way to build a form on iOS

Home Page:http://hyper.no

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Button actions

jeffleeismyhero opened this issue · comments

How can buttons be used to perform actions?

I have the following buttons:

screen shot 2015-06-02 at 4 20 50 pm 1

The objective is to increase and decrease the date without the need for a picker.

Whenever a button gets pressed you'll received a call to this block, then you can do whatever you want by using the button's fieldID

I've attempted something like this. Where am I going wrong? Both the decrease and increase steps fire.

- (FORMDataSource *)dataSource {
    if (_dataSource) return _dataSource;

    _dataSource = [[FORMDataSource alloc] initWithJSON:self.JSON
                                        collectionView:self.collectionView
                                                layout:self.layout
                                                values:self.initialValues
                                              disabled:NO];

    __weak typeof(self)weakSelf = self;

    _dataSource.fieldUpdatedBlock = ^(id cell, FORMField *field) {
        HYPParsedRelationship *parsedRelationship = [field.fieldID hyp_parseRelationship];
        NSString *attribute = parsedRelationship.attribute;

        if ([attribute isEqualToString:@"decrease_start_date"]) {
            [weakSelf adjustStartDate:-1];
        } else if ([attribute isEqualToString:@"increase_start_date"]) {
            [weakSelf adjustStartDate:1];
        }
    };

    return _dataSource;
}

The issue here was with my date comparison to prevent dates earlier than today:

- (void)adjustStartDate:(NSInteger)value {
    __weak typeof(self)weakSelf = self;

    [self.dataSource fieldWithID:@"start_date" includingHiddenFields:YES completion:^(FORMField *field, NSIndexPath *indexPath) {
      if (field) {
        NSDate *adjustedDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
                                                                value:value
                                                               toDate:field.value
                                                              options:0];

        if (adjustedDate < [NSDate date]) {
            adjustedDate = [NSDate date];
        }

        field.value = adjustedDate;
        [weakSelf.dataSource updateValuesWithDictionary:@{ field.fieldID : adjustedDate }];
        [weakSelf.dataSource reloadFieldsAtIndexPaths:@[indexPath]];
      }
    }];
}

The working version of that method is:

- (void)adjustStartDate:(NSInteger)value {
    __weak typeof(self)weakSelf = self;

    [self.dataSource fieldWithID:@"start_date" includingHiddenFields:YES completion:^(FORMField *field, NSIndexPath *indexPath) {
      if (field) {
        NSDate *adjustedDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
                                                                value:value
                                                               toDate:field.value
                                                              options:0];

        if ([adjustedDate compare:[NSDate date]] == NSOrderedAscending) {
            adjustedDate = [NSDate date];
        }

        field.value = adjustedDate;
        [weakSelf.dataSource updateValuesWithDictionary:@{ field.fieldID : adjustedDate }];
        [weakSelf.dataSource reloadFieldsAtIndexPaths:@[indexPath]];
      }
    }];
}

While learning Objective-C has been fun, this sort of stuff makes me miss the conveniences of Ruby-land.

Of course! I hate when I have to do that. Hopefully it will get better with Swift.