Skip to content
Snippets Groups Projects
AccountViewController_iPad.m 3.47 KiB
Newer Older
//
//  AccountViewController_iPad.m
//  ISDS
//
//  Created by Petr Hruška on 4/18/11.
//  Copyright 2011 CZ.NIC, z.s.p.o. All rights reserved.
//

#import "AccountViewController_iPad.h"

#define ALIAS_INDEX             (AUTOALIAS_INDEX + 1)
#define REMOVEBUTTON_OFFSET     (30 + 54)
#define CELL_HEIGHT             54

@implementation AccountViewController_iPad

@synthesize autoAliasCell=_autoAliasCell;
@synthesize aliasCell=_aliasCell;
@synthesize aliasTextField=_aliasTextField;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	switch (section) {
		case TEXTFIELDS_SECTION:
			return AUTOALIAS_INDEX + 1 + (self.account.useAlias ? 1 : 0);
Petr Hruška's avatar
Petr Hruška committed

- (void)setupAliasCell
{
    BOOL visible = self.account.useAlias;
    [self.autoAliasCell setOn:!visible animated:TRUE];
    NSUInteger indexes[2];
    indexes[0] = TEXTFIELDS_SECTION;
    indexes[1] = ALIAS_INDEX;
    NSIndexPath* index = [NSIndexPath indexPathWithIndexes:indexes length:2];
    NSArray* aliasRow = [NSArray arrayWithObject:index];
    if (visible)
        [self.tableView insertRowsAtIndexPaths:aliasRow withRowAnimation:UITableViewRowAnimationTop];
    else
        [self.tableView deleteRowsAtIndexPaths:aliasRow withRowAnimation:UITableViewRowAnimationTop];
    
    CGRect frame = self.removeButton.frame;
    frame.origin.y = self.tableView.frame.origin.y + [self tableView:self.tableView numberOfRowsInSection:TEXTFIELDS_SECTION] * CELL_HEIGHT + REMOVEBUTTON_OFFSET;
    [UIView animateWithDuration:0.3 animations:^{ self.removeButton.frame = frame; }];
Petr Hruška's avatar
Petr Hruška committed
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	
	[tv deselectRowAtIndexPath:indexPath animated:YES];
	
	switch (indexPath.section) {
		case TEXTFIELDS_SECTION:
			switch (indexPath.row) {
				case AUTOALIAS_INDEX:
                    self.account.useAlias = !self.account.useAlias;
                    [self setupAliasCell];
Petr Hruška's avatar
Petr Hruška committed
					return;
Petr Hruška's avatar
Petr Hruška committed
    
    [super tableView:tv didSelectRowAtIndexPath:indexPath];
}


- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	switch (indexPath.section) {
			
		case TEXTFIELDS_SECTION:
			switch (indexPath.row) {
                
				case ALIAS_INDEX:
					return self.aliasCell;
                default:
                    return [super tableView:tableView_ cellForRowAtIndexPath:indexPath];
			}
			break;
        default:
            return [super tableView:tableView_ cellForRowAtIndexPath:indexPath];
	}
}

Petr Hruška's avatar
Petr Hruška committed

- (void)viewWillAppear:(BOOL)animated {
    
    self.autoAliasCell.on = !self.account.useAlias;
    self.aliasTextField.text = self.account.alias;
    [super viewWillAppear:animated];
}

Petr Hruška's avatar
Petr Hruška committed

- (void)switchCell:(SwitchCell*)cell switchedTo:(BOOL)value
{
    if (cell == self.autoAliasCell) {

        if (self.account.useAlias != !value) {
            self.account.useAlias = !value;
            [self setupAliasCell];
        }
        
    } else {
        [super switchCell:cell switchedTo:value];
    }
}


Petr Hruška's avatar
Petr Hruška committed
- (IBAction)aliasEditingChanged:(id)sender
{
}


- (void)viewDidUnload 
{
    //self.autoAliasCell = nil, it's used to keep state
    self.aliasCell = nil;
    self.aliasTextField = nil;
}

Petr Hruška's avatar
Petr Hruška committed

- (void)dealloc
{
    [_autoAliasCell release];
    [_aliasCell release];
    [_aliasTextField release];
    [super dealloc];
}

Petr Hruška's avatar
Petr Hruška committed

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
	return YES;
}

@end