Skip to content
This repository has been archived by the owner on Jul 18, 2020. It is now read-only.

Bridge user_metadata in Android #64

Merged
merged 1 commit into from
Jun 15, 2016
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 @@ -29,9 +29,14 @@

import com.auth0.core.UserProfile;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

public class UserProfileBridge implements LockReactBridge {
Expand All @@ -45,7 +50,11 @@ public class UserProfileBridge implements LockReactBridge {

private UserProfile profile;

private final SimpleDateFormat formatter;
public UserProfileBridge(@Nullable UserProfile profile) {
// use ISO 8601 international standard date/time format
this.formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
this.formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
this.profile = profile;
}

Expand All @@ -58,13 +67,74 @@ public WritableMap toMap() {
profileMap.putString(NAME_KEY, profile.getName());
profileMap.putString(NICKNAME_KEY, profile.getNickname());
if (profile.getCreatedAt() != null) {
// use ISO 8601 international standard date/time format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
profileMap.putString(CREATED_AT_KEY, simpleDateFormat.format(profile.getCreatedAt()));
profileMap.putString(CREATED_AT_KEY, formatter.format(profile.getCreatedAt()));
}
profileMap.putString(PICTURE_KEY, profile.getPictureURL());
put("userMetadata", profile.getExtraInfo().get("user_metadata"), profileMap);
}
return profileMap;
}

private void put(String key, Map<String, Object> map, WritableMap into) {
if (map == null || map.isEmpty()) {
return;
}

final WritableMap writableMap = Arguments.createMap();
for (Map.Entry<String, Object> entry: map.entrySet()) {
put(entry.getKey(), entry.getValue(), writableMap);
}
into.putMap(key, writableMap);
}

private void put(String key, List<?> list, WritableMap into) {
if (list == null || list.isEmpty()) {
return;
}

final WritableArray array = Arguments.createArray();
for (Object item: list) {
if (item instanceof String) {
array.pushString((String) item);
}
if (item instanceof Integer) {
array.pushInt((Integer) item);
}
if (item instanceof Boolean) {
array.pushBoolean((Boolean) item);
}
if (item instanceof Double) {
array.pushDouble((Double) item);
}
if (item instanceof Date) {
array.pushString(formatter.format(item));
}
}
into.putArray(key, array);
}

private void put(String key, Object value, WritableMap map) {
if (value instanceof String) {
map.putString(key, (String) value);
}
if (value instanceof Integer) {
map.putInt(key, (Integer) value);
}
if (value instanceof Boolean) {
map.putBoolean(key, (Boolean) value);
}
if (value instanceof Double) {
map.putDouble(key, (Double) value);
}
if (value instanceof Date) {
map.putString(key, formatter.format(value));
}
if (value instanceof Map) {
//noinspection unchecked
put(key, (Map) value, map);
}
if (value instanceof List) {
put(key, (List)value, map);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@
import org.robolectric.RobolectricTestRunner;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

/*
Expand Down Expand Up @@ -127,9 +130,10 @@ public void testAll() throws Exception {
userProfileMap.put("nickname", "nickname-value");
userProfileMap.put("picture", "picture-value");
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
userProfileMap.put("created_at", sdf.format(now));
userProfileMap.put("user_metadata", Collections.singletonMap("role", "admin"));
UserProfile userProfile = new UserProfile(userProfileMap);

UserProfileBridge userProfileBridge = new UserProfileBridge(userProfile);
Expand All @@ -141,5 +145,6 @@ public void testAll() throws Exception {
assertThat(map.getString("nickname"), is(equalTo("nickname-value")));
assertThat(map.getString("createdAt"), is(equalTo(sdf.format(now))));
assertThat(map.getString("picture"), is(equalTo("picture-value")));
assertThat(map.getMap("userMetadata"), is(notNullValue()));
}
}