From 8f546be53bd5dc568d053c32da51f861f88eb413 Mon Sep 17 00:00:00 2001 From: giomfo Date: Wed, 29 Oct 2014 17:48:11 +0100 Subject: [PATCH] Add new App settings: notifications, all events. Disabled by default (not implemented yet). --- .../syMessaging.xcodeproj/project.pbxproj | 12 ++ syMessaging/syMessaging/AppDelegate.m | 3 + syMessaging/syMessaging/AppSettings.h | 27 ++++ syMessaging/syMessaging/AppSettings.m | 66 ++++++++ .../syMessaging/Base.lproj/Main.storyboard | 79 +++++++--- .../syMessaging/View/SettingsTableViewCell.h | 29 ++++ .../syMessaging/View/SettingsTableViewCell.m | 26 ++++ .../ViewController/SettingsViewController.m | 143 +++++++++++++++++- 8 files changed, 355 insertions(+), 30 deletions(-) create mode 100644 syMessaging/syMessaging/AppSettings.h create mode 100644 syMessaging/syMessaging/AppSettings.m create mode 100644 syMessaging/syMessaging/View/SettingsTableViewCell.h create mode 100644 syMessaging/syMessaging/View/SettingsTableViewCell.m diff --git a/syMessaging/syMessaging.xcodeproj/project.pbxproj b/syMessaging/syMessaging.xcodeproj/project.pbxproj index f23ad9bab..e6d3f2fac 100644 --- a/syMessaging/syMessaging.xcodeproj/project.pbxproj +++ b/syMessaging/syMessaging.xcodeproj/project.pbxproj @@ -31,6 +31,8 @@ F0CEA5AE19E6895E00E47915 /* logoHighRes.png in Resources */ = {isa = PBXBuildFile; fileRef = F0CEA5AC19E6895E00E47915 /* logoHighRes.png */; }; F0CEA5AF19E6895E00E47915 /* tab_recents.png in Resources */ = {isa = PBXBuildFile; fileRef = F0CEA5AD19E6895E00E47915 /* tab_recents.png */; }; F0CEA5B119E6898800E47915 /* tab_home.ico in Resources */ = {isa = PBXBuildFile; fileRef = F0CEA5B019E6898800E47915 /* tab_home.ico */; }; + F0D3C30C1A011EF10000D49E /* AppSettings.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D3C30B1A011EF10000D49E /* AppSettings.m */; }; + F0D3C30F1A01330F0000D49E /* SettingsTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D3C30E1A01330F0000D49E /* SettingsTableViewCell.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -86,6 +88,10 @@ F0CEA5AC19E6895E00E47915 /* logoHighRes.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = logoHighRes.png; sourceTree = ""; }; F0CEA5AD19E6895E00E47915 /* tab_recents.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = tab_recents.png; sourceTree = ""; }; F0CEA5B019E6898800E47915 /* tab_home.ico */ = {isa = PBXFileReference; lastKnownFileType = image.ico; path = tab_home.ico; sourceTree = ""; }; + F0D3C30A1A011EF10000D49E /* AppSettings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppSettings.h; sourceTree = ""; }; + F0D3C30B1A011EF10000D49E /* AppSettings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppSettings.m; sourceTree = ""; }; + F0D3C30D1A01330F0000D49E /* SettingsTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SettingsTableViewCell.h; sourceTree = ""; }; + F0D3C30E1A01330F0000D49E /* SettingsTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SettingsTableViewCell.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -165,6 +171,8 @@ F02D707519F1DC9E007B47D3 /* RoomMemberTableCell.m */, F03EF5FD19F1762000A0EE52 /* RoomMessageTableCell.h */, F03EF5FE19F1762000A0EE52 /* RoomMessageTableCell.m */, + F0D3C30D1A01330F0000D49E /* SettingsTableViewCell.h */, + F0D3C30E1A01330F0000D49E /* SettingsTableViewCell.m */, ); path = View; sourceTree = ""; @@ -196,6 +204,8 @@ F03EF5E919F171EB00A0EE52 /* ViewController */, F07A80D919DD9DE700B621A1 /* AppDelegate.h */, F07A80DA19DD9DE700B621A1 /* AppDelegate.m */, + F0D3C30A1A011EF10000D49E /* AppSettings.h */, + F0D3C30B1A011EF10000D49E /* AppSettings.m */, F05B955D19DEED8A008761B0 /* MatrixHandler.h */, F05B955E19DEED8A008761B0 /* MatrixHandler.m */, F03EF60019F19E7C00A0EE52 /* MediaManager.h */, @@ -388,7 +398,9 @@ F03EF60219F19E7C00A0EE52 /* MediaManager.m in Sources */, F03EF5F919F171EB00A0EE52 /* RecentsViewController.m in Sources */, F02D707619F1DC9E007B47D3 /* RoomMemberTableCell.m in Sources */, + F0D3C30C1A011EF10000D49E /* AppSettings.m in Sources */, F03EF5F719F171EB00A0EE52 /* LoginViewController.m in Sources */, + F0D3C30F1A01330F0000D49E /* SettingsTableViewCell.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/syMessaging/syMessaging/AppDelegate.m b/syMessaging/syMessaging/AppDelegate.m index bb5504aa7..1fe118199 100644 --- a/syMessaging/syMessaging/AppDelegate.m +++ b/syMessaging/syMessaging/AppDelegate.m @@ -15,6 +15,7 @@ */ #import "AppDelegate.h" +#import "AppSettings.h" #import "RoomViewController.h" #import "MatrixHandler.h" #import "MediaManager.h" @@ -83,6 +84,8 @@ - (void)logout { // Clear cache [MediaManager clearCache]; + // Reset App settings + [[AppSettings sharedSettings] reset]; // Logout Matrix [[MatrixHandler sharedHandler] logout]; [self.masterTabBarController showLoginScreen]; diff --git a/syMessaging/syMessaging/AppSettings.h b/syMessaging/syMessaging/AppSettings.h new file mode 100644 index 000000000..114089e89 --- /dev/null +++ b/syMessaging/syMessaging/AppSettings.h @@ -0,0 +1,27 @@ +/* + Copyright 2014 OpenMarket Ltd + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +#import + +@interface AppSettings : NSObject + +@property (nonatomic) BOOL enableNotifications; +@property (nonatomic) BOOL displayAllEvents; + ++ (AppSettings *)sharedSettings; + +- (void)reset; + +@end diff --git a/syMessaging/syMessaging/AppSettings.m b/syMessaging/syMessaging/AppSettings.m new file mode 100644 index 000000000..318720516 --- /dev/null +++ b/syMessaging/syMessaging/AppSettings.m @@ -0,0 +1,66 @@ +/* + Copyright 2014 OpenMarket Ltd + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#import "AppSettings.h" + +static AppSettings *sharedSettings = nil; + +@implementation AppSettings + ++ (AppSettings *)sharedSettings { + @synchronized(self) { + if(sharedSettings == nil) + { + sharedSettings = [[super allocWithZone:NULL] init]; + } + } + return sharedSettings; +} + +#pragma mark - + +-(AppSettings *)init { + if (self = [super init]) { + } + return self; +} + +- (void)dealloc { +} + +- (void)reset { + self.enableNotifications = NO; + self.displayAllEvents = NO; +} + +#pragma mark - + +- (BOOL)enableNotifications { + return [[NSUserDefaults standardUserDefaults] boolForKey:@"enableNotifications"]; +} + +- (void)setEnableNotifications:(BOOL)notifications { + [[NSUserDefaults standardUserDefaults] setBool:notifications forKey:@"enableNotifications"]; +} + +- (BOOL)displayAllEvents { + return [[NSUserDefaults standardUserDefaults] boolForKey:@"displayAllEvents"]; +} + +- (void)setDisplayAllEvents:(BOOL)displayAllEvents { + [[NSUserDefaults standardUserDefaults] setBool:displayAllEvents forKey:@"displayAllEvents"]; +} +@end diff --git a/syMessaging/syMessaging/Base.lproj/Main.storyboard b/syMessaging/syMessaging/Base.lproj/Main.storyboard index f6ed87e95..72fd79bfa 100644 --- a/syMessaging/syMessaging/Base.lproj/Main.storyboard +++ b/syMessaging/syMessaging/Base.lproj/Main.storyboard @@ -740,33 +740,64 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. + + + + + + + + + + + + + + @@ -776,9 +807,7 @@ - - @@ -787,7 +816,7 @@ - + diff --git a/syMessaging/syMessaging/View/SettingsTableViewCell.h b/syMessaging/syMessaging/View/SettingsTableViewCell.h new file mode 100644 index 000000000..51f61299a --- /dev/null +++ b/syMessaging/syMessaging/View/SettingsTableViewCell.h @@ -0,0 +1,29 @@ +/* + Copyright 2014 OpenMarket Ltd + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#import + +@interface SettingsTableViewCell : UITableViewCell +@end + +@interface SettingsTableCellWithSwitch : SettingsTableViewCell +@property (strong, nonatomic) IBOutlet UILabel *settingLabel; +@property (strong, nonatomic) IBOutlet UISwitch *settingSwitch; +@end + +@interface SettingsTableCellWithTextView : SettingsTableViewCell +@property (strong, nonatomic) IBOutlet UITextView *settingTextView; +@end \ No newline at end of file diff --git a/syMessaging/syMessaging/View/SettingsTableViewCell.m b/syMessaging/syMessaging/View/SettingsTableViewCell.m new file mode 100644 index 000000000..74f11fba5 --- /dev/null +++ b/syMessaging/syMessaging/View/SettingsTableViewCell.m @@ -0,0 +1,26 @@ +/* + Copyright 2014 OpenMarket Ltd + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#import "SettingsTableViewCell.h" + +@implementation SettingsTableViewCell +@end + +@implementation SettingsTableCellWithSwitch +@end + +@implementation SettingsTableCellWithTextView +@end \ No newline at end of file diff --git a/syMessaging/syMessaging/ViewController/SettingsViewController.m b/syMessaging/syMessaging/ViewController/SettingsViewController.m index 4880598d5..1056356db 100644 --- a/syMessaging/syMessaging/ViewController/SettingsViewController.m +++ b/syMessaging/syMessaging/ViewController/SettingsViewController.m @@ -17,22 +17,35 @@ #import "SettingsViewController.h" #import "AppDelegate.h" +#import "AppSettings.h" #import "MatrixHandler.h" #import "MediaManager.h" +#import "SettingsTableViewCell.h" + +#define SETTINGS_SECTION_NOTIFICATIONS_INDEX 0 +#define SETTINGS_SECTION_MESSAGES_INDEX 1 +#define SETTINGS_SECTION_CONFIGURATION_INDEX 2 +#define SETTINGS_SECTION_COMMANDS_INDEX 3 + +NSString* const kConfigurationFormatText = @"Home server: %@\r\nIdentity server: %@\r\nUser ID: %@\r\nAccess token: %@"; +NSString* const kCommandsDescriptionText = @"The following commands are available in the room chat:\r\n\r\n /nick : change your display name\r\n /me : send the action you are doing. /me will be replaced by your display name\r\n /join : join a room\r\n /kick []: kick the user\r\n /ban []: ban the user\r\n /unban : unban the user\r\n /op : set user power level\r\n /deop : reset user power level to the room default value"; + @interface SettingsViewController () { id imageLoader; NSString *currentDisplayName; NSString *currentPictureURL; + + UIButton *logoutBtn; + UISwitch *notificationsSwitch; + UISwitch *allEventsSwitch; } @property (strong, nonatomic) IBOutlet UITableView *tableView; @property (weak, nonatomic) IBOutlet UIView *tableHeader; @property (weak, nonatomic) IBOutlet UIButton *userPicture; @property (weak, nonatomic) IBOutlet UITextField *userDisplayName; @property (weak, nonatomic) IBOutlet UIButton *saveBtn; -@property (weak, nonatomic) IBOutlet UIView *tableFooter; -@property (weak, nonatomic) IBOutlet UIButton *logoutBtn; - (IBAction)onButtonPressed:(id)sender; @@ -44,6 +57,14 @@ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. + // Add logout button in nav bar + logoutBtn = [UIButton buttonWithType:UIButtonTypeSystem]; + logoutBtn.frame = CGRectMake(0, 0, 60, 44); + [logoutBtn setTitle:@"Logout" forState:UIControlStateNormal]; + [logoutBtn setTitle:@"Logout" forState:UIControlStateHighlighted]; + [logoutBtn addTarget:self action:@selector(onButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:logoutBtn]; + [self reset]; } @@ -63,6 +84,10 @@ [MediaManager cancel:imageLoader]; imageLoader = nil; } + + logoutBtn = nil; + notificationsSwitch = nil; + allEventsSwitch = nil; } - (void)viewWillAppear:(BOOL)animated { @@ -112,6 +137,9 @@ //Alert user [[AppDelegate theDelegate] showErrorAsAlert:error]; }]; + + // Refresh settings + [self.tableView reloadData]; } #pragma mark - @@ -132,7 +160,7 @@ self.userDisplayName.text = nil; } -#pragma mark - +#pragma mark - Actions - (IBAction)onButtonPressed:(id)sender { [self dismissKeyboard]; @@ -153,9 +181,13 @@ }]; } // TODO check picture change - } else if (sender == _logoutBtn) { + } else if (sender == logoutBtn) { [self reset]; [[AppDelegate theDelegate] logout]; + } else if (sender == notificationsSwitch) { + [AppSettings sharedSettings].enableNotifications = notificationsSwitch.on; + } else if (sender == allEventsSwitch) { + [AppSettings sharedSettings].displayAllEvents = allEventsSwitch.on; } } @@ -173,7 +205,108 @@ { // "Done" key has been pressed [textField resignFirstResponder]; - return YES; } + +#pragma mark - Table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 4; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + if (section == SETTINGS_SECTION_NOTIFICATIONS_INDEX) { + return 1; + } else if (section == SETTINGS_SECTION_MESSAGES_INDEX) { + return 1; + } else if (section == SETTINGS_SECTION_CONFIGURATION_INDEX) { + return 1; + } else if (section == SETTINGS_SECTION_COMMANDS_INDEX) { + return 1; + } + + return 0; +} + +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { + if (indexPath.section == SETTINGS_SECTION_NOTIFICATIONS_INDEX) { + return 44; + } else if (indexPath.section == SETTINGS_SECTION_MESSAGES_INDEX) { + return 44; + } else if (indexPath.section == SETTINGS_SECTION_CONFIGURATION_INDEX) { + UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, MAXFLOAT)]; + textView.font = [UIFont systemFontOfSize:14]; + MatrixHandler *mxHandler = [MatrixHandler sharedHandler]; + textView.text = [NSString stringWithFormat:kConfigurationFormatText, mxHandler.homeServerURL, nil, mxHandler.userId, mxHandler.accessToken]; + CGSize contentSize = [textView sizeThatFits:textView.frame.size]; + return contentSize.height + 1; + } else if (indexPath.section == SETTINGS_SECTION_COMMANDS_INDEX) { + UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, MAXFLOAT)]; + textView.font = [UIFont systemFontOfSize:14]; + textView.text = kCommandsDescriptionText; + CGSize contentSize = [textView sizeThatFits:textView.frame.size]; + return contentSize.height + 1; + } + + return 44; +} + +- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section +{ + return 30; +} +- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section +{ + return 1; +} + +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section +{ + UILabel *sectionHeader = [[UILabel alloc] initWithFrame:[tableView rectForHeaderInSection:section]]; + sectionHeader.font = [UIFont boldSystemFontOfSize:16]; + sectionHeader.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]; + + if (section == SETTINGS_SECTION_NOTIFICATIONS_INDEX) { + sectionHeader.text = @" Notifications"; + } else if (section == SETTINGS_SECTION_MESSAGES_INDEX) { + sectionHeader.text = @" Messages"; + } else if (section == SETTINGS_SECTION_CONFIGURATION_INDEX) { + sectionHeader.text = @" Configuration"; + } else if (section == SETTINGS_SECTION_COMMANDS_INDEX) { + sectionHeader.text = @" Commands"; + } else { + sectionHeader = nil; + } + return sectionHeader; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + SettingsTableViewCell *cell = nil; + + if (indexPath.section == SETTINGS_SECTION_NOTIFICATIONS_INDEX) { + SettingsTableCellWithSwitch *notificationsCell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCellWithSwitch" forIndexPath:indexPath]; + notificationsCell.settingLabel.text = @"Enable notifications"; + notificationsCell.settingSwitch.on = [[AppSettings sharedSettings] enableNotifications]; + notificationsSwitch = notificationsCell.settingSwitch; + cell = notificationsCell; + } else if (indexPath.section == SETTINGS_SECTION_MESSAGES_INDEX) { + SettingsTableCellWithSwitch *allEventsCell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCellWithSwitch" forIndexPath:indexPath]; + allEventsCell.settingLabel.text = @"Display all events"; + allEventsCell.settingSwitch.on = [[AppSettings sharedSettings] displayAllEvents]; + allEventsSwitch = allEventsCell.settingSwitch; + cell = allEventsCell; + } else if (indexPath.section == SETTINGS_SECTION_CONFIGURATION_INDEX) { + SettingsTableCellWithTextView *configCell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCellWithTextView" forIndexPath:indexPath]; + MatrixHandler *mxHandler = [MatrixHandler sharedHandler]; + configCell.settingTextView.text = [NSString stringWithFormat:kConfigurationFormatText, mxHandler.homeServerURL, nil, mxHandler.userId, mxHandler.accessToken]; + cell = configCell; + } else if (indexPath.section == SETTINGS_SECTION_COMMANDS_INDEX) { + SettingsTableCellWithTextView *commandsCell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCellWithTextView" forIndexPath:indexPath]; + commandsCell.settingTextView.text = kCommandsDescriptionText; + cell = commandsCell; + } + + return cell; +} + @end