Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ static WritableMap mapFromIdentityMap(final IdentityMap map) {
for (IdentityItem item : items) {
WritableMap itemAsWritableMap = new WritableNativeMap();

itemAsWritableMap.putString(ID_KEY, item.getId());
itemAsWritableMap.putString(AEP_AUTH_STATE_KEY, item.getAuthenticatedState().getName());
itemAsWritableMap.putBoolean(IS_PRIMARY_KEY, item.isPrimary());
itemAsWritableMap.putString(AEP_AUTH_STATE_KEY, item.getAuthenticatedState().getName());
itemAsWritableMap.putString(ID_KEY, item.getId());

itemsAsArray.pushMap(itemAsWritableMap);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/edgeidentity/ios/src/RCTAEPEdgeIdentity.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

@interface RCTAEPEdgeIdentity : NSObject <RCTBridgeModule>

@end
@end
36 changes: 34 additions & 2 deletions packages/edgeidentity/ios/src/RCTAEPEdgeIdentity.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
governing permissions and limitations under the License.
*/

#import "RCTAEPEdgeIdentity.h"
@import AEPEdgeIdentity;
@import AEPCore;
#import "RCTAEPEdgeIdentity.h"
#import "RCTAEPEdgeIdentityDataBridge.h"


@implementation RCTAEPEdgeIdentity

Expand Down Expand Up @@ -39,6 +41,36 @@ - (dispatch_queue_t)methodQueue
}];
}

RCT_EXPORT_METHOD(getIdentities:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock)reject) {

[AEPMobileEdgeIdentity getIdentities:^(AEPIdentityMap * _Nullable IdentityMap, NSError * _Nullable error) {

if (error) {
[self handleError:error rejecter:reject errorLocation:@"getIdentities"];
} else {
resolve([RCTAEPEdgeIdentityDataBridge dictionaryFromIdentityMap:IdentityMap]);
}
}];
}

RCT_EXPORT_METHOD(updateIdentities:(nonnull NSDictionary*) map) {
AEPIdentityMap *convertMap = [RCTAEPEdgeIdentityDataBridge dictionaryToIdentityMap:map];

[AEPMobileEdgeIdentity updateIdentities:(AEPIdentityMap * _Nonnull) convertMap];
}

