diff --git a/Signal/src/ViewControllers/NotificationSoundsViewController.h b/Signal/src/ViewControllers/NotificationSoundsViewController.h index ceabfc320..04d6170d9 100644 --- a/Signal/src/ViewControllers/NotificationSoundsViewController.h +++ b/Signal/src/ViewControllers/NotificationSoundsViewController.h @@ -4,6 +4,12 @@ #import "OWSTableViewController.h" +@class TSThread; + @interface NotificationSoundsViewController : OWSTableViewController +// This property is optional. If it is not set, we are +// editing the global notification sound. +@property (nonatomic, nullable) TSThread *thread; + @end diff --git a/Signal/src/ViewControllers/NotificationSoundsViewController.m b/Signal/src/ViewControllers/NotificationSoundsViewController.m index 61d47e45d..f113a2f73 100644 --- a/Signal/src/ViewControllers/NotificationSoundsViewController.m +++ b/Signal/src/ViewControllers/NotificationSoundsViewController.m @@ -3,15 +3,13 @@ // #import "NotificationSoundsViewController.h" -#import #import -#import @interface NotificationSoundsViewController () @property (nonatomic) BOOL isDirty; -@property (nonatomic) NotificationSound globalNotificationSound; +@property (nonatomic) NotificationSound currentNotificationSound; @end @@ -26,8 +24,8 @@ [self setTitle:NSLocalizedString(@"NOTIFICATIONS_ITEM_SOUND", @"Label for settings view that allows user to change the notification sound.")]; - OWSPreferences *preferences = [Environment preferences]; - self.globalNotificationSound = preferences.globalNotificationSound; + self.currentNotificationSound = (self.thread ? [NotificationSounds notificationSoundForThread:self.thread] + : [NotificationSounds globalNotificationSound]); [self updateTableContents]; [self updateNavigationItems]; @@ -69,7 +67,7 @@ for (NSNumber *nsNotificationSound in [NotificationSounds allNotificationSounds]) { NotificationSound notificationSound = (NotificationSound)nsNotificationSound.intValue; OWSTableItem *item; - if (notificationSound == self.globalNotificationSound) { + if (notificationSound == self.currentNotificationSound) { item = [OWSTableItem checkmarkItemWithText:[NotificationSounds displayNameForNotificationSound:notificationSound] actionBlock:^{ @@ -96,11 +94,11 @@ { [NotificationSounds playNotificationSound:notificationSound]; - if (self.globalNotificationSound == notificationSound) { + if (self.currentNotificationSound == notificationSound) { return; } - self.globalNotificationSound = notificationSound; + self.currentNotificationSound = notificationSound; self.isDirty = YES; [self updateTableContents]; [self updateNavigationItems]; @@ -114,8 +112,11 @@ - (void)saveWasPressed:(id)sender { - OWSPreferences *preferences = [Environment preferences]; - preferences.globalNotificationSound = self.globalNotificationSound; + if (self.thread) { + [NotificationSounds setNotificationSound:self.currentNotificationSound forThread:self.thread]; + } else { + [NotificationSounds setGlobalNotificationSound:self.currentNotificationSound]; + } [self.navigationController popViewControllerAnimated:YES]; } diff --git a/Signal/src/environment/NotificationsManager.m b/Signal/src/environment/NotificationsManager.m index 3feb8df73..cda67308c 100644 --- a/Signal/src/environment/NotificationsManager.m +++ b/Signal/src/environment/NotificationsManager.m @@ -100,7 +100,7 @@ }; if ([self shouldPlaySoundForNotification]) { - NotificationSound notificationSound = [Environment preferences].globalNotificationSound; + NotificationSound notificationSound = [NotificationSounds notificationSoundForThread:thread]; notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound]; } @@ -140,7 +140,7 @@ Signal_Thread_UserInfo_Key : thread.uniqueId }; if ([self shouldPlaySoundForNotification]) { - NotificationSound notificationSound = [Environment preferences].globalNotificationSound; + NotificationSound notificationSound = [NotificationSounds notificationSoundForThread:thread]; notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound]; } @@ -181,7 +181,7 @@ Signal_Thread_UserInfo_Key : thread.uniqueId }; if ([self shouldPlaySoundForNotification]) { - NotificationSound notificationSound = [Environment preferences].globalNotificationSound; + NotificationSound notificationSound = [NotificationSounds notificationSoundForThread:thread]; notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound]; } @@ -226,7 +226,7 @@ UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId }; if (shouldPlaySound) { - NotificationSound notificationSound = [Environment preferences].globalNotificationSound; + NotificationSound notificationSound = [NotificationSounds notificationSoundForThread:thread]; notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound]; } @@ -247,7 +247,7 @@ [[PushManager sharedManager] presentNotification:notification checkForCancel:NO]; } else { if (shouldPlaySound && [Environment.preferences soundInForeground]) { - NotificationSound notificationSound = [Environment preferences].globalNotificationSound; + NotificationSound notificationSound = [NotificationSounds notificationSoundForThread:thread]; [NotificationSounds playNotificationSound:notificationSound]; } } @@ -289,7 +289,7 @@ if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive && messageText) { UILocalNotification *notification = [[UILocalNotification alloc] init]; if (shouldPlaySound) { - NotificationSound notificationSound = [Environment preferences].globalNotificationSound; + NotificationSound notificationSound = [NotificationSounds notificationSoundForThread:thread]; notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound]; } @@ -352,7 +352,7 @@ [[PushManager sharedManager] presentNotification:notification checkForCancel:YES]; } else { if (shouldPlaySound && [Environment.preferences soundInForeground]) { - NotificationSound notificationSound = [Environment preferences].globalNotificationSound; + NotificationSound notificationSound = [NotificationSounds notificationSoundForThread:thread]; [NotificationSounds playNotificationSound:notificationSound]; } } diff --git a/SignalMessaging/environment/NotificationSounds.h b/SignalMessaging/environment/NotificationSounds.h index fcdd3e84f..422e60050 100644 --- a/SignalMessaging/environment/NotificationSounds.h +++ b/SignalMessaging/environment/NotificationSounds.h @@ -3,7 +3,8 @@ // typedef NS_ENUM(NSUInteger, NotificationSound) { - NotificationSound_Aurora = 0, + NotificationSound_Default = 0, + NotificationSound_Aurora, NotificationSound_Bamboo, NotificationSound_Chord, NotificationSound_Circles, @@ -15,11 +16,12 @@ typedef NS_ENUM(NSUInteger, NotificationSound) { NotificationSound_Popcorn, NotificationSound_Pulse, NotificationSound_Synth, - NotificationSound_Default = NotificationSound_Note, }; NS_ASSUME_NONNULL_BEGIN +@class TSThread; + @interface NotificationSounds : NSObject - (instancetype)init NS_UNAVAILABLE; @@ -32,6 +34,12 @@ NS_ASSUME_NONNULL_BEGIN + (void)playNotificationSound:(NotificationSound)notificationSound; ++ (NotificationSound)globalNotificationSound; ++ (void)setGlobalNotificationSound:(NotificationSound)notificationSound; + ++ (NotificationSound)notificationSoundForThread:(TSThread *)thread; ++ (void)setNotificationSound:(NotificationSound)notificationSound forThread:(TSThread *)thread; + @end NS_ASSUME_NONNULL_END diff --git a/SignalMessaging/environment/NotificationSounds.m b/SignalMessaging/environment/NotificationSounds.m index d3009ac30..5c90296b8 100644 --- a/SignalMessaging/environment/NotificationSounds.m +++ b/SignalMessaging/environment/NotificationSounds.m @@ -4,9 +4,17 @@ #import "NotificationSounds.h" #import +#import +#import +#import + +NSString *const kNotificationSoundsStorageNotificationCollection = @"kNotificationSoundsStorageNotificationCollection"; +NSString *const kNotificationSoundsStorageGlobalNotificationKey = @"kNotificationSoundsStorageGlobalNotificationKey"; @interface NotificationSounds () +@property (nonatomic, readonly) YapDatabaseConnection *dbConnection; + @property (nonatomic) NSMutableDictionary *systemSoundIDMap; @end @@ -26,6 +34,13 @@ } - (instancetype)initDefault +{ + TSStorageManager *storageManager = [TSStorageManager sharedManager]; + + return [self initWithStorageManager:storageManager]; +} + +- (instancetype)initWithStorageManager:(TSStorageManager *)storageManager { self = [super init]; @@ -33,6 +48,10 @@ return self; } + OWSAssert(storageManager); + + _dbConnection = storageManager.newDatabaseConnection; + OWSSingletonAssert(); return self; @@ -76,6 +95,7 @@ return @"Input"; case NotificationSound_Keys: return @"Keys"; + case NotificationSound_Default: case NotificationSound_Note: return @"Note"; case NotificationSound_Popcorn: @@ -107,6 +127,7 @@ return @"input.m4r"; case NotificationSound_Keys: return @"keys.m4r"; + case NotificationSound_Default: case NotificationSound_Note: return @"note.m4r"; case NotificationSound_Popcorn: @@ -161,4 +182,43 @@ AudioServicesPlayAlertSound(systemSoundID); } ++ (NotificationSound)defaultNotificationSound +{ + return NotificationSound_Note; +} + ++ (NotificationSound)globalNotificationSound +{ + NotificationSounds *notificationSounds = NotificationSounds.sharedManager; + NSNumber *_Nullable value = + [notificationSounds.dbConnection objectForKey:kNotificationSoundsStorageGlobalNotificationKey + inCollection:kNotificationSoundsStorageNotificationCollection]; + return (value ? (NotificationSound)value.intValue : [self defaultNotificationSound]); +} + ++ (void)setGlobalNotificationSound:(NotificationSound)notificationSound +{ + NotificationSounds *notificationSounds = NotificationSounds.sharedManager; + [notificationSounds.dbConnection setObject:@(notificationSound) + forKey:kNotificationSoundsStorageGlobalNotificationKey + inCollection:kNotificationSoundsStorageNotificationCollection]; +} + ++ (NotificationSound)notificationSoundForThread:(TSThread *)thread +{ + NotificationSounds *notificationSounds = NotificationSounds.sharedManager; + NSNumber *_Nullable value = + [notificationSounds.dbConnection objectForKey:kNotificationSoundsStorageGlobalNotificationKey + inCollection:kNotificationSoundsStorageNotificationCollection]; + return (value ? (NotificationSound)value.intValue : [self globalNotificationSound]); +} + ++ (void)setNotificationSound:(NotificationSound)notificationSound forThread:(TSThread *)thread +{ + NotificationSounds *notificationSounds = NotificationSounds.sharedManager; + [notificationSounds.dbConnection setObject:@(notificationSound) + forKey:thread.uniqueId + inCollection:kNotificationSoundsStorageNotificationCollection]; +} + @end diff --git a/SignalMessaging/utils/OWSPreferences.h b/SignalMessaging/utils/OWSPreferences.h index bee1339e9..4000ae1a9 100644 --- a/SignalMessaging/utils/OWSPreferences.h +++ b/SignalMessaging/utils/OWSPreferences.h @@ -2,8 +2,6 @@ // Copyright (c) 2018 Open Whisper Systems. All rights reserved. // -#import "NotificationSounds.h" - NS_ASSUME_NONNULL_BEGIN /** @@ -57,9 +55,6 @@ extern NSString *const OWSPreferencesKeyEnableDebugLog; - (void)setIOSUpgradeNagDate:(NSDate *)value; - (nullable NSDate *)iOSUpgradeNagDate; -- (NotificationSound)globalNotificationSound; -- (void)setGlobalNotificationSound:(NotificationSound)notificationSound; - #pragma mark - Calling #pragma mark Callkit diff --git a/SignalMessaging/utils/OWSPreferences.m b/SignalMessaging/utils/OWSPreferences.m index 050d46491..ea084b5fb 100644 --- a/SignalMessaging/utils/OWSPreferences.m +++ b/SignalMessaging/utils/OWSPreferences.m @@ -26,7 +26,6 @@ NSString *const OWSPreferencesKeyCallsHideIPAddress = @"CallsHideIPAddress"; NSString *const OWSPreferencesKeyHasDeclinedNoContactsView = @"hasDeclinedNoContactsView"; NSString *const OWSPreferencesKeyIOSUpgradeNagDate = @"iOSUpgradeNagDate"; NSString *const OWSPreferencesKey_IsReadyForAppExtensions = @"isReadyForAppExtensions_5"; -NSString *const OWSPreferencesKey_GlobalNotificationSound = @"GlobalNotificationSound"; @implementation OWSPreferences @@ -170,17 +169,6 @@ NSString *const OWSPreferencesKey_GlobalNotificationSound = @"GlobalNotification return [self tryGetValueForKey:OWSPreferencesKeyIOSUpgradeNagDate]; } -- (NotificationSound)globalNotificationSound -{ - NSNumber *value = [self tryGetValueForKey:OWSPreferencesKey_GlobalNotificationSound]; - return value ? (NotificationSound)[value intValue] : NotificationSound_Default; -} - -- (void)setGlobalNotificationSound:(NotificationSound)notificationSound -{ - [self setValueForKey:OWSPreferencesKey_GlobalNotificationSound toValue:@(notificationSound)]; -} - #pragma mark - Calling #pragma mark CallKit