Fixed timer and pass progress update behaviour for the last pass in the list. Recalculating the passes after the last one is finished.

This commit is contained in:
Arty Bishop committed 2020-01-12 19:57:01 +00:00
1 parent efdb9fe7fc
commit 32605496b8
3 files changed
+75 -51

No files matched your search

@@ -178,7 +178,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
}
suspend fun getPasses() {
fun getPasses() {
val passList = mutableListOf<SatPass>()
val dateNow = Date()
val dateFuture = Calendar.getInstance().let {
@@ -186,25 +186,27 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
it.add(Calendar.HOUR, hoursAhead)
it.time
}
withContext(Dispatchers.Default) {
tleSelection.forEach { indexOfSelection ->
val tle = tleMainList[indexOfSelection]
try {
val predictor = PassPredictor(tle, gsp.value)
val passes = predictor.getPasses(dateNow, hoursAhead, true)
passes.forEach { passList.add(SatPass(tle, predictor, it)) }
} catch (exception: IllegalArgumentException) {
_debugMessage.postValue("There was a problem with ${tle.name}")
} catch (exception: SatNotFoundException) {
_debugMessage.postValue("${tle.name} shall not pass")
viewModelScope.launch {
withContext(Dispatchers.Default) {
tleSelection.forEach { indexOfSelection ->
val tle = tleMainList[indexOfSelection]
try {
val predictor = PassPredictor(tle, gsp.value)
val passes = predictor.getPasses(dateNow, hoursAhead, true)
passes.forEach { passList.add(SatPass(tle, predictor, it)) }
} catch (exception: IllegalArgumentException) {
_debugMessage.postValue("There was a problem with ${tle.name}")
} catch (exception: SatNotFoundException) {
_debugMessage.postValue("${tle.name} shall not pass")
}
}
passList.removeAll { it.pass.startTime.after(dateFuture) }
passList.removeAll { it.pass.endTime.before(dateNow) }
passList.removeAll { it.pass.maxEl < minEl }
passList.sortBy { it.pass.startTime }
}
passList.removeAll { it.pass.startTime.after(dateFuture) }
passList.removeAll { it.pass.endTime.before(dateNow) }
passList.removeAll { it.pass.maxEl < minEl }
passList.sortBy { it.pass.startTime }
_satPassList.postValue(passList)
}
_satPassList.postValue(passList)
}
suspend fun getTransmittersForSat(id: Int): List<Transmitter> {
@@ -32,7 +32,6 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
@@ -42,8 +41,6 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.rtbishop.look4sat.MainViewModel
import com.rtbishop.look4sat.R
import com.rtbishop.look4sat.repo.SatPass
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*
import java.util.concurrent.TimeUnit
@@ -97,7 +94,7 @@ class SkyFragment : Fragment() {
(itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
}
recyclerAdapter.setList(satPassList)
setTimerForNextPass()
setTimer()
swipeLayout.setProgressBackgroundColorSchemeResource(R.color.themeAccent)
swipeLayout.setColorSchemeResources(R.color.darkOnLight)
@@ -114,11 +111,16 @@ class SkyFragment : Fragment() {
viewModel.passSatList.observe(viewLifecycleOwner, Observer {
satPassList = it
recyclerAdapter.setList(satPassList)
setTimerForNextPass()
setTimer()
swipeLayout.isRefreshing = false
})
}
private fun calculatePasses() {
swipeLayout.isRefreshing = true
viewModel.getPasses()
}
private fun showSatPassPrefsDialog(hoursAhead: Int, minEl: Double) {
val satPassPrefView = View.inflate(mainActivity, R.layout.sat_pass_pref, null)
val etHoursAhead = satPassPrefView.findViewById<EditText>(R.id.pref_hours_ahead_et)
@@ -202,49 +204,68 @@ class SkyFragment : Fragment() {
}
}
private fun calculatePasses() {
swipeLayout.isRefreshing = true
lifecycleScope.launch(Dispatchers.Main) { viewModel.getPasses() }
}
private fun setTimerForNextPass() {
private fun setTimer() {
if (satPassList.isNotEmpty()) {
resetTimer()
val timeNow = Date()
try {
val timeNow = Date()
val nextPass = satPassList.first { it.pass.startTime.after(timeNow) }
val totalMillis = nextPass.pass.startTime.time.minus(timeNow.time)
aosTimer = object : CountDownTimer(totalMillis, 1000) {
override fun onFinish() {
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
)
recyclerAdapter.updateRecycler()
}
}
setTimerForNext(timeNow)
} catch (e: NoSuchElementException) {
setTimerForLast(timeNow)
} finally {
aosTimer.start()
isTimerSet = true
} catch (e: NoSuchElementException) {
resetTimer()
}
} else {
resetTimer(true)
}
}
private fun resetTimer(toNull: Boolean = false) {
private fun setTimerForNext(timeNow: Date) {
val nextPass = satPassList.first { it.pass.startTime.after(timeNow) }
val millisBeforeStart = nextPass.pass.startTime.time.minus(timeNow.time)
aosTimer = object : CountDownTimer(millisBeforeStart, 1000) {
override fun onFinish() {
setTimer()
}
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
)
recyclerAdapter.updateRecycler()
}
}
}
private fun setTimerForLast(timeNow: Date) {
val lastPass = satPassList.last()
val millisBeforeEnd = lastPass.pass.endTime.time.minus(timeNow.time)
aosTimer = object : CountDownTimer(millisBeforeEnd, 1000) {
override fun onFinish() {
calculatePasses()
}
override fun onTick(millisUntilFinished: Long) {
timeToAos.text = String.format(
mainActivity.getString(R.string.pattern_los),
TimeUnit.MILLISECONDS.toHours(millisUntilFinished) % 60,
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60
)
recyclerAdapter.updateRecycler()
}
}
}
private fun resetTimer(resetToNull: Boolean = false) {
if (isTimerSet) {
aosTimer.cancel()
isTimerSet = false
}
if (toNull) timeToAos.text = String.format(getString(R.string.pattern_aos), 0, 0, 0)
if (resetToNull) timeToAos.text = String.format(getString(R.string.pattern_aos), 0, 0, 0)
}
}
+1
View File
@@ -90,6 +90,7 @@
<string name="pattern_location">%.4f</string>
<string name="pattern_aos">AOS -%02d:%02d:%02d</string>
<string name="pattern_los">LOS -%02d:%02d:%02d</string>
<string name="pattern_trans_mode">Mode: %s</string>
<string name="pattern_trans_inverted">Inverted: %s</string>
<string name="pattern_trans_uplink_low">Uplink: %.4f MHz</string>