Skip to content

Commit

Permalink
Fix time stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
meiron03 committed Sep 15, 2023
1 parent a0fa1f4 commit 6c6af19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import com.pennapps.labs.pennmobile.classes.FitnessRoom
import com.pennapps.labs.pennmobile.classes.FitnessRoomUsage
import com.pennapps.labs.pennmobile.classes.RoundedBarChartRenderer
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.LocalTime
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

Expand Down Expand Up @@ -225,11 +225,20 @@ class FitnessAdapter(private val fitnessRooms: List<FitnessRoom>) :
holder.roomView.text = room.roomName

// check if the room is currently open
val currentTime = ZonedDateTime.now()
val startTime = ZonedDateTime.parse(room.openTime)
val endTime = ZonedDateTime.parse(room.closeTime)
// NOT time zone safe
val currentTime = LocalTime.now()

val isOpen = currentTime.isAfter(startTime) and currentTime.isBefore(endTime)
// Sunday -> 0, Monday -> 1, etc.
val dayOfWeek = ZonedDateTime.now().dayOfWeek.value;

// the open and close time lists start with monday
val openTimeString = room.openTimeList?.get((dayOfWeek + 6) % 7)
val closeTimeString = room.closeTimeList?.get((dayOfWeek + 6) % 7)

val openTime = LocalTime.parse(openTimeString)
val closeTime = LocalTime.parse(closeTimeString)

val isOpen = currentTime.isAfter(openTime) and currentTime.isBefore(closeTime)
if (isOpen) {
holder.statusView.setText(R.string.fitness_room_open)
holder.statusView.background = ContextCompat.getDrawable(mContext, R.drawable.label_green)
Expand All @@ -238,14 +247,9 @@ class FitnessAdapter(private val fitnessRooms: List<FitnessRoom>) :
holder.statusView.background = ContextCompat.getDrawable(mContext, R.drawable.label_red)
}

// convert time zone to display
val zid = ZoneId.systemDefault()
val localStart = startTime.withZoneSameInstant(zid)
val localEnd = endTime.withZoneSameLocal(zid)

// format and assign the times
val timeFormatter = DateTimeFormatter.ofPattern("hh:mm a")
val hours = "${localStart.format(timeFormatter)} - ${localEnd.format(timeFormatter)}"
val hours = "${openTime.format(timeFormatter)} - ${closeTime.format(timeFormatter)}"
holder.hoursView.text = hours

// make progress bar invisible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ class FitnessRoom {

@SerializedName("open")
@Expose
var openTime: String? = null
var openTimeList: List<String>? = null

@SerializedName("close")
@Expose
var closeTime: String? = null
var closeTimeList: List<String>? = null

val openTime: String = "2023-09-15T16:52:00-04:00"
val closeTime: String = "2023-09-15T16:52:00-04:00"

val imageURL = "https://s3.us-east-2.amazonaws.com/labs.api/dining/1920-commons.jpg"
}

0 comments on commit 6c6af19

Please sign in to comment.