Skip to content

Commit

Permalink
Spins out factory and controller to their own files.
Browse files Browse the repository at this point in the history
  • Loading branch information
camilopayan committed Jul 9, 2015
1 parent 3a7b32c commit 49b4956
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 78 deletions.
12 changes: 7 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

</head>
<body>
<div id="timer" data-ng-controller="TimerController">
<div id="time">{{ pom.timeLeft | timeify }}</div>
<div>Current phase: {{ pom.state ? pom.state : "STOPPED" }}</div>
<div><a ng-click="pom.start()">Start</a> <a ng-click="pom.cancel()">Cancel</a></div>
<div id="timer" data-ng-controller="TimerController as tc">
<div id="time">{{ tc.timeLeft | timeify }}</div>
<div>Current phase: {{ tc.phase ? tc.phase : "STOPPED" }}</div>
<div><a ng-click="tc.start()">Start</a> <a ng-click="tc.cancel()">Cancel</a></div>
<div>
Poms done: {{ pom.count }}
Poms done: {{ tc.poms }}
</div>
</div>

Expand All @@ -21,5 +21,7 @@


<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
<script src="js/services/pomTimerService.js" type="text/javascript" charset="utf-8"></script>
<script src="js/timer/timerController.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
76 changes: 3 additions & 73 deletions js/app.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,9 @@
var app = angular.module("EstimatePom", [])
// Factory to keep the timer state
.controller('TimerController', ['$scope', '$timeout',
function($scope, $timeout){
$scope.pom = {};
settings = {};
settings.pomTime = 5;
settings.shortRestTime = 3;
settings.longRestTime = 3;
$scope.pom.state = "POMODORO";
$scope.pom.count = 0;
$scope.pom.startTime = settings.pomTime;
$scope.pom.timeoutId = null;
//Kept in milliseconds.
$scope.pom.timeLeft = settings.pomTime * 1000;


$scope.pom.start = function(){
$scope.pom.state = "POMODORO";
tick();
};

$scope.pom.cancel = function(){
clearTimeout();
$scope.pom.state = "POMODORO";
$scope.pom.timeLeft = settings.pomTime * 1000;
};

var finishPhase = function(){
switch ( $scope.pom.state ){
case "POMODORO":
$scope.pom.count++;
if ($scope.pom.count % 4 === 0 && $scope.pom.count > 0) {
$scope.pom.state = "LONGBREAK";
$scope.pom.timeLeft = settings.longRestTime * 1000;
break;
}
$scope.pom.state = "SHORTBREAK";
$scope.pom.timeLeft = settings.shortRestTime * 1000;
break;
case "LONGBREAK":
case "SHORTBREAK":
$scope.pom.state = "POMODORO";
$scope.pom.timeLeft = settings.pomTime * 1000;
break;
}
};

var tick = function (){
$scope.pom.timeLeft -= 1000;

if($scope.pom.timeLeft <= 0){
finishPhase();
if($scope.pom.state === "POMODORO") {
clearTimeout();
return;
}
}

$scope.pom.timeoutId = $timeout( tick, 1000 );
};

var clearTimeout = function(){
$timeout.cancel( $scope.pom.timeoutId );
$scope.pom.timeoutId = null;
};

}]
)

var app = angular.module("EstimatePom", []);

// Seconds to Minutes:Seconds filter
.filter('timeify',
app.filter('timeify',
function() {
return function(input){
var time = input/1000;
return function(time){
m = time / 60;
s = time % 60;
return Math.floor(m) + ':' + ( s < 10 ? ('0'+s) : s );
Expand Down
96 changes: 96 additions & 0 deletions js/services/pomTimerService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
app.factory('PomTimer',
[ '$timeout', '$log',
function($timeout, $log){
var settings = {};
settings.pomTime = 5;
settings.shortRestTime = 3;
settings.longRestTime = 3;

var pom = {};
pom.state = "POMODORO";
pom.count = 0;
pom.startTime = settings.pomTime;
pom.timeoutId = null;
//Kept in milliseconds.
pom.timeLeft = settings.pomTime;

var callbacks = {};
callbacks.tick = [];
callbacks.phase = [];

function tick(){
pom.timeLeft -= 1;
var done = false;
if(pom.timeLeft <= 0){
finishPhase();
if (pom.state === "POMODORO") {
clearTimeout();
done = true;
}
}
angular.forEach(callbacks.tick, function(callback){
callback();
});
if (!done) {
pom.timeoutId = $timeout( tick, 1000 );
}
}

function finishPhase(){
switch ( pom.state ){
case "POMODORO":
pom.count++;
if (pom.count % 4 === 0 && pom.count > 0) {
pom.state = "LONGBREAK";
pom.timeLeft = settings.longRestTime;
break;
}
pom.state = "SHORTBREAK";
pom.timeLeft = settings.shortRestTime;
break;
case "LONGBREAK":
case "SHORTBREAK":
pom.state = "POMODORO";
pom.timeLeft = settings.pomTime;
break;
}
angular.forEach(callbacks.phase, function(callback){
callback();
});
}

function clearTimeout(){
$timeout.cancel( pom.timeoutId );
pom.timeoutId = null;
}

// Public service API code.
var PomTimer = {};
PomTimer.startTimer = function(){
pom.state = "POMODORO";
pom.timeLeft = settings.pomTime;
tick();
};
PomTimer.stopTimer = function(){
clearTimeout();
pom.state = "POMODORO";
pom.timeLeft = settings.pomTime;
};
PomTimer.registerTickCallback = function( callback ){
callbacks.tick.push(callback);
};
PomTimer.registerPhaseCallback = function( callback ){
callbacks.phase.push(callback);
};
PomTimer.getTimeLeft = function(){
return pom.timeLeft;
};
PomTimer.getPhase = function(){
return pom.state;
};
PomTimer.getPomCount = function(){
return pom.count;
};

return PomTimer;
}]);
29 changes: 29 additions & 0 deletions js/timer/timerController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

app.controller('TimerController', ['PomTimer',
function(PomTimer){
var tc = this;
tc.timeLeft = 0;
tc.phase = "POMODORO";
tc.poms = 0;

function tickCallback(){
tc.timeLeft = PomTimer.getTimeLeft();
}
tickCallback();
PomTimer.registerTickCallback(tickCallback);

PomTimer.registerPhaseCallback(function(){
tc.phase = PomTimer.getPhase();
tc.poms = PomTimer.getPomCount();
});

tc.start = function(){
PomTimer.startTimer();
};

tc.cancel = function(){
PomTimer.stopTimer();
};

}]
);

0 comments on commit 49b4956

Please sign in to comment.