2017-07-21 21:08:37 +02:00
|
|
|
//
|
2018-01-20 17:27:32 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-07-21 21:08:37 +02:00
|
|
|
//
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
#import "OWSDisappearingMessagesConfiguration.h"
|
2017-07-21 21:08:37 +02:00
|
|
|
#import "NSDate+OWS.h"
|
2018-03-21 22:11:38 +01:00
|
|
|
#import "NSString+SSK.h"
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
@interface OWSDisappearingMessagesConfiguration ()
|
|
|
|
|
|
|
|
// Transient record lifecycle attributes.
|
|
|
|
@property (atomic) NSDictionary *originalDictionaryValue;
|
|
|
|
@property (atomic, getter=isNewRecord) BOOL newRecord;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation OWSDisappearingMessagesConfiguration
|
|
|
|
|
|
|
|
- (instancetype)initDefaultWithThreadId:(NSString *)threadId
|
|
|
|
{
|
|
|
|
return [self initWithThreadId:threadId
|
|
|
|
enabled:NO
|
|
|
|
durationSeconds:OWSDisappearingMessagesConfigurationDefaultExpirationDuration];
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:15:48 +01:00
|
|
|
- (nullable instancetype)initWithCoder:(NSCoder *)coder
|
2016-10-05 17:42:44 +02:00
|
|
|
{
|
|
|
|
self = [super initWithCoder:coder];
|
|
|
|
|
|
|
|
_originalDictionaryValue = [self dictionaryValue];
|
|
|
|
_newRecord = NO;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithThreadId:(NSString *)threadId enabled:(BOOL)isEnabled durationSeconds:(uint32_t)seconds
|
|
|
|
{
|
|
|
|
self = [super initWithUniqueId:threadId];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_enabled = isEnabled;
|
|
|
|
_durationSeconds = seconds;
|
|
|
|
_newRecord = YES;
|
2018-05-08 16:18:11 +02:00
|
|
|
_originalDictionaryValue = self.dictionaryValue;
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:24:06 +02:00
|
|
|
+ (instancetype)fetchOrCreateDefaultWithThreadId:(NSString *)threadId
|
2018-04-17 00:38:29 +02:00
|
|
|
transaction:(YapDatabaseReadTransaction *)transaction
|
2016-10-24 21:24:06 +02:00
|
|
|
{
|
2018-04-17 00:38:29 +02:00
|
|
|
OWSDisappearingMessagesConfiguration *savedConfiguration =
|
|
|
|
[self fetchObjectWithUniqueID:threadId transaction:transaction];
|
2016-10-24 21:24:06 +02:00
|
|
|
if (savedConfiguration) {
|
|
|
|
return savedConfiguration;
|
|
|
|
} else {
|
|
|
|
return [[self alloc] initDefaultWithThreadId:threadId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
+ (NSArray<NSNumber *> *)validDurationsSeconds
|
|
|
|
{
|
|
|
|
return @[ @(5),
|
|
|
|
@(10),
|
|
|
|
@(30),
|
|
|
|
@(60),
|
|
|
|
@(300),
|
|
|
|
@(1800),
|
|
|
|
@(3600),
|
|
|
|
@(21600),
|
|
|
|
@(43200),
|
|
|
|
@(86400),
|
|
|
|
@(604800) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger)durationIndex
|
|
|
|
{
|
|
|
|
return [[self.class validDurationsSeconds] indexOfObject:@(self.durationSeconds)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)durationString
|
|
|
|
{
|
2018-03-21 22:11:38 +01:00
|
|
|
return [NSString formatDurationSeconds:self.durationSeconds useShortFormat:NO];
|
2016-10-05 17:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Dirty Tracking
|
|
|
|
|
|
|
|
+ (MTLPropertyStorage)storageBehaviorForPropertyWithKey:(NSString *)propertyKey
|
|
|
|
{
|
|
|
|
// Don't persist transient properties
|
|
|
|
if ([propertyKey isEqualToString:@"originalDictionaryValue"]
|
|
|
|
||[propertyKey isEqualToString:@"newRecord"]) {
|
|
|
|
return MTLPropertyStorageNone;
|
|
|
|
} else {
|
|
|
|
return [super storageBehaviorForPropertyWithKey:propertyKey];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)dictionaryValueDidChange
|
|
|
|
{
|
|
|
|
return ![self.originalDictionaryValue isEqual:[self dictionaryValue]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
|
|
{
|
|
|
|
[super saveWithTransaction:transaction];
|
|
|
|
self.originalDictionaryValue = [self dictionaryValue];
|
|
|
|
self.newRecord = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|