Satellite tracker and pass predictor for Android, inspired by Gpredict
Kotlin amsatcelestrakgpsham-radiohamlibhamradiokotlinnasaradiosatellitesatellite-datasatellite-positionsatellite-predictionsatellitessatnogssdrspacespacexstarlink
Updated passes calculation logic with observers and liveData. Passes preferences are now correctly applied to the calculations.
Arty Bishop35b4215c72a5
diff --git a/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt b/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.ktindex e00bcbbe..e91eee4f 100644--- a/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt+++ b/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt@@ -41,9 +41,18 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {private val preferences = PreferenceManager.getDefaultSharedPreferences(application)private val fusedLocationClient = LocationServices.getFusedLocationProviderClient(application)+ init {+ (application as LookingSatApp).appComponent.inject(this)+ }+private val _debugMessage = MutableLiveData("")val debugMessage: LiveData<String> = _debugMessage+ var tleMainList = loadTwoLineElementFile().sortedWith(compareBy { it.name })++ private val _tleSelectedMap = MutableLiveData<MutableMap<TLE, Boolean>>(mutableMapOf())+ val tleSelectedMap: LiveData<MutableMap<TLE, Boolean>> = _tleSelectedMap+private val _satPassPrefs = MutableLiveData<SatPassPrefs>(SatPassPrefs(preferences.getInt("hoursAhead", 8),@@ -61,13 +70,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {)val gsp: LiveData<GroundStationPosition> = _gsp- var tleMainList = loadTwoLineElementFile().sortedWith(compareBy { it.name })- var tleSelectedMap = mutableMapOf<TLE, Boolean>()-- init {- (application as LookingSatApp).appComponent.inject(this)- }-private fun loadTwoLineElementFile(): List<TLE> {return try {TLE.importSat(getApplication<Application>().openFileInput(tleFile))@@ -77,33 +79,30 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {}}- suspend fun getPassesForSelectedSatellites(): List<SatPass> {+ suspend fun getPasses(satMap: Map<TLE, Boolean>, hours: Int, maxEl: Double): List<SatPass> {val satPassList = mutableListOf<SatPass>()- var sortedList = listOf<SatPass>()- val hoursAhead = satPassPrefs.value?.hoursAhead ?: 8withContext(Dispatchers.Default) {- for ((tle, value) in tleSelectedMap) {+ satMap.forEach { (tle, value) ->if (value) {try {- Log.d(tag, "Trying ${tle.name}")val passPredictor = PassPredictor(tle, gsp.value)- val passes = passPredictor.getPasses(Date(), hoursAhead, false)- for (pass in passes) {- Log.d(tag, "Trying ${pass.maxEl}")- satPassList.add(SatPass(tle.name, tle.catnum, pass))- }+ val passes = passPredictor.getPasses(Date(), hours, false)+ passes.forEach { satPassList.add(SatPass(tle.name, tle.catnum, it)) }} catch (exception: IllegalArgumentException) {- val tleProblem = "There was a problem with TLE"- Log.d(tag, tleProblem)- _debugMessage.postValue(tleProblem)+ Log.d(tag, "There was a problem with TLE")} catch (exception: SatNotFoundException) {Log.d(tag, "Certain satellites shall not pass")}}}- sortedList = satPassList.sortedWith(compareBy { it.pass.startTime })+ satPassList.retainAll { it.pass.maxEl >= maxEl }+ satPassList.sortBy { it.pass.startTime }}- return sortedList+ return satPassList+ }++ fun updateSelectedSatMap(mutableMap: MutableMap<TLE, Boolean>) {+ _tleSelectedMap.postValue(mutableMap)}fun updatePassPrefs(hoursAhead: Int, maxEl: Double) {diff --git a/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt b/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.ktindex 6eeb6a23..bab36312 100644--- a/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt+++ b/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt@@ -8,6 +8,7 @@ import android.view.ViewGroupimport android.widget.*import androidx.appcompat.app.AlertDialogimport androidx.fragment.app.Fragment+import androidx.lifecycle.Observerimport androidx.lifecycle.ViewModelProviderimport androidx.lifecycle.lifecycleScopeimport androidx.recyclerview.widget.LinearLayoutManager@@ -17,6 +18,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButtonimport com.rtbishop.lookingsat.MainViewModelimport com.rtbishop.lookingsat.Rimport com.rtbishop.lookingsat.repo.SatPass+import com.rtbishop.lookingsat.repo.SatPassPrefsimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.launchimport java.util.concurrent.TimeUnit@@ -32,7 +34,8 @@ class SkyFragment : Fragment() {private lateinit var btnPassPrefs: ImageButtonprivate lateinit var progressBar: ProgressBarprivate lateinit var fab: FloatingActionButton-+ private lateinit var selectedSatMap: MutableMap<TLE, Boolean>+ private lateinit var satPassPrefs: SatPassPrefsprivate var aosTimer: CountDownTimer? = nulloverride fun onCreateView(@@ -55,6 +58,18 @@ class SkyFragment : Fragment() {btnPassPrefs.setOnClickListener { showSatPassPrefsDialog() }fab.setOnClickListener { showSelectSatDialog() }+ satPassPrefs = viewModel.satPassPrefs.value ?: SatPassPrefs(8, 20.0)++ viewModel.tleSelectedMap.observe(this, Observer {+ selectedSatMap = it+ calculatePasses()+ })++ viewModel.satPassPrefs.observe(this, Observer {+ satPassPrefs = it+ calculatePasses()+ })+resetTimer()}@@ -82,9 +97,8 @@ class SkyFragment : Fragment() {}private fun showSelectSatDialog() {- val tleSelectedMap = viewModel.tleSelectedMap+ val tleSelectedMap = selectedSatMapval tleMainListSize = viewModel.tleMainList.size-val tleNameArray = arrayOfNulls<String>(tleMainListSize)val tleCheckedArray = BooleanArray(tleMainListSize)@@ -96,7 +110,7 @@ class SkyFragment : Fragment() {} else {for ((position, tle) in viewModel.tleMainList.withIndex()) {tleNameArray[position] = tle.name- tleCheckedArray[position] = viewModel.tleSelectedMap.getOrDefault(tle, false)+ tleCheckedArray[position] = tleSelectedMap.getOrDefault(tle, false)}}@@ -109,7 +123,7 @@ class SkyFragment : Fragment() {.setPositiveButton(getString(R.string.btn_ok)) { _, _ ->for ((tle, value) in selectionMap) {tleSelectedMap[tle] = value- calculatePasses()+ viewModel.updateSelectedSatMap(tleSelectedMap)}}.setNegativeButton(getString(R.string.btn_cancel)) { dialog, _ ->@@ -117,19 +131,21 @@ class SkyFragment : Fragment() {}.setNeutralButton(getString(R.string.btn_clear)) { _, _ ->tleSelectedMap.clear()- calculatePasses()+ viewModel.updateSelectedSatMap(tleSelectedMap)}.create().show()}private fun calculatePasses() {+ val hoursAhead = satPassPrefs.hoursAhead+ val maxEl = satPassPrefs.maxElvar satPassList: List<SatPass>lifecycleScope.launch(Dispatchers.Main) {recViewFuture.visibility = View.INVISIBLEprogressBar.visibility = View.VISIBLEprogressBar.isIndeterminate = true- satPassList = viewModel.getPassesForSelectedSatellites()+ satPassList = viewModel.getPasses(selectedSatMap, hoursAhead, maxEl)recAdapterFuture = SatPassAdapter(satPassList)recViewFuture.adapter = recAdapterFutureprogressBar.isIndeterminate = false