session-ios/SignalServiceKit/src/Contacts/OWSDisappearingMessagesConf...

116 lines
2.8 KiB
Mathematica
Raw Normal View History

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "OWSDisappearingMessagesConfiguration.h"
#import "NSDate+OWS.h"
2018-03-21 22:11:38 +01:00
#import "NSString+SSK.h"
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
{
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;
_originalDictionaryValue = [NSDictionary new];
_newRecord = YES;
return self;
}
+ (instancetype)fetchOrCreateDefaultWithThreadId:(NSString *)threadId
{
OWSDisappearingMessagesConfiguration *savedConfiguration = [self fetchObjectWithUniqueID:threadId];
if (savedConfiguration) {
return savedConfiguration;
} else {
return [[self alloc] initDefaultWithThreadId:threadId];
}
}
+ (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];
}
#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