Skip to content

Commit 9cdef1d

Browse files
triniwizNathanWalker
authored andcommitted
updates
1 parent 4efb025 commit 9cdef1d

File tree

8 files changed

+115
-66
lines changed

8 files changed

+115
-66
lines changed

apps/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
"@nativescript/android": "8.0.0",
1818
"@nativescript/ios": "8.0.0"
1919
}
20-
}
20+
}

apps/demo/src/plugin-demos/firebase-database.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class DemoModel extends DemoSharedFirebaseDatabase {
2323
this.children();
2424
}
2525
randomData() {
26-
this.database
27-
.ref()
28-
.push({ name: 'Test', date: Date.now() })
26+
this.database
27+
.ref('/random')
28+
.push({ name: 'random' , float: 1.1 })
2929
.then((value) => {
3030
console.log('push randomData', value);
3131
});
@@ -59,24 +59,24 @@ export class DemoModel extends DemoSharedFirebaseDatabase {
5959
console.log('transaction', value.val());
6060
});
6161

62-
this.database
63-
.ref('/posts')
64-
.child('1')
65-
.transaction((data: any) => {
66-
if (data) {
67-
data.likes += 1;
68-
return data;
69-
} else {
70-
return {
71-
likes: 1,
72-
};
73-
}
74-
})
75-
.then((result) => {
76-
console.log('transaction', 'result', result);
77-
})
78-
.catch((e) => {
79-
console.log('transaction', 'error', e);
80-
});
62+
// this.database
63+
// .ref('/posts')
64+
// .child('1')
65+
// .transaction((data: any) => {
66+
// if (data) {
67+
// data.likes += 1;
68+
// return data;
69+
// } else {
70+
// return {
71+
// likes: 1,
72+
// };
73+
// }
74+
// })
75+
// .then((result) => {
76+
// console.log('transaction', 'result', result);
77+
// })
78+
// .catch((e) => {
79+
// console.log('transaction', 'error', e);
80+
// });
8181
}
8282
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@nativescript/core": "^8.0.0",
3030
"@nativescript/plugin-tools": "2.0.4",
3131
"@nativescript/types": "^8.0.0",
32-
"@nativescript/webpack": "beta",
32+
"@nativescript/webpack": "rc",
3333
"@ngtools/webpack": "^12.0.0",
3434
"husky": "^5.1.3",
3535
"nativescript-vue": "~2.9.0",

packages/firebase-core/index.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { FirebaseConfig, IFirebaseOptions } from './common';
22

3-
export declare function serialize(data: any): any;
4-
export declare function deserialize(data: any): any;
3+
declare function serialize(data: any): any;
4+
declare function deserialize(data: any): any;
55

