From 0438b8a51fcbda5fc46d1cc8e4a6e39ff13fcacf Mon Sep 17 00:00:00 2001 From: Arty Bishop Date: Thu, 25 Feb 2021 21:07:31 +0000 Subject: [PATCH] Removed redundant interfaces, simplified the code --- .../com/rtbishop/look4sat/SharedViewModel.kt | 12 +-- ...{UtilityModule.kt => ApplicationModule.kt} | 25 +++--- .../com/rtbishop/look4sat/di/NetworkModule.kt | 11 ++- .../com/rtbishop/look4sat/di/RepoModule.kt | 61 -------------- ...rsistenceModule.kt => RepositoryModule.kt} | 30 ++++--- .../look4sat/repo/DefaultEntriesRepo.kt | 79 ------------------- .../look4sat/repo/DefaultSourcesRepo.kt | 11 ++- .../look4sat/repo/DefaultTransmittersRepo.kt | 40 ---------- .../com/rtbishop/look4sat/repo/EntriesRepo.kt | 63 ++++++++++++--- .../com/rtbishop/look4sat/repo/SourcesRepo.kt | 30 ------- .../look4sat/repo/TransmittersRepo.kt | 20 +++-- 11 files changed, 123 insertions(+), 259 deletions(-) rename app/src/main/java/com/rtbishop/look4sat/di/{UtilityModule.kt => ApplicationModule.kt} (77%) delete mode 100644 app/src/main/java/com/rtbishop/look4sat/di/RepoModule.kt rename app/src/main/java/com/rtbishop/look4sat/di/{PersistenceModule.kt => RepositoryModule.kt} (76%) delete mode 100644 app/src/main/java/com/rtbishop/look4sat/repo/DefaultEntriesRepo.kt delete mode 100644 app/src/main/java/com/rtbishop/look4sat/repo/DefaultTransmittersRepo.kt delete mode 100644 app/src/main/java/com/rtbishop/look4sat/repo/SourcesRepo.kt diff --git a/app/src/main/java/com/rtbishop/look4sat/SharedViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/SharedViewModel.kt index 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.look4sat import android.net.Uri -import androidx.hilt.lifecycle.ViewModelInject import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.rtbishop.look4sat.data.* import com.rtbishop.look4sat.repo.EntriesRepo -import com.rtbishop.look4sat.repo.SourcesRepo +import com.rtbishop.look4sat.repo.DefaultSourcesRepo import com.rtbishop.look4sat.repo.TransmittersRepo import com.rtbishop.look4sat.utility.PassPredictor import com.rtbishop.look4sat.utility.PrefsManager +import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch import 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>>() private val _appEvent = MutableLiveData>() diff --git a/app/src/main/java/com/rtbishop/look4sat/di/UtilityModule.kt b/app/src/main/java/com/rtbishop/look4sat/di/ApplicationModule.kt similarity index 77% rename from app/src/main/java/com/rtbishop/look4sat/di/UtilityModule.kt rename to app/src/main/java/com/rtbishop/look4sat/di/ApplicationModule.kt index 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.PreferenceManager import dagger.Module import dagger.Provides import 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) - } - - @Provides - fun getLocationManager(@ActivityContext context: Context): LocationManager { + @Singleton + fun getLocationManager(@ApplicationContext context: Context): LocationManager { return context.getSystemService(Context.LOCATION_SERVICE) as LocationManager } + + @Provides + @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.kt index 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.TransmittersApi import dagger.Module import dagger.Provides import dagger.hilt.InstallIn -import dagger.hilt.android.components.ActivityComponent +import dagger.hilt.components.SingletonComponent import okhttp3.OkHttpClient import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory +import javax.inject.Singleton @Module -@InstallIn(ActivityComponent::class) +@InstallIn(SingletonComponent::class) object NetworkModule { - + @Provides + @Singleton fun getOkHttpClient(): OkHttpClient { return OkHttpClient() } - + @Provides + @Singleton fun 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.kt deleted file mode 100644 index 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 (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 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.kt similarity index 76% rename from app/src/main/java/com/rtbishop/look4sat/di/PersistenceModule.kt rename to app/src/main/java/com/rtbishop/look4sat/di/RepositoryModule.kt index 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.ContentResolver import android.content.Context import androidx.room.Room import com.rtbishop.look4sat.repo.local.EntriesDao @@ -28,30 +29,41 @@ import com.rtbishop.look4sat.repo.local.TransmittersDao import dagger.Module import dagger.Provides import 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 + @Singleton fun getEntriesDao(db: SatelliteDb): EntriesDao { return db.entriesDao() } - + @Provides + @Singleton fun getSourcesDao(db: SatelliteDb): SourcesDao { return db.sourcesDao() } - + @Provides + @Singleton fun 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.kt deleted file mode 100644 index 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 (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 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> { - 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) { - withContext(Dispatchers.IO) { - val streams = mutableListOf() - 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() - 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) { - entriesDao.updateEntriesSelection(satIds) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultSourcesRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/DefaultSourcesRepo.kt index 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.TleSource import com.rtbishop.look4sat.repo.local.SourcesDao import javax.inject.Inject -class DefaultSourcesRepo @Inject constructor(private val sourcesDao: SourcesDao) : - SourcesRepo { - - override fun getSources(): LiveData> { +class DefaultSourcesRepo @Inject constructor(private val sourcesDao: SourcesDao) { + + fun getSources(): LiveData> { return sourcesDao.getSources() } - - override suspend fun updateSources(sources: List) { + + suspend fun updateSources(sources: List) { sourcesDao.updateSources(sources) } } \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/repo/DefaultTransmittersRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/DefaultTransmittersRepo.kt deleted file mode 100644 index 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 (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 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> { - return transmittersDao.getTransmittersForSat(satId) - } - - override suspend fun updateTransmitters() { - transmittersDao.updateTransmitters(transmittersApi.getTransmitters()) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/repo/EntriesRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/EntriesRepo.kt index 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.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 -interface EntriesRepo { - - fun getEntries(): LiveData> - - suspend fun updateEntriesFromFile(fileUri: Uri) - - suspend fun updateEntriesFromSources(sources: List) - - suspend fun updateEntriesSelection(satIds: List) +class EntriesRepo @Inject constructor( + private val resolver: ContentResolver, + private val client: OkHttpClient, + private val entriesDao: EntriesDao +) { + + fun getEntries(): LiveData> { + 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) { + withContext(Dispatchers.IO) { + val streams = mutableListOf() + 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() + 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) { + entriesDao.updateEntriesSelection(satIds) + } } \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/repo/SourcesRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/SourcesRepo.kt deleted file mode 100644 index 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 (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 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> - - suspend fun updateSources(sources: List) -} \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/repo/TransmittersRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repo/TransmittersRepo.kt index 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.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 -interface TransmittersRepo { - - suspend fun updateTransmitters() - - fun getTransmittersForSat(satId: Int): LiveData> +class TransmittersRepo @Inject constructor( + private val transmittersDao: TransmittersDao, + private val transmittersApi: TransmittersApi +) { + + fun getTransmittersForSat(satId: Int): LiveData> { + return transmittersDao.getTransmittersForSat(satId) + } + + suspend fun updateTransmitters() { + transmittersDao.updateTransmitters(transmittersApi.getTransmitters()) + } } \ No newline at end of file