Added mockito dependency, fixed radar view update bug #51

This commit is contained in:
Arty Bishop committed 2021-05-16 19:13:10 +01:00
1 parent 4cfab17413
commit 1806e0d147
13 files changed
+31 -31

No files matched your search

+2
View File
@@ -67,6 +67,8 @@ dependencies {
implementation "com.jakewharton.timber:timber:$timber_version" implementation "com.jakewharton.timber:timber:$timber_version"
testImplementation "junit:junit:$junit_version" testImplementation "junit:junit:$junit_version"
testImplementation "org.mockito:mockito-core:$mockito_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_test_version" testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_test_version"
androidTestImplementation "org.mockito:mockito-android:$mockito_version"
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leak_canary_version" debugImplementation "com.squareup.leakcanary:leakcanary-android:$leak_canary_version"
} }
@@ -44,8 +44,8 @@ object InteractorsModule {
} }
@Provides @Provides
fun provideImportDataFromFileUseCase(satelliteRepo: SatelliteRepo): ImportDataFromFile { fun provideImportDataFromStreamUseCase(satelliteRepo: SatelliteRepo): ImportDataFromStream {
return ImportDataFromFile(satelliteRepo) return ImportDataFromStream(satelliteRepo)
} }
@Provides @Provides
@@ -6,7 +6,7 @@ import java.io.InputStream
class NetworkDataSource(private val api: SatelliteService) : RemoteDataSource { class NetworkDataSource(private val api: SatelliteService) : RemoteDataSource {
override suspend fun fetchFileStream(url: String): InputStream? { override suspend fun fetchDataStream(url: String): InputStream? {
return api.fetchFileByUrl(url).body()?.byteStream() return api.fetchFileByUrl(url).body()?.byteStream()
} }
@@ -29,7 +29,7 @@ import com.rtbishop.look4sat.domain.model.SatItem
import com.rtbishop.look4sat.framework.model.Result import com.rtbishop.look4sat.framework.model.Result
import com.rtbishop.look4sat.framework.model.TleSource import com.rtbishop.look4sat.framework.model.TleSource
import com.rtbishop.look4sat.interactors.GetSatItems import com.rtbishop.look4sat.interactors.GetSatItems
import com.rtbishop.look4sat.interactors.ImportDataFromFile import com.rtbishop.look4sat.interactors.ImportDataFromStream
import com.rtbishop.look4sat.interactors.ImportDataFromWeb import com.rtbishop.look4sat.interactors.ImportDataFromWeb
import com.rtbishop.look4sat.interactors.UpdateEntriesSelection import com.rtbishop.look4sat.interactors.UpdateEntriesSelection
import com.rtbishop.look4sat.utility.PrefsManager import com.rtbishop.look4sat.utility.PrefsManager
@@ -46,7 +46,7 @@ class EntriesViewModel @Inject constructor(
private val prefsManager: PrefsManager, private val prefsManager: PrefsManager,
private val resolver: ContentResolver, private val resolver: ContentResolver,
private val getSatItems: GetSatItems, private val getSatItems: GetSatItems,
private val importDataFromFile: ImportDataFromFile, private val importDataFromStream: ImportDataFromStream,
private val importDataFromWeb: ImportDataFromWeb, private val importDataFromWeb: ImportDataFromWeb,
private val updateEntriesSelection: UpdateEntriesSelection, private val updateEntriesSelection: UpdateEntriesSelection,
) : ViewModel(), EntriesAdapter.EntriesClickListener, SearchView.OnQueryTextListener { ) : ViewModel(), EntriesAdapter.EntriesClickListener, SearchView.OnQueryTextListener {
@@ -70,7 +70,7 @@ class EntriesViewModel @Inject constructor(
_satData.value = Result.InProgress _satData.value = Result.InProgress
runCatching { runCatching {
resolver.openInputStream(uri)?.use { stream -> resolver.openInputStream(uri)?.use { stream ->
importDataFromFile(stream) importDataFromStream(stream)
} }
}.onFailure { _satData.value = Result.Error(it) } }.onFailure { _satData.value = Result.Error(it) }
} }
@@ -50,7 +50,9 @@ class MapViewModel @Inject constructor(
private lateinit var selectedSat: Satellite private lateinit var selectedSat: Satellite
val stationPos = liveData { val stationPos = liveData {
emit(Position(gsp.latitude.toOsmLat(), gsp.longitude.toOsmLon())) val osmLat = getOsmLat(gsp.latitude)
val osmLon = getOsmLon(gsp.longitude)
emit(Position(osmLat, osmLon))
} }
private val _satTrack = MutableLiveData<List<List<Position>>>() private val _satTrack = MutableLiveData<List<List<Position>>>()
@@ -120,8 +122,8 @@ class MapViewModel @Inject constructor(
val satPositions = mutableMapOf<Satellite, Position>() val satPositions = mutableMapOf<Satellite, Position>()
list.forEach { satellite -> list.forEach { satellite ->
val satPos = satellite.getPredictor(gsp).getSatPos(date) val satPos = satellite.getPredictor(gsp).getSatPos(date)
val osmLat = Math.toDegrees(satPos.latitude).toOsmLat() val osmLat = getOsmLat(Math.toDegrees(satPos.latitude))
val osmLon = Math.toDegrees(satPos.longitude).toOsmLon() val osmLon = getOsmLon(Math.toDegrees(satPos.longitude))
satPositions[satellite] = Position(osmLat, osmLon) satPositions[satellite] = Position(osmLat, osmLon)
} }
_allSatPositions.postValue(satPositions) _allSatPositions.postValue(satPositions)
@@ -133,9 +135,9 @@ class MapViewModel @Inject constructor(
val satTracks = mutableListOf<List<Position>>() val satTracks = mutableListOf<List<Position>>()
val currentTrack = mutableListOf<Position>() val currentTrack = mutableListOf<Position>()
var oldLongitude = 0.0 var oldLongitude = 0.0
sat.getPredictor(gsp).getPositions(date, 15, 0, 2.4).forEach { pos -> sat.getPredictor(gsp).getPositions(date, 15, 0, 2.4).forEach { satPos ->
val osmLat = Math.toDegrees(pos.latitude).toOsmLat() val osmLat = getOsmLat(Math.toDegrees(satPos.latitude))
val osmLon = Math.toDegrees(pos.longitude).toOsmLon() val osmLon = getOsmLon(Math.toDegrees(satPos.longitude))
val currentPosition = Position(osmLat, osmLon) val currentPosition = Position(osmLat, osmLon)
if (oldLongitude < -170.0 && currentPosition.longitude > 170.0) { if (oldLongitude < -170.0 && currentPosition.longitude > 170.0) {
// adding left terminal position // adding left terminal position
@@ -161,7 +163,9 @@ class MapViewModel @Inject constructor(
private suspend fun setSelectedSatFootprint(sat: Satellite, gsp: StationPosition, date: Date) { private suspend fun setSelectedSatFootprint(sat: Satellite, gsp: StationPosition, date: Date) {
withContext(defaultDispatcher) { withContext(defaultDispatcher) {
val satFootprint = sat.getPosition(gsp, date).getRangeCircle().map { rangePos -> val satFootprint = sat.getPosition(gsp, date).getRangeCircle().map { rangePos ->
Position(rangePos.latitude.toOsmLat(), rangePos.longitude.toOsmLon()) val osmLat = getOsmLat(rangePos.latitude)
val osmLon = getOsmLon(rangePos.longitude)
Position(osmLat, osmLon)
} }
_satFootprint.postValue(satFootprint) _satFootprint.postValue(satFootprint)
} }
@@ -170,8 +174,8 @@ class MapViewModel @Inject constructor(
private suspend fun setSelectedSatData(sat: Satellite, gsp: StationPosition, date: Date) { private suspend fun setSelectedSatData(sat: Satellite, gsp: StationPosition, date: Date) {
withContext(defaultDispatcher) { withContext(defaultDispatcher) {
val satPos = sat.getPredictor(gsp).getSatPos(date) val satPos = sat.getPredictor(gsp).getSatPos(date)
val osmLat = Math.toDegrees(satPos.latitude).toOsmLat() val osmLat = getOsmLat(Math.toDegrees(satPos.latitude))
val osmLon = Math.toDegrees(satPos.longitude).toOsmLon() val osmLon = getOsmLon(Math.toDegrees(satPos.longitude))
val osmPos = Position(osmLat, osmLon) val osmPos = Position(osmLat, osmLon)
val qthLoc = qthConverter.positionToQTH(osmPos.latitude, osmPos.longitude) ?: "-- --" val qthLoc = qthConverter.positionToQTH(osmPos.latitude, osmPos.longitude) ?: "-- --"
val velocity = getOrbitalVelocity(satPos.altitude) val velocity = getOrbitalVelocity(satPos.altitude)
@@ -183,19 +187,10 @@ class MapViewModel @Inject constructor(
} }
} }
private fun Double.toOsmLat(): Double {
return min(max(this, -85.0), 85.0)
}
private fun getOsmLat(latitude: Double): Double { private fun getOsmLat(latitude: Double): Double {
return min(max(latitude, -85.0), 85.0) return min(max(latitude, -85.0), 85.0)
} }
private fun Double.toOsmLon(): Double {
val newLon = if (this < -180.0) this + 360.0 else if (this > 180.0) this - 360.0 else this
return min(max(newLon, -180.0), 180.0)
}
private fun getOsmLon(longitude: Double): Double { private fun getOsmLon(longitude: Double): Double {
val newLongitude = when { val newLongitude = when {
longitude < -180.0 -> longitude + 360.0 longitude < -180.0 -> longitude + 360.0
@@ -92,6 +92,7 @@ class PolarView(context: Context) : View(context) {
} }
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
canvas.drawColor(ContextCompat.getColor(context, R.color.greyDark))
canvas.translate(polarCenter, polarCenter) canvas.translate(polarCenter, polarCenter)
drawRadarView(canvas) drawRadarView(canvas)
drawRadarText(canvas) drawRadarText(canvas)
+1
View File
@@ -14,6 +14,7 @@ buildscript {
osmdroid_version = '6.1.10' osmdroid_version = '6.1.10'
timber_version = '4.7.1' timber_version = '4.7.1'
junit_version = '4.13.2' junit_version = '4.13.2'
mockito_version = '3.9.0'
coroutines_test_version = '1.3.2' coroutines_test_version = '1.3.2'
leak_canary_version = '2.5' leak_canary_version = '2.5'
} }
+1
View File
@@ -13,5 +13,6 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
testImplementation "junit:junit:$junit_version" testImplementation "junit:junit:$junit_version"
testImplementation "org.mockito:mockito-core:$mockito_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_test_version" testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_test_version"
} }
@@ -31,7 +31,7 @@ class DefaultSatelliteRepo(
return localSource.getSelectedSatellites() return localSource.getSelectedSatellites()
} }
override suspend fun importDataFromFile(stream: InputStream) = withContext(ioDispatcher) { override suspend fun importDataFromStream(stream: InputStream) = withContext(ioDispatcher) {
val importedEntries = Satellite.importElements(stream).map { tle -> SatEntry(tle) } val importedEntries = Satellite.importElements(stream).map { tle -> SatEntry(tle) }
localSource.updateEntries(importedEntries) localSource.updateEntries(importedEntries)
} }
@@ -42,7 +42,7 @@ class DefaultSatelliteRepo(
val importedEntries = mutableListOf<SatEntry>() val importedEntries = mutableListOf<SatEntry>()
val streams = mutableListOf<InputStream>() val streams = mutableListOf<InputStream>()
sources.forEach { source -> sources.forEach { source ->
remoteSource.fetchFileStream(source)?.let { stream -> remoteSource.fetchDataStream(source)?.let { stream ->
if (source.contains(".zip", true)) { if (source.contains(".zip", true)) {
val zipStream = ZipInputStream(stream).apply { nextEntry } val zipStream = ZipInputStream(stream).apply { nextEntry }
streams.add(zipStream) streams.add(zipStream)
@@ -5,7 +5,7 @@ import java.io.InputStream
interface RemoteDataSource { interface RemoteDataSource {
suspend fun fetchFileStream(url: String): InputStream? suspend fun fetchDataStream(url: String): InputStream?
suspend fun fetchTransmitters(): List<SatTrans> suspend fun fetchTransmitters(): List<SatTrans>
} }
@@ -14,7 +14,7 @@ interface SatelliteRepo {
suspend fun getSelectedSatellites(): List<Satellite> suspend fun getSelectedSatellites(): List<Satellite>
suspend fun importDataFromFile(stream: InputStream) suspend fun importDataFromStream(stream: InputStream)
suspend fun importDataFromWeb(sources: List<String>) suspend fun importDataFromWeb(sources: List<String>)
@@ -21,5 +21,5 @@ data class StationPosition(
val latitude: Double, val latitude: Double,
val longitude: Double, val longitude: Double,
val altitude: Double, val altitude: Double,
val name: String = "base" val name: String = "default"
) )
@@ -3,9 +3,9 @@ package com.rtbishop.look4sat.interactors
import com.rtbishop.look4sat.domain.SatelliteRepo import com.rtbishop.look4sat.domain.SatelliteRepo
import java.io.InputStream import java.io.InputStream
class ImportDataFromFile(private val satelliteRepo: SatelliteRepo) { class ImportDataFromStream(private val satelliteRepo: SatelliteRepo) {
suspend operator fun invoke(stream: InputStream) { suspend operator fun invoke(stream: InputStream) {
satelliteRepo.importDataFromFile(stream) satelliteRepo.importDataFromStream(stream)
} }
} }