mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			169 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			169 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Matlab
		
	
| 
								 
											8 years ago
										 
									 | 
							
								//
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								//  Copyright (c) 2019 Open Whisper Systems. All rights reserved.
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								//
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								#import "OWSSoundSettingsViewController.h"
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								#import <AVFoundation/AVFoundation.h>
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#import <SessionMessagingKit/OWSAudioPlayer.h>
							 | 
						||
| 
								 | 
							
								#import <SessionMessagingKit/OWSSounds.h>
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
							 | 
						||
| 
								 | 
							
								#import <SignalUtilitiesKit/UIUtil.h>
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#import "Session-Swift.h"
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								NS_ASSUME_NONNULL_BEGIN
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								@interface OWSSoundSettingsViewController ()
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								@property (nonatomic) BOOL isDirty;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								@property (nonatomic) OWSSound currentSound;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								@property (nonatomic, nullable) OWSAudioPlayer *audioPlayer;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								@end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma mark -
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								@implementation OWSSoundSettingsViewController
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								- (void)viewDidLoad
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    [super viewDidLoad];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [self setTitle:NSLocalizedString(@"SETTINGS_ITEM_NOTIFICATION_SOUND",
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                       @"Label for settings view that allows user to change the notification sound.")];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.currentSound
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        = (self.thread ? [OWSSounds notificationSoundForThread:self.thread] : [OWSSounds globalNotificationSound]);
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    [self updateTableContents];
							 | 
						||
| 
								 | 
							
								    [self updateNavigationItems];
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    [LKViewControllerUtilities setUpDefaultSessionStyleForVC:self withTitle:NSLocalizedString(@"Sound", @"") customBackButton:NO];
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    self.tableView.backgroundColor = UIColor.clearColor;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								- (void)viewDidAppear:(BOOL)animated
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    [super viewDidAppear:animated];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [self updateTableContents];
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								- (void)updateNavigationItems
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    UIBarButtonItem *cancelItem =
							 | 
						||
| 
								 | 
							
								        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
							 | 
						||
| 
								 | 
							
								                                                      target:self
							 | 
						||
| 
								 | 
							
								                                                      action:@selector(cancelWasPressed:)
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								                                     accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, @"cancel")];
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    cancelItem.tintColor = LKColors.text;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    self.navigationItem.leftBarButtonItem = cancelItem;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (self.isDirty) {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        UIBarButtonItem *saveItem =
							 | 
						||
| 
								 | 
							
								            [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
							 | 
						||
| 
								 | 
							
								                                                          target:self
							 | 
						||
| 
								 | 
							
								                                                          action:@selector(saveWasPressed:)
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								                                         accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, @"save")];
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        self.navigationItem.rightBarButtonItem = saveItem;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    } else {
							 | 
						||
| 
								 | 
							
								        self.navigationItem.rightBarButtonItem = nil;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma mark - Table Contents
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								- (void)updateTableContents
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    OWSTableContents *contents = [OWSTableContents new];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    __weak OWSSoundSettingsViewController *weakSelf = self;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    OWSTableSection *soundsSection = [OWSTableSection new];
							 | 
						||
| 
								 | 
							
								    soundsSection.headerTitle = NSLocalizedString(
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        @"NOTIFICATIONS_SECTION_SOUNDS", @"Label for settings UI that allows user to change the notification sound.");
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    NSArray<NSNumber *> *allSounds = [OWSSounds allNotificationSounds];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    for (NSNumber *nsValue in allSounds) {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        OWSSound sound = (OWSSound)nsValue.intValue;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        OWSTableItem *item;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								        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];
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        if (sound == self.currentSound) {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								            item = [OWSTableItem
							 | 
						||
| 
								 | 
							
								                  checkmarkItemWithText:soundLabelText
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								                accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, [OWSSounds displayNameForSound:sound])
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								                            actionBlock:^{
							 | 
						||
| 
								 | 
							
								                                [weakSelf soundWasSelected:sound];
							 | 
						||
| 
								 | 
							
								                            }];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        } else {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								            item = [OWSTableItem
							 | 
						||
| 
								 | 
							
								                     actionItemWithText:soundLabelText
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								                accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, [OWSSounds displayNameForSound:sound])
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								                            actionBlock:^{
							 | 
						||
| 
								 | 
							
								                                [weakSelf soundWasSelected:sound];
							 | 
						||
| 
								 | 
							
								                            }];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        [soundsSection addItem:item];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    [contents addSection:soundsSection];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    self.contents = contents;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma mark - Events
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								- (void)soundWasSelected:(OWSSound)sound
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [self.audioPlayer stop];
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    self.audioPlayer = [OWSSounds audioPlayerForSound:sound audioBehavior:OWSAudioBehavior_Playback];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    // Suppress looping in this view.
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.audioPlayer.isLooping = NO;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    [self.audioPlayer play];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    if (self.currentSound == sound) {
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        return;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.currentSound = sound;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.isDirty = YES;
							 | 
						||
| 
								 | 
							
								    [self updateTableContents];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [self updateNavigationItems];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								- (void)cancelWasPressed:(id)sender
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    // TODO: Add "discard changes?" alert.
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [self.audioPlayer stop];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [self.navigationController popViewControllerAnimated:YES];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								- (void)saveWasPressed:(id)sender
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    if (self.thread) {
							 | 
						||
| 
								 | 
							
								        [OWSSounds setNotificationSound:self.currentSound forThread:self.thread];
							 | 
						||
| 
								 | 
							
								    } else {
							 | 
						||
| 
								 | 
							
								        [OWSSounds setGlobalNotificationSound:self.currentSound];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [self.audioPlayer stop];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [self.navigationController popViewControllerAnimated:YES];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								@end
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								NS_ASSUME_NONNULL_END
							 |