mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Now downloading TLE files by the list of URLs and using coroutines.
This commit is contained in:
1 parent
0b93780d7c
commit
5e53bb9db9
3 files changed
+16
-15
No files matched your search
@@ -73,9 +73,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
(app as Look4SatApp).appComponent.inject(this)
|
||||
}
|
||||
|
||||
private var passPrefs = SatPassPrefs(
|
||||
preferences.getString(keyHours, defValueHours)!!.toInt(),
|
||||
preferences.getString(keyMinEl, defValueMinEl)!!.toDouble()
|
||||
private val urlList = listOf(
|
||||
"https://celestrak.com/NORAD/elements/amateur.txt",
|
||||
"https://celestrak.com/NORAD/elements/weather.txt"
|
||||
)
|
||||
|
||||
private val _debugMessage = MutableLiveData<String>()
|
||||
@@ -123,7 +123,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
fun updateAndSaveTleFile() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val inputStream = repository.fetchTleStream()
|
||||
val inputStream = repository.getStreamForUrl(urlList)
|
||||
val tleList = TLE.importSat(inputStream).apply { sortBy { it.name } }
|
||||
val fileOutStream = app.openFileOutput(tleMainListFileName, Context.MODE_PRIVATE)
|
||||
ObjectOutputStream(fileOutStream).apply {
|
||||
|
||||
@@ -33,15 +33,16 @@ class RemoteDataSource @Inject constructor(
|
||||
private val api: TransmittersApi,
|
||||
private val client: OkHttpClient
|
||||
) {
|
||||
|
||||
fun fetchTleStream(): InputStream {
|
||||
val requestAmateur =
|
||||
Request.Builder().url("https://celestrak.com/NORAD/elements/amateur.txt").build()
|
||||
val requestWeather =
|
||||
Request.Builder().url("https://celestrak.com/NORAD/elements/weather.txt").build()
|
||||
val streamAmateur = client.newCall(requestAmateur).execute().body()?.byteStream()
|
||||
val streamWeather = client.newCall(requestWeather).execute().body()?.byteStream()
|
||||
return SequenceInputStream(streamAmateur, streamWeather)
|
||||
suspend fun getStreamForUrl(urlList: List<String>): InputStream {
|
||||
val streamTable = Hashtable<String, InputStream>()
|
||||
withContext(Dispatchers.IO) {
|
||||
urlList.forEach {
|
||||
val request = Request.Builder().url(it).build()
|
||||
val stream = client.newCall(request).execute().body()?.byteStream()!!
|
||||
streamTable[it] = stream
|
||||
}
|
||||
}
|
||||
return SequenceInputStream(streamTable.elements())
|
||||
}
|
||||
|
||||
suspend fun fetchTransmittersList(): List<Transmitter> {
|
||||
|
||||
@@ -28,8 +28,8 @@ class Repository @Inject constructor(
|
||||
private val localSource: LocalDataSource,
|
||||
private val remoteSource: RemoteDataSource
|
||||
) {
|
||||
fun fetchTleStream(): InputStream {
|
||||
return remoteSource.fetchTleStream()
|
||||
suspend fun getStreamForUrl(urlList: List<String>): InputStream {
|
||||
return remoteSource.getStreamForUrl(urlList)
|
||||
}
|
||||
|
||||
suspend fun updateTransmittersDatabase() {
|
||||
|
||||
Reference in new issue
Block a user