Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
astronomersiva committed Oct 29, 2019
1 parent 6d78381 commit c4b4ece
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 68 deletions.
9 changes: 7 additions & 2 deletions app/components/presentation-container.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
" as |slide|
>
{{#if @speaker}}
{{#unless @hasStarted}}
{{#unless this.presentation.hasStarted}}
<div class="timer-overlay w-full md:w-3/4 md:h-full bottom-26 md:top-0">
<button class="underline" {{on "click" @startTicking}}>Start Presentation</button>
<button
class="underline"
{{on "click" this.presentation.startPresentation}}
>
Start Presentation
</button>
</div>
{{/unless}}

Expand Down
6 changes: 6 additions & 0 deletions app/components/presentation-container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';

export default class PresentationContainerComponent extends Component {
@service presentation;
}
53 changes: 0 additions & 53 deletions app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';
import { bind } from '@ember/runloop';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';

export default class ApplicationController extends Controller {
Expand All @@ -11,60 +9,9 @@ export default class ApplicationController extends Controller {
'download'
];

@service socketManager;
@service presentation;
@service router;

@tracked speaker;
@tracked slide;
@tracked download;

init() {
super.init(...arguments);

this.set('keyPressHandler', bind(this, 'respondToKeyPress'));
document.addEventListener('keydown', this.keyPressHandler);

this.addSocketHandlers();
}

addSocketHandlers() {
this.socketManager.socket.on('transition-to', this.transitionSlides, this);
this.socketManager.socket.on('play-state', this.handlePlayStateChange, this);
}

respondToKeyPress({ keyCode }) {
let isLeft = [37, 65].includes(keyCode);
let isRight = [39, 68].includes(keyCode);

if (isLeft) {
this.presentation.goToPrevSlide();
}

if (isRight) {
this.presentation.goToNextSlide()
}
}

transitionSlides(targetSlide) {
this.presentation.goToSlide(targetSlide);
}

handlePlayStateChange(status) {
this.presentation.isRunning = status;
}

@action
startTicking() {
this.presentation.startPresentation();
}

willDestroy() {
document.addEventListener('keydown', this.keyPressHandler);

this.socketManager.socket.off('transition-to', this.transitionSlides);
this.socketManager.socket.off('play-state', this.handlePlayStateChange);

super.willDestroy(...arguments);
}
}
38 changes: 30 additions & 8 deletions app/services/presentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { later } from '@ember/runloop';
import { getOwner } from '@ember/application';
import { action } from '@ember/object';
import { bind } from '@ember/runloop';

export default class PresentationService extends Service {
@service socketManager;
Expand All @@ -29,6 +31,9 @@ export default class PresentationService extends Service {

// this is to reset the timer on all tabs of the speaker
this.socketManager.socket.on('sync-time', this.syncTime, this);

this.set('keyPressHandler', bind(this, 'respondToKeyPress'));
document.addEventListener('keydown', this.keyPressHandler);
}

get isPaused() {
Expand Down Expand Up @@ -92,14 +97,6 @@ export default class PresentationService extends Service {
this.startTime = new Date(newStartTime);
}

startPresentation() {
this.hasStarted = true;
this.startTime = new Date();
this.startTicking();

this.socketManager.socket.emit('reset-time', this.startTime);
}

// Need to check this for memory leaks
startTicking() {
this.elapsedTime = new Date() - this.startTime;
Expand All @@ -110,8 +107,33 @@ export default class PresentationService extends Service {
this.canShowAdjacentSlides = !this.canShowAdjacentSlides;
}

respondToKeyPress({ keyCode }) {
// left arrow or A key
let isLeft = [37, 65].includes(keyCode);
// right arrow or D key
let isRight = [39, 68].includes(keyCode);

if (isLeft) {
this.goToPrevSlide();
}

if (isRight) {
this.goToNextSlide()
}
}

@action
startPresentation() {
this.hasStarted = true;
this.startTime = new Date();
this.startTicking();

this.socketManager.socket.emit('reset-time', this.startTime);
}

willDestroy() {
this.socketManager.socket.off('sync-time', this.syncTime);
document.removeEventListener('keydown', this.keyPressHandler);

super.willDestroy(...arguments);
}
Expand Down
23 changes: 23 additions & 0 deletions app/services/socket-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,32 @@ import { inject as service } from '@ember/service';

export default class SocketManagerService extends Service {
@service('socket-io') socketIOService;
@service presentation;

init() {
super.init(...arguments);
this.socket = this.socketIOService.socketFor(`http://${location.hostname}:1512/`);

this.addSocketHandlers();
}

addSocketHandlers() {
this.socket.on('transition-to', this.transitionToSlide, this);
this.socket.on('play-state', this.handlePlayStateChange, this);
}

transitionToSlide(targetSlide) {
this.presentation.goToSlide(targetSlide);
}

handlePlayStateChange(status) {
this.presentation.isRunning = status;
}

willDestroy() {
this.socket.off('transition-to', this.transitionToSlide);
this.socket.off('play-state', this.handlePlayStateChange);

super.willDestroy(...arguments);
}
}
6 changes: 1 addition & 5 deletions app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
<PresentationContainer
@slide={{slide}}
@speaker={{this.speaker}}
@hasStarted={{this.presentation.hasStarted}}
@startTicking={{this.startTicking}}
@isExport={{true}} />
{{/each}}
{{else}}
<PresentationContainer
@slide={{this.slide}}
@speaker={{this.speaker}}
@hasStarted={{this.presentation.hasStarted}}
@startTicking={{this.startTicking}} />
@speaker={{this.speaker}} />
{{/if}}

{{#unless this.speaker}}
Expand Down

0 comments on commit c4b4ece

Please sign in to comment.