Fixed TleSources storage and addition order

This commit is contained in:
Arty Bishop committed 2020-09-09 21:13:24 +01:00
1 parent 5cdd3fdc46
commit 0d3c9887e8
6 files changed
+48 -30

No files matched your search

@@ -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> {
@@ -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>
}
@@ -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)
}
@@ -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")
@@ -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,
@@ -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())
}