Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device details refactoring #1441

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/ARTDeviceDetails.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (strong, nonatomic) NSString *platform;
@property (strong, nonatomic) NSString *formFactor;
@property (strong, nonatomic) NSDictionary<NSString *, NSString *> *metadata;
@property (strong, nonatomic) ARTDevicePushDetails *push;
@property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *pushRecipient;

- (instancetype)init;
- (instancetype)initWithId:(ARTDeviceId *)deviceId;
Expand Down
4 changes: 2 additions & 2 deletions Source/ARTDeviceDetails.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ @implementation ARTDeviceDetails

- (instancetype)init {
if (self = [super init]) {
_push = [[ARTDevicePushDetails alloc] init];
_metadata = [[NSDictionary alloc] init];
_pushRecipient = [[NSMutableDictionary alloc] init];
}
return self;
}
Expand All @@ -26,7 +26,7 @@ - (id)copyWithZone:(NSZone *)zone {
device.platform = [self.platform copy];
device.formFactor = [self.formFactor copy];
device.metadata = [self.metadata copy];
device.push = [self.push copy];
device.pushRecipient = [self.pushRecipient copy];

return device;
}
Expand Down
13 changes: 10 additions & 3 deletions Source/ARTDevicePushDetails.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
#import <Foundation/Foundation.h>

@class ARTErrorInfo;
@class ARTDeviceDetails;

NS_ASSUME_NONNULL_BEGIN

@interface ARTDevicePushDetails : NSObject
@interface ARTDevicePushStatus : NSObject

@property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *recipient;
@property (strong, nullable, nonatomic) NSString *state;
@property (strong, nonatomic) NSString *state;
@property (strong, nullable, nonatomic) ARTErrorInfo *errorReason;

- (instancetype)init;

@end

@interface ARTDeviceDetailsResponse : NSObject

@property (nonatomic) ARTDeviceDetails *deviceDetails;
@property (nonatomic, nullable) ARTDevicePushStatus *pushStatus;

@end

NS_ASSUME_NONNULL_END
22 changes: 15 additions & 7 deletions Source/ARTDevicePushDetails.m
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
#import "ARTDevicePushDetails.h"
#import "ARTPush.h"

@implementation ARTDevicePushDetails
@implementation ARTDevicePushStatus

- (instancetype)init {
if (self = [super init]) {
_recipient = [[NSMutableDictionary alloc] init];
//
}
return self;
}

- (id)copyWithZone:(NSZone *)zone {
ARTDevicePushDetails *push = [[[self class] allocWithZone:zone] init];

push.recipient = [self.recipient copy];
ARTDevicePushStatus *push = [[[self class] allocWithZone:zone] init];
push.state = self.state;
push.errorReason = [self.errorReason copy];

return push;
}

- (NSString *)description {
return [NSString stringWithFormat:@"%@ - \n\t recipient: %@; \n\t state: %@; \n\t errorReason: %@;", [super description], self.recipient, self.state, self.errorReason];
return [NSString stringWithFormat:@"%@ - \n\t state: %@; \n\t errorReason: %@;", [super description], self.state, self.errorReason];
}

@end

@implementation ARTDeviceDetailsResponse

- (instancetype)init {
if (self = [super init]) {
//
}
return self;
}

@end
8 changes: 2 additions & 6 deletions Source/ARTEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@class ARTTokenRequest;
@class ARTDeviceDetails;
@class ARTDeviceIdentityTokenDetails;
@class ARTDevicePushDetails;
@class ARTDeviceDetailsResponse;
@class ARTPushChannelSubscription;

@protocol ARTPushRecipient;
Expand Down Expand Up @@ -63,14 +63,10 @@ NS_ASSUME_NONNULL_BEGIN

// DeviceDetails
- (nullable NSData *)encodeDeviceDetails:(ARTDeviceDetails *)deviceDetails error:(NSError *_Nullable *_Nullable)error;
- (nullable ARTDeviceDetails *)decodeDeviceDetails:(NSData *)data error:(NSError *_Nullable *_Nullable)error;
- (nullable ARTDeviceDetailsResponse *)decodeDeviceDetailsResponse:(NSData *)data error:(NSError *_Nullable *_Nullable)error;
- (nullable NSArray<ARTDeviceDetails *> *)decodeDevicesDetails:(NSData *)data error:(NSError * __autoreleasing *)error;
- (nullable ARTDeviceIdentityTokenDetails *)decodeDeviceIdentityTokenDetails:(NSData *)data error:(NSError * __autoreleasing *)error;

// DevicePushDetails
- (nullable NSData *)encodeDevicePushDetails:(ARTDevicePushDetails *)devicePushDetails error:(NSError *_Nullable *_Nullable)error;
- (nullable ARTDevicePushDetails *)decodeDevicePushDetails:(NSData *)data error:(NSError * __autoreleasing *)error;

