Skip to content

Commit af3051c

Browse files
Merge pull request #28 from NativeScript/update-demo-apps
Update demo apps
2 parents 0de3192 + a2cf923 commit af3051c

37 files changed

+715
-138
lines changed

README.md

Lines changed: 282 additions & 40 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
<StackLayout>
2-
<Label text="{{userId}}"></Label>
3-
<FacebookLoginButton (login)="onLogin($event)" (logout)="onLogout($event)"></FacebookLoginButton>
4-
<Button text="Custom Login Button" (tap)="manualLogin()"></Button>
5-
<Button text="Custom Logout Button" (tap)="manualLogout()"></Button>
6-
</StackLayout>
1+
<page-router-outlet></page-router-outlet>

demo-angular/app/app.component.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,4 @@ import * as Facebook from "nativescript-facebook";
55
selector: "ns-app",
66
templateUrl: "app.component.html",
77
})
8-
export class AppComponent {
9-
userId: string = "not logged in";
10-
11-
constructor(private ref: ChangeDetectorRef) {
12-
}
13-
14-
onLogin = function (eventData: Facebook.LoginEventData) {
15-
if (eventData.error) {
16-
alert("Error during login: " + eventData.error);
17-
} else {
18-
this.userId = "UserId: " + eventData.loginResponse.userId;
19-
this.ref.detectChanges();
20-
}
21-
};
22-
23-
manualLogin = function () {
24-
Facebook.login((error, loginResponse) => {
25-
this.userId = "UserId: " + loginResponse.userId;
26-
this.ref.detectChanges();
27-
});
28-
};
29-
30-
manualLogout = function () {
31-
Facebook.logout(() => {
32-
this.userId = "not logged in";
33-
this.ref.detectChanges();
34-
});
35-
};
36-
37-
public onLogout() {
38-
this.userId = "not logged in";
39-
this.ref.detectChanges();
40-
}
41-
}
8+
export class AppComponent {}

demo-angular/app/app.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const config = {
2+
FACEBOOK_GRAPH_API_URL: "https://graph.facebook.com/v2.9"
3+
};

demo-angular/app/app.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ of writing your own CSS rules. For a full list of class names in the theme
1010
refer to http://docs.nativescript.org/ui/theme.
1111
*/
1212
@import 'nativescript-theme-core/css/core.light.css';
13+

demo-angular/app/app.module.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
1+
import { NgModule, NO_ERRORS_SCHEMA, NgModuleFactoryLoader } from "@angular/core";
22
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
3+
import { NativeScriptRouterModule, NSModuleFactoryLoader } from "nativescript-angular/router";
34
import { AppComponent } from "./app.component";
4-
5+
import { LoginModule } from "./pages/login/login.module";
6+
import { HomeModule } from "./pages/home/home.module";
57
import { NativeScriptFacebookModule } from "nativescript-facebook/angular";
6-
78
import * as application from 'application';
9+
import { routes } from "./app.routing";
10+
import { NavigationService } from "./services/navigation.service";
11+
812
let nsFacebook = require('nativescript-facebook');
913

1014
application.on(application.launchEvent, function (args) {
1115
nsFacebook.init("1771472059772879");
1216
});
1317

1418
@NgModule({
15-
bootstrap: [ AppComponent ],
19+
bootstrap: [AppComponent],
1620
imports: [
1721
NativeScriptModule,
18-
NativeScriptFacebookModule
22+
NativeScriptFacebookModule,
23+
NativeScriptRouterModule,
24+
NativeScriptRouterModule.forRoot(routes)
25+
],
26+
providers: [
27+
NavigationService,
28+
{ provide: NgModuleFactoryLoader, useClass: NSModuleFactoryLoader }
1929
],
20-
declarations: [ AppComponent ],
21-
schemas: [ NO_ERRORS_SCHEMA ]
30+
declarations: [AppComponent],
31+
schemas: [NO_ERRORS_SCHEMA]
2232
})
2333
export class AppModule { }

demo-angular/app/app.routing.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const routes = [
2+
{
3+
path: "home",
4+
loadChildren: "./pages/home/home.module#HomeModule"
5+
},
6+
{
7+
path: "login",
8+
loadChildren: "./pages/login/login.module#LoginModule"
9+
},
10+
{
11+
path: "",
12+
redirectTo: "login",
13+
pathMatch: "full"
14+
}
15+
16+
];
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.label {
2+
font-size: 10;
3+
font-weight: bold;
4+
padding-top: 10;
5+
}
6+
7+
.home {
8+
margin-left: 30;
9+
margin-right: 30;
10+
margin-top: 20%;
11+
}
12+
13+
.home .buttons {
14+
margin-top: 30%;
15+
}
16+
17+
.home .info {
18+
margin-top: 20;
19+
horizontal-align: center;
20+
}
21+
22+
.avatar {
23+
border-radius: 100;
24+
width: 150;
25+
height: auto;
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<StackLayout class="home">
2+
<Image *ngIf="avatarUrl" src="{{avatarUrl}}" class="avatar"></Image>
3+
<StackLayout class="info">
4+
<Label text="ID:" class="label"></Label>
5+
<Label text="{{ userId }}"></Label>
6+
<Label text="NAME:" class="label"></Label>
7+
<Label text="{{ username }}"></Label>
8+
</StackLayout>
9+
<StackLayout class="buttons">
10+
<FacebookLoginButton (logout)="onLogout($event)"></FacebookLoginButton>
11+
<Button text="Log out (custom)" (tap)="logout()"></Button>
12+
</StackLayout>
13+
</StackLayout>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Component, ChangeDetectorRef } from "@angular/core";
2+
import * as Facebook from "nativescript-facebook";
3+
import { NavigationService } from "../../services/navigation.service";
4+
import { config } from "../../app.config";
5+
let http = require("http");
6+
let appSettings = require("application-settings");
7+
8+
@Component({
9+
selector: "home",
10+
moduleId: module.id,
11+
templateUrl: "home.component.html",
12+
styleUrls: ["home.component.css"]
13+
})
14+
export class HomeComponent {
15+
accessToken: string = appSettings.getString("access_token");
16+
userId: string;
17+
username: string;
18+
avatarUrl: string;
19+
20+
constructor(private ref: ChangeDetectorRef, private navigationService: NavigationService) {
21+
// Get logged in user's info
22+
http.getJSON(config.FACEBOOK_GRAPH_API_URL + "/me?access_token=" + this.accessToken).then((res) => {
23+
this.username = res.name;
24+
this.userId = res.id;
25+
26+
// Get logged in user's avatar
27+
// ref: https://github.com/NativeScript/NativeScript/issues/2176
28+
http.getJSON(config.FACEBOOK_GRAPH_API_URL + "/" + this.userId + "/picture?type=large&redirect=false&access_token=" + this.accessToken).then((res) => {
29+
this.avatarUrl = res.data.url;
30+
this.ref.detectChanges();
31+
}, function (err) {
32+
alert("Error getting user info: " + err);
33+
});
34+
}, function (err) {
35+
alert("Error getting user info: " + err);
36+
});
37+
}
38+
39+
onLogout(eventData: Facebook.LoginEventData) {
40+
if (eventData.error) {
41+
alert("Error during login: " + eventData.error);
42+
} else {
43+
appSettings.clear();
44+
this.navigationService.go(['login'], "slideRight");
45+
}
46+
47+
}
48+
49+
logout() {
50+
Facebook.logout(() => {
51+
appSettings.clear();
52+
this.navigationService.go(['login'], "slideRight");
53+
});
54+
}
55+
}

0 commit comments

Comments
 (0)