Skip to content

Commit 253bb98

Browse files
committed
2.3 - Reduced number of request to API
1 parent 8372a61 commit 253bb98

File tree

5 files changed

+42
-40
lines changed

5 files changed

+42
-40
lines changed

src/app/app.component.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,12 @@ export class AppComponent implements OnInit {
112112
this.authService.checkLogged();
113113
this.db2.list('sessions/' + localStorage.getItem('session_key')).valueChanges().subscribe((data) => {
114114
if (data.length > 0) {
115-
// this.clearSession();
116115
// localStorage.setItem('settings', JSON.parse(data['3']));
117-
// this.currentVideo = data['2'];
118-
// this.shareLink = 'https://youtu.be/' + this.currentVideo.id;
119-
// this.playlistInit();
120-
// this.updatePlaylist();
121-
// this.getRelatedVideos();
116+
// this.shared.getSettings();
117+
// this.setDefaultPlayer();
122118
} else {
123-
this.setDefaultPlayer();
124119
this.shared.getSettings();
120+
this.setDefaultPlayer();
125121
}
126122
});
127123
}
@@ -148,6 +144,12 @@ export class AppComponent implements OnInit {
148144
}
149145

150146
setDefaultPlayer() {
147+
this.shared.initFeed().then(data => {
148+
this.initPlayer();
149+
});
150+
}
151+
152+
initPlayer() {
151153
this.globals.currentVideo = this.globals.feedVideos[0];
152154
this.shareLink = 'https://youtu.be/' + this.globals.currentVideo['id'];
153155
this.getRelatedVideos();

src/app/components/category/category.component.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ export class CategoryComponent implements OnInit {
2121
private activatedRoute: ActivatedRoute,
2222
private router: Router
2323
) {
24-
// INIT SETTINGS
25-
this.shared.getSettings();
2624
}
2725

2826
ngOnInit() {
27+
this.initComponent();
28+
}
2929

30+
async initComponent() {
3031
this.activatedRoute.paramMap.subscribe(data => {
3132
if (data['params'].id === 'all') {
3233
this.getFeedVideos();
@@ -39,37 +40,41 @@ export class CategoryComponent implements OnInit {
3940

4041

4142
async getCategories() {
42-
const res = await this.youtube.categories();
43-
this.globals.categories = res;
44-
if (res) {
45-
if (res['items'].find(x => x.id === this.globals.currentCategory)) {
46-
this.getCategoriesVideos(this.globals.currentCategory);
47-
} else {
48-
this.router.navigate(['']);
49-
}
50-
}
43+
this.shared.getSettings().then(() => {
44+
this.youtube.categories().then(catData => {
45+
this.globals.categories = catData;
46+
if (this.globals.categories['items'].find(x => x.id === this.globals.currentCategory)) {
47+
this.loading = true;
48+
this.getCategoriesVideos(this.globals.currentCategory);
49+
} else {
50+
this.router.navigate(['']);
51+
}
52+
});
53+
});
5154
}
5255

5356
async getCategoriesVideos(val: string) {
5457
const res2 = await this.youtube.videoCategories(val);
5558
this.shared.convertVideoObject(res2['items'], 'feedVideos');
56-
5759
await this.shared.initChannel();
60+
this.loading = false;
5861
}
5962

6063
async getFeedVideos() {
61-
if (!this.globals.feedVideos) {
62-
await this.shared.initFeed();
63-
}
64+
await this.shared.initFeed();
6465
await this.shared.initChannel();
6566
this.loading = false;
6667
}
6768

6869
async resetCategories() {
70+
this.loading = true;
6971
this.globals.currentCategory = 'all';
7072
this.router.navigate(['category/all']);
71-
await this.shared.initFeed();
72-
await this.shared.initChannel();
73+
this.globals.feedVideos = null;
74+
await this.shared.initFeed().then(() => {
75+
this.shared.initChannel();
76+
this.loading = false;
77+
});
7378
}
7479

7580
onClickVideo(event: Event, i: any, list: number) {

src/app/services/auth.service.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class AuthService {
1717
private db2: AngularFireDatabase,
1818
public afAuth: AngularFireAuth,
1919
public shared: SharedService,
20-
public globals: GlobalsService
20+
public globals: GlobalsService,
2121
) {
2222
}
2323

@@ -70,14 +70,10 @@ export class AuthService {
7070
localStorage.removeItem('session_key');
7171
localStorage.setItem('session_key', data['2']);
7272

73-
// this.db2.list('sessions/' + data['2']).valueChanges().subscribe((sessionData) => {
74-
// localStorage.removeItem('settings');
75-
// localStorage.removeItem('playlist');
76-
// this.shared.playlist = sessionData[3];
77-
// this.shared.settings = data[3];
78-
// this.shared.updateSettings();
79-
// this.shared.updatePlaylist();
80-
// });
73+
this.db2.list('sessions/' + data['2']).valueChanges().subscribe((sessionData) => {
74+
localStorage.setItem('settings', JSON.parse(data['3']));
75+
this.shared.getSettings();
76+
});
8177
this.shared.updateData('normal login');
8278
}
8379
this.globals.isLogged = true;

src/app/services/shared.service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ export class SharedService {
6363
}
6464

6565
async initFeed() {
66-
await this.getSettings();
67-
const res = await this.youtube.feedVideos();
68-
this.convertVideoObject(res['items'], 'feedVideos');
66+
if (!this.globals.feedVideos) {
67+
await this.getSettings();
68+
const res = await this.youtube.feedVideos();
69+
this.convertVideoObject(res['items'], 'feedVideos');
70+
}
6971
}
7072

7173
async initChannel() {

src/app/services/youtube.service.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export class YoutubeGetVideo {
1818
private globals: GlobalsService,
1919
) {}
2020

21-
// Calling 1 time
2221
async getChannel(query: string) {
2322
if (this.globals.apiKey) {
2423
const res = await this.http.get(`${this.url}channels?${this.channelDetails}&id=${query}&key=${this.globals.apiKey}`)
@@ -27,7 +26,6 @@ export class YoutubeGetVideo {
2726
}
2827
}
2928

30-
// Calling 2 times
3129
async feedVideos() {
3230
if (this.globals.apiKey) {
3331
const res = await this.http.get(`${this.url}videos?${this.videoDetails}${this.feedDetails}&regionCode=${this.globals.regionCode}&maxResults=25&key=${this.globals.apiKey}`)
@@ -36,7 +34,6 @@ export class YoutubeGetVideo {
3634
}
3735
}
3836

39-
// Calling 1 time
4037
async relatedVideos(query: string) {
4138
if (this.globals.apiKey) {
4239
const res = await this.http.get(`${this.url}search?part=snippet&relatedToVideoId=${query}&maxResults=${this.globals.numRelatedRes}&type=video&key=${this.globals.apiKey}`)
@@ -52,7 +49,7 @@ export class YoutubeGetVideo {
5249
return res;
5350
}
5451
}
55-
52+
5653
async categories() {
5754
if (this.globals.apiKey) {
5855
const res = await this.http.get(`${this.url}videoCategories?part=snippet&regionCode=${this.globals.regionCode}&key=${this.globals.apiKey}`)

0 commit comments

Comments
 (0)