Skip to content

Commit 95deeeb

Browse files
committed
2.3 - Renamed services
1 parent 057c819 commit 95deeeb

10 files changed

+282
-37
lines changed

src/app/app.component.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, ElementRef, ViewChild, OnInit, HostListener } from '@angular/core';
2-
import { YoutubeGetVideo } from './shared/youtube.service';
3-
import { SharedService } from './shared/lists.service';
2+
import { YoutubeGetVideo } from './services/youtube.service';
3+
import { SharedService } from './services/shared.service';
44
import { Event } from '@angular/router/src/events';
55
import { DragulaService } from 'ng2-dragula/ng2-dragula';
66

@@ -134,31 +134,22 @@ export class AppComponent implements OnInit {
134134
// To fix update in realtime
135135
this.authService.checkLogged();
136136
this.db2.list('sessions/' + localStorage.getItem('session_key')).valueChanges().subscribe((data) => {
137+
this.shared.updateData('check status');
137138
if (data.length > 0) {
138-
this.clearSession();
139-
localStorage.setItem('settings', JSON.parse(data['3']));
140-
this.currentVideo = data['2'];
141-
this.shareLink = 'https://youtu.be/' + this.currentVideo.id;
142-
this.playlistInit();
143-
this.updatePlaylist();
144-
this.getRelatedVideos();
139+
// this.clearSession();
140+
// localStorage.setItem('settings', JSON.parse(data['3']));
141+
// this.currentVideo = data['2'];
142+
// this.shareLink = 'https://youtu.be/' + this.currentVideo.id;
143+
// this.playlistInit();
144+
// this.updatePlaylist();
145+
// this.getRelatedVideos();
145146
} else {
146147
this.setDefaultPlayer();
147148
}
148149
this.setSettings();
149150
});
150151
}
151152

152-
refreshNow() {
153-
console.log('si-a facut update');
154-
}
155-
156-
refresh() {
157-
this.clearSession();
158-
// this.playlistInit();
159-
// this.getRelatedVideos();
160-
}
161-
162153
// ---------------- Init player ----------------
163154

164155
savePlayer(player) {
@@ -295,8 +286,6 @@ export class AppComponent implements OnInit {
295286
} else {
296287
playlistItem = this._shared.playlist.find(item => item.id === this.currentVideoObject[0].id);
297288
}
298-
console.log(this._shared.playlist);
299-
console.log(this.currentVideoObject[0].id);
300289
this.currentPlaylistItem = this._shared.playlist.indexOf(playlistItem);
301290
}
302291

@@ -447,8 +436,6 @@ export class AppComponent implements OnInit {
447436
console.log('Updating localstorage...');
448437
localStorage.clear();
449438
this._shared.settings = null;
450-
this._shared.playlist = null;
451-
452439
this._shared.playlist = [];
453440
}
454441
}

src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { routes } from './app.router';
88
import { DragulaModule } from 'ng2-dragula/ng2-dragula';
99

1010
import { AppComponent } from './app.component';
11-
import { SharedService } from './shared/lists.service';
12-
import { YoutubeGetVideo } from './shared/youtube.service';
11+
import { SharedService } from './services/shared.service';
12+
import { YoutubeGetVideo } from './services/youtube.service';
1313
import { SettingsComponent } from './components/youtube-settings.component';
1414
import { SearchComponent } from './components/youtube-search.component';
1515
import { AboutComponent } from './components/youtube-about.component';

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { YoutubeGetVideo } from '../../shared/youtube.service';
2+
import { YoutubeGetVideo } from '../../services/youtube.service';
33
import { AppComponent } from '../../app.component';
4-
import { SharedService } from '../../shared/lists.service';
4+
import { SharedService } from '../../services/shared.service';
55
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
66

77
import { IFeedVideo } from '../../models/feed-video.model';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { AppComponent } from '../app.component';
3-
import { SharedService } from '../shared/lists.service';
3+
import { SharedService } from '../services/shared.service';
44

