Now storing fetched satellite types in shared prefs

pull/101/head
Arty Bishop 2022-04-03 18:36:18 +01:00
rodzic 8c9d2031c2
commit 53697ef0c4
5 zmienionych plików z 70 dodań i 4 usunięć

Wyświetl plik

@ -80,6 +80,16 @@ class SettingsManager @Inject constructor(private val prefs: SharedPreferences)
return catnums?.sorted() ?: emptyList()
}
override fun saveSatType(type: String, catnums: List<Int>) {
val stringList = catnums.map { catnum -> catnum.toString() }
prefs.edit { putStringSet("type$type", stringList.toSet()) }
}
override fun loadSatType(type: String): List<Int> {
val catnums = prefs.getStringSet("type$type", emptySet())?.map { catnum -> catnum.toInt() }
return catnums ?: emptyList()
}
override fun getHoursAhead(): Int {
return prefs.getInt(keyHoursAhead, 24)
}

Wyświetl plik

@ -48,7 +48,10 @@ object BaseModule {
@Provides
@Singleton
fun provideDataRepository(@ApplicationContext context: Context): IDataRepository {
fun provideDataRepository(
@ApplicationContext context: Context,
settings: ISettingsManager
): IDataRepository {
val db = Room.databaseBuilder(context, LocalDatabase::class.java, "Look4SatDb")
.addMigrations(MIGRATION_1_2).fallbackToDestructiveMigration().build()
val parser = DataParser(Dispatchers.Default)
@ -56,8 +59,8 @@ object BaseModule {
val entries = LocalEntrySource(db.entriesDao())
val radios = LocalRadioSource(db.radiosDao())
val remoteSource = RemoteDataSource(Dispatchers.IO)
val repositoryScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
return DataRepository(parser, fileSource, entries, radios, remoteSource, repositoryScope)
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
return DataRepository(parser, fileSource, entries, radios, remoteSource, scope, settings)
}
@Provides

Wyświetl plik

@ -42,6 +42,8 @@ interface IDataRepository {
fun updateFromWeb(urls: List<String>)
fun updateFromWebNew()
fun setUpdateStateHandled()
fun clearAllData()

Wyświetl plik

@ -93,6 +93,10 @@ interface ISettingsManager {
fun loadEntriesSelection(): List<Int>
fun saveSatType(type: String, catnums: List<Int>)
fun loadSatType(type: String): List<Int>
fun getRotatorEnabled(): Boolean
fun setRotatorEnabled(value: Boolean)

Wyświetl plik

@ -18,6 +18,7 @@
package com.rtbishop.look4sat.domain.data
import com.rtbishop.look4sat.domain.IDataRepository
import com.rtbishop.look4sat.domain.ISettingsManager
import com.rtbishop.look4sat.domain.model.DataState
import com.rtbishop.look4sat.domain.model.SatEntry
import com.rtbishop.look4sat.utility.DataParser
@ -33,7 +34,8 @@ class DataRepository(
private val entrySource: ILocalEntrySource,
private val radioSource: ILocalRadioSource,
private val remoteSource: IRemoteDataSource,
private val repositoryScope: CoroutineScope
private val repositoryScope: CoroutineScope,
private val settingsManager: ISettingsManager
) : IDataRepository {
private val exceptionHandler = CoroutineExceptionHandler { _, exception ->
@ -95,6 +97,51 @@ class DataRepository(
}
}
override fun updateFromWebNew() {
_updateState.value = DataState.Loading
repositoryScope.launch(exceptionHandler) {
val importedEntries = mutableListOf<SatEntry>()
val sourcesMap = settingsManager.sourcesMap
val jobsMap = sourcesMap.mapValues { async { remoteSource.getDataStream(it.value) } }
jobsMap.mapValues { job -> job.value.await() }.forEach { entry ->
entry.value?.let { stream ->
when (val type = entry.key) {
"AMSAT" -> {
// parse tle stream
val satellites = importSatellites(stream)
val catnums = satellites.map { it.data.catnum }
settingsManager.saveSatType(type, catnums)
importedEntries.addAll(satellites)
}
"McCants", "Classified" -> {
// unzip and parse tle stream
val unzipped = ZipInputStream(stream).apply { nextEntry }
val satellites = importSatellites(unzipped)
val catnums = satellites.map { it.data.catnum }
settingsManager.saveSatType(type, catnums)
importedEntries.addAll(satellites)
}
else -> {
// parse csv stream
val parsed = dataParser.parseCSVStream(stream)
val satellites = parsed.map { data -> SatEntry(data) }
val catnums = satellites.map { it.data.catnum }
settingsManager.saveSatType(type, catnums)
importedEntries.addAll(satellites)
}
}
}
}
entrySource.insertEntries(importedEntries)
_updateState.value = DataState.Success(0L)
}
repositoryScope.launch(exceptionHandler) {
remoteSource.getDataStream(remoteSource.radioApi)?.let { stream ->
radioSource.insertRadios(dataParser.parseJSONStream(stream))
}
}
}
override fun setUpdateStateHandled() {
_updateState.value = DataState.Handled
}