Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inline date picker #307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Examples/BasicExample/Classes/RootForm.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

@interface RootForm : NSObject <FXForm>

@property (nonatomic, strong) NSDate *startDate;
@property (nonatomic, strong) NSDate *endDate;
@property (nonatomic, strong) LoginForm *login;
@property (nonatomic, strong) RegistrationForm *registration;

Expand Down
21 changes: 21 additions & 0 deletions Examples/BasicExample/Classes/RootForm.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@

@implementation RootForm


- (NSDictionary *)startDateField
{

return @{
FXFormFieldInline: @YES,
FXFormFieldType: FXFormFieldTypeDate
};
}

- (NSDictionary *)endDateField
{

return @{
FXFormFieldInline: @YES,
FXFormFieldType: FXFormFieldTypeDate
};
}

//we want to display our login form inline instead
//of in a separate view controller, so by implementing
//the <propertyName>Field method, we can specify that
Expand All @@ -22,9 +41,11 @@ - (NSDictionary *)loginField

//let's specify a header for our registration form field


- (NSDictionary *)registrationField
{
return @{FXFormFieldHeader: @"Not Registered?"};
}


@end
1 change: 1 addition & 0 deletions Examples/BasicExample/Classes/RootFormViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ - (void)submitLoginForm
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil] show];
NSLog(@"%@",((RootForm *)self.formController.form).startDate);
}

- (void)submitRegistrationForm:(UITableViewCell<FXFormFieldCell> *)cell
Expand Down
8 changes: 8 additions & 0 deletions FXForms/FXForms.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ UIKIT_EXTERN NSString *const FXFormFieldTypeDate; //date
UIKIT_EXTERN NSString *const FXFormFieldTypeTime; //time
UIKIT_EXTERN NSString *const FXFormFieldTypeDateTime; //datetime
UIKIT_EXTERN NSString *const FXFormFieldTypeImage; //image
UIKIT_EXTERN NSString *const FXFormFieldTypeInlinePicker; // Inline picker for Private


#pragma mark -
Expand Down Expand Up @@ -125,6 +126,7 @@ UIKIT_EXTERN NSString *const FXFormFieldTypeImage; //image
- (NSString *)optionDescriptionAtIndex:(NSUInteger)index;
- (void)setOptionSelected:(BOOL)selected atIndex:(NSUInteger)index;
- (BOOL)isOptionSelectedAtIndex:(NSUInteger)index;
- (BOOL)isInlinePickerType;

@end

Expand Down Expand Up @@ -273,6 +275,12 @@ UIKIT_EXTERN NSString *const FXFormFieldTypeImage; //image

@end

@interface FXFormInlinePickerCell : FXFormBaseCell;

@property (nonatomic, readonly) UIDatePicker *datePicker;

@end


#pragma clang diagnostic pop

112 changes: 101 additions & 11 deletions FXForms/FXForms.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
NSString *const FXFormFieldTypeTime = @"time";
NSString *const FXFormFieldTypeDateTime = @"datetime";
NSString *const FXFormFieldTypeImage = @"image";
// Private
NSString *const FXFormFieldTypeInlinePicker = @"inlinePicker";


static NSString *const FXFormsException = @"FXFormsException";
Expand Down Expand Up @@ -609,6 +611,7 @@ @interface FXFormController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, copy) NSArray *sections;
@property (nonatomic, strong) NSMutableDictionary *cellHeightCache;
@property (nonatomic, strong) NSMutableDictionary *cellResponderCache;
@property (nonatomic, strong) NSMutableDictionary *cellClassesForFieldTypes;
@property (nonatomic, strong) NSMutableDictionary *cellClassesForFieldClasses;
@property (nonatomic, strong) NSMutableDictionary *controllerClassesForFieldTypes;
Expand Down Expand Up @@ -1360,6 +1363,11 @@ - (BOOL)isOptionSelectedAtIndex:(NSUInteger)index
}
}

- (BOOL)isInlinePickerType
{
return self.isInline && [@[FXFormFieldTypeDateTime, FXFormFieldTypeTime, FXFormFieldTypeDate] containsObject:self.type];
}

@end


Expand Down Expand Up @@ -1827,10 +1835,12 @@ - (instancetype)init
FXFormFieldTypeDate: [FXFormDatePickerCell class],
FXFormFieldTypeTime: [FXFormDatePickerCell class],
FXFormFieldTypeDateTime: [FXFormDatePickerCell class],
FXFormFieldTypeInlinePicker: [FXFormInlinePickerCell class],
FXFormFieldTypeImage: [FXFormImagePickerCell class]} mutableCopy];
_cellClassesForFieldClasses = [NSMutableDictionary dictionary];
_controllerClassesForFieldTypes = [@{FXFormFieldTypeDefault: [FXFormViewController class]} mutableCopy];
_controllerClassesForFieldClasses = [NSMutableDictionary dictionary];
_cellResponderCache = [NSMutableDictionary dictionary];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
Expand Down Expand Up @@ -2118,9 +2128,16 @@ - (UITableViewCell *)cellForField:(FXFormField *)field
{
style = UITableViewCellStyleValue1;
}


