Unverified Commit 6d158a62 authored by Dylan Jones's avatar Dylan Jones
Browse files

Fix the date logic

parent 6b1a4641
Pipeline #4815 passed with stages
in 3 minutes and 40 seconds
...@@ -2,6 +2,7 @@ package srct.whatsopen ...@@ -2,6 +2,7 @@ package srct.whatsopen
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Bundle import android.os.Bundle
import android.util.Log
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.view.Menu import android.view.Menu
...@@ -35,7 +36,7 @@ class MainActivity : AppCompatActivity() { ...@@ -35,7 +36,7 @@ class MainActivity : AppCompatActivity() {
.build() .build()
service = retrofit.create(WhatsOpenService::class.java) service = retrofit.create(WhatsOpenService::class.java)
// setup refresh listener // setup refresh listener
refreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipe_refresh) refreshLayout = findViewById(R.id.swipe_refresh)
refreshLayout!!.setOnRefreshListener(this::refresh) refreshLayout!!.setOnRefreshListener(this::refresh)
refresh() refresh()
} }
...@@ -69,9 +70,12 @@ class MainActivity : AppCompatActivity() { ...@@ -69,9 +70,12 @@ class MainActivity : AppCompatActivity() {
}, { err -> }, { err ->
Toast.makeText( Toast.makeText(
applicationContext, applicationContext,
"Network Error: $err", "Network Error: $err, retrying",
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
// TODO exponental retry?
Log.d("MainActivity.refresh", "network error: $err")
this.refresh()
}) })
} }
} }
...@@ -23,6 +23,39 @@ data class Facility( ...@@ -23,6 +23,39 @@ data class Facility(
} }
fun isOpen(): Boolean { 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 package srct.whatsopen.model
import android.util.Log
import com.google.gson.annotations.SerializedName import com.google.gson.annotations.SerializedName
import java.util.* 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( data class OpenTimes(
@SerializedName("start_day") var startDay: Int = 0, @SerializedName("start_day") var startDay: Int = 0,
@SerializedName("end_day") var endDay: Int = 0, @SerializedName("end_day") var endDay: Int = 0,
// These really should be Dates or something but idk exactly how to represent them // These really should be Dates or something but idk exactly how to represent them
@SerializedName("start_time") var startTime: String = "00:00:00", @SerializedName("start_time") var startTime: String,
@SerializedName("end_time") var endTime: String = "00:00:00" @SerializedName("end_time") var endTime: String
) { ) {
fun isOpen(): Boolean { fun isOpen(): Boolean {
val calendar = Calendar.getInstance(TimeZone.getTimeZone("EST5EDT")) // TODO this is _broken_
val cal2 = Calendar.getInstance(TimeZone.getTimeZone("EST5EDT")) // Log.d( "srct.whatsopen", "start $startTime end $endTime ttc ${timeTillClose()} tto ${timeTillOpen()}" )
val currentDay = calendar.get(Calendar.DAY_OF_WEEK) return timeTillClose() < timeTillOpen()
cal2.set(Calendar.DAY_OF_YEAR, 0) }
cal2.set(Calendar.YEAR, 1970)
println(startDay) fun timeTillOpen(): Int {
// val sdf = SimpleDateFormat("HH:MM:SS") val starts = startTime.split(":")
// val startTimeObj = sdf.parse(startTime) return secondsTillTime(starts[0].toInt(), starts[1].toInt(), startDay)
// val endTimeObj = sdf.parse(endTime) }
// if (startDay == endDay) { // "normal"
// return now in startTime..endTime fun timeTillClose(): Int {
// } else if (startDay < endDay) { val ends = endTime.split(":")
// if (currentDay == startDay) { return secondsTillTime(ends[0].toInt(), ends[1].toInt(), endDay)
// return }
// }
// } override fun toString(): String {
return true 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( ...@@ -8,6 +8,6 @@ data class Schedule(
@SerializedName("open_times") var openTimesList: List<OpenTimes>, @SerializedName("open_times") var openTimesList: List<OpenTimes>,
@SerializedName("valid_start") var validStart: Date, @SerializedName("valid_start") var validStart: Date,
@SerializedName("valid_end") var validEnd: Date, @SerializedName("valid_end") var validEnd: Date,
@SerializedName("twenty_four_hours") var isOpen24Hours: Boolean, @SerializedName("twenty_four_hours") var isOpen24Hours: Boolean
var isMainSchedule: Boolean ) {
) }
package srct.whatsopen.util package srct.whatsopen.util
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
...@@ -28,8 +29,13 @@ class MainViewAdapter(var facilities: List<Facility>) : RecyclerView.Adapter<Hol ...@@ -28,8 +29,13 @@ class MainViewAdapter(var facilities: List<Facility>) : RecyclerView.Adapter<Hol
override fun onBindViewHolder(holder: Holder, position: Int) { override fun onBindViewHolder(holder: Holder, position: Int) {
val facility = facilities[position] val facility = facilities[position]
holder.itemView.findViewById<TextView>(R.id.facility_title).text = facility.name holder.itemView.findViewById<TextView>(R.id.facility_title).text = facility.name
holder.itemView.findViewById<TextView>(R.id.change_text).text = // Log.d("srct.whatsopen", "title: $facility")
"24hr: ${facility.currentSchedule().isOpen24Hours}" 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