Minor refactoring and classes move

This commit is contained in:
Arty Bishop committed 2021-04-24 16:47:42 +01:00
1 parent e9a6471b6d
commit 9ffb793dfd
16 files changed
+158 -173

No files matched your search

@@ -23,6 +23,7 @@ import android.content.SharedPreferences
import android.hardware.SensorManager
import android.location.LocationManager
import androidx.preference.PreferenceManager
import com.rtbishop.look4sat.domain.predict4kotlin.QthConverter
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@@ -33,6 +34,11 @@ import dagger.hilt.components.SingletonComponent
@InstallIn(SingletonComponent::class)
object ApplicationModule {
@Provides
fun provideQthConverter(): QthConverter {
return QthConverter()
}
@Provides
fun provideContentResolver(@ApplicationContext context: Context): ContentResolver {
return context.contentResolver
@@ -23,6 +23,8 @@ import com.rtbishop.look4sat.data.DefaultSatelliteRepo
import com.rtbishop.look4sat.data.LocalDataSource
import com.rtbishop.look4sat.data.RemoteDataSource
import com.rtbishop.look4sat.domain.SatelliteRepo
import com.rtbishop.look4sat.framework.api.NetworkDataSource
import com.rtbishop.look4sat.framework.api.SatelliteService
import com.rtbishop.look4sat.framework.db.RoomConverters
import com.rtbishop.look4sat.framework.db.RoomDataSource
import com.rtbishop.look4sat.framework.db.SatelliteDao
@@ -34,15 +36,17 @@ import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object LocalSourceModule {
object DataSourceModule {
@Provides
fun provideLocalDataSource(satelliteDao: SatelliteDao): LocalDataSource {
return RoomDataSource(satelliteDao)
fun provideMoshi(): Moshi {
return Moshi.Builder().build()
}
@Provides
@@ -56,8 +60,8 @@ object LocalSourceModule {
}
@Provides
fun provideMoshi(): Moshi {
return Moshi.Builder().build()
fun provideLocalDataSource(satelliteDao: SatelliteDao): LocalDataSource {
return RoomDataSource(satelliteDao)
}
@Provides
@@ -70,4 +74,18 @@ object LocalSourceModule {
RoomConverters.initialize(moshi)
return Room.databaseBuilder(context, SatelliteDb::class.java, "SatelliteDb").build()
}
@Provides
fun provideRemoteDataSource(satelliteService: SatelliteService): RemoteDataSource {
return NetworkDataSource(satelliteService)
}
@Provides
fun provideSatDataService(): SatelliteService {
return Retrofit.Builder()
.baseUrl("https://db.satnogs.org/api/")
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create(SatelliteService::class.java)
}
}
@@ -1,3 +1,20 @@
/*
* Look4Sat. Amateur radio satellite tracker and pass predictor.
* Copyright (C) 2019-2021 Arty Bishop (bishop.arty@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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.di
import com.rtbishop.look4sat.domain.SatelliteRepo
@@ -1,47 +0,0 @@
/*
* Look4Sat. Amateur radio satellite tracker and pass predictor.
* Copyright (C) 2019-2021 Arty Bishop (bishop.arty@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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.di
import com.rtbishop.look4sat.data.RemoteDataSource
import com.rtbishop.look4sat.framework.api.NetworkDataSource
import com.rtbishop.look4sat.framework.api.SatelliteService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
@Module
@InstallIn(SingletonComponent::class)
object RemoteSourceModule {
@Provides
fun provideRemoteDataSource(satelliteService: SatelliteService): RemoteDataSource {
return NetworkDataSource(satelliteService)
}
@Provides
fun provideSatDataService(): SatelliteService {
return Retrofit.Builder()
.baseUrl("https://db.satnogs.org/api/")
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create(SatelliteService::class.java)
}
}
@@ -91,7 +91,7 @@ class MapFragment : Fragment(R.layout.fragment_map) {
setInfoWindow(null)
setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM)
icon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_map_pos)
position = GeoPoint(osmPos.lat, osmPos.lon)
position = GeoPoint(osmPos.latitude, osmPos.longitude)
mapView.overlays[0] = this
mapView.invalidate()
}
@@ -105,8 +105,8 @@ class MapFragment : Fragment(R.layout.fragment_map) {
altitude.text = String.format(getString(R.string.pat_altitude), sat.altitude)
distance.text = String.format(getString(R.string.pat_distance), sat.range)
velocity.text = String.format(getString(R.string.pat_osm_vel), sat.velocity)
mapLat.text = String.format(getString(R.string.pat_osm_lat), sat.osmPos.lat)
mapLon.text = String.format(getString(R.string.pat_osm_lon), sat.osmPos.lon)
mapLat.text = String.format(getString(R.string.pat_osm_lat), sat.osmPos.latitude)
mapLon.text = String.format(getString(R.string.pat_osm_lon), sat.osmPos.longitude)
mapView.overlays[1] = sat.groundTrack
mapView.overlays[2] = sat.footprint
mapView.invalidate()
@@ -127,7 +127,7 @@ class MapFragment : Fragment(R.layout.fragment_map) {
setTextIcon(it.key.tle.name)
setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER)
try {
position = GeoPoint(it.value.lat, it.value.lon)
position = GeoPoint(it.value.latitude, it.value.longitude)
} catch (e: IllegalArgumentException) {
Timber.d("Position: $position")
}
@@ -143,7 +143,7 @@ class MapFragment : Fragment(R.layout.fragment_map) {
setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER)
icon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_map_sat)
try {
position = GeoPoint(it.value.lat, it.value.lon)
position = GeoPoint(it.value.latitude, it.value.longitude)
} catch (e: IllegalArgumentException) {
Timber.d("Position: $position")
}
@@ -23,6 +23,7 @@ import androidx.lifecycle.*
import com.rtbishop.look4sat.framework.model.SelectedSat
import com.rtbishop.look4sat.interactors.GetSelectedSatellites
import com.rtbishop.look4sat.domain.predict4kotlin.Position
import com.rtbishop.look4sat.domain.predict4kotlin.QthConverter
import com.rtbishop.look4sat.domain.predict4kotlin.SatPos
import com.rtbishop.look4sat.domain.predict4kotlin.Satellite
import com.rtbishop.look4sat.utility.*
@@ -71,7 +72,7 @@ class MapViewModel @Inject constructor(
fun getSatMarkers(): LiveData<Map<Satellite, Position>> = _satMarkers
val stationPosition = liveData {
emit(Position(gsp.lat.toOsmLat(), gsp.lon.toOsmLon()))
emit(Position(gsp.latitude.toOsmLat(), gsp.longitude.toOsmLon()))
}
init {
@@ -116,7 +117,7 @@ class MapViewModel @Inject constructor(
Math.toDegrees(satPos.latitude).toOsmLat(),
Math.toDegrees(satPos.longitude).toOsmLon()
)
val qthLoc = qthConverter.locationToQTH(osmPos.lat, osmPos.lon) ?: "-- --"
val qthLoc = qthConverter.positionToQTH(osmPos.latitude, osmPos.longitude) ?: "-- --"
val velocity = satPos.altitude.getOrbitalVelocity()
val footprint = getSatFootprint(satPos)
val track = getSatTrack(satellite)
@@ -149,7 +150,7 @@ class MapViewModel @Inject constructor(
Math.toDegrees(it.latitude).toOsmLat(),
Math.toDegrees(it.longitude).toOsmLon()
)
if (oldLon < -170.0 && osmPos.lon > 170.0 || oldLon > 170.0 && osmPos.lon < -170.0) {
if (oldLon < -170.0 && osmPos.longitude > 170.0 || oldLon > 170.0 && osmPos.longitude < -170.0) {
val currentPoints = mutableListOf<GeoPoint>()
currentPoints.addAll(trackPoints)
Polyline().apply {
@@ -159,8 +160,8 @@ class MapViewModel @Inject constructor(
}
trackPoints.clear()
}
oldLon = osmPos.lon
trackPoints.add(GeoPoint(osmPos.lat, osmPos.lon))
oldLon = osmPos.longitude
trackPoints.add(GeoPoint(osmPos.latitude, osmPos.longitude))
}
Polyline().apply {
outlinePaint.set(trackPaint)
@@ -173,8 +174,8 @@ class MapViewModel @Inject constructor(
private fun getSatFootprint(satPos: SatPos): Overlay {
val points = mutableListOf<GeoPoint>()
satPos.getRangeCircle().forEach {
val osmPos = Position(it.lat.toOsmLat(), it.lon.toOsmLon())
points.add(GeoPoint(osmPos.lat, osmPos.lon))
val osmPos = Position(it.latitude.toOsmLat(), it.longitude.toOsmLon())
points.add(GeoPoint(osmPos.latitude, osmPos.longitude))
}
return Polygon().apply {
fillPaint.set(footprintPaint)
@@ -32,7 +32,7 @@ import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.snackbar.Snackbar
import com.rtbishop.look4sat.R
import com.rtbishop.look4sat.utility.PrefsManager
import com.rtbishop.look4sat.utility.QthConverter
import com.rtbishop.look4sat.domain.predict4kotlin.QthConverter
import com.rtbishop.look4sat.utility.round
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
@@ -102,8 +102,8 @@ class PrefsFragment : PreferenceFragmentCompat() {
}
private fun setPositionFromQth(qthString: String): Boolean {
qthConverter.qthToLocation(qthString)?.let { gsp ->
prefsManager.setStationPosition(gsp.lat, gsp.lon, gsp.alt)
qthConverter.qthToPosition(qthString)?.let { gsp ->
prefsManager.setStationPosition(gsp.latitude, gsp.longitude, 0.0)
showSnack(getString(R.string.pref_pos_success))
return true
}
@@ -21,7 +21,7 @@ import android.content.SharedPreferences
import android.hardware.GeomagneticField
import androidx.core.content.edit
import com.rtbishop.look4sat.framework.model.TleSource
import com.rtbishop.look4sat.domain.predict4kotlin.GroundPos
import com.rtbishop.look4sat.domain.predict4kotlin.StationPosition
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import javax.inject.Inject
@@ -60,12 +60,12 @@ class PrefsManager @Inject constructor(val preferences: SharedPreferences, moshi
return preferences.getInt(keyMinElevation, 16).toDouble()
}
fun getStationPosition(): GroundPos {
fun getStationPosition(): StationPosition {
val defaultGSP = "0.0"
val latitude = preferences.getString(keyLatitude, null) ?: defaultGSP
val longitude = preferences.getString(keyLongitude, null) ?: defaultGSP
val altitude = preferences.getString(keyAltitude, null) ?: defaultGSP
return GroundPos(latitude.toDouble(), longitude.toDouble(), altitude.toDouble())
return StationPosition(latitude.toDouble(), longitude.toDouble(), altitude.toDouble())
}
fun setStationPosition(latitude: Double, longitude: Double, altitude: Double) {
@@ -78,9 +78,9 @@ class PrefsManager @Inject constructor(val preferences: SharedPreferences, moshi
fun getMagDeclination(): Float {
val stationPosition = getStationPosition()
val lat = stationPosition.lat.toFloat()
val lon = stationPosition.lon.toFloat()
val alt = stationPosition.alt.toFloat()
val lat = stationPosition.latitude.toFloat()
val lon = stationPosition.longitude.toFloat()
val alt = stationPosition.altitude.toFloat()
return GeomagneticField(lat, lon, alt, System.currentTimeMillis()).declination
}
@@ -1,63 +0,0 @@
/*
* Look4Sat. Amateur radio satellite tracker and pass predictor.
* Copyright (C) 2019-2021 Arty Bishop (bishop.arty@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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.utility
import com.rtbishop.look4sat.domain.predict4kotlin.GroundPos
import javax.inject.Inject
class QthConverter @Inject constructor() {
fun qthToLocation(qthString: String): GroundPos? {
val trimmedQth = qthString.take(6)
if (!isValidQTH(trimmedQth)) return null
val lonFirst = (trimmedQth[0].toUpperCase().toInt() - 65) * 20
val latFirst = (trimmedQth[1].toUpperCase().toInt() - 65) * 10
val lonSecond = trimmedQth[2].toString().toInt() * 2
val latSecond = trimmedQth[3].toString().toInt()
val lonThird = (((trimmedQth[4].toLowerCase().toInt() - 97) / 12.0) + (1.0 / 24.0)) - 180
val latThird = (((trimmedQth[5].toLowerCase().toInt() - 97) / 24.0) + (1.0 / 48.0)) - 90
val longitude = (lonFirst + lonSecond + lonThird).round(4)
val latitude = (latFirst + latSecond + latThird).round(4)
return GroundPos(latitude, longitude, 0.0)
}
fun locationToQTH(lat: Double, lon: Double): String? {
if (!isValidLocation(lat, lon)) return null
val tempLon = if (lon > 180.0) lon - 360 else lon
val upper = "ABCDEFGHIJKLMNOPQRSTUVWX"
val lower = "abcdefghijklmnopqrstuvwx"
val longitude = tempLon + 180
val latitude = lat + 90
val lonFirst = upper[(longitude / 20).toInt()]
val latFirst = upper[(latitude / 10).toInt()]
val lonSecond = ((longitude / 2) % 10).toInt().toString()
val latSecond = (latitude % 10).toInt().toString()
val lonThird = lower[((longitude % 2) * 12).toInt()]
val latThird = lower[((latitude % 1) * 24).toInt()]
return "$lonFirst$latFirst$lonSecond$latSecond$lonThird$latThird"
}
private fun isValidQTH(qthString: String): Boolean {
val qthPattern = "[a-xA-X][a-xA-X][0-9][0-9][a-xA-X][a-xA-X]".toRegex()
return qthString.matches(qthPattern)
}
private fun isValidLocation(lat: Double, lon: Double): Boolean {
return (lat > -90.0 && lat < 90.0) && (lon > -180.0 && lon < 360.0)
}
}
@@ -1,60 +0,0 @@
/*
* Look4Sat. Amateur radio satellite tracker and pass predictor.
* Copyright (C) 2019-2021 Arty Bishop (bishop.arty@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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
import com.rtbishop.look4sat.utility.QthConverter
import org.junit.Test
class QthConverterTest {
private val converter = QthConverter()
@Test
fun `Given valid QTH returns correct location`() {
var result = converter.qthToLocation("io91VL39FX")
assert(result?.latitude == 51.4792 && result.longitude == -0.2083)
result = converter.qthToLocation("JN58TD")
assert(result?.latitude == 48.1458 && result.longitude == 11.6250)
result = converter.qthToLocation("gf15vc")
assert(result?.latitude == -34.8958 && result.longitude == -56.2083)
result = converter.qthToLocation("fm18LW")
assert(result?.latitude == 38.9375 && result.longitude == -77.0417)
}
@Test
fun `Given invalid QTH returns null`() {
var result = converter.qthToLocation("ZZ00zz")
assert(result == null)
result = converter.qthToLocation("JN58tz")
assert(result == null)
}
@Test
fun `Given valid location returns correct QTH`() {
assert(converter.locationToQTH(51.4878, -0.2146) == "IO91vl")
assert(converter.locationToQTH(48.1466, 11.6083) == "JN58td")
assert(converter.locationToQTH(-34.91, -56.2116) == "GF15vc")
assert(converter.locationToQTH(38.92, -77.065) == "FM18lw")
}
@Test
fun `Given invalid location returns null`() {
assert(converter.locationToQTH(91.0542, -170.1142) == null)
assert(converter.locationToQTH(89.0542, -240.1142) == null)
}
}