Skip to content

Satellite tracker and pass predictor for Android, inspired by Gpredict

Main Tle list in now LiveData. Changes to the ground station position now lead to passes recalculation. Minor refactoring.

Arty Bishope44379f43ed0
diff --git a/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt b/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt
index e91eee4f..aba986e5 100644
--- a/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt
+++ b/app/src/main/java/com/rtbishop/lookingsat/MainViewModel.kt
@@ -48,7 +48,17 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
private val _debugMessage = MutableLiveData("")
val debugMessage: LiveData<String> = _debugMessage
- var tleMainList = loadTwoLineElementFile().sortedWith(compareBy { it.name })
+ 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>()
+ }
+
+ )
+ val tleMainList: LiveData<List<TLE>> = _tleMainList
private val _tleSelectedMap = MutableLiveData<MutableMap<TLE, Boolean>>(mutableMapOf())
val tleSelectedMap: LiveData<MutableMap<TLE, Boolean>> = _tleSelectedMap
@@ -70,15 +80,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
)
val gsp: LiveData<GroundStationPosition> = _gsp
- private fun loadTwoLineElementFile(): List<TLE> {
- return try {
- TLE.importSat(getApplication<Application>().openFileInput(tleFile))
- } 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) {
@@ -138,7 +139,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
getApplication<Application>().openFileOutput(tleFile, Context.MODE_PRIVATE).use {
it.write(stream.readBytes())
}
- tleMainList = TLE.importSat(stream)
+ _tleMainList.postValue(TLE.importSat(stream))
_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 bab36312..632793e9 100644
--- a/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt
+++ b/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt
@@ -34,6 +34,7 @@ class SkyFragment : Fragment() {
private lateinit var btnPassPrefs: ImageButton
private lateinit var progressBar: ProgressBar
private lateinit var fab: FloatingActionButton
+ private lateinit var tleMainList: List<TLE>
private lateinit var selectedSatMap: MutableMap<TLE, Boolean>
private lateinit var satPassPrefs: SatPassPrefs
private var aosTimer: CountDownTimer? = null
@@ -46,6 +47,12 @@ class SkyFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
+ setupViews(view)
+ setupObservers()
+ resetTimer()
+ }
+
+ private fun setupViews(view: View) {
viewModel = ViewModelProvider(activity as MainActivity).get(MainViewModel::class.java)
timeToAos = (activity as MainActivity).findViewById(R.id.toolbar_time_to_aos)
btnPassPrefs = (activity as MainActivity).findViewById(R.id.toolbar_btn_refresh)
@@ -59,6 +66,12 @@ class SkyFragment : Fragment() {
fab.setOnClickListener { showSelectSatDialog() }
satPassPrefs = viewModel.satPassPrefs.value ?: SatPassPrefs(8, 20.0)
+ }
+
+ private fun setupObservers() {
+ viewModel.tleMainList.observe(this, Observer {
+ tleMainList = it
+ })
viewModel.tleSelectedMap.observe(this, Observer {
selectedSatMap = it
@@ -70,7 +83,9 @@ class SkyFragment : Fragment() {
calculatePasses()
})
- resetTimer()
+ viewModel.gsp.observe(this, Observer {
+ calculatePasses()
+ })
}
private fun showSatPassPrefsDialog() {
@@ -97,18 +112,19 @@ class SkyFragment : Fragment() {
}
private fun showSelectSatDialog() {
+ val tleList = tleMainList
val tleSelectedMap = selectedSatMap
- val tleMainListSize = viewModel.tleMainList.size
+ val tleMainListSize = tleList.size
val tleNameArray = arrayOfNulls<String>(tleMainListSize)
val tleCheckedArray = BooleanArray(tleMainListSize)
if (tleSelectedMap.isEmpty()) {
- for ((position, tle) in viewModel.tleMainList.withIndex()) {
+ tleList.withIndex().forEach { (position, tle) ->
tleNameArray[position] = tle.name
tleCheckedArray[position] = false
}
} else {
- for ((position, tle) in viewModel.tleMainList.withIndex()) {
+ tleList.withIndex().forEach { (position, tle) ->
tleNameArray[position] = tle.name
tleCheckedArray[position] = tleSelectedMap.getOrDefault(tle, false)
}
@@ -118,7 +134,7 @@ class SkyFragment : Fragment() {
val builder = AlertDialog.Builder(activity as MainActivity)
builder.setTitle(getString(R.string.title_select_sat))
.setMultiChoiceItems(tleNameArray, tleCheckedArray) { _, which, isChecked ->
- selectionMap[viewModel.tleMainList[which]] = isChecked
+ selectionMap[tleList[which]] = isChecked
}
.setPositiveButton(getString(R.string.btn_ok)) { _, _ ->
for ((tle, value) in selectionMap) {
@@ -166,7 +182,6 @@ class SkyFragment : Fragment() {
override fun onFinish() {
Toast.makeText(activity, "Time is up!", Toast.LENGTH_SHORT).show()
this.cancel()
- resetTimer()
}
override fun onTick(millisUntilFinished: Long) {