Skip to content

Commit

Permalink
#295 Timer depends on local time (#304)
Browse files Browse the repository at this point in the history
* Timer is no longer based on the Date time (System time)

* websocket on main state

* Google Java Format

* add a solution to the timer which is based on local time

* Google Java Format

* missing imports

* replaced created with ok, improved Logger Message

---------

Co-authored-by: github-actions <>
  • Loading branch information
SponsoredByPuma authored Oct 4, 2023
1 parent be120a7 commit ccbdc08
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions backend/src/main/java/io/diveni/backend/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.net.URI;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
Expand Down Expand Up @@ -67,4 +68,11 @@ private static final DateFormat getDateFormatISO() {
public static String getTimestampISO8601(Date date) {
return dateFormatISO8601.format(date);
}

public static Date getDateFromString(String dateString) throws ParseException {
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(tz);
return df.parse(dateString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import java.text.ParseException;

import io.diveni.backend.principals.AdminPrincipal;
import io.diveni.backend.principals.MemberPrincipal;
Expand Down Expand Up @@ -232,4 +236,16 @@ public synchronized void adminSelectedUserStory(
webSocketService.sendSelectedUserStoryToMembers(session, index);
LOGGER.debug("<-- adminSelectedUserStory()");
}

@GetMapping("/get-timer-value")
public synchronized ResponseEntity<Long> getTimeValue(String memberID) throws ParseException {
LOGGER.debug("--> get-timer-value()");
Session session =
ControllerUtils.getSessionByMemberIDOrThrowResponse(databaseService, memberID);
Date startDate = Utils.getDateFromString(session.getTimerTimestamp());
Date currentDate = new Date();
long timeDifference = currentDate.getTime() - startDate.getTime();
LOGGER.debug("<-- get-timer-value() + " + timeDifference);
return new ResponseEntity<>(timeDifference, HttpStatus.OK);
}
}
24 changes: 24 additions & 0 deletions frontend/src/components/EstimateTimer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</template>

<script lang="ts">
import constants from "@/constants";
import Vue from "vue";
export default Vue.extend({
Expand All @@ -15,6 +16,7 @@ export default Vue.extend({
startTimestamp: { type: String, required: true },
duration: { type: Number, required: true },
pauseTimer: { type: Boolean, required: true },
member: { type: String, required: false, default: "" },
votingStarted: { type: Boolean, required: true },
},
data() {
Expand Down Expand Up @@ -81,12 +83,34 @@ export default Vue.extend({
const startTime = new Date(this.startTimestamp).getTime();
const currentTime = new Date().getTime();
this.timerCount = Math.ceil(this.duration - (currentTime - startTime) / 1000);
if (this.timerCount > this.duration || this.timerCount < 0) {
clearInterval(this.intervalHandler);
this.localClockTimer();
}
} else {
this.$emit("timerFinished");
clearInterval(this.intervalHandler);
}
}, 100);
},
async localClockTimer() {
if (this.member !== '') {
const response = (await this.axios.get(constants.backendURL + "/get-timer-value",{
params: {
memberID: this.member,
},
})).data;
this.timerCount = Math.ceil(this.duration - (response / 1000));
this.intervalHandler = setInterval(() => {
if (this.timerCount > 0) {
this.timerCount = this.timerCount - 1;
} else {
this.$emit("timerFinished");
clearInterval(this.intervalHandler);
}
}, 1000);
}
}
},
});
</script>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/views/MemberVotePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
:start-timestamp="timerTimestamp"
:pause-timer="estimateFinished || pauseSession"
:duration="timerCountdownNumber"
:member="memberID"
:votingStarted="isStartVoting"
/>
</b-col>
Expand Down

0 comments on commit ccbdc08

Please sign in to comment.