mirror of https://github.com/oxen-io/session-ios
Custom notification sounds.
parent
3d892abc46
commit
5c3f6b0ee5
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,9 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import "OWSTableViewController.h"
|
||||
|
||||
@interface NotificationSoundsViewController : OWSTableViewController
|
||||
|
||||
@end
|
@ -0,0 +1,147 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NotificationSoundsViewController.h"
|
||||
#import <SignalMessaging/Environment.h>
|
||||
#import <SignalMessaging/NotificationSounds.h>
|
||||
#import <SignalMessaging/OWSPreferences.h>
|
||||
|
||||
@interface NotificationSoundsViewController ()
|
||||
|
||||
@property (nonatomic) BOOL isDirty;
|
||||
|
||||
@property (nonatomic) NotificationSound globalNotificationSound;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@implementation NotificationSoundsViewController
|
||||
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
[super viewDidLoad];
|
||||
|
||||
[self setTitle:NSLocalizedString(@"SETTINGS_NOTIFICATION_SOUND", nil)];
|
||||
|
||||
OWSPreferences *preferences = [Environment preferences];
|
||||
self.globalNotificationSound = preferences.globalNotificationSound;
|
||||
|
||||
[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(
|
||||
@"NOTIFICATIONS_SECTION_SOUNDS", @"Label for settings UI allows user to change the notification sound.");
|
||||
for (NSNumber *nsNotificationSound in [NotificationSounds allNotificationSounds]) {
|
||||
NotificationSound notificationSound = (NotificationSound)nsNotificationSound.intValue;
|
||||
// TODO: No disclosure, show checkmark.
|
||||
[soundsSection
|
||||
addItem:[OWSTableItem
|
||||
disclosureItemWithText:[NotificationSounds displayNameForNotificationSound:notificationSound]
|
||||
actionBlock:^{
|
||||
[weakSelf notificationSoundWasSelected:notificationSound];
|
||||
}]];
|
||||
}
|
||||
|
||||
[contents addSection:soundsSection];
|
||||
|
||||
// OWSTableSection *backgroundSection = [OWSTableSection new];
|
||||
// backgroundSection.headerTitle = NSLocalizedString(@"NOTIFICATIONS_SECTION_BACKGROUND", nil);
|
||||
// [backgroundSection addItem:[OWSTableItem itemWithCustomCellBlock:^{
|
||||
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
|
||||
// reuseIdentifier:@"UITableViewCellStyleValue1"];
|
||||
//
|
||||
// NotificationType notifType = [prefs notificationPreviewType];
|
||||
// NSString *detailString = [prefs nameForNotificationPreviewType:notifType];
|
||||
// cell.textLabel.text = NSLocalizedString(@"NOTIFICATIONS_SHOW", nil);
|
||||
// cell.detailTextLabel.text = detailString;
|
||||
// [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
|
||||
//
|
||||
// return cell;
|
||||
// }
|
||||
// actionBlock:^{
|
||||
// NotificationSettingsOptionsViewController *vc =
|
||||
// [NotificationSettingsOptionsViewController new];
|
||||
// [weakSelf.navigationController pushViewController:vc
|
||||
// animated:YES];
|
||||
// }]];
|
||||
// [contents addSection:backgroundSection];
|
||||
//
|
||||
// OWSTableSection *inAppSection = [OWSTableSection new];
|
||||
// inAppSection.headerTitle = NSLocalizedString(@"NOTIFICATIONS_SECTION_INAPP", nil);
|
||||
// [inAppSection addItem:[OWSTableItem switchItemWithText:NSLocalizedString(@"NOTIFICATIONS_SOUND", nil)
|
||||
// isOn:[prefs soundInForeground]
|
||||
// target:weakSelf
|
||||
// selector:@selector(didToggleSoundNotificationsSwitch:)]];
|
||||
// [contents addSection:inAppSection];
|
||||
|
||||
self.contents = contents;
|
||||
}
|
||||
|
||||
#pragma mark - Events
|
||||
|
||||
- (void)notificationSoundWasSelected:(NotificationSound)notificationSound
|
||||
{
|
||||
[NotificationSounds playNotificationSound:notificationSound];
|
||||
|
||||
if (self.globalNotificationSound == notificationSound) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.globalNotificationSound = notificationSound;
|
||||
self.isDirty = YES;
|
||||
[self updateTableContents];
|
||||
}
|
||||
|
||||
//- (void)didToggleSoundNotificationsSwitch:(UISwitch *)sender {
|
||||
// [Environment.preferences setSoundInForeground:sender.on];
|
||||
//}
|
||||
|
||||
- (void)cancelWasPressed:(id)sender
|
||||
{
|
||||
// TODO: Add "discard changes?" alert.
|
||||
[self dismissViewControllerAnimated:YES completion:nil];
|
||||
}
|
||||
|
||||
- (void)saveWasPressed:(id)sender
|
||||
{
|
||||
OWSPreferences *preferences = [Environment preferences];
|
||||
preferences.globalNotificationSound = self.globalNotificationSound;
|
||||
|
||||
[self dismissViewControllerAnimated:YES completion:nil];
|
||||
}
|
||||
|
||||
@end
|
@ -0,0 +1,24 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
typedef NS_ENUM(NSUInteger, NotificationSound) {
|
||||
NotificationSound_Aurora = 0,
|
||||
NotificationSound_Default = NotificationSound_Aurora
|
||||
};
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NotificationSounds : NSObject
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
+ (NSArray<NSNumber *> *)allNotificationSounds;
|
||||
|
||||
+ (NSString *)displayNameForNotificationSound:(NotificationSound)notificationSound;
|
||||
|
||||
+ (void)playNotificationSound:(NotificationSound)notificationSound;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,109 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NotificationSounds.h"
|
||||
#import <AudioToolbox/AudioServices.h>
|
||||
|
||||
@interface NotificationSounds ()
|
||||
|
||||
@property (nonatomic) NSMutableDictionary<NSNumber *, NSNumber *> *systemSoundIDMap;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@implementation NotificationSounds
|
||||
|
||||
+ (instancetype)sharedManager
|
||||
{
|
||||
static NotificationSounds *instance = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
instance = [[self alloc] initDefault];
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
|
||||
- (instancetype)initDefault
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
if (!self) {
|
||||
return self;
|
||||
}
|
||||
|
||||
OWSSingletonAssert();
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
+ (NSArray<NSNumber *> *)allNotificationSounds
|
||||
{
|
||||
return @[
|
||||
@(NotificationSound_Aurora),
|
||||
];
|
||||
}
|
||||
|
||||
+ (NSString *)displayNameForNotificationSound:(NotificationSound)notificationSound
|
||||
{
|
||||
// TODO: Should we localize these sound names?
|
||||
switch (notificationSound) {
|
||||
case NotificationSound_Aurora:
|
||||
return @"Aurora";
|
||||
}
|
||||
}
|
||||
|
||||
- (NSURL *)soundURLForNotificationSound:(NotificationSound)notificationSound
|
||||
{
|
||||
NSString *bundlePath = [NSBundle mainBundle].bundlePath;
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
for (NSString *filename in [fileManager contentsOfDirectoryAtPath:bundlePath error:nil]) {
|
||||
DDLogInfo(@"%@ filename: %@", self.logTag, filename);
|
||||
}
|
||||
[DDLog flushLog];
|
||||
|
||||
NSURL *_Nullable url;
|
||||
switch (notificationSound) {
|
||||
case NotificationSound_Aurora:
|
||||
url = [[NSBundle mainBundle] URLForResource:@"aurora" withExtension:@"m4r"];
|
||||
break;
|
||||
}
|
||||
OWSAssert(url);
|
||||
return url;
|
||||
}
|
||||
|
||||
+ (void)playNotificationSound:(NotificationSound)notificationSound
|
||||
{
|
||||
[self.sharedManager playNotificationSound:notificationSound];
|
||||
}
|
||||
|
||||
- (SystemSoundID)systemSoundIDForNotificationSound:(NotificationSound)notificationSound
|
||||
{
|
||||
@synchronized(self)
|
||||
{
|
||||
if (!self.systemSoundIDMap) {
|
||||
self.systemSoundIDMap = [NSMutableDictionary new];
|
||||
}
|
||||
NSNumber *_Nullable systemSoundID = self.systemSoundIDMap[@(notificationSound)];
|
||||
if (!systemSoundID) {
|
||||
NSURL *soundURL = [self soundURLForNotificationSound:notificationSound];
|
||||
SystemSoundID newSystemSoundID;
|
||||
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &newSystemSoundID);
|
||||
if (error) {
|
||||
OWSFail(@"%@ could not load sound.", self.logTag);
|
||||
}
|
||||
systemSoundID = @(newSystemSoundID);
|
||||
self.systemSoundIDMap[@(notificationSound)] = systemSoundID;
|
||||
}
|
||||
return (SystemSoundID)systemSoundID.unsignedIntegerValue;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)playNotificationSound:(NotificationSound)notificationSound
|
||||
{
|
||||
SystemSoundID systemSoundID = [self systemSoundIDForNotificationSound:notificationSound];
|
||||
AudioServicesPlayAlertSound(systemSoundID);
|
||||
}
|
||||
|
||||
@end
|
Loading…
Reference in New Issue