Satellite tracker and pass predictor for Android, inspired by Gpredict
Kotlin amsatcelestrakgpsham-radiohamlibhamradiokotlinnasaradiosatellitesatellite-datasatellite-positionsatellite-predictionsatellitessatnogssdrspacespacexstarlink
Removed redundant interfaces, simplified the code
Arty Bishop0438b8a51fcb
diff --git a/app/src/main/java/com/rtbishop/look4sat/SharedViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/SharedViewModel.ktindex 291ff295..b32f6d07 100644--- a/app/src/main/java/com/rtbishop/look4sat/SharedViewModel.kt+++ b/app/src/main/java/com/rtbishop/look4sat/SharedViewModel.kt@@ -20,29 +20,31 @@package com.rtbishop.look4satimport android.net.Uri-import androidx.hilt.lifecycle.ViewModelInjectimport androidx.lifecycle.LiveDataimport androidx.lifecycle.MutableLiveDataimport androidx.lifecycle.ViewModelimport androidx.lifecycle.viewModelScopeimport com.rtbishop.look4sat.data.*import com.rtbishop.look4sat.repo.EntriesRepo-import com.rtbishop.look4sat.repo.SourcesRepo+import com.rtbishop.look4sat.repo.DefaultSourcesRepoimport com.rtbishop.look4sat.repo.TransmittersRepoimport com.rtbishop.look4sat.utility.PassPredictorimport com.rtbishop.look4sat.utility.PrefsManager+import dagger.hilt.android.lifecycle.HiltViewModelimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.delayimport kotlinx.coroutines.launchimport java.util.*+import javax.inject.Inject-class SharedViewModel @ViewModelInject constructor(+@HiltViewModel+class SharedViewModel @Inject constructor(private val prefsManager: PrefsManager,- private val sourcesRepo: SourcesRepo,+ private val sourcesRepo: DefaultSourcesRepo,private val entriesRepo: EntriesRepo,private val transmittersRepo: TransmittersRepo) : ViewModel() {-+private val _appTimer = MutableLiveData(System.currentTimeMillis())private val _passes = MutableLiveData<Result<MutableList<SatPass>>>()private val _appEvent = MutableLiveData<Event<Int>>()diff --git a/app/src/main/java/com/rtbishop/look4sat/di/UtilityModule.kt b/app/src/main/java/com/rtbishop/look4sat/di/ApplicationModule.ktsimilarity index 77%rename from app/src/main/java/com/rtbishop/look4sat/di/UtilityModule.ktrename to app/src/main/java/com/rtbishop/look4sat/di/ApplicationModule.ktindex 3a516faa..13558e76 100644--- a/app/src/main/java/com/rtbishop/look4sat/di/UtilityModule.kt+++ b/app/src/main/java/com/rtbishop/look4sat/di/ApplicationModule.kt@@ -26,20 +26,23 @@ import androidx.preference.PreferenceManagerimport dagger.Moduleimport dagger.Providesimport dagger.hilt.InstallIn-import dagger.hilt.android.components.ActivityComponent-import dagger.hilt.android.qualifiers.ActivityContext+import dagger.hilt.android.qualifiers.ApplicationContext+import dagger.hilt.components.SingletonComponent+import javax.inject.Singleton@Module-@InstallIn(ActivityComponent::class)-object UtilityModule {-+@InstallIn(SingletonComponent::class)+object ApplicationModule {+@Provides- fun getSharedPreferences(@ActivityContext context: Context): SharedPreferences {- return PreferenceManager.getDefaultSharedPreferences(context)+ @Singleton+ fun getLocationManager(@ApplicationContext context: Context): LocationManager {+ return context.getSystemService(Context.LOCATION_SERVICE) as LocationManager}-+@Provides- fun getLocationManager(@ActivityContext context: Context): LocationManager {- return context.getSystemService(Context.LOCATION_SERVICE) as LocationManager+ @Singleton+ fun getSharedPreferences(@ApplicationContext context: Context): SharedPreferences {+ return PreferenceManager.getDefaultSharedPreferences(context)}}diff --git a/app/src/main/java/com/rtbishop/look4sat/di/NetworkModule.kt b/app/src/main/java/com/rtbishop/look4sat/di/NetworkModule.ktindex b046f5e6..ff74ac4b 100644--- a/app/src/main/java/com/rtbishop/look4sat/di/NetworkModule.kt+++ b/app/src/main/java/com/rtbishop/look4sat/di/NetworkModule.kt@@ -23,21 +23,24 @@ import com.rtbishop.look4sat.repo.remote.TransmittersApiimport dagger.Moduleimport dagger.Providesimport dagger.hilt.InstallIn-import dagger.hilt.android.components.ActivityComponent+import dagger.hilt.components.SingletonComponentimport okhttp3.OkHttpClientimport retrofit2.Retrofitimport retrofit2.converter.moshi.MoshiConverterFactory+import javax.inject.Singleton@Module-@InstallIn(ActivityComponent::class)+@InstallIn(SingletonComponent::class)object NetworkModule {-+@Provides+ @Singletonfun getOkHttpClient(): OkHttpClient {return OkHttpClient()}-+@Provides+ @Singletonfun getTransmittersApi(): TransmittersApi {return Retrofit.Builder().baseUrl("https://db.satnogs.org/api/")diff --git a/app/src/main/java/com/rtbishop/look4sat/di/RepoModule.kt b/app/src/main/java/com/rtbishop/look4sat/di/RepoModule.ktdeleted file mode 100644index 30aca26b..00000000--- a/app/src/main/java/com/rtbishop/look4sat/di/RepoModule.kt+++ /dev/null@@ -1,61 +0,0 @@-/*******************************************************************************- Look4Sat. Amateur radio satellite tracker and pass predictor.- Copyright (C) 2019, 2020 Arty Bishop ([email protected])-- 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 2 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, write to the Free Software Foundation, Inc.,- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.- ******************************************************************************/--package com.rtbishop.look4sat.di--import android.content.ContentResolver-import android.content.Context-import com.rtbishop.look4sat.repo.*-import com.rtbishop.look4sat.repo.local.EntriesDao-import com.rtbishop.look4sat.repo.local.SourcesDao-import com.rtbishop.look4sat.repo.local.TransmittersDao-import com.rtbishop.look4sat.repo.remote.TransmittersApi-import dagger.Module-import dagger.Provides-import dagger.hilt.InstallIn-import dagger.hilt.android.components.ActivityComponent-import dagger.hilt.android.qualifiers.ActivityContext-import okhttp3.OkHttpClient--@Module-@InstallIn(ActivityComponent::class)-object RepoModule {-- @Provides- fun getEntriesRepo(resolver: ContentResolver, client: OkHttpClient, entriesDao: EntriesDao)- : EntriesRepo {- return DefaultEntriesRepo(resolver, client, entriesDao)- }-- @Provides- fun getSourcesRepo(sourcesDao: SourcesDao): SourcesRepo {- return DefaultSourcesRepo(sourcesDao)- }-- @Provides- fun getTransmittersRepo(transDao: TransmittersDao, transApi: TransmittersApi)- : TransmittersRepo {- return DefaultTransmittersRepo(transDao, transApi)- }-- @Provides- fun getContentResolver(@ActivityContext context: Context): ContentResolver {- return context.applicationContext.contentResolver- }-}diff --git a/app/src/main/java/com/rtbishop/look4sat/di/PersistenceModule.kt b/app/src/main/java/com/rtbishop/look4sat/di/RepositoryModule.ktsimilarity index 76%rename from app/src/main/java/com/rtbishop/look4sat/di/PersistenceModule.ktrename to app/src/main/java/com/rtbishop/look4sat/di/RepositoryModule.ktindex 2ff8bf07..261af698 100644--- a/app/src/main/java/com/rtbishop/look4sat/di/PersistenceModule.kt+++ b/app/src/main/java/com/rtbishop/look4sat/di/RepositoryModule.kt@@ -19,6 +19,7 @@package com.rtbishop.look4sat.di+import android.content.ContentResolverimport android.content.Contextimport androidx.room.Roomimport com.rtbishop.look4sat.repo.local.EntriesDao@@ -28,30 +29,41 @@ import com.rtbishop.look4sat.repo.local.TransmittersDaoimport dagger.Moduleimport dagger.Providesimport dagger.hilt.InstallIn-import dagger.hilt.android.components.ActivityComponent-import dagger.hilt.android.qualifiers.ActivityContext+import dagger.hilt.android.qualifiers.ApplicationContext+import dagger.hilt.components.SingletonComponent+import javax.inject.Singleton@Module-@InstallIn(ActivityComponent::class)-object PersistenceModule {-+@InstallIn(SingletonComponent::class)+object RepositoryModule {++ @Provides+ @Singleton+ fun getContentResolver(@ApplicationContext context: Context): ContentResolver {+ return context.contentResolver+ }+@Provides+ @Singletonfun getEntriesDao(db: SatelliteDb): EntriesDao {return db.entriesDao()}-+@Provides+ @Singletonfun getSourcesDao(db: SatelliteDb): SourcesDao {return db.sourcesDao()}-+@Provides+ @Singletonfun getTransmittersDao(db: SatelliteDb): TransmittersDao {return db.transmittersDao()}-+@Provides- fun getSatelliteDb(@ActivityContext context: Context): SatelliteDb {+ @Singleton+ fun getSatelliteDb(@ApplicationContext context: Context): SatelliteDb {return Room.databaseBuilder(context, SatelliteDb::class.java, "satDb").build()}}diff --git a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultEntriesRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/DefaultEntriesRepo.ktdeleted file mode 100644index bea898be..00000000--- a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultEntriesRepo.kt+++ /dev/null@@ -1,79 +0,0 @@-/*******************************************************************************- Look4Sat. Amateur radio satellite tracker and pass predictor.- Copyright (C) 2019, 2020 Arty Bishop ([email protected])-- 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 2 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, write to the Free Software Foundation, Inc.,- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.- ******************************************************************************/--package com.rtbishop.look4sat.repo--import android.content.ContentResolver-import android.net.Uri-import androidx.lifecycle.LiveData-import com.github.amsacode.predict4java.TLE-import com.rtbishop.look4sat.data.SatEntry-import com.rtbishop.look4sat.data.TleSource-import com.rtbishop.look4sat.repo.local.EntriesDao-import kotlinx.coroutines.Dispatchers-import kotlinx.coroutines.withContext-import okhttp3.OkHttpClient-import okhttp3.Request-import java.io.InputStream-import javax.inject.Inject--class DefaultEntriesRepo @Inject constructor(- private val resolver: ContentResolver,- private val client: OkHttpClient,- private val entriesDao: EntriesDao-) : EntriesRepo {-- override fun getEntries(): LiveData<List<SatEntry>> {- return entriesDao.getEntries()- }-- override suspend fun updateEntriesFromFile(fileUri: Uri) {- withContext(Dispatchers.IO) {- resolver.openInputStream(fileUri)?.use { stream ->- val importedEntries = TLE.importSat(stream).map { SatEntry(it) }- val selection = entriesDao.getEntriesSelection()- entriesDao.updateEntries(importedEntries)- entriesDao.updateEntriesSelection(selection)- }- }- }-- override suspend fun updateEntriesFromSources(sources: List<TleSource>) {- withContext(Dispatchers.IO) {- val streams = mutableListOf<InputStream>()- sources.forEach { source ->- val request = Request.Builder().url(source.url).build()- val stream = client.newCall(request).execute().body()?.byteStream()- stream?.let { inputStream -> streams.add(inputStream) }- }- val importedEntries = mutableListOf<SatEntry>()- streams.forEach { stream ->- val entries = TLE.importSat(stream).map { tle -> SatEntry(tle) }- importedEntries.addAll(entries)- }- val selection = entriesDao.getEntriesSelection()- entriesDao.updateEntries(importedEntries)- entriesDao.updateEntriesSelection(selection)- }- }-- override suspend fun updateEntriesSelection(satIds: List<Int>) {- entriesDao.updateEntriesSelection(satIds)- }-}\ No newline at end of filediff --git a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultSourcesRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/DefaultSourcesRepo.ktindex ab688d90..104248e1 100644--- a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultSourcesRepo.kt+++ b/app/src/main/java/com/rtbishop/look4sat/repo/DefaultSourcesRepo.kt@@ -24,14 +24,13 @@ import com.rtbishop.look4sat.data.TleSourceimport com.rtbishop.look4sat.repo.local.SourcesDaoimport javax.inject.Inject-class DefaultSourcesRepo @Inject constructor(private val sourcesDao: SourcesDao) :- SourcesRepo {-- override fun getSources(): LiveData<List<TleSource>> {+class DefaultSourcesRepo @Inject constructor(private val sourcesDao: SourcesDao) {++ fun getSources(): LiveData<List<TleSource>> {return sourcesDao.getSources()}-- override suspend fun updateSources(sources: List<TleSource>) {++ suspend fun updateSources(sources: List<TleSource>) {sourcesDao.updateSources(sources)}}\ No newline at end of filediff --git a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultTransmittersRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/DefaultTransmittersRepo.ktdeleted file mode 100644index 38f60612..00000000--- a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultTransmittersRepo.kt+++ /dev/null@@ -1,40 +0,0 @@-/*******************************************************************************- Look4Sat. Amateur radio satellite tracker and pass predictor.- Copyright (C) 2019, 2020 Arty Bishop ([email protected])-- 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 2 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, write to the Free Software Foundation, Inc.,- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.- ******************************************************************************/--package com.rtbishop.look4sat.repo--import androidx.lifecycle.LiveData-import com.rtbishop.look4sat.data.SatTrans-import com.rtbishop.look4sat.repo.local.TransmittersDao-import com.rtbishop.look4sat.repo.remote.TransmittersApi-import javax.inject.Inject--class DefaultTransmittersRepo @Inject constructor(- private val transmittersDao: TransmittersDao,- private val transmittersApi: TransmittersApi-) : TransmittersRepo {-- override fun getTransmittersForSat(satId: Int): LiveData<List<SatTrans>> {- return transmittersDao.getTransmittersForSat(satId)- }-- override suspend fun updateTransmitters() {- transmittersDao.updateTransmitters(transmittersApi.getTransmitters())- }-}\ No newline at end of filediff --git a/app/src/main/java/com/rtbishop/look4sat/repo/EntriesRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/EntriesRepo.ktindex 12ee09e2..c47f2daf 100644--- a/app/src/main/java/com/rtbishop/look4sat/repo/EntriesRepo.kt+++ b/app/src/main/java/com/rtbishop/look4sat/repo/EntriesRepo.kt@@ -19,18 +19,63 @@package com.rtbishop.look4sat.repo+import android.content.ContentResolverimport android.net.Uriimport androidx.lifecycle.LiveData+import com.github.amsacode.predict4java.TLEimport com.rtbishop.look4sat.data.SatEntryimport com.rtbishop.look4sat.data.TleSource+import com.rtbishop.look4sat.repo.local.EntriesDao+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.withContext+import okhttp3.OkHttpClient+import okhttp3.Request+import java.io.InputStream+import javax.inject.Inject-interface EntriesRepo {-- fun getEntries(): LiveData<List<SatEntry>>-- suspend fun updateEntriesFromFile(fileUri: Uri)-- suspend fun updateEntriesFromSources(sources: List<TleSource>)-- suspend fun updateEntriesSelection(satIds: List<Int>)+class EntriesRepo @Inject constructor(+ private val resolver: ContentResolver,+ private val client: OkHttpClient,+ private val entriesDao: EntriesDao+) {++ fun getEntries(): LiveData<List<SatEntry>> {+ return entriesDao.getEntries()+ }++ suspend fun updateEntriesFromFile(fileUri: Uri) {+ withContext(Dispatchers.IO) {+ kotlin.runCatching {+ resolver.openInputStream(fileUri)?.use { stream ->+ val importedEntries = TLE.importSat(stream).map { SatEntry(it) }+ val selection = entriesDao.getEntriesSelection()+ entriesDao.updateEntries(importedEntries)+ entriesDao.updateEntriesSelection(selection)+ }+ }+ }+ }++ suspend fun updateEntriesFromSources(sources: List<TleSource>) {+ withContext(Dispatchers.IO) {+ val streams = mutableListOf<InputStream>()+ sources.forEach { source ->+ val request = Request.Builder().url(source.url).build()+ val stream = client.newCall(request).execute().body()?.byteStream()+ stream?.let { inputStream -> streams.add(inputStream) }+ }+ val importedEntries = mutableListOf<SatEntry>()+ streams.forEach { stream ->+ val entries = TLE.importSat(stream).map { tle -> SatEntry(tle) }+ importedEntries.addAll(entries)+ }+ val selection = entriesDao.getEntriesSelection()+ entriesDao.updateEntries(importedEntries)+ entriesDao.updateEntriesSelection(selection)+ }+ }++ suspend fun updateEntriesSelection(satIds: List<Int>) {+ entriesDao.updateEntriesSelection(satIds)+ }}\ No newline at end of filediff --git a/app/src/main/java/com/rtbishop/look4sat/repo/SourcesRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/SourcesRepo.ktdeleted file mode 100644index bcf218ce..00000000--- a/app/src/main/java/com/rtbishop/look4sat/repo/SourcesRepo.kt+++ /dev/null@@ -1,30 +0,0 @@-/*******************************************************************************- Look4Sat. Amateur radio satellite tracker and pass predictor.- Copyright (C) 2019, 2020 Arty Bishop ([email protected])-- 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 2 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, write to the Free Software Foundation, Inc.,- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.- ******************************************************************************/--package com.rtbishop.look4sat.repo--import androidx.lifecycle.LiveData-import com.rtbishop.look4sat.data.TleSource--interface SourcesRepo {-- fun getSources(): LiveData<List<TleSource>>-- suspend fun updateSources(sources: List<TleSource>)-}\ No newline at end of filediff --git a/app/src/main/java/com/rtbishop/look4sat/repo/TransmittersRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/TransmittersRepo.ktindex dfeff1c2..b811ddc8 100644--- a/app/src/main/java/com/rtbishop/look4sat/repo/TransmittersRepo.kt+++ b/app/src/main/java/com/rtbishop/look4sat/repo/TransmittersRepo.kt@@ -21,10 +21,20 @@ package com.rtbishop.look4sat.repoimport androidx.lifecycle.LiveDataimport com.rtbishop.look4sat.data.SatTrans--interface TransmittersRepo {-- suspend fun updateTransmitters()-- fun getTransmittersForSat(satId: Int): LiveData<List<SatTrans>>+import com.rtbishop.look4sat.repo.local.TransmittersDao+import com.rtbishop.look4sat.repo.remote.TransmittersApi+import javax.inject.Inject++class TransmittersRepo @Inject constructor(+ private val transmittersDao: TransmittersDao,+ private val transmittersApi: TransmittersApi+) {++ fun getTransmittersForSat(satId: Int): LiveData<List<SatTrans>> {+ return transmittersDao.getTransmittersForSat(satId)+ }++ suspend fun updateTransmitters() {+ transmittersDao.updateTransmitters(transmittersApi.getTransmitters())+ }}\ No newline at end of file