-/* Settings Controller {{{ */
-@interface SettingsController : CyteViewController <
- UITableViewDataSource,
- UITableViewDelegate
-> {
- _transient Database *database_;
- // XXX: ok, "roledelegate_"?...
- _transient id roledelegate_;
- _H<UITableView, 2> table_;
- _H<UISegmentedControl> segment_;
- _H<UIView> container_;
-}
-
-- (void) showDoneButton;
-- (void) resizeSegmentedControl;
-
-@end
-
-@implementation SettingsController
-
-- (void) loadView {
- table_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped] autorelease];
- [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
- [table_ setDelegate:self];
- [(UITableView *) table_ setDataSource:self];
- [self setView:table_];
-
- NSArray *items = [NSArray arrayWithObjects:
- UCLocalize("USER"),
- UCLocalize("HACKER"),
- UCLocalize("DEVELOPER"),
- nil];
- segment_ = [[[UISegmentedControl alloc] initWithItems:items] autorelease];
- [segment_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin)];
- container_ = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, 44.0f)] autorelease];
- [container_ addSubview:segment_];
-}
-
-- (void) viewDidLoad {
- [super viewDidLoad];
-
- [[self navigationItem] setTitle:UCLocalize("WHO_ARE_YOU")];
-
- int index = -1;
- if ([Role_ isEqualToString:@"User"]) index = 0;
- if ([Role_ isEqualToString:@"Hacker"]) index = 1;
- if ([Role_ isEqualToString:@"Developer"]) index = 2;
- if (index != -1) {
- [segment_ setSelectedSegmentIndex:index];
- [self showDoneButton];
- }
-
- [segment_ addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
- [self resizeSegmentedControl];
-}
-
-- (void) releaseSubviews {
- table_ = nil;
- segment_ = nil;
- container_ = nil;
-
- [super releaseSubviews];
-}
-
-- (id) initWithDatabase:(Database *)database delegate:(id)delegate {
- if ((self = [super init]) != nil) {
- database_ = database;
- roledelegate_ = delegate;
- } return self;
-}
-
-- (void) resizeSegmentedControl {
- CGFloat width = [[self view] frame].size.width;
- [segment_ setFrame:CGRectMake(width / 32.0f, 0, width - (width / 32.0f * 2.0f), 44.0f)];
-}
-
-- (void) viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [self resizeSegmentedControl];
-}
-
-- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
- [self resizeSegmentedControl];
-}
-
-- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
- [self resizeSegmentedControl];
-}
-
-- (void) save {
- NSString *role(nil);
-
- switch ([segment_ selectedSegmentIndex]) {
- case 0: role = @"User"; break;
- case 1: role = @"Hacker"; break;
- case 2: role = @"Developer"; break;
-
- _nodefault
- }
-
- if (![role isEqualToString:Role_]) {
- bool rolling(Role_ == nil);
- Role_ = role;
-
- Settings_ = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- Role_, @"Role",
- nil];
-
- [Metadata_ setObject:Settings_ forKey:@"Settings"];
- Changed_ = true;
-
- if (rolling)
- [roledelegate_ loadData];
- else
- [roledelegate_ updateData];
- }
-}
-
-- (void) segmentChanged:(UISegmentedControl *)control {
- [self showDoneButton];
-}
-
-- (void) saveAndClose {
- [self save];
-
- [[self navigationItem] setRightBarButtonItem:nil];
- [[self navigationController] dismissModalViewControllerAnimated:YES];
-}
-
-- (void) doneButtonClicked {
- UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)] autorelease];
- [spinner startAnimating];
- UIBarButtonItem *spinItem = [[[UIBarButtonItem alloc] initWithCustomView:spinner] autorelease];
- [[self navigationItem] setRightBarButtonItem:spinItem];
-
- [self performSelector:@selector(saveAndClose) withObject:nil afterDelay:0];
-}
-
-- (void) showDoneButton {
- [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc]
- initWithTitle:UCLocalize("DONE")
- style:UIBarButtonItemStyleDone
- target:self
- action:@selector(doneButtonClicked)
- ] autorelease] animated:([[self navigationItem] rightBarButtonItem] == nil)];
-}
-
-- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
- // XXX: For not having a single cell in the table, this sure is a lot of sections.
- return 6;
-}
-
-- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return 0; // :(
-}
-
-- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- return nil; // This method is required by the protocol.
-}
-
-- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
- if (section == 1)
- return UCLocalize("ROLE_EX");
- if (section == 4)
- return [NSString stringWithFormat:
- @"%@: %@\n%@: %@\n%@: %@",
- UCLocalize("USER"), UCLocalize("USER_EX"),
- UCLocalize("HACKER"), UCLocalize("HACKER_EX"),
- UCLocalize("DEVELOPER"), UCLocalize("DEVELOPER_EX")
- ];
- else return nil;
-}
-
-- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- return section == 3 ? 44.0f : 0;
-}
-
-- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- return section == 3 ? container_ : nil;
-}
-
-- (void) reloadData {
- [super reloadData];
-
- [table_ reloadData];
-}
-
-@end
-/* }}} */