Skip to content

Commit 1324783

Browse files
committed
2.3 - Migrating settings to globals service
1 parent 59e4d50 commit 1324783

File tree

7 files changed

+33
-81
lines changed

7 files changed

+33
-81
lines changed

src/app/app.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ <h2>Player</h2>
189189
<div class="app app-feed">
190190
<router-outlet></router-outlet>
191191
</div>
192-
<div [ngClass]="{'active': notify.enabled }" class="notif notif-primary">
192+
<div [ngClass]="{'active': _shared.notify.enabled }" class="notif notif-primary">
193193
<span class="fa fa-cog fa-spin fa-fw"></span>
194-
<p>{{ notify.message }}</p>
194+
<p>{{ _shared.notify.message }}</p>
195195
</div>
196196
</div>
197197
</div>

src/app/app.component.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import { AngularFireDatabase } from 'angularfire2/database';
1313
import { AngularFirestore } from 'angularfire2/firestore';
1414
import * as firebase from 'firebase/app';
1515

16-
import { IRelatedVideo } from './models/related-video.model';
17-
import { INotify } from './models/notify.model';
1816
import { Observable } from 'rxjs/Observable';
1917

2018
@Component({
@@ -27,8 +25,6 @@ export class AppComponent implements OnInit {
2725
@ViewChild('playlistContainer') private myScrollContainer: ElementRef;
2826
@ViewChild('videoItemIDvalue') private videoItemIDvalue: ElementRef;
2927

30-
notify: INotify;
31-
nw: any;
3228
videoRangePercent = 0;
3329

3430
tempPlaylist: Array<any> = [];
@@ -95,7 +91,6 @@ export class AppComponent implements OnInit {
9591
private dbcrud: DbCrudService
9692
) {
9793
this._shared = shared;
98-
this.notify = this._shared.notify;
9994
}
10095

10196
ngOnInit() {
@@ -134,7 +129,6 @@ export class AppComponent implements OnInit {
134129
// To fix update in realtime
135130
this.authService.checkLogged();
136131
this.db2.list('sessions/' + localStorage.getItem('session_key')).valueChanges().subscribe((data) => {
137-
this.shared.updateData('check status');
138132
if (data.length > 0) {
139133
// this.clearSession();
140134
// localStorage.setItem('settings', JSON.parse(data['3']));
@@ -309,13 +303,11 @@ export class AppComponent implements OnInit {
309303
this.getVideo(this.globals.playlist[i]);
310304
} else {
311305
this._shared.triggerNotify('Playlist is empty');
312-
this.updateNotify();
313306
}
314307
}
315308

316309
removePlaylistItem(i: number) {
317310
this._shared.triggerNotify('Video removed');
318-
this.updateNotify();
319311
setTimeout(() => {
320312
if (i === this.currentPlaylistItem) {
321313
this.currentPlaylistItem = -1;
@@ -344,22 +336,22 @@ export class AppComponent implements OnInit {
344336
listType = this.globals.historyVideos[i];
345337
}
346338

339+
// TO BE FIXED duplicated related and feed
347340
if (typeof listType.id.videoId !== 'undefined') {
348341
playlistItem = this.globals.playlist.find(item => item.id.videoId === listType.id.videoId);
349342
} else {
350343
playlistItem = this.globals.playlist.find(item => item.id === listType.id);
344+
// playlistItem = this.globals.playlist.find(item => item.id.videoId === listType.id);
351345
}
352346

353347
if (typeof playlistItem === 'undefined') {
354348
this.globals.playlist.push(listType);
355349
this.updatePlaylist();
356350

357351
this._shared.triggerNotify('Added to playlist');
358-
this.updateNotify();
359352
this.scrollToBottom();
360353
} else {
361354
this._shared.triggerNotify('Video is already in playlist');
362-
this.updateNotify();
363355
}
364356
}
365357

@@ -435,19 +427,19 @@ export class AppComponent implements OnInit {
435427
if (localStorage.length === 1 || !localStorage.getItem('version')) {
436428
console.log('Updating localstorage...');
437429
localStorage.clear();
438-
this._shared.settings = null;
430+
this.globals.settings = null;
439431
this.globals.playlist = [];
440432
}
441433
}
442434

443435
setSettings() {
444436
this._shared.getSettings();
445-
if (this._shared.settings) {
446-
this.regionCode = this._shared.settings.api_settings[1].value;
447-
this.globals.thumbnails = this._shared.settings.form_settings[0].value;
448-
this.displayVideoPlayer = this._shared.settings.form_settings[2].value;
449-
this.repeatMode = this._shared.settings.form_settings[3].value;
450-
this.darkMode = this._shared.settings.form_settings[4].value;
437+
if (this.globals.settings) {
438+
this.regionCode = this.globals.settings.api_settings[1].value;
439+
this.globals.thumbnails = this.globals.settings.form_settings[0].value;
440+
this.displayVideoPlayer = this.globals.settings.form_settings[2].value;
441+
this.repeatMode = this.globals.settings.form_settings[3].value;
442+
this.darkMode = this.globals.settings.form_settings[4].value;
451443
}
452444
}
453445

@@ -667,22 +659,8 @@ export class AppComponent implements OnInit {
667659

668660

669661
copyShareLink() {
670-
if (!this.notify.enabled) {
671662
document.execCommand('Copy');
672663
this._shared.triggerNotify('Copied');
673-
this.updateNotify();
674-
} else {
675-
setTimeout(() => {
676-
document.execCommand('Copy');
677-
this._shared.triggerNotify('Copied');
678-
this.updateNotify();
679-
}, 1000);
680-
}
681-
}
682-
683-
updateNotify() {
684-
this.notify = this._shared.notify;
685-
setTimeout(() => this.notify = this._shared.notify, 1000);
686664
}
687665

688666
timeFormat(time: number) {

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import { SharedService } from '../../services/shared.service';
55
import { GlobalsService } from '../../services/globals.service';
66
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
77

8-
import { IFeedVideo } from '../../models/feed-video.model';
9-
import { IChannelList } from '../../models/channel.model';
10-
118
@Component({
129
selector: 'app-category',
1310
templateUrl: './category.component.html',
@@ -58,7 +55,7 @@ export class CategoryComponent implements OnInit {
5855
} else {
5956
this.globals.currentCategory = data['params'].id;
6057
}
61-
this.setSettings();
58+
this.initCategories();
6259
});
6360
}
6461

@@ -83,16 +80,13 @@ export class CategoryComponent implements OnInit {
8380
this.getChannelTrending();
8481
}
8582

86-
async setSettings() {
87-
if (this._shared.settings) {
88-
this.globals.listGrid = this._shared.settings.form_settings[1].value;
89-
this._shared.setApiSettings();
83+
async initCategories() {
84+
this._shared.setApiSettings();
85+
if (this.globals.settings) {
9086
this.getCategories();
9187
} else {
9288
await this._shared.initSettings().then(
9389
(done) => {
94-
this.globals.listGrid = this._shared.settings.form_settings[1].value;
95-
this._shared.setApiSettings();
9690
this.getCategories();
9791
}
9892
);
@@ -143,7 +137,6 @@ export class CategoryComponent implements OnInit {
143137
}
144138
}
145139

146-
147140
onCopyVideoItemLink(i: number, list: number) {
148141
this._app.onCopyVideoItemLink(i, list);
149142
}

src/app/components/youtube-settings.component.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { HttpClient } from '@angular/common/http';
1717
export class SettingsComponent implements OnInit {
1818

1919
finished = false;
20-
notify: any;
2120

2221
_shared: any;
2322
_fb: any;
@@ -42,7 +41,6 @@ export class SettingsComponent implements OnInit {
4241
this._app = app;
4342
this._search = search;
4443
this._category = category;
45-
this.notify = this._shared.notify;
4644
}
4745

4846
ngOnInit() {
@@ -83,14 +81,13 @@ export class SettingsComponent implements OnInit {
8381
Object.keys(data.settings).map(i => {
8482
this.globals.internal_settings[i].value = data.settings[i];
8583
});
86-
this._shared.settings.form_settings = this.globals.internal_settings;
84+
this.globals.settings.form_settings = this.globals.internal_settings;
8785
this._shared.updateSettings();
8886

8987
this._app.setSettings();
9088
this._app.checkVolumeRange();
9189

9290
this._shared.triggerNotify('Changed');
93-
this.updateNotify();
9491
});
9592
}
9693

@@ -102,41 +99,31 @@ export class SettingsComponent implements OnInit {
10299
}
103100

104101
getDefaultSettings() {
105-
if (!this._shared.settings) {
106-
this._shared.setApiSettings();
107-
}
108-
this.globals.internal_settings = this._shared.settings.form_settings;
109-
this.globals.external_settings = this._shared.settings.api_settings;
102+
this._shared.setApiSettings();
103+
this.globals.internal_settings = this.globals.settings.form_settings;
104+
this.globals.external_settings = this.globals.settings.api_settings;
110105
this.initExternalForm();
111106
this.finished = true;
112107
this.setForm();
113108
}
114109

115-
updateNotify() {
116-
this.notify = this._shared.notify;
117-
setTimeout(() => this.notify = this._shared.notify, 1000);
118-
}
119-
120110
externalSave() {
121111
if (this.externalSettings.valid) {
122112
this.globals.external_settings[0].value = this.externalSettings.controls.fcApi.value;
123113
this.globals.external_settings[1].value = this.externalSettings.controls.fcRegion.value;
124114
this.globals.external_settings[2].value = parseInt(this.externalSettings.controls.fcSearchresults.value, 10);
125115
this.globals.external_settings[3].value = parseInt(this.externalSettings.controls.fcRelatedResults.value, 10);
126-
this._shared.settings.api_settings = this.globals.external_settings;
127-
this._shared.feedVideos = null;
116+
this.globals.settings.api_settings = this.globals.external_settings;
117+
this.globals.feedVideos = null;
128118

129119
this._shared.updateSettings();
130-
131120
this._shared.setApiSettings();
132121
this._app.setSettings();
133122
this._app.getFeedVideos();
134123

135124
this._shared.triggerNotify('Saved');
136-
this.updateNotify();
137125
} else {
138126
this._shared.triggerNotify('Please check external settings');
139-
this.updateNotify();
140127
}
141128
}
142129
}

src/app/services/globals.service.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { Injectable } from '@angular/core';
2+
import { IFeedVideo } from '../models/feed-video.model';
3+
import { ISearchVideo } from '../models/search-video.model';
4+
import { IRelatedVideo } from '../models/related-video.model';
25

36
@Injectable()
47
export class GlobalsService {
58

6-
relatedVideos: Array<any>;
7-
feedVideos: Array<any>;
9+
relatedVideos: Array<IRelatedVideo>;
10+
feedVideos: Array<IFeedVideo>;
811
playlist: Array<any> = [];
912
lastSearchedVideos: Array<any>;
1013
historyVideos: Array<any> = [];
11-
searchedVideos: Array<any>;
14+
searchedVideos: Array<ISearchVideo>;
1215

1316
categories: any;
1417
currentCategory: string;

src/app/services/shared.service.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ import { GlobalsService } from './globals.service';
99
@Injectable()
1010
export class SharedService {
1111

12-
public historyVideos: Array<any> = [];
13-
public settings: any;
14-
public channel: any;
15-
public isLogged = false;
16-
17-
_update: any;
18-
1912
notify = {
2013
enabled: false,
2114
message: 'No message'
@@ -41,11 +34,11 @@ export class SharedService {
4134
async getSettings() {
4235
if (localStorage.length < 1) {
4336
const res = await this.initSettings();
44-
this.settings = res;
37+
this.globals.settings = res;
4538
localStorage.setItem('settings', JSON.stringify(res));
4639
localStorage.setItem('session_key', this.generateKey());
4740
} else {
48-
this.settings = JSON.parse(localStorage.getItem('settings'));
41+
this.globals.settings = JSON.parse(localStorage.getItem('settings'));
4942
}
5043
}
5144

@@ -67,11 +60,11 @@ export class SharedService {
6760
}
6861

6962
async setApiSettings() {
70-
if (this.settings) {
71-
this.youtube.defaultApiSet(this.settings);
63+
if (this.globals.settings) {
64+
this.youtube.defaultApiSet(this.globals.settings);
7265
} else {
7366
await this.getSettings();
74-
this.youtube.defaultApiSet(this.settings);
67+
this.youtube.defaultApiSet(this.globals.settings);
7568
}
7669
}
7770

@@ -80,7 +73,7 @@ export class SharedService {
8073
}
8174

8275
updateSettings() {
83-
localStorage.setItem('settings', JSON.stringify(this.settings));
76+
localStorage.setItem('settings', JSON.stringify(this.globals.settings));
8477
this.setLocalVersion();
8578
}
8679

src/app/services/youtube.service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@ export class YoutubeGetVideo {
7373
}
7474

7575
async videoCategories(category: string) {
76-
if (this.apiKey) {
7776
const res = await this.http.get(`${this.url}videos?part=snippet,contentDetails,statistics&chart=mostPopular&maxResults=25&videoCategoryId=${category}&regionCode=${this.regionCode}&key=${this.apiKey}`)
7877
.map(response => response).toPromise();
7978
return res;
80-
}
8179
}
8280

8381
async statsVideos(query: string) {

0 commit comments

Comments
 (0)