// Push Channel Subscription
- (nullable NSData *)encodePushChannelSubscription:(ARTPushChannelSubscription *)channelSubscription error:(NSError * __autoreleasing *)error;
- (nullable ARTPushChannelSubscription *)decodePushChannelSubscription:(NSData *)data error:(NSError * __autoreleasing *)error;
Expand Down
50 changes: 23 additions & 27 deletions Source/ARTJsonLikeEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ - (NSData *)encodeDeviceDetails:(ARTDeviceDetails *)deviceDetails error:(NSError
return [self encode:[self deviceDetailsToDictionary:deviceDetails] error:error];
}

- (ARTDeviceDetails *)decodeDeviceDetails:(NSData *)data error:(NSError **)error {
return [self deviceDetailsFromDictionary:[self decodeDictionary:data error:nil] error:error];
- (ARTDeviceDetailsResponse *)decodeDeviceDetailsResponse:(NSData *)data error:(NSError **)error {
return [self deviceDetailsResponseFromDictionary:[self decodeDictionary:data error:nil] error:error];
}

- (NSArray<ARTDeviceDetails *> *)decodeDevicesDetails:(NSData *)data error:(NSError * __autoreleasing *)error {
Expand All @@ -156,14 +156,6 @@ - (ARTDeviceIdentityTokenDetails *)decodeDeviceIdentityTokenDetails:(NSData *)da
return [self deviceIdentityTokenDetailsFromDictionary:[self decodeDictionary:data error:nil] error:error];
}

- (NSData *)encodeDevicePushDetails:(ARTDevicePushDetails *)devicePushDetails error:(NSError **)error {
return [self encode:[self devicePushDetailsToDictionary:devicePushDetails] error:error];
}

- (ARTDevicePushDetails *)decodeDevicePushDetails:(NSData *)data error:(NSError * __autoreleasing *)error {
return [self devicePushDetailsFromDictionary:[self decode:data error:nil] error:error];
}

- (NSData *)encodePushChannelSubscription:(ARTPushChannelSubscription *)channelSubscription error:(NSError * __autoreleasing *)error {
return [self encode:[self pushChannelSubscriptionToDictionary:channelSubscription] error:error];
}
Expand Down Expand Up @@ -634,7 +626,7 @@ - (NSDictionary *)deviceDetailsToDictionary:(ARTDeviceDetails *)deviceDetails {
dictionary[@"clientId"] = deviceDetails.clientId;
}

dictionary[@"push"] = [self devicePushDetailsToDictionary:deviceDetails.push];
dictionary[@"push"] = @{ @"recipient": deviceDetails.pushRecipient };

return dictionary;
}
Expand All @@ -651,7 +643,7 @@ - (ARTDeviceDetails *)deviceDetailsFromDictionary:(NSDictionary *)input error:(N
deviceDetails.platform = [input artString:@"platform"];
deviceDetails.formFactor = [input artString:@"formFactor"];
deviceDetails.metadata = [input valueForKey:@"metadata"];
deviceDetails.push = [self devicePushDetailsFromDictionary:input[@"push"] error:error];
deviceDetails.pushRecipient = [input valueForKeyPath:@"push.recipient"];

return deviceDetails;
}
Expand All @@ -677,30 +669,34 @@ - (ARTDeviceIdentityTokenDetails *)deviceIdentityTokenDetailsFromDictionary:(NSD
return deviceIdentityTokenDetails;
}

- (NSDictionary *)devicePushDetailsToDictionary:(ARTDevicePushDetails *)devicePushDetails {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

dictionary[@"recipient"] = devicePushDetails.recipient;

return dictionary;
}

