3 * Copyright (c) 2016 Apple Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #import "HostnameController.h"
19 #import "BonjourSCStore.h"
20 #import <AssertMacros.h>
21 #import <Preferences/Preferences.h>
23 #define LocalizedStringFromMyBundle(key, comment) \
24 NSLocalizedStringFromTableInBundle(key, @"Localizable", [NSBundle bundleForClass: [self class]], comment)
26 @interface HostnameController ()
28 @property (strong) UITextField * textField;
32 @implementation HostnameController
38 self.tableView.dataSource = (id<UITableViewDataSource>)self;
39 self.tableView.delegate = (id<UITableViewDelegate>)self;
42 - (void)viewWillDisappear:(BOOL)animated
44 [super viewWillDisappear: animated];
45 if (self.isMovingFromParentViewController)
47 [[self firstResponder] resignFirstResponder]; // Ends any outstanding edits
48 self.bonjourHostname = _textField.attributedText.string;
49 [self savePreferences]; }
52 -(void)savePreferences
54 [BonjourSCStore setObject: _bonjourHostname.length ? @[@{
55 (NSString *)SC_DYNDNS_DOMAIN_KEY : _bonjourHostname,
56 (NSString *)SC_DYNDNS_ENABLED_KEY : @YES
58 forKey: (NSString *)SC_DYNDNS_HOSTNAMES_KEY];
61 - (void)_setHostname:(NSString *)value
63 self.bonjourHostname = value;
66 #pragma mark - TableView Delegates
68 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
70 (void)tableView; // Unused
71 (void)indexPath; // Unused
72 return UITableViewAutomaticDimension;
75 - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
77 (void)tableView; // Unused
78 (void)section; // Unused
79 return(LocalizedStringFromMyBundle(@"_bonjour.hostname.desc", nil));
82 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
84 (void)tableView; // Unused
85 (void)section; // Unused
89 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
91 PSEditableTableCell *cell = nil;
93 if (tableView == self.tableView && indexPath.section == 0)
95 static NSString *MyIdentifier = @"hostname_cell_id";
96 cell = (PSEditableTableCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];
99 cell = [[PSEditableTableCell alloc] initWithStyle: [PSEditableTableCell cellStyle] reuseIdentifier: MyIdentifier];
101 cell.placeholderText = LocalizedStringFromMyBundle(@"_bonjour.hostname.placeholder", nil);
102 cell.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
103 cell.textField.clearButtonMode = UITextFieldViewModeAlways;
104 cell.textField.autocorrectionType = UITextAutocorrectionTypeNo;
105 cell.textField.keyboardType = UIKeyboardTypeURL;
107 cell.value = _bonjourHostname;
108 self.textField = cell.textField;
114 - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
116 (void)tableView; // Unused
119 if (indexPath.section == 0 && indexPath.row == 0) result = NO;