Skip to content

Satellite tracker and pass predictor for Android, inspired by Gpredict

Fixed TLE list update logic. It now also triggers satellite passes recalculation.

Arty Bishope219d42a9b2b
diff --git a/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt b/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt
index aba986e5..55a9fff5 100644
--- a/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt
+++ b/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt
@@ -48,16 +48,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
private val _debugMessage = MutableLiveData("")
val debugMessage: LiveData<String> = _debugMessage
- private val _tleMainList = MutableLiveData<List<TLE>>(
- try {
- TLE.importSat(getApplication<Application>().openFileInput(tleFile))
- .sortedWith(compareBy { it.name })
- } catch (exception: FileNotFoundException) {
- _debugMessage.postValue("TLE file wasn't found")
- emptyList<TLE>()
- }
-
- )
+ private val _tleMainList = MutableLiveData<List<TLE>>(loadTwoLineElementFile())
val tleMainList: LiveData<List<TLE>> = _tleMainList
private val _tleSelectedMap = MutableLiveData<MutableMap<TLE, Boolean>>(mutableMapOf())
@@ -80,6 +71,16 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
)
val gsp: LiveData<GroundStationPosition> = _gsp
+ private fun loadTwoLineElementFile(): List<TLE> {
+ return try {
+ TLE.importSat(getApplication<Application>().openFileInput(tleFile))
+ .sortedWith(compareBy { it.name })
+ } catch (exception: FileNotFoundException) {
+ _debugMessage.postValue("TLE file wasn't found")
+ emptyList()
+ }
+ }
+
suspend fun getPasses(satMap: Map<TLE, Boolean>, hours: Int, maxEl: Double): List<SatPass> {
val satPassList = mutableListOf<SatPass>()
withContext(Dispatchers.Default) {
@@ -139,7 +140,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
getApplication<Application>().openFileOutput(tleFile, Context.MODE_PRIVATE).use {
it.write(stream.readBytes())
}
- _tleMainList.postValue(TLE.importSat(stream))
+ _tleMainList.postValue(loadTwoLineElementFile())
_debugMessage.postValue("TLE file was updated")
} catch (exception: IOException) {
_debugMessage.postValue("Couldn't update TLE file")
diff --git a/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt b/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt
index 824ff836..e4065e5c 100644
--- a/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt
+++ b/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt
@@ -35,7 +35,7 @@ class SkyFragment : Fragment() {
private lateinit var progressBar: ProgressBar
private lateinit var fab: FloatingActionButton
private lateinit var tleMainList: List<TLE>
- private lateinit var selectedSatMap: MutableMap<TLE, Boolean>
+ private var selectedSatMap: MutableMap<TLE, Boolean> = mutableMapOf()
private lateinit var satPassPrefs: SatPassPrefs
private lateinit var aosTimer: CountDownTimer
private var isTimerSet: Boolean = false
@@ -72,6 +72,8 @@ class SkyFragment : Fragment() {
private fun setupObservers() {
viewModel.tleMainList.observe(this, Observer {
tleMainList = it
+ selectedSatMap.clear()
+ viewModel.updateSelectedSatMap(selectedSatMap)
})
viewModel.tleSelectedMap.observe(this, Observer {
@@ -113,42 +115,39 @@ class SkyFragment : Fragment() {
}
private fun showSelectSatDialog() {
- val tleList = tleMainList
- val tleSelectedMap = selectedSatMap
- val tleMainListSize = tleList.size
- val tleNameArray = arrayOfNulls<String>(tleMainListSize)
- val tleCheckedArray = BooleanArray(tleMainListSize)
-
- if (tleSelectedMap.isEmpty()) {
- tleList.withIndex().forEach { (position, tle) ->
+ val tleNameArray = arrayOfNulls<String>(tleMainList.size)
+ val tleCheckedArray = BooleanArray(tleMainList.size)
+ val builder = AlertDialog.Builder(activity as MainActivity)
+ val selectionMap = mutableMapOf<TLE, Boolean>()
+
+ if (selectedSatMap.isEmpty()) {
+ tleMainList.withIndex().forEach { (position, tle) ->
tleNameArray[position] = tle.name
tleCheckedArray[position] = false
}
} else {
- tleList.withIndex().forEach { (position, tle) ->
+ tleMainList.withIndex().forEach { (position, tle) ->
tleNameArray[position] = tle.name
- tleCheckedArray[position] = tleSelectedMap.getOrDefault(tle, false)
+ tleCheckedArray[position] = selectedSatMap.getOrDefault(tle, false)
}
}
- val selectionMap = mutableMapOf<TLE, Boolean>()
- val builder = AlertDialog.Builder(activity as MainActivity)
builder.setTitle(getString(R.string.title_select_sat))
.setMultiChoiceItems(tleNameArray, tleCheckedArray) { _, which, isChecked ->
- selectionMap[tleList[which]] = isChecked
+ selectionMap[tleMainList[which]] = isChecked
}
.setPositiveButton(getString(R.string.btn_ok)) { _, _ ->
for ((tle, value) in selectionMap) {
- tleSelectedMap[tle] = value
- viewModel.updateSelectedSatMap(tleSelectedMap)
+ selectedSatMap[tle] = value
+ viewModel.updateSelectedSatMap(selectedSatMap)
}
}
.setNegativeButton(getString(R.string.btn_cancel)) { dialog, _ ->
dialog.cancel()
}
.setNeutralButton(getString(R.string.btn_clear)) { _, _ ->
- tleSelectedMap.clear()
- viewModel.updateSelectedSatMap(tleSelectedMap)
+ selectedSatMap.clear()
+ viewModel.updateSelectedSatMap(selectedSatMap)
}
.create()
.show()