Skip to content

Satellite tracker and pass predictor for Android, inspired by Gpredict

Fixed TleSources storage and addition order

Arty Bishop0d3c9887e8c2
diff --git a/app/src/main/java/com/rtbishop/look4sat/network/RemoteDataSource.kt b/app/src/main/java/com/rtbishop/look4sat/network/RemoteDataSource.kt
index b365f5ab..bec3cab6 100644
--- a/app/src/main/java/com/rtbishop/look4sat/network/RemoteDataSource.kt
+++ b/app/src/main/java/com/rtbishop/look4sat/network/RemoteDataSource.kt
@@ -19,6 +19,7 @@
package com.rtbishop.look4sat.network
+import android.util.Log
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.data.Transmitter
import kotlinx.coroutines.Dispatchers
@@ -26,8 +27,6 @@ import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.InputStream
-import java.io.SequenceInputStream
-import java.util.*
import javax.inject.Inject
class RemoteDataSource @Inject constructor(
@@ -35,16 +34,17 @@ class RemoteDataSource @Inject constructor(
private val client: OkHttpClient
) : RemoteSource {
- override suspend fun fetchTleStream(urlList: List<TleSource>): InputStream {
- val streamTable = Hashtable<String, InputStream>()
+ override suspend fun fetchTleStreams(urlList: List<TleSource>): List<InputStream> {
+ val streams = mutableListOf<InputStream>()
withContext(Dispatchers.IO) {
- urlList.forEach {
- val request = Request.Builder().url(it.url).build()
+ urlList.withIndex().forEach {
+ val request = Request.Builder().url(it.value.url).build()
val stream = client.newCall(request).execute().body?.byteStream()
- streamTable[it.url] = stream
+ stream?.let { inputStream -> streams.add(inputStream) }
}
}
- return SequenceInputStream(streamTable.elements())
+ Log.d("myTag", urlList.toString())
+ return streams
}
override suspend fun fetchTransmitters(): List<Transmitter> {
diff --git a/app/src/main/java/com/rtbishop/look4sat/network/RemoteSource.kt b/app/src/main/java/com/rtbishop/look4sat/network/RemoteSource.kt
index 50412a07..840d6eeb 100644
--- a/app/src/main/java/com/rtbishop/look4sat/network/RemoteSource.kt
+++ b/app/src/main/java/com/rtbishop/look4sat/network/RemoteSource.kt
@@ -6,7 +6,7 @@ import java.io.InputStream
interface RemoteSource {
- suspend fun fetchTleStream(urlList: List<TleSource>): InputStream
+ suspend fun fetchTleStreams(urlList: List<TleSource>): List<InputStream>
suspend fun fetchTransmitters(): List<Transmitter>
}
diff --git a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultRepository.kt b/app/src/main/java/com/rtbishop/look4sat/repo/DefaultRepository.kt
index f9d435e4..ed91fae7 100644
--- a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultRepository.kt
+++ b/app/src/main/java/com/rtbishop/look4sat/repo/DefaultRepository.kt
@@ -50,9 +50,12 @@ class DefaultRepository @Inject constructor(
override suspend fun updateEntriesFromUrl(urlList: List<TleSource>) {
withContext(Dispatchers.IO) {
- val stream = remoteSource.fetchTleStream(urlList)
- val tleList = TLE.importSat(stream)
- val entries = tleList.map { SatEntry(it) }
+ val streams = remoteSource.fetchTleStreams(urlList)
+ val entries = mutableListOf<SatEntry>()
+ streams.forEach {
+ val list = TLE.importSat(it).map { tle -> SatEntry(tle) }
+ entries.addAll(list)
+ }
localSource.clearEntries()
localSource.insertEntries(entries)
}
diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt b/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt
index 6d0fdd0e..2a53ea6d 100644
--- a/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt
+++ b/app/src/main/java/com/rtbishop/look4sat/ui/MainActivity.kt
@@ -81,6 +81,8 @@ class MainActivity : AppCompatActivity(), TleSourcesDialogFragment.SourcesSubmit
setupComponents()
setupObservers()
setupDrawer()
+
+ viewModel.setDefaultTleSources()
}
@SuppressLint("SourceLockedOrientationActivity")
@@ -181,7 +183,7 @@ class MainActivity : AppCompatActivity(), TleSourcesDialogFragment.SourcesSubmit
}
private fun showTleSourcesDialog() {
- val sources = viewModel.getTleSources().map { TleSource(it) }
+ val sources = viewModel.getTleSources()
TleSourcesDialogFragment(sources).apply {
setSourcesListener(this@MainActivity)
show(supportFragmentManager, "TleSourcesDialog")
diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt
index 9ef1a9bc..d347fa83 100644
--- a/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt
+++ b/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt
@@ -32,6 +32,7 @@ import com.rtbishop.look4sat.utility.PrefsManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
+import kotlinx.coroutines.runBlocking
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton
@@ -59,7 +60,6 @@ class SharedViewModel @Inject constructor(
fun getRefreshRate() = prefsManager.getRefreshRate()
fun getHoursAhead() = prefsManager.getHoursAhead()
fun getMinElevation() = prefsManager.getMinElevation()
- fun getTleSources() = prefsManager.getTleSources()
fun setPositionFromPref() = _gsp.postValue(Result.Success(prefsManager.getPosition()))
suspend fun getAllEntries() = repository.getAllEntries()
@@ -92,11 +92,9 @@ class SharedViewModel @Inject constructor(
}
fun updateEntriesFromWeb(list: List<TleSource>) {
- val sourcesSet = list.map { it.url }.toSet()
- prefsManager.setTleSources(sourcesSet)
-
viewModelScope.launch(Dispatchers.IO) {
try {
+ repository.updateSources(list)
val selected = repository.getSelectedEntries().map { it.catNum }
repository.updateEntriesFromUrl(list)
repository.updateEntriesSelection(selected)
@@ -140,6 +138,22 @@ class SharedViewModel @Inject constructor(
}
}
+ fun setDefaultTleSources() {
+ if (prefsManager.isFirstLaunch()) {
+ viewModelScope.launch {
+ val defSourcesList = listOf(
+ TleSource("https://celestrak.com/NORAD/elements/active.txt"),
+ TleSource("https://amsat.org/tle/current/nasabare.txt")
+ )
+ repository.updateSources(defSourcesList)
+ }
+ }
+ }
+
+ fun getTleSources(): List<TleSource> = runBlocking {
+ return@runBlocking repository.getSources()
+ }
+
private fun getPassesForEntries(
entry: SatEntry,
dateNow: Date,
diff --git a/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt b/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt
index d825739c..8f210364 100644
--- a/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt
+++ b/app/src/main/java/com/rtbishop/look4sat/utility/PrefsManager.kt
@@ -37,12 +37,22 @@ class PrefsManager @Inject constructor(
private val keyAltitude = "altitude"
private val keyRefreshRate = "rate"
private val keyCompass = "compass"
- private val keyTleSources = "tleSources"
private val defaultHoursAhead = 8
private val defaultMinEl = 16.0
private val defaultRefreshRate = "3000"
private val defaultGSP = "0.0"
- private val defaultTleUrl = "https://celestrak.com/NORAD/elements/active.txt"
+
+ fun isFirstLaunch(): Boolean {
+ val keyIsFirstLaunch = "keyIsFirstLaunch"
+ val isFirstLaunch = preferences.getBoolean(keyIsFirstLaunch, true)
+ return if (isFirstLaunch) {
+ preferences.edit {
+ putBoolean(keyIsFirstLaunch, false)
+ apply()
+ }
+ true
+ } else isFirstLaunch
+ }
fun getHoursAhead(): Int {
return preferences.getInt(keyHoursAhead, defaultHoursAhead)
@@ -99,17 +109,6 @@ class PrefsManager @Inject constructor(
}
}
- fun getTleSources(): Set<String> {
- return preferences.getStringSet(keyTleSources, null) ?: setOf(defaultTleUrl)
- }
-
- fun setTleSources(sources: Set<String>) {
- preferences.edit {
- putStringSet(keyTleSources, sources)
- apply()
- }
- }
-
private fun SharedPreferences.Editor.putDouble(key: String, double: Double) {
putLong(key, double.toRawBits())
}