Skip to content

Commit

Permalink
Android implementation of the remaining ::firebase::Firebase Mojo APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
collinjackson committed Jan 27, 2016
1 parent 0532dd9 commit 93b9588
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 19 deletions.
18 changes: 15 additions & 3 deletions sky/services/firebase/firebase.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,24 @@ interface ChildEventListener {
[ServiceName="firebase::Firebase"]
interface Firebase {
InitWithUrl(string url);
GetRoot(Firebase& root);
GetChild(string path, Firebase& child);
AddValueEventListener(ValueEventListener listener);
AddChildEventListener(ChildEventListener listener);
ObserveSingleEventOfType(EventType eventType) => (DataSnapshot snapshot);
AuthWithCustomToken(string token) => (Error? error, AuthData? authData);
AuthAnonymously() => (Error? error, AuthData? authData);
AuthWithOAuthToken(string provider, string credentials) => (Error? error, AuthData? authData);
AuthWithPassword(string email, string password) => (Error? error, AuthData? authData);
SetValue(string jsonValue) => (Error? error);
Unauth() => (Error? error);
GetChild(string path, Firebase& child);
GetParent(Firebase& parent);
GetRoot(Firebase& root);
SetValue(string jsonValue, int32 priority, bool hasPriority) => (Error? error);
RemoveValue() => (Error? error);
Push(Firebase& child) => (string key);
SetPriority(int32 priority) => (Error? error);
CreateUser(string email, string password) => (Error? error, string? jsonValue);
ChangeEmail(string oldEmail, string password, string newEmail) => (Error? error);
ChangePassword(string newPassword, string email, string oldPassword) => (Error? error);
RemoveUser(string email, string password) => (Error? error);
ResetPassword(string email) => (Error? error);
};
175 changes: 159 additions & 16 deletions sky/services/firebase/src/org/domokit/firebase/FirebaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.firebase.client.FirebaseError;
import com.firebase.client.Firebase.AuthResultHandler;
import com.firebase.client.Firebase.CompletionListener;
import com.firebase.client.Firebase.ResultHandler;
import com.firebase.client.Firebase.ValueResultHandler;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -59,20 +61,6 @@ public void initWithUrl(String url) {
mClient = new com.firebase.client.Firebase(url);
}

@Override
public void getRoot(InterfaceRequest<Firebase> request) {
FirebaseImpl root = new FirebaseImpl(mContext);
root.mClient = mClient.getRoot();
Firebase.MANAGER.bind(root, request);
}

@Override
public void getChild(String path, InterfaceRequest<Firebase> request) {
FirebaseImpl child = new FirebaseImpl(mContext);
child.mClient = mClient.child(path);
Firebase.MANAGER.bind(child, request);
}

@Override
public void addValueEventListener(org.chromium.mojom.firebase.ValueEventListener listener) {
final org.chromium.mojom.firebase.ValueEventListener.Proxy proxy =
Expand Down Expand Up @@ -148,6 +136,32 @@ public void onCancelled(FirebaseError error) {
});
}

@Override
public void authWithCustomToken(String token, final AuthWithCustomTokenResponse response) {
mClient.authWithCustomToken(token, new AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
response.call(null, toMojoAuthData(authData));
}
public void onAuthenticationError(FirebaseError error) {
response.call(toMojoError(error), null);
}
});
}

@Override
public void authAnonymously(final AuthAnonymouslyResponse response) {
mClient.authAnonymously(new AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
response.call(null, toMojoAuthData(authData));
}
public void onAuthenticationError(FirebaseError error) {
response.call(toMojoError(error), null);
}
});
}

