Skip to content

Commit

Permalink
Better Live Detection
Browse files Browse the repository at this point in the history
* Better Live Detection (fixes #89)
* Fix linting errors
  • Loading branch information
clarkmalmgren committed Oct 21, 2017
1 parent 1e220b1 commit af97d22
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bolingbrook-church",
"version": "1.2.14",
"version": "1.2.15",
"license": "MIT",
"angular-cli": {},
"scripts": {
Expand Down
12 changes: 7 additions & 5 deletions src/app/components/pages/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import
})
export class HomeComponent implements OnInit {

streaming: boolean = false;
live: boolean = false;
sources: BackgroundVideoSource[];
sermon: Sermon;

Expand All @@ -27,11 +27,13 @@ export class HomeComponent implements OnInit {

this.sermons.latest()
.subscribe((sermon) => { this.sermon = sermon; });
}

get live(): boolean {
const now = moment.tz('America/Chicago');
return (now.day() === 6 && now.hour() >= 11 && now.hour() < 14);
this.sermons
.liveToday()
.subscribe((liveToday) => {
const now = moment.tz('America/Chicago');
this.live = (liveToday && now.day() === 6 && now.hour() >= 11 && now.hour() < 14);
});
}

}
5 changes: 5 additions & 0 deletions src/app/services/firebase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ export class Database {

constructor(private fb: FirebaseService) {}

exists(path: string): Observable<Boolean> {
return observe(this.fb.database.ref(path).once('value'))
.map((snap: firebase.database.DataSnapshot) => snap.exists());
}

getOnce(path: string): Observable<any> {
return observe(this.fb.database.ref(path).once('value'))
.map((snap: firebase.database.DataSnapshot) => snap.val());
Expand Down
32 changes: 32 additions & 0 deletions src/app/services/sermon.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, sinon, async, MockBuilder } from 'testing';
import {
Database,
Storage,
Sermon,
SermonService,
Observable
} from './index';
import * as moment from 'moment';
import 'moment-timezone';

describe('SermonService', () => {
describe('liveToday', () => {
it('should work', async(() => {
const db = MockBuilder
.of(Database)
.withStub('exists', Observable.of(true))
.build();

const store = MockBuilder.of(Storage).build();

const service = new SermonService(db, store);
service
.liveToday()
.subscribe((isLive) => {
expect(isLive).to.be.true;
expect(db.exists).to.have.been.calledOnce;
});

}));
});
});
5 changes: 5 additions & 0 deletions src/app/services/sermon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export class SermonService {
return cleaned;
}

liveToday(date: moment.Moment = moment.tz('America/Chicago')): Observable<Boolean> {
const path = date.format('YYYY-MM-DD');
return this.db.exists(path);
}

getSermon(date: string): Observable<Sermon> {
return this.db.getOnce(`data/sermons/${date}`)
.map((sermon: Sermon) => {
Expand Down

0 comments on commit af97d22

Please sign in to comment.