|
|
|
@ -5,26 +5,90 @@
|
|
|
|
|
#import "OWSDevice.h"
|
|
|
|
|
#import "NSDate+OWS.h"
|
|
|
|
|
#import "OWSError.h"
|
|
|
|
|
#import "TSStorageManager.h"
|
|
|
|
|
#import "YapDatabaseConnection.h"
|
|
|
|
|
#import "YapDatabaseTransaction.h"
|
|
|
|
|
#import <Mantle/MTLValueTransformer.h>
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
static MTLValueTransformer *_millisecondTimestampToDateTransformer;
|
|
|
|
|
uint32_t const OWSDevicePrimaryDeviceId = 1;
|
|
|
|
|
NSString *const kTSStorageManager_OWSDeviceCollection = @"kTSStorageManager_OWSDeviceCollection";
|
|
|
|
|
NSString *const kTSStorageManager_MayHaveLinkedDevices = @"kTSStorageManager_MayHaveLinkedDevices";
|
|
|
|
|
|
|
|
|
|
@interface OWSDeviceManager ()
|
|
|
|
|
|
|
|
|
|
@property (atomic, nullable) NSNumber *mayHaveLinkedDevicesCached;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
@implementation OWSDeviceManager
|
|
|
|
|
|
|
|
|
|
+ (instancetype)sharedManager
|
|
|
|
|
{
|
|
|
|
|
static OWSDeviceManager *instance = nil;
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
instance = [[self alloc] initDefault];
|
|
|
|
|
});
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)initDefault
|
|
|
|
|
{
|
|
|
|
|
return [super init];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)mayHaveLinkedDevices:(YapDatabaseConnection *)dbConnection
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(dbConnection);
|
|
|
|
|
|
|
|
|
|
@synchronized(self)
|
|
|
|
|
{
|
|
|
|
|
if (!self.mayHaveLinkedDevicesCached) {
|
|
|
|
|
self.mayHaveLinkedDevicesCached = @([dbConnection boolForKey:kTSStorageManager_MayHaveLinkedDevices
|
|
|
|
|
inCollection:kTSStorageManager_OWSDeviceCollection
|
|
|
|
|
defaultValue:YES]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [self.mayHaveLinkedDevicesCached boolValue];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setMayHaveLinkedDevices:(BOOL)value dbConnection:(YapDatabaseConnection *)dbConnection
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(dbConnection);
|
|
|
|
|
|
|
|
|
|
@synchronized(self)
|
|
|
|
|
{
|
|
|
|
|
self.mayHaveLinkedDevicesCached = @(value);
|
|
|
|
|
|
|
|
|
|
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
|
[transaction setObject:@(value)
|
|
|
|
|
forKey:kTSStorageManager_MayHaveLinkedDevices
|
|
|
|
|
inCollection:kTSStorageManager_OWSDeviceCollection];
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
@interface OWSDevice ()
|
|
|
|
|
|
|
|
|
|
@property NSString *name;
|
|
|
|
|
@property NSDate *createdAt;
|
|
|
|
|
@property NSDate *lastSeenAt;
|
|
|
|
|
@property (nonatomic) NSInteger deviceId;
|
|
|
|
|
@property (nonatomic, nullable) NSString *name;
|
|
|
|
|
@property (nonatomic) NSDate *createdAt;
|
|
|
|
|
@property (nonatomic) NSDate *lastSeenAt;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation OWSDevice
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
@synthesize name = _name;
|
|
|
|
|
@implementation OWSDevice
|
|
|
|
|
|
|
|
|
|
+ (instancetype)deviceFromJSONDictionary:(NSDictionary *)deviceAttributes error:(NSError **)error
|
|
|
|
|
{
|
|
|
|
@ -76,9 +140,10 @@ uint32_t const OWSDevicePrimaryDeviceId = 1;
|
|
|
|
|
|
|
|
|
|
+ (MTLValueTransformer *)millisecondTimestampToDateTransformer
|
|
|
|
|
{
|
|
|
|
|
if (!_millisecondTimestampToDateTransformer) {
|
|
|
|
|
_millisecondTimestampToDateTransformer =
|
|
|
|
|
[MTLValueTransformer transformerUsingForwardBlock:^id(id value, BOOL *success, NSError **error) {
|
|
|
|
|
static MTLValueTransformer *instance = nil;
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
instance = [MTLValueTransformer transformerUsingForwardBlock:^id(id value, BOOL *success, NSError **error) {
|
|
|
|
|
if ([value isKindOfClass:[NSNumber class]]) {
|
|
|
|
|
NSNumber *number = (NSNumber *)value;
|
|
|
|
|
NSDate *result = [NSDate ows_dateWithMillisecondsSince1970:[number longLongValue]];
|
|
|
|
@ -106,8 +171,8 @@ uint32_t const OWSDevicePrimaryDeviceId = 1;
|
|
|
|
|
*success = NO;
|
|
|
|
|
return nil;
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
return _millisecondTimestampToDateTransformer;
|
|
|
|
|
});
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (uint32_t)currentDeviceId
|
|
|
|
|