Skip to content
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
9 changes: 1 addition & 8 deletions InAppSettings/InAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,13 @@ extern NSString *const InAppSettingsTapNotification;

@end

@interface InAppSettingsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@interface InAppSettingsViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

@property (nonatomic, strong) NSString *file;
@property (nonatomic, strong) UITableView *settingsTableView;
@property (nonatomic, weak) UIControl *firstResponder;
@property (nonatomic, strong) InAppSettingsReader *settingsReader;

// modal view
- (IBAction)dismissModalView:(id)sender;
- (void)addDoneButton;

//keyboard notification
- (void)registerForKeyboardNotifications;
- (void)keyboardWillShow:(NSNotification*)notification;
- (void)keyboardWillHide:(NSNotification*)notification;

@end
101 changes: 18 additions & 83 deletions InAppSettings/InAppSettings.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ - (void)addDoneButton{
#pragma mark setup view

- (id)initWithFile:(NSString *)inputFile{
self = [super init];
self = [super initWithStyle:UITableViewStyleGrouped];
if (self != nil){
self.file = inputFile;
}
Expand All @@ -65,13 +65,6 @@ - (id)initWithFile:(NSString *)inputFile{
- (void)viewDidLoad{
[super viewDidLoad];

//setup the table
self.settingsTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.settingsTableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
self.settingsTableView.delegate = self;
self.settingsTableView.dataSource = self;
[self.view addSubview:self.settingsTableView];

//if the title is nil set it to Settings
if(!self.title){
self.title = NSLocalizedString(@"Settings", nil);
Expand All @@ -83,108 +76,51 @@ - (void)viewDidLoad{
}

self.settingsReader = [[InAppSettingsReader alloc] initWithFile:self.file];

//setup keyboard notification
self.firstResponder = nil;
[self registerForKeyboardNotifications];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

self.firstResponder = nil;
// Get indexpath for highlighted cell, to re-highlight
NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow];

[self.tableView reloadData];

self.settingsTableView.contentInset = UIEdgeInsetsZero;
self.settingsTableView.scrollIndicatorInsets = UIEdgeInsetsZero;
// Re-select cell
[self.tableView selectRowAtIndexPath:selectedIndex animated:NO scrollPosition:UITableViewScrollPositionNone];

[self.settingsTableView reloadData];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.firstResponder = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)applicationWillEnterForeground:(NSNotification*)notify{
// reload in settings in case they were changed in the prefs app.
[[NSUserDefaults standardUserDefaults] synchronize];
// show any changes if they happened in the background.
[self.tableView reloadData];
}

- (void)dealloc{
self.firstResponder = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark text field cell delegate

- (void)textFieldDidBeginEditing:(UITextField *)cellTextField{
self.firstResponder = cellTextField;

//TODO: find a better way to get the cell from the text view
NSIndexPath *indexPath = [self.settingsTableView indexPathForCell:(UITableViewCell *)[[cellTextField superview] superview]];
[self.settingsTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[cellTextField superview] superview]];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)cellTextField{
self.firstResponder = nil;
[cellTextField resignFirstResponder];
return YES;
}

#pragma mark keyboard notification

// TODO: handle the case where the settings are in a popover or sheet modal
// The offset amount will not be the same as on iPhone
// Maybe bring in KGKeyboardChangeManager?

- (void)registerForKeyboardNotifications{
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification*)notification{
if(self.firstResponder == nil){
CGRect keyboardEndFrame;
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
NSDictionary *userInfo = [notification userInfo];
[userInfo[UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[userInfo[UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[userInfo[UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

UIEdgeInsets settingsTableInset = self.settingsTableView.contentInset;
CGPoint tableViewScreenSpace = [self.settingsTableView.superview convertPoint:self.settingsTableView.frame.origin toView:nil];
CGFloat tableViewBottomOffset = CGRectGetHeight(self.view.bounds)-(tableViewScreenSpace.y+self.settingsTableView.frame.size.height);
settingsTableInset.bottom = CGRectGetHeight(keyboardEndFrame)-tableViewBottomOffset;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationBeginsFromCurrentState:YES];
self.settingsTableView.contentInset = settingsTableInset;
self.settingsTableView.scrollIndicatorInsets = settingsTableInset;
[UIView commitAnimations];
}
}

- (void)keyboardWillHide:(NSNotification*)notification{
if(self.firstResponder == nil){
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
NSDictionary *userInfo = [notification userInfo];
[userInfo[UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[userInfo[UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationBeginsFromCurrentState:YES];
self.settingsTableView.contentInset = UIEdgeInsetsZero;
self.settingsTableView.scrollIndicatorInsets = UIEdgeInsetsZero;
[UIView commitAnimations];
}
}

#pragma mark Table view methods

- (InAppSettingsSpecifier *)settingAtIndexPath:(NSIndexPath *)indexPath{
Expand Down Expand Up @@ -264,7 +200,6 @@ - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NS
if([cell.setting isType:@"PSTextFieldSpecifier"]){
[cell.valueInput becomeFirstResponder];
}else if(cell.canSelectCell){
[self.firstResponder resignFirstResponder];
return indexPath;
}
return nil;
Expand Down