55
@Component({
66
selector: 'app-history',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { YoutubeGetVideo } from '../shared/youtube.service';
2+
import { YoutubeGetVideo } from '../services/youtube.service';
33
import { AppComponent } from '../app.component';
4-
import { SharedService } from '../shared/lists.service';
4+
import { SharedService } from '../services/shared.service';
55
import { FormControl, FormGroup, Validators } from '@angular/forms';
66

77
import { IFeedVideo } from '../models/feed-video.model';

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { FormControl, FormArray, FormGroup, FormBuilder, ReactiveFormsModule, Va
33
import { AppComponent } from '../app.component';
44
import { SearchComponent } from './youtube-search.component';
55
import { CategoryComponent } from './category/category.component';
6-
import { SharedService } from '../shared/lists.service';
7-
import { NumberVal } from '../shared/validators.service';
6+
import { SharedService } from '../services/shared.service';
7+
import { NumberVal } from '../services/validators.service';
88
import { HttpClient } from '@angular/common/http';
99

1010
@Component({

src/app/services/auth.service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { SharedService } from '../shared/lists.service';
2+
import { SharedService } from '../services/shared.service';
33
// DB
44
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
55
import { AngularFireAuth } from 'angularfire2/auth';
@@ -35,8 +35,8 @@ export class AuthService {
3535
// Sign-out successful.
3636
console.log('Sign-out successful');
3737
localStorage.clear();
38+
this.shared.updateData('logout');
3839
this.shared.isLogged = false;
39-
// location.reload();
4040
}, (error) => {
4141
// An error happened.
4242
console.log('An error happened sign out');
@@ -51,7 +51,6 @@ export class AuthService {
5151
this.itemsRef.valueChanges().subscribe(data => {
5252
this.db2.list('users/' + authData.user.uid).valueChanges().subscribe(userData => {
5353
if (userData.length === 0) {
54-
console.log('First time login');
5554
// First time login create user and session
5655
const profile = authData.additionalUserInfo.profile;
5756
const defaultUser = {
@@ -65,12 +64,12 @@ export class AuthService {
6564

6665
afList.set(authData.user.uid, defaultUser);
6766
this.initSession(currentDetails);
67+
this.shared.updateData('first time login');
6868
} else {
6969
// Get session and settings from logged user
70-
console.log('Normal login');
7170
localStorage.removeItem('session_key');
7271
localStorage.setItem('session_key', data['2']);
73-
this.shared.isAffected(true);
72+
7473
// this.db2.list('sessions/' + data['2']).valueChanges().subscribe((sessionData) => {
7574
// localStorage.removeItem('settings');
7675
// localStorage.removeItem('playlist');
@@ -79,6 +78,7 @@ export class AuthService {
7978
// this.shared.updateSettings();
8079
// this.shared.updatePlaylist();
8180
// });
81+
this.shared.updateData('normal login');
8282
}
8383
this.shared.isLogged = true;
8484
});

src/app/services/shared.service.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { Injectable } from '@angular/core';
2+
import { YoutubeGetVideo } from './youtube.service';
3+
import { HttpClient } from '@angular/common/http';
4+
import { Observable } from 'rxjs/Observable';
5+
import 'rxjs/add/operator/map';
6+
7+
@Injectable()
8+
export class SharedService {
9+
10+
public feedVideos: Array<any>;
11+
public playlist: Array<any> = [];
12+
public lastSearchedVideos: Array<any>;
13+
public historyVideos: Array<any> = [];
14+
public settings: any;
15+
public channel: any;
16+
public videoCategories: any;
17+
public user: any;
18+
public isLogged = false;
19+
20+
_update: any;
21+
22+
notify = {
23+
enabled: false,
24+
message: 'No message'
25+
};
26+
27+
constructor(
28+
private youtube: YoutubeGetVideo,
29+
private http: HttpClient,
30+
) {}
31+
32+
33+
generateKey() {
34+
const code = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
35+
const length = 16;
36+
let rtn = '';
37+
for (let i = 0; i < length; i++) {
38+
rtn += code.charAt(Math.floor(Math.random() * code.length));
39+
}
40+
return rtn;
41+
}
42+
43+
async getSettings() {
44+
if (localStorage.length < 1) {
45+
const res = await this.initSettings();
46+
this.settings = res;
47+
localStorage.setItem('settings', JSON.stringify(res));
48+
localStorage.setItem('session_key', this.generateKey());
49+
} else {
50+
this.settings = JSON.parse(localStorage.getItem('settings'));
51+
}
52+
}
53+
54+
async initSettings() {
55+
const res = await this.http.get('assets/settings.json')
56+
.map(response => response).toPromise();
57+
return res;
58+
}
59+
60+
async initFeed() {
61+
await this.setApiSettings();
62+
const res = await this.youtube.feedVideos();
63+
this.feedVideos = res['items'];
64+
}
65+
66+
async initChannel() {
67+
const res = await this.youtube.getChannel(this.feedVideos[0].snippet.channelId);
68+
this.channel = res;
69+
}
70+
71+
async setApiSettings() {
72+
if (this.settings) {
73+
this.youtube.defaultApiSet(this.settings);
74+
} else {
75+
await this.getSettings();
76+
this.youtube.defaultApiSet(this.settings);
77+
}
78+
}
79+
80+
updateData(state: string) {
81+
console.log(state);
82+
}
83+
84+
updateSettings() {
85+
localStorage.setItem('settings', JSON.stringify(this.settings));
86+
this.setLocalVersion();
87+
}
88+
89+
getPlaylist() {
90+
this.playlist = JSON.parse(localStorage.getItem('playlist'));
91+
}
92+
93+
updatePlaylist() {
94+
localStorage.setItem('playlist', JSON.stringify(this.playlist));
95+
this.setLocalVersion();
96+
}
97+
98+
setLocalVersion() {
99+
if (localStorage.getItem('version') === null) {
100+
localStorage.setItem('version', '1');
101+
}
102+
}
103+
104+
triggerNotify(message: string) {
105+
this.notify.enabled = true;
106+
this.notify.message = message;
107+
setTimeout(() => this.notify.enabled = false, 1000);
108+
}
109+
110+
addHistoryVideo(data: any) {
111+
let key;
112+
for (key in this.historyVideos) {
113+
if (this.historyVideos[key].id === data.id) {
114+
this.historyVideos.splice(key, 1);
115+
if (this.historyVideos[this.historyVideos.length - 1] === data) {
116+
this.historyVideos.splice(-1, 1);
117+
}
118+
}
119+
}
120+
this.historyVideos.unshift(data);
121+
}
122+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Injectable } from '@angular/core';
2+
import { FormControl, ValidatorFn, AbstractControl } from '@angular/forms';
3+
4+
@Injectable()
5+
export class NumberVal {
6+
7+
constructor() { }
8+
9+
static max(max: number): ValidatorFn {
10+
return (control: FormControl): { [key: string]: boolean } | null => {
11+
12+
const val: number = control.value;
13+
if (control.pristine || control.pristine) {
14+
return null;
15+
}
16+
if (val <= max) {
17+
return null;
18+
}
19+
return { 'max': true };
20+
};
21+
}
22+
23+
static min(min: number): ValidatorFn {
24+
return (control: FormControl): { [key: string]: boolean } | null => {
25+
const val: number = control.value;
26+
if (control.pristine || control.pristine) {
27+
return null;
28+
}
29+
if (val >= min) {
30+
return null;
31+
}
32+
return { 'min': true };
33+
};
34+
}
35+
36+
static isNumber(value: boolean): ValidatorFn {
37+
return (control: FormControl): { [key: string]: boolean } | null => {
38+
const valid = /\d{0,9}/.test(control.value);
39+
if (valid) {
40+
return null;
41+
}
42+
return { 'invalid': true };
43+
};
44+
}
45+
46+
}

0 commit comments

Comments
 (0)