Further code refactoring and cleanup

This commit is contained in:
Arty Bishop committed 2021-10-27 21:47:02 +01:00
1 parent 253321f3c2
commit bf72cd2061
20 files changed
+185 -224

No files matched your search

@@ -17,62 +17,49 @@
*/
package com.rtbishop.look4sat.data
import com.rtbishop.look4sat.domain.SatelliteRepo
import com.rtbishop.look4sat.domain.DataRepository
import com.rtbishop.look4sat.domain.model.SatEntry
import com.rtbishop.look4sat.domain.model.SatItem
import com.rtbishop.look4sat.domain.model.Transmitter
import com.rtbishop.look4sat.domain.predict.Satellite
import com.rtbishop.look4sat.domain.predict.TLE
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.InputStream
import java.util.zip.ZipInputStream
class DataRepository(
class DefaultRepository(
private val localSource: LocalDataSource,
private val remoteSource: RemoteDataSource,
private val repoDispatcher: CoroutineDispatcher
) : SatelliteRepo {
) : DataRepository {
override fun getSatItems(): Flow<List<SatItem>> {
return localSource.getEntriesWithModes()
override val defaultSelection = listOf(43700, 25544, 25338, 28654, 33591, 40069, 27607, 24278)
override val defaultSources = listOf(
"https://celestrak.com/NORAD/elements/active.txt",
"https://amsat.org/tle/current/nasabare.txt",
"https://www.prismnet.com/~mmccants/tles/classfd.zip",
"https://www.prismnet.com/~mmccants/tles/inttles.zip"
)
override fun getSatelliteItems() = localSource.getSatelliteItems()
override suspend fun getSelectedSatellites() = localSource.getSelectedSatellites()
override suspend fun getTransmitters(catnum: Int) = localSource.getTransmitters(catnum)
override suspend fun getWebSources() = localSource.getWebSources().also { sources ->
return if (sources.isNotEmpty()) sources
else defaultSources
}
override fun getTransmitters(catnum: Int): Flow<List<Transmitter>> {
return localSource.getTransmitters(catnum)
override suspend fun updateDataFromFile(stream: InputStream) = withContext(repoDispatcher) {
localSource.updateEntries(importSatellites(stream))
}
override fun getDefaultSources(): List<String> {
return listOf(
"https://celestrak.com/NORAD/elements/active.txt",
"https://amsat.org/tle/current/nasabare.txt",
"https://www.prismnet.com/~mmccants/tles/classfd.zip",
"https://www.prismnet.com/~mmccants/tles/inttles.zip"
)
}
override suspend fun getSavedSources(): List<String> {
val savedSources = localSource.getSources()
return if (savedSources.isEmpty()) {
getDefaultSources()
} else savedSources
}
override suspend fun getSelectedSatellites(): List<Satellite> {
return localSource.getSelectedSatellites()
}
override suspend fun updateEntriesFromFile(stream: InputStream) = withContext(repoDispatcher) {
localSource.updateEntries(importSatEntries(stream))
}
override suspend fun updateEntriesFromWeb(sources: List<String>) {
override suspend fun updateDataFromWeb(sources: List<String>) {
coroutineScope {
launch(repoDispatcher) {
localSource.updateSources(sources)
localSource.updateWebSources(sources)
}
launch(repoDispatcher) {
val streams = mutableListOf<InputStream>()
@@ -87,7 +74,7 @@ class DataRepository(
}
}
}
streams.forEach { stream -> entries.addAll(importSatEntries(stream)) }
streams.forEach { stream -> entries.addAll(importSatellites(stream)) }
localSource.updateEntries(entries)
}
launch(repoDispatcher) {
@@ -97,11 +84,11 @@ class DataRepository(
}
}
override suspend fun updateEntriesSelection(catnums: List<Int>, isSelected: Boolean) {
localSource.updateEntriesSelection(catnums, isSelected)
override suspend fun updateSelection(catnums: List<Int>, isSelected: Boolean) {
localSource.updateSelection(catnums, isSelected)
}
private fun importSatEntries(stream: InputStream): List<SatEntry> {
private fun importSatellites(stream: InputStream): List<SatEntry> {
return TLE.parseTleStream(stream).map { tle -> SatEntry(tle) }
}
}
@@ -25,22 +25,19 @@ import kotlinx.coroutines.flow.Flow
interface LocalDataSource {
// Entries
fun getEntriesWithModes(): Flow<List<SatItem>>
fun getSatelliteItems(): Flow<List<SatItem>>
suspend fun getSelectedSatellites(): List<Satellite>
suspend fun getTransmitters(catnum: Int): List<Transmitter>
suspend fun getWebSources(): List<String>
suspend fun updateEntries(entries: List<SatEntry>)
suspend fun updateEntriesSelection(catnums: List<Int>, isSelected: Boolean)
// Sources
suspend fun getSources(): List<String>
suspend fun updateSources(sources: List<String>)
// Transmitters
fun getTransmitters(catnum: Int): Flow<List<Transmitter>>
suspend fun updateSelection(catnums: List<Int>, isSelected: Boolean)
suspend fun updateTransmitters(transmitters: List<Transmitter>)
suspend fun updateWebSources(sources: List<String>)
}
@@ -23,21 +23,22 @@ import com.rtbishop.look4sat.domain.predict.Satellite
import kotlinx.coroutines.flow.Flow
import java.io.InputStream
interface SatelliteRepo {
interface DataRepository {
fun getSatItems(): Flow<List<SatItem>>
val defaultSelection: List<Int>
val defaultSources: List<String>
fun getTransmitters(catnum: Int): Flow<List<Transmitter>>
fun getDefaultSources(): List<String>
suspend fun getSavedSources(): List<String>
fun getSatelliteItems(): Flow<List<SatItem>>
suspend fun getSelectedSatellites(): List<Satellite>
suspend fun updateEntriesFromFile(stream: InputStream)
suspend fun getTransmitters(catnum: Int): List<Transmitter>
suspend fun updateEntriesFromWeb(sources: List<String>)
suspend fun getWebSources(): List<String>
suspend fun updateEntriesSelection(catnums: List<Int>, isSelected: Boolean)
suspend fun updateDataFromFile(stream: InputStream)
suspend fun updateDataFromWeb(sources: List<String> = defaultSources)
suspend fun updateSelection(catnums: List<Int> = defaultSelection, isSelected: Boolean = true)
}
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.rtbishop.look4sat.domain
package com.rtbishop.look4sat.domain.model
sealed class DataState<out T> {
data class Success<out T>(val data: T) : DataState<T>()