Skip to content

Commit 7548813

Browse files
author
Ihor Khomiak
committed
fix login config functionality
1 parent 3da7fb2 commit 7548813

File tree

4 files changed

+62
-34
lines changed

4 files changed

+62
-34
lines changed

app/common/config.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as appSettings from 'application-settings';
2+
import { User } from 'nativescript-plugin-firebase';
23

34
export class Config {
45

@@ -10,8 +11,8 @@ export class Config {
1011
this.Save("username", value);
1112
}
1213

13-
public static saveUserUid(value) {
14-
this.Save("userUid", value);
14+
public static saveUserToken(value) {
15+
this.Save("token", value);
1516
}
1617

1718
public static getEmail() {
@@ -22,8 +23,20 @@ export class Config {
2223
return this.GetString("username");
2324
}
2425

25-
public static getUserUid() {
26-
return this.GetString("userUid");
26+
public static getUserToken() {
27+
return this.GetString("token");
28+
}
29+
30+
public static saveAllUserInfo(user: User) {
31+
Config.saveEmail(user.email);
32+
Config.saveUserName(user.name);
33+
Config.saveUserToken(user.uid);
34+
}
35+
36+
public static removeAllUserInfo() {
37+
Config.saveEmail('');
38+
Config.saveUserName('');
39+
Config.saveUserToken('');
2740
}
2841

2942
private static Save(type: string, data: any){

app/components/cost/cost.service.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from '@angular/core';
22
import * as Rx from 'rxjs/Rx';
33

4+
import firebase = require('nativescript-plugin-firebase');
45
import { Cost } from '../../common/protocol';
56

67
@Injectable()
@@ -9,25 +10,45 @@ export class CostService {
910
private _costs: Rx.Observable<Cost[]>;
1011

1112
constructor() {
12-
this._costsSource = new Rx.BehaviorSubject<Cost[]>(this.costsCollection);
13-
this._costs = this._costsSource.asObservable();
13+
this._costsSource = new Rx.BehaviorSubject<Cost[]>([]);
14+
this._costs = this._costsSource.asObservable();
1415
}
1516

16-
private costsCollection = new Array<Cost>(
17-
{ id: 0, quantity: 310, type: "Products", changesDate: "Jul 01 2017", isFavorite: false },
18-
{ id: 1, quantity: 25, type: "Products", changesDate: "Aug 25 2017", isFavorite: false },
19-
{ id: 2, quantity: 127, type: "ForHome", changesDate: "Aug 30 2017", isFavorite: false },
20-
{ id: 3, quantity: 80, type: "Products", changesDate: "Aug 29 2010", isFavorite: false },
21-
{ id: 4, quantity: 32, type: "Products", changesDate: "Nov 13 2017", isFavorite: false },
22-
{ id: 5, quantity: 235, type: "Products", changesDate: "Jan 31 2017", isFavorite: false },
23-
{ id: 6, quantity: 98, type: "ForHome", changesDate: "Feb 20 2017", isFavorite: false },
24-
{ id: 7, quantity: 105, type: "Products", changesDate: "Oct 01 2017", isFavorite: false },
25-
{ id: 8, quantity: 80, type: "Products", changesDate: "Aug 29 2017", isFavorite: false },
26-
{ id: 9, quantity: 75, type: "Products", changesDate: "Aug 08 2017", isFavorite: false },
27-
{ id: 10, quantity: 235, type: "Products", changesDate: "Aug 26 2017", isFavorite: false },
28-
{ id: 11, quantity: 264, type: "ForHome", changesDate: "May 30 2017", isFavorite: false },
29-
{ id: 12, quantity: 78, type: "Products", changesDate: "April 04 2017", isFavorite: false }
30-
);
17+
// private costsCollection = new Array<Cost>(
18+
// { id: 0, quantity: 310, type: "Products", changesDate: "Jul 01 2017", isFavorite: false },
19+
// { id: 1, quantity: 25, type: "Products", changesDate: "Aug 25 2017", isFavorite: false },
20+
// { id: 2, quantity: 127, type: "ForHome", changesDate: "Aug 30 2017", isFavorite: false },
21+
// { id: 3, quantity: 80, type: "Products", changesDate: "Aug 29 2010", isFavorite: false },
22+
// { id: 4, quantity: 32, type: "Products", changesDate: "Nov 13 2017", isFavorite: false },
23+
// { id: 5, quantity: 235, type: "Products", changesDate: "Jan 31 2017", isFavorite: false },
24+
// { id: 6, quantity: 98, type: "ForHome", changesDate: "Feb 20 2017", isFavorite: false },
25+
// { id: 7, quantity: 105, type: "Products", changesDate: "Oct 01 2017", isFavorite: false },
26+
// { id: 8, quantity: 80, type: "Products", changesDate: "Aug 29 2017", isFavorite: false },
27+
// { id: 9, quantity: 75, type: "Products", changesDate: "Aug 08 2017", isFavorite: false },
28+
// { id: 10, quantity: 235, type: "Products", changesDate: "Aug 26 2017", isFavorite: false },
29+
// { id: 11, quantity: 264, type: "ForHome", changesDate: "May 30 2017", isFavorite: false },
30+
// { id: 12, quantity: 78, type: "Products", changesDate: "April 04 2017", isFavorite: false }
31+
// );
32+
33+
getCosts() {
34+
firebase.addValueEventListener(this.onValueEvent, "/money-tracker-2cd33").then(
35+
function(listenerWrapper) {
36+
var path = listenerWrapper.path;
37+
var listeners = listenerWrapper.listeners; // an Array of listeners added
38+
// you can store the wrapper somewhere to later call 'removeEventListeners'
39+
}
40+
);
41+
42+
firebase.query((value) => { console.log(value); }, '/money-tracker-2cd33', { orderBy: { type: 1 }}).then((val) => {
43+
console.log("Val: " + val);
44+
}, (error) => {
45+
alert("Error: " + error);
46+
});
47+
}
48+
49+
onValueEvent(result) {
50+
console.log(JSON.stringify(result));
51+
};
3152

3253
public get costs(): Rx.Observable<Cost[]> {
3354
return this._costs;

app/components/cost/costs.component.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { TextField } from 'ui/text-field';
55

66
import { Cost } from '../../common/protocol';
77
import { CostService } from './cost.service';
8+
import { Config } from '../../common/config';
89

910
@Component({
1011
selector: "costs",
@@ -19,11 +20,7 @@ export class CostsComponent implements OnInit {
1920
constructor(private router: RouterExtensions, private costService: CostService) { }
2021

2122
ngOnInit(): void {
22-
// this.costService.costs.subscribe((costs) => {
23-
// if(costs) {
24-
// this.costs = costs;
25-
// }
26-
// });
23+
this.costService.getCosts();
2724
}
2825

2926
add() {
@@ -50,6 +47,7 @@ export class CostsComponent implements OnInit {
5047
firebase.logout()
5148
.then(() => {
5249
alert("Logged out successfully!");
50+
Config.removeAllUserInfo();
5351
this.router.navigate([""], { clearHistory: true });
5452
}, (error) => {
5553
alert("Error: " + error);

app/components/login/login.component.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class LoginComponent {
3232
})
3333
.then((user) => {
3434
this.isLoading = false;
35-
this.saveUserInfo(user);
35+
Config.saveAllUserInfo(user);
3636
alert("Logged in as " + user['email']);
3737
this.router.navigate(["/tabs"], { clearHistory: true });
3838
}, (error) => {
@@ -47,16 +47,12 @@ export class LoginComponent {
4747
firebase.init({
4848
onAuthStateChanged: function(data) {
4949
if (data.loggedIn) {
50-
that.saveUserInfo(data.user);
50+
Config.saveAllUserInfo(data.user);
5151
that.router.navigate(["/tabs"], { clearHistory: true })
52+
} else {
53+
Config.saveUserToken('');
5254
}
5355
}
5456
});
5557
}
56-
57-
private saveUserInfo(user: User) {
58-
Config.saveEmail(user.email);
59-
Config.saveUserName(user.name);
60-
Config.saveUserUid(user.uid);
61-
}
6258
}

0 commit comments

Comments
 (0)