6-
export declare class FirebaseOptions implements IFirebaseOptions {
6+
declare class FirebaseOptions implements IFirebaseOptions {
77
readonly ios: any;
88
readonly android: any;
99
readonly native: any;
1010
}
1111

12-
export declare class FirebaseApp {
12+
declare class FirebaseApp {
1313
readonly native;
1414
readonly ios;
1515
readonly name;
@@ -20,15 +20,15 @@ export declare class FirebaseApp {
2020
apps(): FirebaseApp[];
2121
}
2222

23-
export declare class Firebase {
23+
declare class Firebase {
2424
static app(name?: string): FirebaseApp;
2525
static initializeApp(options: FirebaseOptions, configOrName?: FirebaseConfig | string): Promise<FirebaseApp>;
2626
static analytics(): any;
2727
static auth(app?: FirebaseApp): any;
2828
static database(app?: FirebaseApp): any;
2929
}
3030

31-
export class FirebaseError extends Error {
31+
declare class FirebaseError extends Error {
3232
static fromNative(native: any, message?: string);
3333
readonly native;
3434
}

packages/firebase-core/utils.ts

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1+
function numberHasDecimals(item: number) {
2+
return !(item % 1 === 0);
3+
}
4+
5+
function numberIs64Bit(item: number) {
6+
return item < -Math.pow(2, 31) + 1 || item > Math.pow(2, 31) - 1;
7+
}
8+
19
export function serialize(data: any): any {
210
if (global.isIOS) {
311
switch (typeof data) {
412
case 'string':
5-
case 'boolean':
6-
case 'number': {
13+
case 'boolean': {
714
return data;
815
}
16+
case 'number': {
17+
const hasDecimals = numberHasDecimals(data);
18+
if (numberIs64Bit(data)) {
19+
if (hasDecimals) {
20+
return NSNumber.alloc().initWithDouble(data);
21+
} else {
22+
return NSNumber.alloc().initWithLongLong(data);
23+
}
24+
} else {
25+
if (hasDecimals) {
26+
return NSNumber.alloc().initWithFloat(data);
27+
} else {
28+
return data;
29+
}
30+
}
31+
}
932

1033
case 'object': {
1134
if (data instanceof Date) {
12-
return data.toJSON();
35+
return NSDate.dateWithTimeIntervalSince1970(data.getTime() / 1000);
1336
}
1437

1538
if (!data) {
@@ -37,27 +60,44 @@ export function serialize(data: any): any {
3760
let store;
3861
switch (typeof data) {
3962
case 'string':
40-
case 'boolean':
41-
case 'number': {
63+
case 'boolean': {
4264
return data;
4365
}
66+
case 'number': {
67+
const hasDecimals = numberHasDecimals(data);
68+
if (numberIs64Bit(data)) {
69+
if (hasDecimals) {
70+
return java.lang.Double.valueOf(data);
71+
} else {
72+
return java.lang.Long.valueOf(data);
73+
}
74+
} else {
75+
if (hasDecimals) {
76+
return java.lang.Float.valueOf(data);
77+
} else {
78+
return data;
79+
}
80+
}
81+
}
4482

4583
case 'object': {
4684
if (!data) {
4785
return null;
4886
}
4987

5088
if (data instanceof Date) {
51-
return data.toJSON();
89+
return new java.util.Date(data.getTime());
5290
}
91+
5392
if (Array.isArray(data)) {
54-
store = new org.json.JSONArray();
55-
data.forEach((item) => store.put(serialize(item)));
93+
store = new java.util.ArrayList();
94+
data.forEach((item) => store.add(serialize(item)));
5695
return store;
5796
}
5897

59-
store = new org.json.JSONObject();
98+
store = new java.util.HashMap();
6099
Object.keys(data).forEach((key) => store.put(key, serialize(data[key])));
100+
console.log(store.toString());
61101
return store;
62102
}
63103

@@ -132,15 +172,15 @@ export function deserialize(data: any): any {
132172
}
133173

134174
case 'java.util.HashMap':
135-
case 'java.util.Map': {
136-
store = {};
137-
const keys = data.keySet().toArray();
138-
for (let k = 0; k < keys.length; k++) {
139-
const key = keys[k];
140-
store[key] = deserialize(data.get(key));
141-
}
142-
break;
175+
case 'java.util.Map': {
176+
store = {};
177+
const keys = data.keySet().toArray();
178+
for (let k = 0; k < keys.length; k++) {
179+
const key = keys[k];
180+
store[key] = deserialize(data.get(key));
143181
}
182+
break;
183+
}
144184

145185
default:
146186
store = null;

packages/firebase-database/index.android.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ export class Reference extends Query implements IReference {
410410
return new Promise((resolve, reject) => {
411411
NSDatabaseReference().set(
412412
this.native,
413-
value,
413+
serialize(value),
414414
new org.nativescript.firebase.database.FirebaseDatabase.Callback({
415415
onSuccess(param0) {
416416
onComplete?.(null);
@@ -448,7 +448,7 @@ export class Reference extends Query implements IReference {
448448
return new Promise((resolve, reject) => {
449449
NSDatabaseReference().setWithPriority(
450450
this.native,
451-
newVal,
451+
serialize(newVal),
452452
newPriority as any,
453453
new org.nativescript.firebase.database.FirebaseDatabase.Callback({
454454
onSuccess(param0) {
@@ -476,14 +476,13 @@ export class Reference extends Query implements IReference {
476476
return serialize(newData);
477477
},
478478
onComplete(error: com.google.firebase.database.DatabaseError, commited: boolean, snapshot: com.google.firebase.database.DataSnapshot): void {
479-
console.log(error);
480479
const ss = DataSnapshot.fromNative(snapshot);
481480
if (error) {
482481
const err = FirebaseError.fromNative(error);
483482
onComplete?.(err, commited, ss);
484483
reject(err);
485484
} else {
486-
onComplete(null, commited, ss);
485+
onComplete?.(null, commited, ss);
487486
resolve({
488487
commited,
489488
snapshot: ss,
@@ -498,7 +497,7 @@ export class Reference extends Query implements IReference {
498497
return new Promise((resolve, reject) => {
499498
NSDatabaseReference().update(
500499
this.native,
501-
values as any,
500+
serialize(values),
502501
new org.nativescript.firebase.database.FirebaseDatabase.Callback({
503502
onSuccess(param0) {
504503
onComplete?.(null);
@@ -557,7 +556,10 @@ export class DataSnapshot implements IDataSnapshot {
557556
return this.native?.exists();
558557
}
559558
exportVal() {
560-
return deserialize(this.native.getValue(true));
559+
return {
560+
'.priority': this.native.getPriority?.(),
561+
'.value': deserialize(this.native.getValue(true)),
562+
};
561563
}
562564
forEach(action: (child: DataSnapshot) => true | undefined): boolean {
563565
const iterator = this.native.getChildren().iterator();

packages/firebase-database/index.ios.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export class Reference extends Query implements IReference {
323323
}
324324
set(value: any, onComplete?: (error: FirebaseError) => void): Promise<void> {
325325
return new Promise((resolve, reject) => {
326-
this.native.setValueWithCompletionBlock(value, (error, ref) => {
326+
this.native.setValueWithCompletionBlock(serialize(value), (error, ref) => {
327327
if (error) {
328328
const err = FirebaseError.fromNative(error);
329329
onComplete?.(err);
@@ -355,7 +355,7 @@ export class Reference extends Query implements IReference {
355355
}
356356
setWithPriority(newVal: any, newPriority: string | number, onComplete?: (error: FirebaseError) => void): Promise<void> {
357357
return new Promise((resolve, reject) => {
358-
this.native.setValueAndPriorityWithCompletionBlock(newVal, newPriority, (error, ref) => {
358+
this.native.setValueAndPriorityWithCompletionBlock(serialize(newVal), newPriority, (error, ref) => {
359359
if (error) {
360360
const err = FirebaseError.fromNative(error);
361361
onComplete?.(err);
@@ -458,17 +458,22 @@ export class DataSnapshot implements IDataSnapshot {
458458
};
459459
}
460460
forEach(action: (child: DataSnapshot) => true | undefined): boolean {
461-
let stopEnumerate = false;
462-
while (!stopEnumerate) {
463-
const object = this.native.children.nextObject();
464-
console.log('forEach','object', object);
465-
if (!object) {
466-
stopEnumerate = true;
467-
} else {
468-
stopEnumerate = action(DataSnapshot.fromNative(object)) || false;
461+
let didStop = false;
462+
const children = this.native.children;
463+
let object = children.nextObject();
464+
while (object) {
465+
didStop = action(DataSnapshot.fromNative(object));
466+
if (didStop || !object) {
467+
break;
469468
}
469+
object = children.nextObject();
470+
}
471+
if (didStop) {
472+
return true;
473+
}
474+
if (!object) {
475+
return false;
470476
}
471-
return stopEnumerate;
472477
}
473478
getPriority(): string | number {
474479
return this.native.priority;

workspace.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
"ios": {
2121
"executor": "@nativescript/nx:build",
2222
"options": {
23-
"platform": "ios"
23+
"platform": "ios",
24+
"noHmr": true
2425
}
2526
},
2627
"android": {
2728
"executor": "@nativescript/nx:build",
2829
"options": {
29-
"platform": "android"
30+
"platform": "android",
31+
"noHmr": true
3032
}
3133
},
3234
"clean": {

0 commit comments

Comments
 (0)