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

138 lines
4 KiB
Mathematica
Raw Normal View History

2018-02-21 03:49:10 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
2018-02-22 04:31:55 +01:00
#import "OWSSoundSettingsViewController.h"
2018-02-22 04:34:40 +01:00
#import <AVFoundation/AVFoundation.h>
2018-02-23 21:44:46 +01:00
#import <SignalMessaging/OWSAudioPlayer.h>
2018-02-22 02:59:03 +01:00
#import <SignalMessaging/OWSSounds.h>
2018-02-21 03:49:10 +01:00
2018-02-22 04:31:55 +01:00
NS_ASSUME_NONNULL_BEGIN
@interface OWSSoundSettingsViewController ()
2018-02-21 03:49:10 +01:00
@property (nonatomic) BOOL isDirty;
2018-02-22 02:59:03 +01:00
@property (nonatomic) OWSSound currentSound;
2018-02-21 03:49:10 +01:00
2018-02-23 21:44:46 +01:00
@property (nonatomic, nullable) OWSAudioPlayer *audioPlayer;
2018-02-22 04:34:40 +01:00
2018-02-21 03:49:10 +01:00
@end
#pragma mark -
2018-02-22 04:31:55 +01:00
@implementation OWSSoundSettingsViewController
2018-02-21 03:49:10 +01:00
- (void)viewDidLoad
{
[super viewDidLoad];
2018-02-26 21:07:43 +01:00
[self setTitle:NSLocalizedString(@"SETTINGS_ITEM_NOTIFICATION_SOUND",
@"Label for settings view that allows user to change the notification sound.")];
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 =
2018-02-22 04:31:55 +01:00
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
2018-02-21 03:49:10 +01:00
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];
2018-02-22 04:31:55 +01:00
__weak OWSSoundSettingsViewController *weakSelf = self;
2018-02-21 03:49:10 +01:00
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 04:31:55 +01:00
2018-02-26 21:07:43 +01:00
NSArray<NSNumber *> *allSounds = [OWSSounds allNotificationSounds];
2018-02-22 04:31:55 +01:00
for (NSNumber *nsValue in allSounds) {
2018-02-22 02:59:03 +01:00
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 04:34:40 +01:00
[self.audioPlayer stop];
self.audioPlayer = [OWSSounds audioPlayerForSound:sound];
// Suppress looping in this view.
2018-02-23 21:44:46 +01:00
self.audioPlayer.isLooping = NO;
2018-02-22 04:34:40 +01:00
[self.audioPlayer play];
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-22 04:34:40 +01:00
[self.audioPlayer stop];
2018-02-21 04:13:01 +01:00
[self.navigationController popViewControllerAnimated:YES];
2018-02-21 03:49:10 +01:00
}
- (void)saveWasPressed:(id)sender
{
2018-02-26 21:07:43 +01:00
if (self.thread) {
[OWSSounds setNotificationSound:self.currentSound forThread:self.thread];
} else {
[OWSSounds setGlobalNotificationSound:self.currentSound];
}
2018-02-21 03:49:10 +01:00
2018-02-22 04:34:40 +01:00
[self.audioPlayer stop];
2018-02-21 04:13:01 +01:00
[self.navigationController popViewControllerAnimated:YES];
2018-02-21 03:49:10 +01:00
}
@end
2018-02-22 04:31:55 +01:00
NS_ASSUME_NONNULL_END