@Override
public void authWithOAuthToken(String provider, String credentials, final AuthWithOAuthTokenResponse response) {
mClient.authWithOAuthToken(provider, credentials, new AuthResultHandler() {
Expand Down Expand Up @@ -175,11 +189,42 @@ public void onAuthenticationError(FirebaseError error) {
}

@Override
public void setValue(String jsonValue, final SetValueResponse response) {
public void unauth(final UnauthResponse response) {
mClient.unauth(new CompletionListener() {
@Override
public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) {
response.call(toMojoError(error));
}
});
}

@Override
public void getChild(String path, InterfaceRequest<Firebase> request) {
FirebaseImpl child = new FirebaseImpl(mContext);
child.mClient = mClient.child(path);
Firebase.MANAGER.bind(child, request);
}

@Override
public void getParent(InterfaceRequest<Firebase> request) {
FirebaseImpl parent = new FirebaseImpl(mContext);
parent.mClient = mClient.getParent();
Firebase.MANAGER.bind(parent, request);
}

@Override
public void getRoot(InterfaceRequest<Firebase> request) {
FirebaseImpl root = new FirebaseImpl(mContext);
root.mClient = mClient.getRoot();
Firebase.MANAGER.bind(root, request);
}

@Override
public void setValue(String jsonValue, int priority, boolean hasPriority, final SetValueResponse response) {
try {
JSONObject root = new JSONObject(jsonValue);
Object value = toMap(root).get("value");
mClient.setValue(value, null, new CompletionListener() {
mClient.setValue(value, hasPriority ? priority : null, new CompletionListener() {
@Override
public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) {
response.call(toMojoError(error));
Expand All @@ -195,6 +240,104 @@ public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) {
}
}

@Override
public void removeValue(final RemoveValueResponse response) {
mClient.removeValue(new CompletionListener() {
@Override
public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) {
response.call(toMojoError(error));
}
});
}

@Override
public void push(InterfaceRequest<Firebase> request, final PushResponse response) {
FirebaseImpl child = new FirebaseImpl(mContext);
child.mClient = mClient.push();
Firebase.MANAGER.bind(child, request);
response.call(child.mClient.getKey());
}

@Override
public void setPriority(int priority, final SetPriorityResponse response) {
mClient.setPriority(priority, new CompletionListener() {
@Override
public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) {
response.call(toMojoError(error));
}
});
}

@Override
public void createUser(String email, String password, final CreateUserResponse response) {
mClient.createUser(email, password, new ValueResultHandler<Map<String,Object>>() {
@Override
public void onError(FirebaseError error) {
response.call(toMojoError(error), null);
}
@Override
public void onSuccess(Map<String,Object> result) {
response.call(null, new JSONObject(result).toString());
}
});
}

@Override
public void changeEmail(String oldEmail, String password, String newExample, final ChangeEmailResponse response) {
mClient.changeEmail(oldEmail, password, newExample, new ResultHandler() {
@Override
public void onError(FirebaseError error) {
response.call(toMojoError(error));
}
@Override
public void onSuccess() {
response.call(null);
}
});
}

@Override
public void changePassword(String newPassword, String email, String oldPassword, final ChangePasswordResponse response) {
mClient.changePassword(newPassword, email, oldPassword, new ResultHandler() {
@Override
public void onError(FirebaseError error) {
response.call(toMojoError(error));
}
@Override
public void onSuccess() {
response.call(null);
}
});
}

@Override
public void removeUser(String email, String password, final RemoveUserResponse response) {
mClient.removeUser(email, password, new ResultHandler() {
@Override
public void onError(FirebaseError error) {
response.call(toMojoError(error));
}
@Override
public void onSuccess() {
response.call(null);
}
});
}

@Override
public void resetPassword(String email, final ResetPasswordResponse response) {
mClient.resetPassword(email, new ResultHandler() {
@Override
public void onError(FirebaseError error) {
response.call(toMojoError(error));
}
@Override
public void onSuccess() {
response.call(null);
}
});
}

DataSnapshot toMojoSnapshot(com.firebase.client.DataSnapshot snapshot) {
DataSnapshot mojoSnapshot = new DataSnapshot();
mojoSnapshot.key = snapshot.getKey();
Expand Down

0 comments on commit 93b9588

Please sign in to comment.