UITableViewCell *cell = [[cellClass alloc] initWithStyle:style reuseIdentifier:NSStringFromClass(cellClass)];
if ([field.type isEqualToString:FXFormFieldTypeInlinePicker]) {
NSIndexPath *indexPath = [self indexPathForField:field];
UIDatePicker *datePicker = self.cellResponderCache[[NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]];
[cell.contentView addSubview:datePicker];
cell.frame = datePicker.bounds;
}
//don't recycle cells - it would make things complicated
return [[cellClass alloc] initWithStyle:style reuseIdentifier:NSStringFromClass(cellClass)];
return cell;
}
}

Expand All @@ -2141,7 +2158,6 @@ - (CGFloat)tableView:(__unused UITableView *)tableView heightForRowAtIndexPath:(
cachedHeight = @(cell.bounds.size.height);
_cellHeightCache[className] = cachedHeight;
}

return [cachedHeight floatValue];
}

Expand Down Expand Up @@ -2337,6 +2353,7 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
}
}


#pragma mark -
#pragma mark Keyboard events

Expand Down Expand Up @@ -3306,7 +3323,7 @@ @implementation FXFormDatePickerCell
- (void)setUp
{
self.datePicker = [[UIDatePicker alloc] init];
[self.datePicker addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];
[self.datePicker addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
}

- (void)update
Expand Down Expand Up @@ -3337,12 +3354,12 @@ - (BOOL)canBecomeFirstResponder

- (UIView *)inputView
{
return self.datePicker;
return self.field.isInlinePickerType ? nil : self.datePicker;
}

- (void)valueChanged
- (void)valueChanged:(UIDatePicker *)datePicker
{
self.field.value = self.datePicker.date;
self.field.value = datePicker.date;
self.detailTextLabel.text = [self.field fieldDescription];
[self setNeedsLayout];

Expand All @@ -3351,15 +3368,77 @@ - (void)valueChanged

- (void)didSelectWithTableView:(UITableView *)tableView controller:(__unused UIViewController *)controller
{
if (![self isFirstResponder])
{
if (![self isFirstResponder]) {
[self becomeFirstResponder];
}
else
{
else {
[self resignFirstResponder];
}
[tableView deselectRowAtIndexPath:tableView.indexPathForSelectedRow animated:YES];

}

- (BOOL)becomeFirstResponder
{
BOOL flag = [super becomeFirstResponder];
if (self.field.isInlinePickerType) {
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showInlineDatePicker];
});
} else {
[self showInlineDatePicker];
}
}
return flag;
}

- (void)showInlineDatePicker
{
UITableView *tableView = [self tableView];
FXFormField *field = [[FXFormField alloc] initWithForm:self.field.form controller:self.field.formController attributes:@{FXFormFieldType: FXFormFieldTypeInlinePicker}];
FXFormSection *section = [self.field.formController sectionAtIndex:tableView.indexPathForSelectedRow.section];
NSIndexPath *indexPath = [self.field.formController indexPathForField:self.field];
field.formController.cellResponderCache[indexPath] = self.datePicker;
[section.fields insertObject:field atIndex:indexPath.row+1];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
[tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

- (BOOL)resignFirstResponder
{
BOOL flag = [super resignFirstResponder];
if (self.field.isInlinePickerType) {
[self hideInlineDatePicker];
}
return flag;
}

- (void)hideInlineDatePicker
{
UITableView *tableView = [self tableView];
NSIndexPath *indexPath = [self.field.formController indexPathForField:self.field];
FXFormSection *section = [self.field.formController sectionAtIndex:indexPath.section];
[section.fields removeObjectAtIndex:indexPath.row+1];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];


[CATransaction begin];
[CATransaction setCompletionBlock:^{
[self.field.formController.cellResponderCache[indexPath] removeFromSuperview];
if (!self.isFirstResponder) {
[self.field.formController.cellResponderCache removeObjectForKey:indexPath];
}
}];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

[CATransaction commit];
}

@end
Expand Down Expand Up @@ -3667,3 +3746,14 @@ - (void)valueChanged
}

@end

@interface FXFormInlinePickerCell ()

@property (nonatomic, strong, readwrite) UIDatePicker *datePicker;

@end

@implementation FXFormInlinePickerCell

@end