Commit 6d158a62 authored by Dylan Jones's avatar Dylan Jones

Fix the date logic

parent 6b1a4641
Pipeline #4815 passed with stages
in 3 minutes and 40 seconds
......@@ -2,6 +2,7 @@ package srct.whatsopen
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
......@@ -35,7 +36,7 @@ class MainActivity : AppCompatActivity() {
.build()
service = retrofit.create(WhatsOpenService::class.java)
// setup refresh listener
refreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipe_refresh)
refreshLayout = findViewById(R.id.swipe_refresh)
refreshLayout!!.setOnRefreshListener(this::refresh)
refresh()
}
......@@ -69,9 +70,12 @@ class MainActivity : AppCompatActivity() {
}, { err ->
Toast.makeText(
applicationContext,
"Network Error: $err",
"Network Error: $err, retrying",
Toast.LENGTH_SHORT
).show()
// TODO exponental retry?
Log.d("MainActivity.refresh", "network error: $err")
this.refresh()
})
}
}
......@@ -23,6 +23,39 @@ data class Facility(
}
fun isOpen(): Boolean {
return currentSchedule().openTimesList.any { time -> time.isOpen() }
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()
}
}
}
package srct.whatsopen.model
import android.util.Log
import com.google.gson.annotations.SerializedName
import java.util.*
// _Should_ return the number of seconds until the given time.
fun secondsTillTime(hour: Int, minute: Int, day: Int): Int {
val cal = Calendar.getInstance(TimeZone.getTimeZone("EST5EDT"))
// Log.d( "timebug", "current: D ${(cal.get(Calendar.DAY_OF_WEEK) + Calendar.MONDAY) % 7} H ${cal.get(Calendar.HOUR_OF_DAY)} M ${cal.get( Calendar.MINUTE )}" )
val days = (day - (cal.get(Calendar.DAY_OF_WEEK) + Calendar.MONDAY) % 7)
val hours = hour - cal.get(Calendar.HOUR_OF_DAY)
val minutes = minute - cal.get(Calendar.MINUTE)
val seconds = 60 - cal.get(Calendar.SECOND)
// Log.d("timebug", "other : D $day H $hour M $minute")
// Log.d("timebug", "delta : D $days H $hours M $minutes S $seconds")
var time = days * (60 * 60 * 24) + (60 * 60 * hours) + (60 * minutes) + seconds
if (time < 0) {
time += (7 * 24 * 60 * 60)
} // else {
// Log.d("timebug", " good")
// }
// Log.d( "timebug", "final : D ${time / (24 * 60 * 60)} H ${(time % (24 * 60 * 60)) / (60 * 60)} M ${(time % 3600) / 60} S ${time % 60}\n" )
return time
}
data class OpenTimes(
@SerializedName("start_day") var startDay: Int = 0,
@SerializedName("end_day") var endDay: Int = 0,
// These really should be Dates or something but idk exactly how to represent them
@SerializedName("start_time") var startTime: String = "00:00:00",
@SerializedName("end_time") var endTime: String = "00:00:00"
@SerializedName("start_time") var startTime: String,
@SerializedName("end_time") var endTime: String
) {
fun isOpen(): Boolean {
val calendar = Calendar.getInstance(TimeZone.getTimeZone("EST5EDT"))
val cal2 = Calendar.getInstance(TimeZone.getTimeZone("EST5EDT"))
val currentDay = calendar.get(Calendar.DAY_OF_WEEK)
cal2.set(Calendar.DAY_OF_YEAR, 0)
cal2.set(Calendar.YEAR, 1970)
println(startDay)
// val sdf = SimpleDateFormat("HH:MM:SS")
// val startTimeObj = sdf.parse(startTime)
// val endTimeObj = sdf.parse(endTime)
// if (startDay == endDay) { // "normal"
// return now in startTime..endTime
// } else if (startDay < endDay) {
// if (currentDay == startDay) {
// return
// }
// }
return true
// TODO this is _broken_
// Log.d( "srct.whatsopen", "start $startTime end $endTime ttc ${timeTillClose()} tto ${timeTillOpen()}" )
return timeTillClose() < timeTillOpen()
}
fun timeTillOpen(): Int {
val starts = startTime.split(":")
return secondsTillTime(starts[0].toInt(), starts[1].toInt(), startDay)
}
fun timeTillClose(): Int {
val ends = endTime.split(":")
return secondsTillTime(ends[0].toInt(), ends[1].toInt(), endDay)
}
override fun toString(): String {
val sh = startTime.split(":")[0].toInt()
val sm = startTime.split(":")[1]
val eh = endTime.split(":")[0].toInt()
val em = endTime.split(":")[1]
return (if (sh < 12) "$sh:${sm}am" else "${(sh - 12)}:${sm}pm") + "-" + (if (eh < 12) "$eh:${em}am" else "${eh - 12}:${em}pm")
}
}
\ No newline at end of file
......@@ -8,6 +8,6 @@ data class Schedule(
@SerializedName("open_times") var openTimesList: List<OpenTimes>,
@SerializedName("valid_start") var validStart: Date,
@SerializedName("valid_end") var validEnd: Date,
@SerializedName("twenty_four_hours") var isOpen24Hours: Boolean,
var isMainSchedule: Boolean
)
@SerializedName("twenty_four_hours") var isOpen24Hours: Boolean
) {
}
package srct.whatsopen.util
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -28,8 +29,13 @@ class MainViewAdapter(var facilities: List<Facility>) : RecyclerView.Adapter<Hol
override fun onBindViewHolder(holder: Holder, position: Int) {
val facility = facilities[position]
holder.itemView.findViewById<TextView>(R.id.facility_title).text = facility.name
holder.itemView.findViewById<TextView>(R.id.change_text).text =
"24hr: ${facility.currentSchedule().isOpen24Hours}"
// Log.d("srct.whatsopen", "title: $facility")
val text: String = if (facility.currentSchedule().isOpen24Hours) {
"Open 24 hours"
} else {
"${if (facility.isOpen()) "Open" else "Closed"}: ${facility.currentHours()}"
}
holder.itemView.findViewById<TextView>(R.id.change_text).text = text
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment