session-ios/Signal/src/ViewControllers/NotificationSoundsViewController.m

123 lines
3.5 KiB
Mathematica
Raw Normal View History

2018-02-21 03:49:10 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "NotificationSoundsViewController.h"
2018-02-22 02:59:03 +01:00
#import <SignalMessaging/OWSSounds.h>
2018-02-21 03:49:10 +01:00
@interface NotificationSoundsViewController ()
@property (nonatomic) BOOL isDirty;
2018-02-22 02:59:03 +01:00
@property (nonatomic) OWSSound currentSound;
2018-02-21 03:49:10 +01:00
@end
#pragma mark -
@implementation NotificationSoundsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
2018-02-21 04:13:01 +01:00
[self setTitle:NSLocalizedString(@"NOTIFICATIONS_ITEM_SOUND",
@"Label for settings view that allows user to change the notification sound.")];
2018-02-21 03:49:10 +01:00
2018-02-22 02:59:03 +01:00
self.currentSound
= (self.thread ? [OWSSounds notificationSoundForThread:self.thread] : [OWSSounds globalNotificationSound]);
2018-02-21 03:49:10 +01:00
[self updateTableContents];
[self updateNavigationItems];
}
- (void)viewDidAppear:(BOOL)animated
{
[self updateTableContents];
}
- (void)updateNavigationItems
{
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:@selector(cancelWasPressed:)];
if (self.isDirty) {
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:@selector(saveWasPressed:)];
} else {
self.navigationItem.rightBarButtonItem = nil;
}
}
#pragma mark - Table Contents
- (void)updateTableContents
{
OWSTableContents *contents = [OWSTableContents new];
__weak NotificationSoundsViewController *weakSelf = self;
OWSTableSection *soundsSection = [OWSTableSection new];
soundsSection.headerTitle = NSLocalizedString(
2018-02-21 04:13:01 +01:00
@"NOTIFICATIONS_SECTION_SOUNDS", @"Label for settings UI that allows user to change the notification sound.");
2018-02-22 02:59:03 +01:00
for (NSNumber *nsValue in [OWSSounds allNotificationSounds]) {
OWSSound sound = (OWSSound)nsValue.intValue;
2018-02-21 04:13:01 +01:00
OWSTableItem *item;
2018-02-22 02:59:03 +01:00
if (sound == self.currentSound) {
item = [OWSTableItem checkmarkItemWithText:[OWSSounds displayNameForSound:sound]
actionBlock:^{
[weakSelf soundWasSelected:sound];
}];
2018-02-21 04:13:01 +01:00
} else {
2018-02-22 02:59:03 +01:00
item = [OWSTableItem actionItemWithText:[OWSSounds displayNameForSound:sound]
actionBlock:^{
[weakSelf soundWasSelected:sound];
}];
2018-02-21 04:13:01 +01:00
}
[soundsSection addItem:item];
2018-02-21 03:49:10 +01:00
}
[contents addSection:soundsSection];
self.contents = contents;
}
#pragma mark - Events
2018-02-22 02:59:03 +01:00
- (void)soundWasSelected:(OWSSound)sound
2018-02-21 03:49:10 +01:00
{
2018-02-22 02:59:03 +01:00
[OWSSounds playSound:sound];
2018-02-21 03:49:10 +01:00
2018-02-22 02:59:03 +01:00
if (self.currentSound == sound) {
2018-02-21 03:49:10 +01:00
return;
}
2018-02-22 02:59:03 +01:00
self.currentSound = sound;
2018-02-21 03:49:10 +01:00
self.isDirty = YES;
[self updateTableContents];
2018-02-21 04:13:01 +01:00
[self updateNavigationItems];
2018-02-21 03:49:10 +01:00
}
- (void)cancelWasPressed:(id)sender
{
// TODO: Add "discard changes?" alert.
2018-02-21 04:13:01 +01:00
[self.navigationController popViewControllerAnimated:YES];
2018-02-21 03:49:10 +01:00
}
- (void)saveWasPressed:(id)sender
{
if (self.thread) {
2018-02-22 02:59:03 +01:00
[OWSSounds setNotificationSound:self.currentSound forThread:self.thread];
} else {
2018-02-22 02:59:03 +01:00
[OWSSounds setGlobalNotificationSound:self.currentSound];
}
2018-02-21 03:49:10 +01:00
2018-02-21 04:13:01 +01:00
[self.navigationController popViewControllerAnimated:YES];
2018-02-21 03:49:10 +01:00
}
@end