- (ARTDevicePushDetails *)devicePushDetailsFromDictionary:(NSDictionary *)input error:(NSError * __autoreleasing *)error {
[_logger verbose:@"RS:%p ARTJsonLikeEncoder<%@>: devicePushDetailsFromDictionary %@", _rest, [_delegate formatAsString], input];
- (ARTDevicePushStatus *)devicePushStatusFromDictionary:(NSDictionary *)input error:(NSError * __autoreleasing *)error {
[_logger verbose:@"RS:%p ARTJsonLikeEncoder<%@>: devicePushStatusFromDictionary %@", _rest, [_delegate formatAsString], input];

if (![input isKindOfClass:[NSDictionary class]]) {
return nil;
}

ARTDevicePushDetails *devicePushDetails = [[ARTDevicePushDetails alloc] init];
devicePushDetails.state = [input artString:@"state"];
ARTDevicePushStatus *devicePushStatus = [[ARTDevicePushStatus alloc] init];
devicePushStatus.state = [input artString:@"state"];
NSDictionary *errorReason = [input valueForKey:@"errorReason"];
if (errorReason) {
devicePushDetails.errorReason = [ARTErrorInfo createWithCode:[[errorReason artNumber:@"code"] intValue] status:[[errorReason artNumber:@"statusCode"] intValue] message:[errorReason artString:@"message"]];
devicePushStatus.errorReason = [ARTErrorInfo createWithCode:[[errorReason artNumber:@"code"] intValue] status:[[errorReason artNumber:@"statusCode"] intValue] message:[errorReason artString:@"message"]];
}
devicePushDetails.recipient = [input valueForKey:@"recipient"];
return devicePushStatus;
}

return devicePushDetails;
- (ARTDeviceDetailsResponse *)deviceDetailsResponseFromDictionary:(NSDictionary *)input error:(NSError * __autoreleasing *)error {
[_logger verbose:@"RS:%p ARTJsonLikeEncoder<%@>: deviceDetailsResponseFromDictionary %@", _rest, [_delegate formatAsString], input];

if (![input isKindOfClass:[NSDictionary class]]) {
return nil;
}

ARTDeviceDetailsResponse *response = [[ARTDeviceDetailsResponse alloc] init];
response.deviceDetails = [self deviceDetailsFromDictionary:input error:error];
response.pushStatus = [self devicePushStatusFromDictionary:input[@"push"] error:error];

return response;
}

- (ARTProtocolMessage *)protocolMessageFromDictionary:(NSDictionary *)input {
Expand Down
7 changes: 7 additions & 0 deletions Source/ARTLocalDevice+Private.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
#import <Ably/ARTRest.h>

@protocol ARTDeviceStorage;
@class ARTPushActivationPersistentState;

NS_ASSUME_NONNULL_BEGIN

extern NSString *const ARTDeviceIdKey;
extern NSString *const ARTDeviceSecretKey;
extern NSString *const ARTDeviceIdentityTokenKey;
extern NSString *const ARTAPNSDeviceTokenKey;
extern NSString *const ARTDeviceActivationErrorKey;

@interface ARTLocalDevice ()

@property (strong, nonatomic) id<ARTDeviceStorage> storage;

#if TARGET_OS_IOS
@property (nullable, nonatomic, readonly) ARTErrorInfo *activationError;
@property (nullable, nonatomic, readonly) ARTPushActivationPersistentState *activationState;
#endif

+ (ARTLocalDevice *)load:(NSString *)clientId storage:(id<ARTDeviceStorage>)storage;
- (nullable NSString *)apnsDeviceToken;
- (void)setAndPersistAPNSDeviceToken:(nullable NSString *)deviceToken;
Expand Down
29 changes: 26 additions & 3 deletions Source/ARTLocalDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#import "ARTDeviceStorage.h"
#import "ARTDeviceIdentityTokenDetails.h"
#import "ARTCrypto+Private.h"
#import "ARTPushActivationStateMachine+Private.h"
#import "ARTPushActivationState.h"

NSString *const ARTDeviceIdKey = @"ARTDeviceId";
NSString *const ARTDeviceSecretKey = @"ARTDeviceSecret";
NSString *const ARTDeviceIdentityTokenKey = @"ARTDeviceIdentityToken";
NSString *const ARTAPNSDeviceTokenKey = @"ARTAPNSDeviceToken";

NSString *const ARTDevicePlatform = @"ios";

Expand Down Expand Up @@ -50,7 +57,7 @@ + (ARTLocalDevice *)load:(NSString *)clientId storage:(id<ARTDeviceStorage>)stor
#else
device.formFactor = ARTDeviceFormFactor;
#endif
device.push.recipient[@"transportType"] = ARTDevicePushTransportType;
device.pushRecipient[@"transportType"] = ARTDevicePushTransportType;

NSString *deviceId = [storage objectForKey:ARTDeviceIdKey];
NSString *deviceSecret = deviceId == nil ? nil : [storage secretForDevice:deviceId];
Expand Down Expand Up @@ -86,11 +93,27 @@ + (NSString *)generateSecret {
}

- (NSString *)apnsDeviceToken {
return self.push.recipient[@"deviceToken"];
return self.pushRecipient[@"deviceToken"];
}

#if TARGET_OS_IOS

- (ARTErrorInfo *)activationError {
NSData *errorData = [_storage objectForKey:ARTPushActivationErrorInfoKey];
ARTErrorInfo* errorInfo = [ARTErrorInfo art_unarchiveFromData:errorData];
return errorInfo;
}

- (ARTPushActivationState *)activationState {
NSData *stateData = [_storage objectForKey:ARTPushActivationCurrentStateKey];
ARTPushActivationState* state = [ARTPushActivationState art_unarchiveFromData:stateData];
return state;
}

#endif

- (void)setAPNSDeviceToken:(NSString *_Nonnull)token {
self.push.recipient[@"deviceToken"] = token;
self.pushRecipient[@"deviceToken"] = token;
}

- (void)setAndPersistAPNSDeviceToken:(NSString *)token {
Expand Down
5 changes: 0 additions & 5 deletions Source/ARTPush.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ - (void)deactivate {

@end

NSString *const ARTDeviceIdKey = @"ARTDeviceId";
NSString *const ARTDeviceSecretKey = @"ARTDeviceSecret";
NSString *const ARTDeviceIdentityTokenKey = @"ARTDeviceIdentityToken";
NSString *const ARTAPNSDeviceTokenKey = @"ARTAPNSDeviceToken";

@implementation ARTPushInternal {
__weak ARTRestInternal *_rest; // weak because rest owns self
ARTLog *_logger;
Expand Down
3 changes: 3 additions & 0 deletions Source/ARTPushActivationState.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ NS_ASSUME_NONNULL_BEGIN

/// Persistent State
@interface ARTPushActivationPersistentState : ARTPushActivationState

- (BOOL)isFailed;

@end

#pragma mark - States
Expand Down
5 changes: 5 additions & 0 deletions Source/ARTPushActivationState.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ + (ARTPushActivationState *)unarchive:(NSData *)data {
#pragma mark - Persistent State

@implementation ARTPushActivationPersistentState

- (BOOL)isFailed {
return [self isKindOfClass:[ARTPushActivationStateAfterRegistrationSyncFailed class]] || [self isKindOfClass:[ARTPushActivationEventDeregistrationFailed class]];
}

@end

#pragma mark - Activation States
Expand Down
3 changes: 3 additions & 0 deletions Source/ARTPushActivationStateMachine+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NS_ASSUME_NONNULL_BEGIN

extern NSString *const ARTPushActivationCurrentStateKey;
extern NSString *const ARTPushActivationPendingEventsKey;
extern NSString *const ARTPushActivationErrorInfoKey;

@interface ARTPushActivationStateMachine ()

Expand All @@ -24,6 +25,8 @@ extern NSString *const ARTPushActivationPendingEventsKey;
@property (readonly, nonatomic) ARTPushActivationState *current_nosync;

- (void)registerForAPNS;
- (void)saveErrorInfo:(ARTErrorInfo *)errorInfo;
- (void)clearErrorInfo;

@end

Expand Down
16 changes: 15 additions & 1 deletion Source/ARTPushActivationStateMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

NSString *const ARTPushActivationCurrentStateKey = @"ARTPushActivationCurrentState";
NSString *const ARTPushActivationPendingEventsKey = @"ARTPushActivationPendingEvents";
NSString *const ARTPushActivationErrorInfoKey = @"ARTPushActivationErrorInfo";

@implementation ARTPushActivationStateMachine {
ARTPushActivationEvent *_lastHandledEvent;
Expand Down Expand Up @@ -127,6 +128,11 @@ - (void)handleEvent:(nonnull ARTPushActivationEvent *)event {
}

[self persist];

if ([event isKindOfClass:[ARTPushActivationErrorEvent class]]) {
ARTPushActivationErrorEvent* errorEvent = (ARTPushActivationErrorEvent *)event;
[self saveErrorInfo:errorEvent.error];
}
}

- (void)persist {
Expand All @@ -137,6 +143,14 @@ - (void)persist {
[self.rest.storage setObject:[_pendingEvents art_archive] forKey:ARTPushActivationPendingEventsKey];
}

- (void)saveErrorInfo:(ARTErrorInfo *)errorInfo {
[_rest.storage setObject:[errorInfo art_archive] forKey:ARTPushActivationErrorInfoKey];
}

- (void)clearErrorInfo {
[_rest.storage setObject:nil forKey:ARTPushActivationErrorInfoKey];
}

- (void)deviceRegistration:(ARTErrorInfo *)error {
#if TARGET_OS_IOS
ARTLocalDevice *local = _rest.device_nosync;
Expand Down Expand Up @@ -231,7 +245,7 @@ - (void)deviceUpdateRegistration:(ARTErrorInfo *)error {
request.HTTPMethod = @"PATCH";
request.HTTPBody = [[_rest defaultEncoder] encode:@{
@"push": @{
@"recipient": local.push.recipient
@"recipient": local.pushRecipient
}
} error:nil];
[request setValue:[[_rest defaultEncoder] mimeType] forHTTPHeaderField:@"Content-Type"];
Expand Down
Loading