Skip to content

Satellite tracker and pass predictor for Android, inspired by Gpredict

Added dialog for satellite selection. Showing only selected passes in the recycler.

Arty Bishopbfc0561c34ba
diff --git a/app/build.gradle b/app/build.gradle
index 49da233e..b29c5d9c 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -8,7 +8,7 @@ android {
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.rtbishop.lookingsat"
- minSdkVersion 22
+ minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "0.2"
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 eea85751..2ede8948 100644
--- a/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt
+++ b/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt
@@ -55,7 +55,7 @@ class SkyFragment : Fragment() {
recView.visibility = View.INVISIBLE
progressBar.visibility = View.VISIBLE
progressBar.isIndeterminate = true
- satPassList = viewModel.updateRecycler()
+ satPassList = viewModel.getPassesForSelectedSatellites()
recAdapter = SatPassAdapter(satPassList)
recView.adapter = recAdapter
progressBar.isIndeterminate = false
@@ -67,41 +67,43 @@ class SkyFragment : Fragment() {
fab.setOnClickListener { showSelectSatDialog() }
}
- private var selectedSatList: List<TLE> = emptyList()
-
private fun showSelectSatDialog() {
- val tleList = mutableListOf<TLE>()
- val listSize = viewModel.tleList.size
- val satList = arrayOfNulls<String>(listSize)
- val checkedSatList = BooleanArray(listSize)
- if (selectedSatList.isEmpty()) {
- for ((position, tle) in viewModel.tleList.withIndex()) {
- satList[position] = tle.name
- checkedSatList[position] = false
+ val tleSelectedMap = viewModel.tleSelectedMap
+ val tleMainListSize = viewModel.tleMainList.size
+
+ val tleNameArray = arrayOfNulls<String>(tleMainListSize)
+ val tleCheckedArray = BooleanArray(tleMainListSize)
+
+ if (tleSelectedMap.isEmpty()) {
+ for ((position, tle) in viewModel.tleMainList.withIndex()) {
+ tleNameArray[position] = tle.name
+ tleCheckedArray[position] = false
+ }
+ } else {
+ for ((position, tle) in viewModel.tleMainList.withIndex()) {
+ tleNameArray[position] = tle.name
+ tleCheckedArray[position] = viewModel.tleSelectedMap.getOrDefault(tle, false)
}
}
+ val selectionMap = mutableMapOf<TLE, Boolean>()
val builder = AlertDialog.Builder(activity as MainActivity)
builder.setTitle("Select satellites to track")
-
- builder.setMultiChoiceItems(satList, checkedSatList) { _, which, isChecked ->
- checkedSatList[which] = isChecked
- }
-
- builder.setPositiveButton("Select") { _, _ ->
- for (index in checkedSatList.indices) {
- val checked = checkedSatList[index]
- if (checked) {
- tleList.add(viewModel.tleList[index])
+ .setMultiChoiceItems(tleNameArray, tleCheckedArray) { _, which, isChecked ->
+ selectionMap[viewModel.tleMainList[which]] = isChecked
+ }
+ .setPositiveButton("Ok") { _, _ ->
+ for ((tle, value) in selectionMap) {
+ tleSelectedMap[tle] = value
}
}
- }
-
- builder.setNegativeButton("Cancel") { dialog, _ ->
- dialog.cancel()
- }
-
- val dialog = builder.create()
- dialog.show()
+ .setNeutralButton("Clear All") { _, _ ->
+ tleSelectedMap.clear()
+ }
+ .setNegativeButton("Cancel") { dialog, _ ->
+ dialog.cancel()
+ }
+ .create()
+ .show()
}
}
\ No newline at end of file
diff --git a/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt b/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt
index cde6e41e..8d9ae206 100644
--- a/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt
+++ b/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt
@@ -50,7 +50,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
)
val gsp: LiveData<GroundStationPosition> = _gsp
- var tleList: List<TLE> = loadTwoLineElementFile().sortedWith(compareBy { it.name })
+ var tleMainList = loadTwoLineElementFile().sortedWith(compareBy { it.name })
+ var tleSelectedMap = mutableMapOf<TLE, Boolean>()
private fun loadTwoLineElementFile(): List<TLE> {
return try {
@@ -61,25 +62,27 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
}
- suspend fun updateRecycler(): List<SatPass> {
+ suspend fun getPassesForSelectedSatellites(): List<SatPass> {
val satPassList = mutableListOf<SatPass>()
var sortedList = listOf<SatPass>()
withContext(Dispatchers.Default) {
- for (tle in tleList.subList(0, 1)) {
- try {
- Log.d(tag, "Trying ${tle.name}")
- val passPredictor = PassPredictor(tle, gsp.value)
- val passes = passPredictor.getPasses(Date(), 6, false)
- for (pass in passes) {
- Log.d(tag, "Trying ${pass.maxEl}")
- satPassList.add(SatPass(tle.name, tle.catnum, pass))
+ for ((tle, value) in tleSelectedMap) {
+ if (value) {
+ try {
+ Log.d(tag, "Trying ${tle.name}")
+ val passPredictor = PassPredictor(tle, gsp.value)
+ val passes = passPredictor.getPasses(Date(), 6, true)
+ for (pass in passes) {
+ Log.d(tag, "Trying ${pass.maxEl}")
+ satPassList.add(SatPass(tle.name, tle.catnum, pass))
+ }
+ } catch (exception: IllegalArgumentException) {
+ val tleProblem = "There was a problem with TLE"
+ Log.d(tag, tleProblem)
+ _debugMessage.postValue(tleProblem)
+ } catch (exception: SatNotFoundException) {
+ Log.d(tag, "Certain satellites shall not pass")
}
- } catch (exception: IllegalArgumentException) {
- val tleProblem = "There was a problem with TLE"
- Log.d(tag, tleProblem)
- _debugMessage.postValue(tleProblem)
- } catch (exception: SatNotFoundException) {
- Log.d(tag, "Certain satellites shall not pass")
}
}
sortedList = satPassList.sortedWith(compareBy { it.pass.startTime })
@@ -111,7 +114,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
getApplication<Application>().openFileOutput(tleFile, Context.MODE_PRIVATE).use {
it.write(stream.readBytes())
}
- tleList = TLE.importSat(stream)
+ tleMainList = TLE.importSat(stream)
_debugMessage.postValue("TLE file was updated")
} catch (exception: IOException) {
_debugMessage.postValue("Couldn't update TLE file")