Facility.kt 1.97 KB
Newer Older
Robert Hitt's avatar
Robert Hitt committed
1 2
package srct.whatsopen.model

3
import android.graphics.Bitmap
Robert Hitt's avatar
Robert Hitt committed
4
import com.google.gson.annotations.SerializedName
Dylan Jones's avatar
Dylan Jones committed
5 6 7 8 9 10 11
import java.util.*

data class Facility(
    @SerializedName("facility_name") val name: String,
    @SerializedName("main_schedule") val mainSchedule: Schedule,
    @SerializedName("special_schedules") val specialSchedules: List<Schedule>,
    @SerializedName("facility_location") val location: Location,
12
    @SerializedName("slug") val slug: String,
13 14 15
    @SerializedName("logo") val logoUrl: String,
    val isFavorite: Boolean,
    var logoBitmap: Bitmap?
Dylan Jones's avatar
Dylan Jones committed
16 17 18 19 20 21 22 23 24
) {
    fun currentSchedule(): Schedule {
        val now: Date = Calendar.getInstance(TimeZone.getTimeZone("EST5EDT")).time
        for (schedule in specialSchedules) {
            if (schedule.validStart < now && now < schedule.validEnd) {
                return schedule
            }
        }
        return mainSchedule
Robert Hitt's avatar
Robert Hitt committed
25 26
    }

Dylan Jones's avatar
Dylan Jones committed
27
    fun isOpen(): Boolean {
Dylan Jones's avatar
Dylan Jones committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        return currentSchedule().isOpen24Hours || currentSchedule().openTimesList.any { time -> time.isOpen() }
    }

    fun nextOpenTime(): OpenTimes? {
        // TODO handle special schedules possibly?
        var leastSeconds: Int = Int.MAX_VALUE
        var nextOpening: OpenTimes? = null
        for (time in currentSchedule().openTimesList) {
            if (time.timeTillOpen() < leastSeconds) {
                leastSeconds = time.timeTillOpen()
                nextOpening = time
            }
        }
        return nextOpening
    }

    fun nextCloseTime(): OpenTimes? {
        var leastSeconds: Int = Int.MAX_VALUE
        var nextClosing: OpenTimes? = null
        for (time in currentSchedule().openTimesList) {
            if (time.timeTillClose() < leastSeconds) {
                leastSeconds = time.timeTillClose()
                nextClosing = time
            }
        }
        return nextClosing
    }

    fun currentHours(): OpenTimes? {
        return if (isOpen()) {
            nextCloseTime()
        } else {
            nextOpenTime()
        }
Dylan Jones's avatar
Dylan Jones committed
62
    }
Robert Hitt's avatar
Robert Hitt committed
63
}