Refactored timer behaviour. Now it's always showing AOS time for the next upcoming pass.

pull/30/head
Arty Bishop 2020-01-05 20:04:58 +00:00
rodzic e8394aaf7d
commit 504fabd8ca
1 zmienionych plików z 38 dodań i 25 usunięć

Wyświetl plik

@ -95,7 +95,7 @@ class SkyFragment : Fragment() {
} }
recyclerAdapter.setList(satPassList) recyclerAdapter.setList(satPassList)
recyclerAdapter.notifyDataSetChanged() recyclerAdapter.notifyDataSetChanged()
if (satPassList.isEmpty()) resetTimer() setTimerForNextPass()
swipeLayout.setProgressBackgroundColorSchemeResource(R.color.themeAccent) swipeLayout.setProgressBackgroundColorSchemeResource(R.color.themeAccent)
swipeLayout.setColorSchemeResources(R.color.darkOnLight) swipeLayout.setColorSchemeResources(R.color.darkOnLight)
@ -113,11 +113,7 @@ class SkyFragment : Fragment() {
satPassList = it satPassList = it
recyclerAdapter.setList(satPassList) recyclerAdapter.setList(satPassList)
recyclerAdapter.notifyDataSetChanged() recyclerAdapter.notifyDataSetChanged()
if (satPassList.isNotEmpty()) setTimer(satPassList.first().pass.startTime.time) setTimerForNextPass()
else {
resetTimer()
timeToAos.text = String.format(getString(R.string.pattern_aos), 0, 0, 0)
}
swipeLayout.isRefreshing = false swipeLayout.isRefreshing = false
}) })
} }
@ -210,32 +206,49 @@ class SkyFragment : Fragment() {
lifecycleScope.launch(Dispatchers.Main) { viewModel.getPasses() } lifecycleScope.launch(Dispatchers.Main) { viewModel.getPasses() }
} }
private fun setTimer(passTime: Long) { private fun setTimerForNextPass() {
resetTimer() if (satPassList.isNotEmpty()) {
val totalMillis = passTime.minus(System.currentTimeMillis()) resetTimer()
aosTimer = object : CountDownTimer(totalMillis, 1000) { try {
override fun onFinish() { val timeNow = System.currentTimeMillis()
Toast.makeText(activity, "Time is up!", Toast.LENGTH_SHORT).show() val nextPass = satPassList.first {
this.cancel() it.pass.startTime.time.minus(timeNow) > 0
} }
val passTime = nextPass.pass.startTime.time
val totalMillis = passTime.minus(timeNow)
override fun onTick(millisUntilFinished: Long) { aosTimer = object : CountDownTimer(totalMillis, 1000) {
timeToAos.text = String.format( override fun onFinish() {
mainActivity.getString(R.string.pattern_aos), Toast
TimeUnit.MILLISECONDS.toHours(millisUntilFinished) % 60, .makeText(activity, "Incoming ${nextPass.tle.name}", Toast.LENGTH_SHORT)
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60, .show()
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60 setTimerForNextPass()
) }
override fun onTick(millisUntilFinished: Long) {
timeToAos.text = String.format(
mainActivity.getString(R.string.pattern_aos),
TimeUnit.MILLISECONDS.toHours(millisUntilFinished) % 60,
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60
)
}
}
aosTimer.start()
isTimerSet = true
} catch (e: NoSuchElementException) {
resetTimer()
} }
} else {
resetTimer(true)
} }
aosTimer.start()
isTimerSet = true
} }
private fun resetTimer() { private fun resetTimer(toNull: Boolean = false) {
if (isTimerSet) { if (isTimerSet) {
aosTimer.cancel() aosTimer.cancel()
isTimerSet = false isTimerSet = false
} else timeToAos.text = String.format(getString(R.string.pattern_aos), 0, 0, 0) }
if (toNull) timeToAos.text = String.format(getString(R.string.pattern_aos), 0, 0, 0)
} }
} }