Skip to content

Commit 63155e1

Browse files
committed
WIP: Android module
1 parent b0eedfa commit 63155e1

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

android/src/main/java/io/fullstack/firestack/FirestackModule.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class FirestackModule extends ReactContextBaseJavaModule {
3333
private static final String TAG = "FirestackModule";
3434
private Context context;
3535
private FirebaseAuth mAuth;
36+
private FirebaseApp app;
3637
private FirebaseUser user;
3738
private FirebaseAuth.AuthStateListener mAuthListener;
3839

@@ -49,7 +50,7 @@ public String getName() {
4950

5051
@ReactMethod
5152
public void configureWithOptions(ReadableMap params, @Nullable final Callback onComplete) {
52-
Log.d(TAG, "configureWithOptions");
53+
Log.i(TAG, "configureWithOptions");
5354

5455
ReactContext mCtx = getReactApplicationContext();
5556
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
@@ -66,35 +67,45 @@ public void configureWithOptions(ReadableMap params, @Nullable final Callback on
6667
}
6768
if (params.hasKey("gcmSenderID")) {
6869
final String gcmSenderID = params.getString("gcmSenderID");
69-
Log.d(TAG, "Setting gcmSenderID from params" + gcmSenderID );
70+
Log.d(TAG, "Setting gcmSenderID from params " + gcmSenderID );
7071
builder.setGcmSenderId(gcmSenderID);
7172
}
7273
if (params.hasKey("storageBucket")) {
7374
final String storageBucket = params.getString("storageBucket");
74-
Log.d(TAG, "Setting storageBucket from params" + storageBucket);
75+
Log.d(TAG, "Setting storageBucket from params " + storageBucket);
7576
builder.setStorageBucket(storageBucket);
7677
}
7778
if (params.hasKey("databaseURL")) {
7879
final String databaseURL = params.getString("databaseURL");
79-
Log.d(TAG, "Setting databaseURL from params" + databaseURL);
80+
Log.d(TAG, "Setting databaseURL from params " + databaseURL);
8081
builder.setDatabaseUrl(databaseURL);
8182
}
8283
if (params.hasKey("clientID")) {
8384
final String clientID = params.getString("clientID");
84-
Log.d(TAG, "Setting clientID from params" + clientID);
85+
Log.d(TAG, "Setting clientID from params " + clientID);
8586
builder.setApplicationId(clientID);
8687
}
8788

8889
try {
89-
Log.i(TAG, "Configuring");
90-
FirebaseApp app = FirebaseApp.initializeApp(mCtx, builder.build());
90+
Log.i(TAG, "Configuring app");
91+
if (app == null) {
92+
app = FirebaseApp.initializeApp(mCtx, builder.build());
93+
}
9194
Log.i(TAG, "Configured");
92-
onComplete.invoke(app);
95+
System.out.println("Configured");
96+
97+
WritableMap resp = Arguments.createMap();
98+
resp.putString("msg", "success");
99+
onComplete.invoke(null, resp);
93100
}
94101
catch (Exception ex){
95102
Log.e(TAG, "ERROR configureWithOptions");
96103
Log.e(TAG, ex.getMessage());
97-
onComplete.invoke(ex.getMessage());
104+
105+
WritableMap resp = Arguments.createMap();
106+
resp.putString("msg", ex.getMessage());
107+
108+
onComplete.invoke(resp);
98109
}
99110
}
100111

lib/firestack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class Firestack {
5858
this.configured = true;
5959
return args;
6060
}).catch((err) => {
61-
log.error('Native error occurred while calling configure', e);
61+
log.error('Native error occurred while calling configure', err);
6262
})
6363
}
6464

lib/firestackModule.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,10 @@ export class FirestackModule {
163163
const toObject = this._toObject;
164164

165165
return new Promise((resolve, reject) => {
166-
ref.set(value, (error, snapshot) => {
166+
ref.set(value, (error) => {
167167
this._handleUpdate(T.ACTION_SET, null, (state) => {
168168
if (cb) {
169-
console.log('error ->', error, snapshot);
170-
cb(toObject(snapshot, state));
169+
cb(error, value);
171170
}
172171
return error ? reject(error) : resolve(value)
173172
});
@@ -212,23 +211,20 @@ export class FirestackModule {
212211
// hackish, for now
213212
get actions() {
214213
const T = this._types;
214+
215+
const wrap = (fn) => (...args) => {
216+
const params = args && args.length > 0 ? args : [];
217+
const promise = fn.bind(this)(...params)
218+
return {type: T.ACTION_CALL, payload: promise}
219+
}
220+
215221
return [
216222
'listen', 'unlisten',
217223
'getAt', 'setAt', 'updateAt', 'removeAt'
218224
].reduce((sum, name) => {
219225
return {
220226
...sum,
221-
[name]: (...args) => (dispatch, getState) => {
222-
const fn = this[name].bind(this);
223-
fn.apply(this, args)
224-
.then((resp) => {
225-
dispatch({type: T.ACTION_SUCCESS, payload: resp});
226-
})
227-
.catch((err) => {
228-
dispatch({type: T.ACTION_FAIL, payload: err});
229-
});
230-
return {type: T.ACTION_CALL, payload: name}
231-
}
227+
[name]: wrap(this[name])
232228
}
233229
}, {})
234230
}

lib/promisify.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const FirebaseHelper = NativeModules.Firestack;
33

44
export const promisify = fn => (...args) => {
55
return new Promise((resolve, reject) => {
6-
const handler = (err, resp) => err ? reject(err) : resolve(resp);
6+
const handler = (err, resp) => {
7+
err ? reject(err) : resolve(resp);
8+
}
79
args.push(handler);
810
(typeof fn === 'function' ? fn : FirebaseHelper[fn])
911
.call(FirebaseHelper, ...args);

0 commit comments

Comments
 (0)