RCT_EXPORT_METHOD(removeIdentity:(nonnull NSDictionary*)item
namespace:(NSString *)namespace) {

AEPIdentityItem *convertItem = [RCTAEPEdgeIdentityDataBridge dictionaryToIdentityItem:item];

if (!convertItem || !namespace) {
return;
}

[AEPMobileEdgeIdentity removeIdentityItem:(AEPIdentityItem * _Nonnull) convertItem withNamespace:(NSString * _Nonnull) namespace];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment here, item is nullable, but then the AEPMobileEdgeIdentity removeIdentityItem expects a nonnull, make sure the items is not null before calling this api

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What to do if the dictionary is nil in dictionaryToIdneityItem? I return it to nil right now.
if (!dict) {
return nil;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all dictionaryToIdentityItem should not be null but if it evaluates to null you send null, and you cannot pass null item to removeIdentityMap, so you can just log an error and exit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have something like

if (!item || !namespace) { 
return; 
}
[AEPMobileEdgeIdentity removeIdentityItem:convertItem withNamespace: namespace];

}

#pragma mark - Helper methods

- (void) handleError:(NSError *) error rejecter:(RCTPromiseRejectBlock) reject errorLocation:(NSString *) location {
Expand All @@ -59,4 +91,4 @@ - (void) handleError:(NSError *) error rejecter:(RCTPromiseRejectBlock) reject e

}

@end
@end
26 changes: 26 additions & 0 deletions packages/edgeidentity/ios/src/RCTAEPEdgeIdentityDataBridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

@import AEPEdgeIdentity;

@interface RCTAEPEdgeIdentityDataBridge : NSObject

+ (NSDictionary *_Nullable)dictionaryFromIdentityMap: (nullable AEPIdentityMap *) map;

+ (AEPIdentityMap *_Nonnull)dictionaryToIdentityMap: (nonnull NSDictionary *) dict;

+ (AEPIdentityItem *_Nullable)dictionaryToIdentityItem: (nullable NSDictionary *) dict;

+ (AEPAuthenticatedState) authStateFromString: (nullable NSString *) authStateString;

+ (NSString *_Nullable)stringFromAuthState: (AEPAuthenticatedState) authState;

@end
122 changes: 122 additions & 0 deletions packages/edgeidentity/ios/src/RCTAEPEdgeIdentityDataBridge.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

#import "RCTAEPEdgeIdentityDataBridge.h"

@implementation RCTAEPEdgeIdentityDataBridge

static NSString* const ID_KEY = @"id";
static NSString* const IS_PRIMARY_KEY = @"primary";
static NSString* const AUTH_STATE_KEY = @"authenticatedState";
static NSString* const ITEM_KEY = @"items";
static NSString* const AUTHENTICATED = @"authenticated";
static NSString* const LOGGED_OUT = @"loggedOut";
static NSString* const AMBIGUOUS = @"ambiguous";

+ (NSDictionary *)dictionaryFromIdentityMap: (nullable AEPIdentityMap *) idmap {
NSMutableDictionary *mapDict = [NSMutableDictionary dictionary];

if (!idmap){
return mapDict;
}

for (NSString *namespace in idmap.namespaces) {
NSArray* items = [idmap getItemsWithNamespace:namespace];

NSMutableArray *mapArray = [NSMutableArray array];

for (AEPIdentityItem *item in items){
NSMutableDictionary *itemdict = [NSMutableDictionary dictionary];
itemdict[IS_PRIMARY_KEY] = @(item.primary);
itemdict[AUTH_STATE_KEY] = [RCTAEPEdgeIdentityDataBridge stringFromAuthState:(item.authenticatedState)];
itemdict[ID_KEY] = item.id ;

[mapArray addObject:itemdict];
}
if (mapArray.count !=0) {
[mapDict setObject:mapArray forKey:namespace];
}
}

return mapDict;
}

+ (nonnull AEPIdentityMap *)dictionaryToIdentityMap: (nonnull NSDictionary *) dict {
AEPIdentityMap *identityMap = [[AEPIdentityMap alloc] init];
NSDictionary *itemsMap = [[dict objectForKey:ITEM_KEY] isKindOfClass:[NSDictionary class]] ? [dict objectForKey:ITEM_KEY] : nil;

if (!itemsMap){
return identityMap;
}

NSArray <NSString*>* namespaces = [itemsMap allKeys];

for (NSString *namespace in namespaces){
NSArray* items = [itemsMap objectForKey:namespace];
for (NSDictionary *itemMap in items){
AEPIdentityItem *item = [RCTAEPEdgeIdentityDataBridge dictionaryToIdentityItem:itemMap];

if (item){
[identityMap addItem:item withNamespace:namespace];
}
}
}

return identityMap;
}

+ (AEPIdentityItem *)dictionaryToIdentityItem: (nullable NSDictionary *) dict {

if (!dict) {
return nil;
}

NSString *identifier = [[dict objectForKey:ID_KEY] isKindOfClass:[NSString class]] ? [dict objectForKey:ID_KEY] : nil;

NSString *authenticatedString = [[dict objectForKey:AUTH_STATE_KEY] isKindOfClass:[NSString class]] ? [dict objectForKey:AUTH_STATE_KEY] : nil;

AEPAuthenticatedState authenticatedState = [RCTAEPEdgeIdentityDataBridge authStateFromString:(authenticatedString)];

BOOL primary = [[dict objectForKey:IS_PRIMARY_KEY] boolValue];

return [[AEPIdentityItem alloc] initWithId:identifier authenticatedState:authenticatedState primary:primary];
}

+ (AEPAuthenticatedState) authStateFromString: (nullable NSString *) authStateString {
if (!authStateString){
return AEPAuthenticatedStateAmbiguous;
}

if ([authStateString isEqualToString:AUTHENTICATED]) {
return AEPAuthenticatedStateAuthenticated;
} else if ([authStateString isEqualToString:LOGGED_OUT]) {
return AEPAuthenticatedStateLoggedOut;
}

return AEPAuthenticatedStateAmbiguous;
}

+ (NSString *) stringFromAuthState: (AEPAuthenticatedState) authState {
if (!authState){
return AMBIGUOUS;
}

switch (authState) {
case AEPAuthenticatedStateAuthenticated:
return AUTHENTICATED;
case AEPAuthenticatedStateLoggedOut:
return LOGGED_OUT;
default:
return AMBIGUOUS;
}
}

@end