From 32605496b8b4548d889647b07fc05002d2043404 Mon Sep 17 00:00:00 2001 From: Arty Bishop Date: Sun, 12 Jan 2020 19:57:01 +0000 Subject: [PATCH] Fixed timer and pass progress update behaviour for the last pass in the list. Recalculating the passes after the last one is finished. --- .../com/rtbishop/look4sat/MainViewModel.kt | 36 ++++---- .../com/rtbishop/look4sat/ui/SkyFragment.kt | 89 ++++++++++++------- app/src/main/res/values/strings.xml | 1 + 3 files changed, 75 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/rtbishop/look4sat/MainViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/MainViewModel.kt index b3768ba9..9f28c404 100644 --- a/app/src/main/java/com/rtbishop/look4sat/MainViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/MainViewModel.kt @@ -178,7 +178,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } } - suspend fun getPasses() { + fun getPasses() { val passList = mutableListOf() 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 { diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/SkyFragment.kt b/app/src/main/java/com/rtbishop/look4sat/ui/SkyFragment.kt index 90f9ef33..8444133d 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/SkyFragment.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/SkyFragment.kt @@ -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(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) } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5a433f1d..4a2fc86f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -90,6 +90,7 @@ %.4f AOS -%02d:%02d:%02d + LOS -%02d:%02d:%02d Mode: %s Inverted: %s Uplink: %.4f MHz