Skip to content

Commit 99a45de

Browse files
committed
Added homepage service
1 parent 8c5a51c commit 99a45de

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
<app-photo-capture></app-photo-capture>
1+
<div *ngIf="homePageState|async as State">
2+
<div *ngIf="!State.takingPhoto">
3+
James is best teacher.
4+
<Button (click)="startTakingPhoto()"> </Button>
5+
6+
</div>
7+
8+
9+
10+
<app-photo-capture *ngIf="State.takingPhoto"></app-photo-capture>
11+
</div>
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { HomePageService} from '../../services/home-page.service';
23

34
@Component({
45
selector: 'app-home-page',
56
templateUrl: './home-page.component.html',
67
styleUrls: ['./home-page.component.scss']
78
})
89
export class HomePageComponent implements OnInit {
10+
homePageState = this.homepageservice.getState();
911

10-
constructor() { }
12+
constructor(private homepageservice:HomePageService) { }
1113

1214
ngOnInit(): void {
1315
}
1416

17+
startTakingPhoto(){
18+
this.homepageservice.updateState({takingPhoto:true});
19+
}
20+
1521
}

src/lib/site-home/components/photo-capture/photo-capture.component.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ export class PhotoCaptureComponent implements OnInit {
3131
}
3232

3333
startCamera() {
34-
if (!!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia)) {
35-
navigator.mediaDevices.getUserMedia(this.constraints).then(this.attachVideo.bind(this)).catch(this.handleError);
36-
} else {
34+
console.log("called");
35+
if ((navigator.mediaDevices && navigator.mediaDevices.getUserMedia)) {
36+
navigator.mediaDevices.getUserMedia(this.constraints).then(this.attachVideo.bind(this)).catch(this.handleError.bind(this));
37+
}
38+
else {
3739
alert('Sorry, camera not available.');
3840
}
3941
}
4042

43+
4144
attachVideo(stream) {
4245
this.renderer.setProperty(this.videoElement.nativeElement, 'srcObject', stream);
4346
this.renderer.listen(this.videoElement.nativeElement, 'play', (event) => {
@@ -48,6 +51,8 @@ export class PhotoCaptureComponent implements OnInit {
4851

4952
handleError(error) {
5053
console.log('Error: ', error);
54+
alert('Sorry, camera not available.');
55+
this.startCamera();
5156
}
5257

5358
capture() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export interface iHomePageState{takingPhoto:boolean}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { HomePageService } from './home-page.service';
4+
5+
describe('HomePageService', () => {
6+
let service: HomePageService;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
service = TestBed.inject(HomePageService);
11+
});
12+
13+
it('should be created', () => {
14+
expect(service).toBeTruthy();
15+
});
16+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Injectable } from '@angular/core';
2+
import { BehaviorSubject } from 'rxjs';
3+
import { iHomePageState } from '../interfaces/home-page.interface';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class HomePageService {
9+
defaultStateStatus = {
10+
takingPhoto:false
11+
}
12+
homePageState = new BehaviorSubject<iHomePageState>(this.defaultStateStatus);
13+
14+
getState(){
15+
return this.homePageState.asObservable();
16+
}
17+
updateState(newState){
18+
const currState = this.homePageState.getValue();
19+
this.homePageState.next({...currState,...newState})
20+
}
21+
}

0 commit comments

Comments
 (0)