From b6d019ace7dadc3f3de99484e89e5ce1cedebaa0 Mon Sep 17 00:00:00 2001 From: Arty Bishop Date: Sun, 19 Sep 2021 17:57:17 +0100 Subject: [PATCH] Renamed and moved several classes and interfaces --- ...tationProvider.kt => OrientationSource.kt} | 2 +- ...rencesProvider.kt => PreferencesSource.kt} | 6 +-- .../{RemoteSource.kt => RemoteDataSource.kt} | 10 ++--- .../{SatelliteService.kt => SatelliteApi.kt} | 4 +- .../db/{LocalSource.kt => LocalDataSource.kt} | 14 +++--- .../db/{Converters.kt => RoomConverters.kt} | 2 +- .../look4sat/framework/db/SatelliteDb.kt | 2 +- ...{ApplicationModule.kt => AndroidModule.kt} | 2 +- .../{SatelliteDataModule.kt => DataModule.kt} | 39 ++++++++-------- .../{CoroutinesModule.kt => ThreadsModule.kt} | 0 .../entriesScreen/EntriesViewModel.kt | 6 +-- .../presentation/mapScreen/MapViewModel.kt | 4 +- .../passesScreen/PassesViewModel.kt | 32 +++++++------- .../presentation/radarScreen/RadarFragment.kt | 4 +- .../radarScreen/RadarViewModel.kt | 14 +++--- .../settingsScreen/SettingsFragment.kt | 18 ++++---- .../sourcesScreen/SourcesDialog.kt | 4 +- .../{LocalDataSource.kt => LocalSource.kt} | 8 ++-- .../{PreferencesSource.kt => Preferences.kt} | 2 +- .../{RemoteDataSource.kt => RemoteSource.kt} | 4 +- .../rtbishop/look4sat/data/SatelliteRepo.kt | 38 ++++++++-------- .../com/rtbishop/look4sat/domain/SatPos.kt | 35 +++++++-------- .../java/com/rtbishop/look4sat/domain/TLE.kt | 44 ++++++++++--------- .../{Look4SatTest.kt => TleParserTest.kt} | 14 +++--- 24 files changed, 155 insertions(+), 153 deletions(-) rename app/src/main/java/com/rtbishop/look4sat/framework/{OrientationProvider.kt => OrientationSource.kt} (97%) rename app/src/main/java/com/rtbishop/look4sat/framework/{PreferencesProvider.kt => PreferencesSource.kt} (98%) rename app/src/main/java/com/rtbishop/look4sat/framework/api/{RemoteSource.kt => RemoteDataSource.kt} (74%) rename app/src/main/java/com/rtbishop/look4sat/framework/api/{SatelliteService.kt => SatelliteApi.kt} (91%) rename app/src/main/java/com/rtbishop/look4sat/framework/db/{LocalSource.kt => LocalDataSource.kt} (84%) rename app/src/main/java/com/rtbishop/look4sat/framework/db/{Converters.kt => RoomConverters.kt} (98%) rename app/src/main/java/com/rtbishop/look4sat/injection/{ApplicationModule.kt => AndroidModule.kt} (98%) rename app/src/main/java/com/rtbishop/look4sat/injection/{SatelliteDataModule.kt => DataModule.kt} (75%) rename app/src/main/java/com/rtbishop/look4sat/injection/{CoroutinesModule.kt => ThreadsModule.kt} (100%) rename core/src/main/java/com/rtbishop/look4sat/data/{LocalDataSource.kt => LocalSource.kt} (84%) rename core/src/main/java/com/rtbishop/look4sat/data/{PreferencesSource.kt => Preferences.kt} (98%) rename core/src/main/java/com/rtbishop/look4sat/data/{RemoteDataSource.kt => RemoteSource.kt} (91%) rename core/src/test/java/com/rtbishop/look4sat/{Look4SatTest.kt => TleParserTest.kt} (79%) diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/OrientationProvider.kt b/app/src/main/java/com/rtbishop/look4sat/framework/OrientationSource.kt similarity index 97% rename from app/src/main/java/com/rtbishop/look4sat/framework/OrientationProvider.kt rename to app/src/main/java/com/rtbishop/look4sat/framework/OrientationSource.kt index 95858b35..896665a6 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/OrientationProvider.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/OrientationSource.kt @@ -24,7 +24,7 @@ import android.hardware.SensorManager import javax.inject.Inject import kotlin.math.round -class OrientationProvider @Inject constructor(private val sensorManager: SensorManager) : +class OrientationSource @Inject constructor(private val sensorManager: SensorManager) : SensorEventListener { interface OrientationListener { diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesProvider.kt b/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt similarity index 98% rename from app/src/main/java/com/rtbishop/look4sat/framework/PreferencesProvider.kt rename to app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt index b328d9ef..b8627896 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesProvider.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt @@ -21,7 +21,7 @@ import android.content.SharedPreferences import android.hardware.GeomagneticField import android.location.LocationManager import androidx.core.content.edit -import com.rtbishop.look4sat.data.PreferencesSource +import com.rtbishop.look4sat.data.Preferences import com.rtbishop.look4sat.domain.GeoPos import com.rtbishop.look4sat.domain.QthConverter import com.rtbishop.look4sat.utility.round @@ -29,11 +29,11 @@ import com.squareup.moshi.Moshi import com.squareup.moshi.Types import javax.inject.Inject -class PreferencesProvider @Inject constructor( +class PreferencesSource @Inject constructor( moshi: Moshi, private val locationManager: LocationManager, private val preferences: SharedPreferences -) : PreferencesSource { +) : Preferences { private val sourcesType = Types.newParameterizedType(List::class.java, String::class.java) private val sourcesAdapter = moshi.adapter>(sourcesType) diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/api/RemoteSource.kt b/app/src/main/java/com/rtbishop/look4sat/framework/api/RemoteDataSource.kt similarity index 74% rename from app/src/main/java/com/rtbishop/look4sat/framework/api/RemoteSource.kt rename to app/src/main/java/com/rtbishop/look4sat/framework/api/RemoteDataSource.kt index 7cd1165c..724c12ca 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/api/RemoteSource.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/api/RemoteDataSource.kt @@ -17,18 +17,18 @@ */ package com.rtbishop.look4sat.framework.api -import com.rtbishop.look4sat.data.RemoteDataSource +import com.rtbishop.look4sat.data.RemoteSource import com.rtbishop.look4sat.domain.Transmitter import com.rtbishop.look4sat.utility.DataMapper import java.io.InputStream -class RemoteSource(private val api: SatelliteService) : RemoteDataSource { +class RemoteDataSource(private val satelliteApi: SatelliteApi) : RemoteSource { - override suspend fun fetchDataStream(url: String): InputStream? { - return api.fetchFileByUrl(url).body()?.byteStream() + override suspend fun fetchFileStream(url: String): InputStream? { + return satelliteApi.fetchFileStream(url).body()?.byteStream() } override suspend fun fetchTransmitters(): List { - return DataMapper.satTransListToDomainTransList(api.fetchTransmitters()) + return DataMapper.satTransListToDomainTransList(satelliteApi.fetchTransmitters()) } } diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/api/SatelliteService.kt b/app/src/main/java/com/rtbishop/look4sat/framework/api/SatelliteApi.kt similarity index 91% rename from app/src/main/java/com/rtbishop/look4sat/framework/api/SatelliteService.kt rename to app/src/main/java/com/rtbishop/look4sat/framework/api/SatelliteApi.kt index 32e479e9..3781f117 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/api/SatelliteService.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/api/SatelliteApi.kt @@ -24,11 +24,11 @@ import retrofit2.http.GET import retrofit2.http.Streaming import retrofit2.http.Url -interface SatelliteService { +interface SatelliteApi { @Streaming @GET - suspend fun fetchFileByUrl(@Url url: String): Response + suspend fun fetchFileStream(@Url url: String): Response @GET("transmitters/") suspend fun fetchTransmitters(): List diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/db/LocalSource.kt b/app/src/main/java/com/rtbishop/look4sat/framework/db/LocalDataSource.kt similarity index 84% rename from app/src/main/java/com/rtbishop/look4sat/framework/db/LocalSource.kt rename to app/src/main/java/com/rtbishop/look4sat/framework/db/LocalDataSource.kt index e57ef1e3..b52ccfb7 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/db/LocalSource.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/db/LocalDataSource.kt @@ -17,18 +17,18 @@ */ package com.rtbishop.look4sat.framework.db -import com.rtbishop.look4sat.data.LocalDataSource +import com.rtbishop.look4sat.data.LocalSource import com.rtbishop.look4sat.data.SatEntry import com.rtbishop.look4sat.data.SatItem -import com.rtbishop.look4sat.domain.Transmitter import com.rtbishop.look4sat.domain.Satellite +import com.rtbishop.look4sat.domain.Transmitter import com.rtbishop.look4sat.utility.DataMapper import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map -class LocalSource(private val satelliteDao: SatelliteDao) : LocalDataSource { +class LocalDataSource(private val satelliteDao: SatelliteDao) : LocalSource { - override fun getSatItems(): Flow> { + override fun getEntriesWithModes(): Flow> { return satelliteDao.getSatItems() .map { satItems -> DataMapper.satItemsToDomainItems(satItems) } } @@ -46,13 +46,13 @@ class LocalSource(private val satelliteDao: SatelliteDao) : LocalDataSource { satelliteDao.updateEntriesSelection(catNums, isSelected) } - override fun getSatTransmitters(catNum: Int): Flow> { + override fun getTransmitters(catNum: Int): Flow> { return satelliteDao.getSatTransmitters(catNum) .map { satTransList -> DataMapper.satTransListToDomainTransList(satTransList) } } - override suspend fun updateTransmitters(satelliteTrans: List) { - val satTransList = DataMapper.domainTransListToSatTransList(satelliteTrans) + override suspend fun updateTransmitters(transmitters: List) { + val satTransList = DataMapper.domainTransListToSatTransList(transmitters) satelliteDao.updateTransmitters(satTransList) } } diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/db/Converters.kt b/app/src/main/java/com/rtbishop/look4sat/framework/db/RoomConverters.kt similarity index 98% rename from app/src/main/java/com/rtbishop/look4sat/framework/db/Converters.kt rename to app/src/main/java/com/rtbishop/look4sat/framework/db/RoomConverters.kt index eab45fe3..f9da541b 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/db/Converters.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/db/RoomConverters.kt @@ -23,7 +23,7 @@ import com.rtbishop.look4sat.domain.TLE import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Moshi -object Converters { +object RoomConverters { private lateinit var paramsAdapter: JsonAdapter diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/db/SatelliteDb.kt b/app/src/main/java/com/rtbishop/look4sat/framework/db/SatelliteDb.kt index f1ca0034..e543313d 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/db/SatelliteDb.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/db/SatelliteDb.kt @@ -26,7 +26,7 @@ import com.rtbishop.look4sat.framework.model.SatEntry import com.rtbishop.look4sat.framework.model.Transmitter @Database(entities = [SatEntry::class, Transmitter::class], version = 2, exportSchema = true) -@TypeConverters(Converters::class) +@TypeConverters(RoomConverters::class) abstract class SatelliteDb : RoomDatabase() { abstract fun satelliteDao(): SatelliteDao diff --git a/app/src/main/java/com/rtbishop/look4sat/injection/ApplicationModule.kt b/app/src/main/java/com/rtbishop/look4sat/injection/AndroidModule.kt similarity index 98% rename from app/src/main/java/com/rtbishop/look4sat/injection/ApplicationModule.kt rename to app/src/main/java/com/rtbishop/look4sat/injection/AndroidModule.kt index 0010d5d3..e422617b 100644 --- a/app/src/main/java/com/rtbishop/look4sat/injection/ApplicationModule.kt +++ b/app/src/main/java/com/rtbishop/look4sat/injection/AndroidModule.kt @@ -31,7 +31,7 @@ import dagger.hilt.components.SingletonComponent @Module @InstallIn(SingletonComponent::class) -object ApplicationModule { +object AndroidModule { @Provides fun provideContentResolver(@ApplicationContext context: Context): ContentResolver { diff --git a/app/src/main/java/com/rtbishop/look4sat/injection/SatelliteDataModule.kt b/app/src/main/java/com/rtbishop/look4sat/injection/DataModule.kt similarity index 75% rename from app/src/main/java/com/rtbishop/look4sat/injection/SatelliteDataModule.kt rename to app/src/main/java/com/rtbishop/look4sat/injection/DataModule.kt index 45c3d467..22d634c9 100644 --- a/app/src/main/java/com/rtbishop/look4sat/injection/SatelliteDataModule.kt +++ b/app/src/main/java/com/rtbishop/look4sat/injection/DataModule.kt @@ -21,12 +21,15 @@ import android.content.Context import android.content.SharedPreferences import android.location.LocationManager import androidx.room.Room -import com.rtbishop.look4sat.data.* +import com.rtbishop.look4sat.data.LocalSource +import com.rtbishop.look4sat.data.Preferences +import com.rtbishop.look4sat.data.RemoteSource +import com.rtbishop.look4sat.data.SatelliteRepo import com.rtbishop.look4sat.domain.PassReporter import com.rtbishop.look4sat.domain.Predictor -import com.rtbishop.look4sat.framework.PreferencesProvider -import com.rtbishop.look4sat.framework.api.RemoteSource -import com.rtbishop.look4sat.framework.api.SatelliteService +import com.rtbishop.look4sat.framework.PreferencesSource +import com.rtbishop.look4sat.framework.api.RemoteDataSource +import com.rtbishop.look4sat.framework.api.SatelliteApi import com.rtbishop.look4sat.framework.db.* import com.squareup.moshi.Moshi import dagger.Module @@ -41,7 +44,7 @@ import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) -object SatelliteDataModule { +object DataModule { @Provides @Singleton @@ -55,8 +58,8 @@ object SatelliteDataModule { moshi: Moshi, locationManager: LocationManager, preferences: SharedPreferences - ): PreferencesSource { - return PreferencesProvider(moshi, locationManager, preferences) + ): Preferences { + return PreferencesSource(moshi, locationManager, preferences) } @Provides @@ -68,17 +71,17 @@ object SatelliteDataModule { @Provides @Singleton fun provideSatelliteRepo( - preferencesSource: PreferencesSource, - localDataSource: LocalDataSource, - remoteDataSource: RemoteDataSource, + preferences: Preferences, + localSource: LocalSource, + remoteSource: RemoteSource, @IoDispatcher dispatcher: CoroutineDispatcher ): SatelliteRepo { - return SatelliteRepo(preferencesSource, localDataSource, remoteDataSource, dispatcher) + return SatelliteRepo(preferences, localSource, remoteSource, dispatcher) } @Provides - fun provideLocalDataSource(satelliteDao: SatelliteDao): LocalDataSource { - return LocalSource(satelliteDao) + fun provideLocalDataSource(satelliteDao: SatelliteDao): LocalSource { + return LocalDataSource(satelliteDao) } @Provides @@ -90,24 +93,24 @@ object SatelliteDataModule { @Provides @Singleton fun provideSatelliteDb(@ApplicationContext context: Context, moshi: Moshi): SatelliteDb { - Converters.initialize(moshi) + RoomConverters.initialize(moshi) return Room.databaseBuilder(context, SatelliteDb::class.java, "SatelliteDb") .addMigrations(MIGRATION_1_2).build() } @Provides @Singleton - fun provideRemoteDataSource(satelliteService: SatelliteService): RemoteDataSource { - return RemoteSource(satelliteService) + fun provideRemoteDataSource(satelliteApi: SatelliteApi): RemoteSource { + return RemoteDataSource(satelliteApi) } @Provides @Singleton - fun provideSatelliteService(): SatelliteService { + fun provideSatelliteService(): SatelliteApi { return Retrofit.Builder() .baseUrl("https://db.satnogs.org/api/") .addConverterFactory(MoshiConverterFactory.create()) - .build().create(SatelliteService::class.java) + .build().create(SatelliteApi::class.java) } @Provides diff --git a/app/src/main/java/com/rtbishop/look4sat/injection/CoroutinesModule.kt b/app/src/main/java/com/rtbishop/look4sat/injection/ThreadsModule.kt similarity index 100% rename from app/src/main/java/com/rtbishop/look4sat/injection/CoroutinesModule.kt rename to app/src/main/java/com/rtbishop/look4sat/injection/ThreadsModule.kt diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/entriesScreen/EntriesViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/entriesScreen/EntriesViewModel.kt index 5c15a337..5dd8e267 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/entriesScreen/EntriesViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/entriesScreen/EntriesViewModel.kt @@ -21,7 +21,7 @@ import android.content.ContentResolver import android.net.Uri import android.widget.SearchView import androidx.lifecycle.* -import com.rtbishop.look4sat.data.PreferencesSource +import com.rtbishop.look4sat.data.Preferences import com.rtbishop.look4sat.data.SatelliteRepo import com.rtbishop.look4sat.data.SatItem import com.rtbishop.look4sat.framework.model.Result @@ -33,7 +33,7 @@ import javax.inject.Inject @HiltViewModel class EntriesViewModel @Inject constructor( - private val preferencesSource: PreferencesSource, + private val preferences: Preferences, private val resolver: ContentResolver, private val satelliteRepo: SatelliteRepo, ) : ViewModel(), EntriesAdapter.EntriesClickListener, SearchView.OnQueryTextListener { @@ -67,7 +67,7 @@ class EntriesViewModel @Inject constructor( viewModelScope.launch { _satData.value = Result.InProgress try { - preferencesSource.saveTleSources(sources) + preferences.saveTleSources(sources) satelliteRepo.updateEntriesFromWeb(sources) } catch (exception: Exception) { _satData.value = Result.Error(exception) diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/mapScreen/MapViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/mapScreen/MapViewModel.kt index 60f32a11..d69f80f0 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/mapScreen/MapViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/mapScreen/MapViewModel.kt @@ -19,7 +19,7 @@ package com.rtbishop.look4sat.presentation.mapScreen import androidx.lifecycle.* import com.rtbishop.look4sat.domain.Predictor -import com.rtbishop.look4sat.data.PreferencesSource +import com.rtbishop.look4sat.data.Preferences import com.rtbishop.look4sat.data.SatelliteRepo import com.rtbishop.look4sat.domain.* import dagger.hilt.android.lifecycle.HiltViewModel @@ -33,7 +33,7 @@ import kotlin.math.min class MapViewModel @Inject constructor( private val satelliteRepo: SatelliteRepo, private val predictor: Predictor, - private val preferences: PreferencesSource, + private val preferences: Preferences, ) : ViewModel() { private val stationPos = preferences.loadStationPosition() diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/passesScreen/PassesViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/passesScreen/PassesViewModel.kt index 95d653c7..83dfa2cf 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/passesScreen/PassesViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/passesScreen/PassesViewModel.kt @@ -21,7 +21,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.rtbishop.look4sat.data.PreferencesSource +import com.rtbishop.look4sat.data.Preferences import com.rtbishop.look4sat.data.SatelliteRepo import com.rtbishop.look4sat.domain.Predictor import com.rtbishop.look4sat.domain.SatPass @@ -36,7 +36,7 @@ import javax.inject.Inject class PassesViewModel @Inject constructor( private val satelliteRepo: SatelliteRepo, private val predictor: Predictor, - private val preferencesSource: PreferencesSource + private val preferences: Preferences ) : ViewModel() { private val _passes = MutableLiveData>>(Result.InProgress) @@ -46,14 +46,14 @@ class PassesViewModel @Inject constructor( val isFirstLaunchDone: LiveData = _isFirstLaunchDone init { - if (preferencesSource.isSetupDone()) { + if (preferences.isSetupDone()) { viewModelScope.launch { _passes.postValue(Result.InProgress) val dateNow = Date() val satellites = satelliteRepo.getSelectedSatellites() - val stationPos = preferencesSource.loadStationPosition() - val hoursAhead = preferencesSource.getHoursAhead() - val minElev = preferencesSource.getMinElevation() + val stationPos = preferences.loadStationPosition() + val hoursAhead = preferences.getHoursAhead() + val minElev = preferences.getMinElevation() predictor.triggerCalculation(satellites, stationPos, dateNow, hoursAhead, minElev) } } else { @@ -68,19 +68,19 @@ class PassesViewModel @Inject constructor( } fun triggerInitialSetup() { - preferencesSource.updatePositionFromGPS() + preferences.updatePositionFromGPS() viewModelScope.launch { _passes.postValue(Result.InProgress) val defaultCatNums = listOf(43700, 25544, 25338, 28654, 33591, 40069, 27607, 24278) val dateNow = Date() val satellites = satelliteRepo.getSelectedSatellites() - val stationPos = preferencesSource.loadStationPosition() - val hoursAhead = preferencesSource.getHoursAhead() - val minElev = preferencesSource.getMinElevation() - satelliteRepo.updateEntriesFromWeb(preferencesSource.loadDefaultSources()) + val stationPos = preferences.loadStationPosition() + val hoursAhead = preferences.getHoursAhead() + val minElev = preferences.getMinElevation() + satelliteRepo.updateEntriesFromWeb(preferences.loadDefaultSources()) satelliteRepo.updateEntriesSelection(defaultCatNums, true) predictor.forceCalculation(satellites, stationPos, dateNow, hoursAhead, minElev) - preferencesSource.setSetupDone() + preferences.setSetupDone() _isFirstLaunchDone.value = true } } @@ -91,15 +91,15 @@ class PassesViewModel @Inject constructor( passesProcessing?.cancelAndJoin() val dateNow = Date() val satellites = satelliteRepo.getSelectedSatellites() - val stationPos = preferencesSource.loadStationPosition() - val hoursAhead = preferencesSource.getHoursAhead() - val minElev = preferencesSource.getMinElevation() + val stationPos = preferences.loadStationPosition() + val hoursAhead = preferences.getHoursAhead() + val minElev = preferences.getMinElevation() predictor.forceCalculation(satellites, stationPos, dateNow, hoursAhead, minElev) } } fun shouldUseUTC(): Boolean { - return preferencesSource.shouldUseUTC() + return preferences.shouldUseUTC() } private suspend fun tickPasses(passes: List) = withContext(Dispatchers.Default) { diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarFragment.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarFragment.kt index 1a7df5e5..a5e7b4ab 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarFragment.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarFragment.kt @@ -25,7 +25,7 @@ import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.SimpleItemAnimator import com.rtbishop.look4sat.R -import com.rtbishop.look4sat.data.PreferencesSource +import com.rtbishop.look4sat.data.Preferences import com.rtbishop.look4sat.databinding.FragmentRadarBinding import com.rtbishop.look4sat.domain.SatPass import com.rtbishop.look4sat.domain.SatPos @@ -39,7 +39,7 @@ import javax.inject.Inject class RadarFragment : Fragment(R.layout.fragment_radar) { @Inject - lateinit var preferences: PreferencesSource + lateinit var preferences: Preferences private val viewModel: RadarViewModel by viewModels() private var radarView: RadarView? = null diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarViewModel.kt index caaed8e9..39a13045 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarViewModel.kt @@ -19,13 +19,13 @@ package com.rtbishop.look4sat.presentation.radarScreen import androidx.lifecycle.* import com.rtbishop.look4sat.domain.PassReporter -import com.rtbishop.look4sat.data.PreferencesSource +import com.rtbishop.look4sat.data.Preferences import com.rtbishop.look4sat.data.SatelliteRepo import com.rtbishop.look4sat.domain.Predictor import com.rtbishop.look4sat.domain.SatPass import com.rtbishop.look4sat.domain.SatPos import com.rtbishop.look4sat.domain.Transmitter -import com.rtbishop.look4sat.framework.OrientationProvider +import com.rtbishop.look4sat.framework.OrientationSource import com.rtbishop.look4sat.utility.round import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.delay @@ -37,12 +37,12 @@ import javax.inject.Inject @HiltViewModel class RadarViewModel @Inject constructor( - private val orientationProvider: OrientationProvider, - private val preferences: PreferencesSource, + private val orientationSource: OrientationSource, + private val preferences: Preferences, private val predictor: Predictor, private val satelliteRepo: SatelliteRepo, private val passReporter: PassReporter -) : ViewModel(), OrientationProvider.OrientationListener { +) : ViewModel(), OrientationSource.OrientationListener { private val stationPos = preferences.loadStationPosition() private val _passData = MutableLiveData() @@ -64,11 +64,11 @@ class RadarViewModel @Inject constructor( } fun enableSensor() { - if (preferences.shouldUseCompass()) orientationProvider.startListening(this) + if (preferences.shouldUseCompass()) orientationSource.startListening(this) } fun disableSensor() { - if (preferences.shouldUseCompass()) orientationProvider.stopListening() + if (preferences.shouldUseCompass()) orientationSource.stopListening() } override fun onOrientationChanged(azimuth: Float, pitch: Float, roll: Float) { diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SettingsFragment.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SettingsFragment.kt index 5d041752..8cd878a9 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SettingsFragment.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SettingsFragment.kt @@ -29,8 +29,8 @@ import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import com.google.android.material.snackbar.Snackbar import com.rtbishop.look4sat.R -import com.rtbishop.look4sat.data.PreferencesSource -import com.rtbishop.look4sat.framework.PreferencesProvider +import com.rtbishop.look4sat.data.Preferences +import com.rtbishop.look4sat.framework.PreferencesSource import dagger.hilt.android.AndroidEntryPoint import javax.inject.Inject @@ -38,7 +38,7 @@ import javax.inject.Inject class SettingsFragment : PreferenceFragmentCompat() { @Inject - lateinit var preferencesSource: PreferencesSource + lateinit var preferences: Preferences private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> @@ -56,20 +56,20 @@ class SettingsFragment : PreferenceFragmentCompat() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - findPreference(PreferencesProvider.keyPositionGPS)?.apply { + findPreference(PreferencesSource.keyPositionGPS)?.apply { setOnPreferenceClickListener { updatePositionFromGPS() return@setOnPreferenceClickListener true } } - findPreference(PreferencesProvider.keyPositionQTH)?.apply { + findPreference(PreferencesSource.keyPositionQTH)?.apply { setOnPreferenceChangeListener { _, newValue -> updatePositionFromQth(newValue.toString()) } } - findPreference(PreferencesProvider.keyRotatorAddress)?.apply { + findPreference(PreferencesSource.keyRotatorAddress)?.apply { setOnPreferenceChangeListener { _, newValue -> val ip4 = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.(?!\$)|\$)){4}\$" if (newValue.toString().matches(ip4.toRegex())) { @@ -81,7 +81,7 @@ class SettingsFragment : PreferenceFragmentCompat() { } } - findPreference(PreferencesProvider.keyRotatorPort)?.apply { + findPreference(PreferencesSource.keyRotatorPort)?.apply { setOnBindEditTextListener { it.inputType = InputType.TYPE_CLASS_NUMBER } setOnPreferenceChangeListener { _, newValue -> val portValue = newValue.toString() @@ -96,7 +96,7 @@ class SettingsFragment : PreferenceFragmentCompat() { } private fun updatePositionFromQth(qthString: String): Boolean { - return if (preferencesSource.updatePositionFromQTH(qthString)) { + return if (preferences.updatePositionFromQTH(qthString)) { showSnack(getString(R.string.pref_pos_success)) true } else { @@ -109,7 +109,7 @@ class SettingsFragment : PreferenceFragmentCompat() { val locPermString = Manifest.permission.ACCESS_FINE_LOCATION val locPermResult = ContextCompat.checkSelfPermission(requireContext(), locPermString) if (locPermResult == PackageManager.PERMISSION_GRANTED) { - if (preferencesSource.updatePositionFromGPS()) { + if (preferences.updatePositionFromGPS()) { showSnack(getString(R.string.pref_pos_success)) } else { showSnack(getString(R.string.pref_pos_gps_null)) diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/sourcesScreen/SourcesDialog.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/sourcesScreen/SourcesDialog.kt index 505a27f8..7ccfd3b0 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/sourcesScreen/SourcesDialog.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/sourcesScreen/SourcesDialog.kt @@ -25,7 +25,7 @@ import android.view.WindowManager import androidx.appcompat.app.AppCompatDialogFragment import androidx.recyclerview.widget.LinearLayoutManager import com.rtbishop.look4sat.R -import com.rtbishop.look4sat.data.PreferencesSource +import com.rtbishop.look4sat.data.Preferences import com.rtbishop.look4sat.databinding.DialogSourcesBinding import com.rtbishop.look4sat.utility.setNavResult import dagger.hilt.android.AndroidEntryPoint @@ -35,7 +35,7 @@ import javax.inject.Inject class SourcesDialog : AppCompatDialogFragment() { @Inject - lateinit var prefsManager: PreferencesSource + lateinit var prefsManager: Preferences override fun onCreateView(inflater: LayoutInflater, group: ViewGroup?, state: Bundle?): View? { return inflater.inflate(R.layout.dialog_sources, group, false) diff --git a/core/src/main/java/com/rtbishop/look4sat/data/LocalDataSource.kt b/core/src/main/java/com/rtbishop/look4sat/data/LocalSource.kt similarity index 84% rename from core/src/main/java/com/rtbishop/look4sat/data/LocalDataSource.kt rename to core/src/main/java/com/rtbishop/look4sat/data/LocalSource.kt index 9ac884fc..604b5630 100644 --- a/core/src/main/java/com/rtbishop/look4sat/data/LocalDataSource.kt +++ b/core/src/main/java/com/rtbishop/look4sat/data/LocalSource.kt @@ -21,11 +21,11 @@ import com.rtbishop.look4sat.domain.Transmitter import com.rtbishop.look4sat.domain.Satellite import kotlinx.coroutines.flow.Flow -interface LocalDataSource { +interface LocalSource { - fun getSatItems(): Flow> + fun getEntriesWithModes(): Flow> - fun getSatTransmitters(catNum: Int): Flow> + fun getTransmitters(catNum: Int): Flow> suspend fun getSelectedSatellites(): List @@ -33,5 +33,5 @@ interface LocalDataSource { suspend fun updateEntriesSelection(catNums: List, isSelected: Boolean) - suspend fun updateTransmitters(satelliteTrans: List) + suspend fun updateTransmitters(transmitters: List) } diff --git a/core/src/main/java/com/rtbishop/look4sat/data/PreferencesSource.kt b/core/src/main/java/com/rtbishop/look4sat/data/Preferences.kt similarity index 98% rename from core/src/main/java/com/rtbishop/look4sat/data/PreferencesSource.kt rename to core/src/main/java/com/rtbishop/look4sat/data/Preferences.kt index bbf17a2b..08a20883 100644 --- a/core/src/main/java/com/rtbishop/look4sat/data/PreferencesSource.kt +++ b/core/src/main/java/com/rtbishop/look4sat/data/Preferences.kt @@ -19,7 +19,7 @@ package com.rtbishop.look4sat.data import com.rtbishop.look4sat.domain.GeoPos -interface PreferencesSource { +interface Preferences { fun positionToQTH(lat: Double, lon: Double): String? diff --git a/core/src/main/java/com/rtbishop/look4sat/data/RemoteDataSource.kt b/core/src/main/java/com/rtbishop/look4sat/data/RemoteSource.kt similarity index 91% rename from core/src/main/java/com/rtbishop/look4sat/data/RemoteDataSource.kt rename to core/src/main/java/com/rtbishop/look4sat/data/RemoteSource.kt index eb05a3da..9a1e1c98 100644 --- a/core/src/main/java/com/rtbishop/look4sat/data/RemoteDataSource.kt +++ b/core/src/main/java/com/rtbishop/look4sat/data/RemoteSource.kt @@ -20,9 +20,9 @@ package com.rtbishop.look4sat.data import com.rtbishop.look4sat.domain.Transmitter import java.io.InputStream -interface RemoteDataSource { +interface RemoteSource { - suspend fun fetchDataStream(url: String): InputStream? + suspend fun fetchFileStream(url: String): InputStream? suspend fun fetchTransmitters(): List } diff --git a/core/src/main/java/com/rtbishop/look4sat/data/SatelliteRepo.kt b/core/src/main/java/com/rtbishop/look4sat/data/SatelliteRepo.kt index 2ffbeb63..07d1bc63 100644 --- a/core/src/main/java/com/rtbishop/look4sat/data/SatelliteRepo.kt +++ b/core/src/main/java/com/rtbishop/look4sat/data/SatelliteRepo.kt @@ -29,43 +29,43 @@ import java.io.InputStream import java.util.zip.ZipInputStream class SatelliteRepo( - private val preferencesSource: PreferencesSource, - private val localDataSource: LocalDataSource, - private val remoteDataSource: RemoteDataSource, - private val ioDispatcher: CoroutineDispatcher + private val preferences: Preferences, + private val localSource: LocalSource, + private val remoteSource: RemoteSource, + private val repoDispatcher: CoroutineDispatcher ) { fun saveSelectedModes(modes: List) { - preferencesSource.saveModesSelection(modes) + preferences.saveModesSelection(modes) } fun loadSelectedModes(): List { - return preferencesSource.loadModesSelection() + return preferences.loadModesSelection() } fun getSatItems(): Flow> { - return localDataSource.getSatItems() + return localSource.getEntriesWithModes() } fun getSatTransmitters(catNum: Int): Flow> { - return localDataSource.getSatTransmitters(catNum) + return localSource.getTransmitters(catNum) } suspend fun getSelectedSatellites(): List { - return localDataSource.getSelectedSatellites() + return localSource.getSelectedSatellites() } - suspend fun updateEntriesFromFile(stream: InputStream) = withContext(ioDispatcher) { - localDataSource.updateEntries(importSatEntries(stream)) + suspend fun updateEntriesFromFile(stream: InputStream) = withContext(repoDispatcher) { + localSource.updateEntries(importSatEntries(stream)) } suspend fun updateEntriesFromWeb(sources: List) { coroutineScope { - launch(ioDispatcher) { + launch(repoDispatcher) { val streams = mutableListOf() val entries = mutableListOf() sources.forEach { source -> - remoteDataSource.fetchDataStream(source)?.let { stream -> + remoteSource.fetchFileStream(source)?.let { stream -> if (source.contains(".zip", true)) { val zipStream = ZipInputStream(stream).apply { nextEntry } streams.add(zipStream) @@ -75,20 +75,20 @@ class SatelliteRepo( } } streams.forEach { stream -> entries.addAll(importSatEntries(stream)) } - localDataSource.updateEntries(entries) + localSource.updateEntries(entries) } - launch(ioDispatcher) { - val transmitters = remoteDataSource.fetchTransmitters().filter { it.isAlive } - localDataSource.updateTransmitters(transmitters) + launch(repoDispatcher) { + val transmitters = remoteSource.fetchTransmitters().filter { it.isAlive } + localSource.updateTransmitters(transmitters) } } } suspend fun updateEntriesSelection(catNums: List, isSelected: Boolean) { - localDataSource.updateEntriesSelection(catNums, isSelected) + localSource.updateEntriesSelection(catNums, isSelected) } private fun importSatEntries(stream: InputStream): List { - return TLE.parseStream(stream).map { tle -> SatEntry(tle) } + return TLE.parseTleStream(stream).map { tle -> SatEntry(tle) } } } diff --git a/core/src/main/java/com/rtbishop/look4sat/domain/SatPos.kt b/core/src/main/java/com/rtbishop/look4sat/domain/SatPos.kt index ed89a4dc..4b80f874 100644 --- a/core/src/main/java/com/rtbishop/look4sat/domain/SatPos.kt +++ b/core/src/main/java/com/rtbishop/look4sat/domain/SatPos.kt @@ -50,35 +50,32 @@ data class SatPos( fun getRangeCircle(): List { val positions = mutableListOf() - val lat = this.latitude - val lon = this.longitude - // rangeCircleRadiusKm - // earthRadiusKm * acos(earthRadiusKm / (earthRadiusKm + satPos.altitude)) val beta = acos(earthRadiusKm / (earthRadiusKm + this.altitude)) +// val radiusKm = earthRadiusKm * acos(earthRadiusKm / (earthRadiusKm + altitude)) var tempAzimuth = 0 while (tempAzimuth < 360) { val azimuth = tempAzimuth / 360.0 * 2.0 * Math.PI - var rangelat = asin(sin(lat) * cos(beta) + cos(azimuth) * sin(beta) * cos(lat)) - val num = (cos(beta) - (sin(lat) * sin(rangelat))) - val den = cos(lat) * cos(rangelat) - var rangelon = if (tempAzimuth == 0 && (beta > ((Math.PI / 2.0) - lat))) { - lon + Math.PI - } else if (tempAzimuth == 180 && (beta > ((Math.PI / 2.0) - lat))) { - lon + Math.PI + var lat = asin(sin(latitude) * cos(beta) + cos(azimuth) * sin(beta) * cos(latitude)) + val num = (cos(beta) - (sin(latitude) * sin(lat))) + val den = cos(latitude) * cos(lat) + var lon = if (tempAzimuth == 0 && (beta > ((Math.PI / 2.0) - latitude))) { + longitude + Math.PI + } else if (tempAzimuth == 180 && (beta > ((Math.PI / 2.0) - latitude))) { + longitude + Math.PI } else if (abs(num / den) > 1.0) { - lon + longitude } else { if ((180 - tempAzimuth) >= 0) { - lon - acos(num / den) + longitude - acos(num / den) } else { - lon + acos(num / den) + longitude + acos(num / den) } } - while (rangelon < 0.0) rangelon += Math.PI * 2.0 - while (rangelon > Math.PI * 2.0) rangelon -= Math.PI * 2.0 - rangelat = Math.toDegrees(rangelat) - rangelon = Math.toDegrees(rangelon) - positions.add(GeoPos(rangelat, rangelon)) + while (lon < 0.0) lon += Math.PI * 2.0 + while (lon > Math.PI * 2.0) lon -= Math.PI * 2.0 + lat = Math.toDegrees(lat) + lon = Math.toDegrees(lon) + positions.add(GeoPos(lat, lon)) tempAzimuth += 1 } return positions diff --git a/core/src/main/java/com/rtbishop/look4sat/domain/TLE.kt b/core/src/main/java/com/rtbishop/look4sat/domain/TLE.kt index 317d20e0..df3ef2dc 100644 --- a/core/src/main/java/com/rtbishop/look4sat/domain/TLE.kt +++ b/core/src/main/java/com/rtbishop/look4sat/domain/TLE.kt @@ -48,39 +48,41 @@ data class TLE( companion object { - fun parseStream(stream: InputStream): List { - val itemParams = mutableListOf(String(), String(), String()) - val importedItems = mutableListOf() + fun parseTleStream(stream: InputStream): List { + val tleStrings = mutableListOf(String(), String(), String()) + val parsedItems = mutableListOf() var lineIndex = 0 stream.bufferedReader().forEachLine { line -> - itemParams[lineIndex] = line + tleStrings[lineIndex] = line if (lineIndex < 2) { lineIndex++ } else { - val isLineOneValid = itemParams[1].substring(0, 1) == "1" - val isLineTwoValid = itemParams[2].substring(0, 1) == "2" + val isLineOneValid = tleStrings[1].substring(0, 1) == "1" + val isLineTwoValid = tleStrings[2].substring(0, 1) == "2" if (!isLineOneValid && !isLineTwoValid) return@forEachLine - parseParams(itemParams)?.let { tle -> importedItems.add(tle) } + parseTleStrings(tleStrings)?.let { tle -> parsedItems.add(tle) } lineIndex = 0 } } - return importedItems + return parsedItems } - private fun parseParams(params: List): TLE? { - if (params[1].substring(0, 1) != "1" && params[2].substring(0, 1) != "2") return null + private fun parseTleStrings(tleStrings: List): TLE? { + if (tleStrings[1].substring(0, 1) != "1" && tleStrings[2].substring(0, 1) != "2") { + return null + } try { - val name: String = params[0].trim() - val epoch: Double = params[1].substring(18, 32).toDouble() - val meanmo: Double = params[2].substring(52, 63).toDouble() - val eccn: Double = 1.0e-07 * params[2].substring(26, 33).toDouble() - val incl: Double = params[2].substring(8, 16).toDouble() - val raan: Double = params[2].substring(17, 25).toDouble() - val argper: Double = params[2].substring(34, 42).toDouble() - val meanan: Double = params[2].substring(43, 51).toDouble() - val catnum: Int = params[1].substring(2, 7).trim().toInt() - val bstar: Double = 1.0e-5 * params[1].substring(53, 59).toDouble() / - 10.0.pow(params[1].substring(60, 61).toDouble()) + val name: String = tleStrings[0].trim() + val epoch: Double = tleStrings[1].substring(18, 32).toDouble() + val meanmo: Double = tleStrings[2].substring(52, 63).toDouble() + val eccn: Double = 1.0e-07 * tleStrings[2].substring(26, 33).toDouble() + val incl: Double = tleStrings[2].substring(8, 16).toDouble() + val raan: Double = tleStrings[2].substring(17, 25).toDouble() + val argper: Double = tleStrings[2].substring(34, 42).toDouble() + val meanan: Double = tleStrings[2].substring(43, 51).toDouble() + val catnum: Int = tleStrings[1].substring(2, 7).trim().toInt() + val bstar: Double = 1.0e-5 * tleStrings[1].substring(53, 59).toDouble() / + 10.0.pow(tleStrings[1].substring(60, 61).toDouble()) return TLE(name, epoch, meanmo, eccn, incl, raan, argper, meanan, catnum, bstar) } catch (exception: Exception) { return null diff --git a/core/src/test/java/com/rtbishop/look4sat/Look4SatTest.kt b/core/src/test/java/com/rtbishop/look4sat/TleParserTest.kt similarity index 79% rename from core/src/test/java/com/rtbishop/look4sat/Look4SatTest.kt rename to core/src/test/java/com/rtbishop/look4sat/TleParserTest.kt index ad22d71d..2b92a872 100644 --- a/core/src/test/java/com/rtbishop/look4sat/Look4SatTest.kt +++ b/core/src/test/java/com/rtbishop/look4sat/TleParserTest.kt @@ -3,9 +3,9 @@ package com.rtbishop.look4sat import com.rtbishop.look4sat.domain.TLE import org.junit.Test -class Look4SatTest { +class TleParserTest { - private val validParamsStream = """ + private val validTleStream = """ ISS (ZARYA) 1 25544U 98067A 21255.21005818 -.00120443 00000-0 -22592-2 0 9998 2 25544 51.6451 272.4173 0002526 27.1693 64.4213 15.48396490302085 @@ -16,7 +16,7 @@ class Look4SatTest { 1 48869U 21057A 21254.57379002 .00002620 00000-0 56399-4 0 9993 2 48869 51.6436 275.5624 0003404 23.4371 118.9508 15.48629868301984 """.trimIndent().byteInputStream() - private val invalidParamsStream = """ + private val invalidTleStream = """ 1 25544U 98067A 21255.21005818 -.00120443 00000-0 -22592-2 0 9998 2 25544 51.6451 272.4173 0002526 27.1693 64.4213 15.48396490302085 1 48852U 21053A 21254.65783594 .00009779 00000-0 11349-3 0 9992 @@ -26,14 +26,14 @@ class Look4SatTest { """.trimIndent().byteInputStream() @Test - fun `Given valid params stream returns valid data`() { - val parsedList = TLE.parseStream(validParamsStream) + fun `Given valid TLE stream returns valid data`() { + val parsedList = TLE.parseTleStream(validTleStream) assert(parsedList[2].catnum == 48869) } @Test - fun `Given invalid params stream returns empty list`() { - val parsedList = TLE.parseStream(invalidParamsStream) + fun `Given invalid TLE stream returns empty list`() { + val parsedList = TLE.parseTleStream(invalidTleStream) assert(parsedList.isEmpty()) } }