session-ios/Session/Settings/OWSSoundSettingsViewController.m

169 lines
5.4 KiB
Mathematica
Raw Normal View History

2018-02-21 03:49:10 +01:00
//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
2018-02-21 03:49:10 +01:00
//
2018-02-22 04:31:55 +01:00
#import "OWSSoundSettingsViewController.h"
2018-02-22 04:34:40 +01:00
#import <AVFoundation/AVFoundation.h>
2020-11-26 00:37:56 +01:00
#import <SessionMessagingKit/OWSAudioPlayer.h>
#import <SessionMessagingKit/OWSSounds.h>
2020-11-11 07:45:50 +01:00
#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
#import <SignalUtilitiesKit/UIUtil.h>
#import "Session-Swift.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.")];
2018-02-26 21:07:43 +01:00
self.currentSound
= (self.thread ? [OWSSounds notificationSoundForThread:self.thread] : [OWSSounds globalNotificationSound]);
2018-02-21 03:49:10 +01:00
[self updateTableContents];
[self updateNavigationItems];
2020-08-27 07:10:28 +02:00
[LKViewControllerUtilities setUpDefaultSessionStyleForVC:self withTitle:NSLocalizedString(@"Sound", @"") customBackButton:NO];
self.tableView.backgroundColor = UIColor.clearColor;
2018-02-21 03:49:10 +01:00
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
2018-02-21 03:49:10 +01:00
[self updateTableContents];
}
- (void)updateNavigationItems
{
UIBarButtonItem *cancelItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancelWasPressed:)
accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, @"cancel")];
2020-03-17 06:18:53 +01:00
cancelItem.tintColor = LKColors.text;
self.navigationItem.leftBarButtonItem = cancelItem;
2018-02-21 03:49:10 +01:00
if (self.isDirty) {
UIBarButtonItem *saveItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:@selector(saveWasPressed:)
accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, @"save")];
self.navigationItem.rightBarButtonItem = saveItem;
2018-02-21 03:49:10 +01:00
} 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-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;
NSString *soundLabelText = ^{
NSString *baseName = [OWSSounds displayNameForSound:sound];
if (sound == OWSSound_Note) {
NSString *noteStringFormat = NSLocalizedString(@"SETTINGS_AUDIO_DEFAULT_TONE_LABEL_FORMAT",
@"Format string for the default 'Note' sound. Embeds the system {{sound name}}.");
return [NSString stringWithFormat:noteStringFormat, baseName];
} else {
return [OWSSounds displayNameForSound:sound];
}
}();
2018-02-22 02:59:03 +01:00
if (sound == self.currentSound) {
item = [OWSTableItem
checkmarkItemWithText:soundLabelText
accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, [OWSSounds displayNameForSound:sound])
actionBlock:^{
[weakSelf soundWasSelected:sound];
}];
2018-02-21 04:13:01 +01:00
} else {
item = [OWSTableItem
actionItemWithText:soundLabelText
accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, [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];
2018-10-23 16:40:09 +02:00
self.audioPlayer = [OWSSounds audioPlayerForSound:sound audioBehavior:OWSAudioBehavior_Playback];
2018-02-22 04:34:40 +01:00
// Suppress looping in this view.
2018-02-23 21:44:46 +01:00
self.audioPlayer.isLooping = NO;
2018-10-23 16:40:09 +02: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