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 061722c6..8463627a 100644 --- a/app/src/main/java/com/rtbishop/look4sat/di/NetworkModule.kt +++ b/app/src/main/java/com/rtbishop/look4sat/di/NetworkModule.kt @@ -19,13 +19,12 @@ package com.rtbishop.look4sat.di -import com.rtbishop.look4sat.repository.remoteData.TransmittersApi +import com.rtbishop.look4sat.repository.remoteData.SatelliteService import com.squareup.moshi.Moshi import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent -import okhttp3.OkHttpClient import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory import javax.inject.Singleton @@ -34,12 +33,6 @@ import javax.inject.Singleton @InstallIn(SingletonComponent::class) object NetworkModule { - @Provides - @Singleton - fun provideOkHttpClient(): OkHttpClient { - return OkHttpClient() - } - @Provides @Singleton fun provideMoshi(): Moshi { @@ -48,11 +41,11 @@ object NetworkModule { @Provides @Singleton - fun provideTransmittersApi(): TransmittersApi { + fun provideSatDataService(): SatelliteService { return Retrofit.Builder() .baseUrl("https://db.satnogs.org/api/") .addConverterFactory(MoshiConverterFactory.create()) .build() - .create(TransmittersApi::class.java) + .create(SatelliteService::class.java) } } diff --git a/app/src/main/java/com/rtbishop/look4sat/di/RepositoryModule.kt b/app/src/main/java/com/rtbishop/look4sat/di/RepositoryModule.kt index 1c7b2693..e9bfa7ba 100644 --- a/app/src/main/java/com/rtbishop/look4sat/di/RepositoryModule.kt +++ b/app/src/main/java/com/rtbishop/look4sat/di/RepositoryModule.kt @@ -21,11 +21,10 @@ package com.rtbishop.look4sat.di import android.content.Context import androidx.room.Room -import com.rtbishop.look4sat.repository.localData.EntriesDao import com.rtbishop.look4sat.repository.localData.MIGRATION_1_2 +import com.rtbishop.look4sat.repository.localData.SatelliteDao import com.rtbishop.look4sat.repository.localData.SatelliteDb -import com.rtbishop.look4sat.repository.localData.TransmittersDao -import com.rtbishop.look4sat.utility.RoomConverters +import com.rtbishop.look4sat.repository.localData.RoomConverters import com.squareup.moshi.Moshi import dagger.Module import dagger.Provides @@ -40,14 +39,8 @@ object RepositoryModule { @Provides @Singleton - fun provideEntriesDao(db: SatelliteDb): EntriesDao { - return db.entriesDao() - } - - @Provides - @Singleton - fun provideTransmittersDao(db: SatelliteDb): TransmittersDao { - return db.transmittersDao() + fun provideSatDataDao(db: SatelliteDb): SatelliteDao { + return db.satelliteDao() } @Provides diff --git a/app/src/main/java/com/rtbishop/look4sat/repository/EntriesRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repository/SatelliteRepo.kt similarity index 75% rename from app/src/main/java/com/rtbishop/look4sat/repository/EntriesRepo.kt rename to app/src/main/java/com/rtbishop/look4sat/repository/SatelliteRepo.kt index 3497757f..f54f3474 100644 --- a/app/src/main/java/com/rtbishop/look4sat/repository/EntriesRepo.kt +++ b/app/src/main/java/com/rtbishop/look4sat/repository/SatelliteRepo.kt @@ -23,34 +23,30 @@ import android.content.ContentResolver import android.net.Uri import com.github.amsacode.predict4java.TLE import com.rtbishop.look4sat.data.SatEntry +import com.rtbishop.look4sat.data.SatTrans import com.rtbishop.look4sat.data.TleSource import com.rtbishop.look4sat.di.IoDispatcher -import com.rtbishop.look4sat.repository.localData.EntriesDao +import com.rtbishop.look4sat.repository.localData.SatelliteDao +import com.rtbishop.look4sat.repository.remoteData.SatelliteService import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.withContext -import okhttp3.OkHttpClient -import okhttp3.Request import java.io.InputStream import javax.inject.Inject -class EntriesRepo @Inject constructor( - private val entriesDao: EntriesDao, - private val client: OkHttpClient, +class SatelliteRepo @Inject constructor( private val resolver: ContentResolver, + private val satelliteDao: SatelliteDao, + private val satelliteService: SatelliteService, @IoDispatcher private val ioDispatcher: CoroutineDispatcher ) { fun getEntries(): Flow> { - return entriesDao.getEntries() - } - - suspend fun getSelectedEntries(): List { - return entriesDao.getSelectedEntries() + return satelliteDao.getEntries() } suspend fun updateEntriesSelection(catNums: List) { - entriesDao.updateEntriesSelection(catNums) + satelliteDao.updateEntriesSelection(catNums) } suspend fun updateEntriesFromFile(uri: Uri) { @@ -68,8 +64,7 @@ class EntriesRepo @Inject constructor( withContext(ioDispatcher) { val streams = mutableListOf() sources.forEach { source -> - val request = Request.Builder().url(source.url).build() - val stream = client.newCall(request).execute().body()?.byteStream() + val stream = satelliteService.fetchFile(source.url).body()?.byteStream() stream?.let { inputStream -> streams.add(inputStream) } } val importedEntries = mutableListOf() @@ -81,9 +76,17 @@ class EntriesRepo @Inject constructor( } } + fun getTransmittersForSat(catNum: Int): Flow> { + return satelliteDao.getTransmittersForSat(catNum) + } + + suspend fun updateTransmitters() { + satelliteDao.insertTransmitters(satelliteService.fetchTransmitters()) + } + private suspend fun updateAndRestoreSelection(entries: List) { - val selectedCatNums = entriesDao.getSelectedCatNums() - entriesDao.insertEntries(entries) - entriesDao.updateEntriesSelection(selectedCatNums) + val selectedCatNums = satelliteDao.getSelectedCatNums() + satelliteDao.insertEntries(entries) + satelliteDao.updateEntriesSelection(selectedCatNums) } } \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/repository/TransmittersRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repository/TransmittersRepo.kt deleted file mode 100644 index b2566dac..00000000 --- a/app/src/main/java/com/rtbishop/look4sat/repository/TransmittersRepo.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.repository - -import com.rtbishop.look4sat.data.SatTrans -import com.rtbishop.look4sat.repository.localData.TransmittersDao -import com.rtbishop.look4sat.repository.remoteData.TransmittersApi -import kotlinx.coroutines.flow.Flow -import javax.inject.Inject - -class TransmittersRepo @Inject constructor( - private val transmittersApi: TransmittersApi, - private val transmittersDao: TransmittersDao -) { - - fun getTransmittersForSat(catNum: Int): Flow> { - return transmittersDao.getTransmittersForSat(catNum) - } - - suspend fun updateTransmitters() { - transmittersDao.insertTransmitters(transmittersApi.fetchTransmitters()) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/utility/RoomConverters.kt b/app/src/main/java/com/rtbishop/look4sat/repository/localData/RoomConverters.kt similarity index 84% rename from app/src/main/java/com/rtbishop/look4sat/utility/RoomConverters.kt rename to app/src/main/java/com/rtbishop/look4sat/repository/localData/RoomConverters.kt index 0370cec4..e7ec41cc 100644 --- a/app/src/main/java/com/rtbishop/look4sat/utility/RoomConverters.kt +++ b/app/src/main/java/com/rtbishop/look4sat/repository/localData/RoomConverters.kt @@ -2,22 +2,22 @@ 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 +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, +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 +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.utility +package com.rtbishop.look4sat.repository.localData import androidx.room.TypeConverter import com.github.amsacode.predict4java.TLE diff --git a/app/src/main/java/com/rtbishop/look4sat/repository/localData/EntriesDao.kt b/app/src/main/java/com/rtbishop/look4sat/repository/localData/SatelliteDao.kt similarity index 84% rename from app/src/main/java/com/rtbishop/look4sat/repository/localData/EntriesDao.kt rename to app/src/main/java/com/rtbishop/look4sat/repository/localData/SatelliteDao.kt index ae6741e1..1360bc53 100644 --- a/app/src/main/java/com/rtbishop/look4sat/repository/localData/EntriesDao.kt +++ b/app/src/main/java/com/rtbishop/look4sat/repository/localData/SatelliteDao.kt @@ -21,10 +21,11 @@ package com.rtbishop.look4sat.repository.localData import androidx.room.* import com.rtbishop.look4sat.data.SatEntry +import com.rtbishop.look4sat.data.SatTrans import kotlinx.coroutines.flow.Flow @Dao -interface EntriesDao { +interface SatelliteDao { @Query("SELECT * FROM entries ORDER BY name ASC") fun getEntries(): Flow> @@ -32,9 +33,6 @@ interface EntriesDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insertEntries(entries: List) - @Query("SELECT * FROM entries WHERE isSelected = 1") - suspend fun getSelectedEntries(): List - @Query("SELECT catNum FROM entries WHERE isSelected = 1") suspend fun getSelectedCatNums(): List @@ -49,4 +47,10 @@ interface EntriesDao { @Query("UPDATE entries SET isSelected = 1 WHERE catNum = :catNum") suspend fun updateSelection(catNum: Int) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insertTransmitters(transmitters: List) + + @Query("SELECT * FROM transmitters WHERE isAlive = 1 and catNum = :catNum") + fun getTransmittersForSat(catNum: Int): Flow> } diff --git a/app/src/main/java/com/rtbishop/look4sat/repository/localData/SatelliteDb.kt b/app/src/main/java/com/rtbishop/look4sat/repository/localData/SatelliteDb.kt index 5cf23d93..6031d3fa 100644 --- a/app/src/main/java/com/rtbishop/look4sat/repository/localData/SatelliteDb.kt +++ b/app/src/main/java/com/rtbishop/look4sat/repository/localData/SatelliteDb.kt @@ -26,15 +26,12 @@ import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase import com.rtbishop.look4sat.data.SatEntry import com.rtbishop.look4sat.data.SatTrans -import com.rtbishop.look4sat.utility.RoomConverters @Database(entities = [SatEntry::class, SatTrans::class], version = 2) @TypeConverters(RoomConverters::class) abstract class SatelliteDb : RoomDatabase() { - abstract fun entriesDao(): EntriesDao - - abstract fun transmittersDao(): TransmittersDao + abstract fun satelliteDao(): SatelliteDao } val MIGRATION_1_2 = object : Migration(1, 2) { diff --git a/app/src/main/java/com/rtbishop/look4sat/repository/localData/TransmittersDao.kt b/app/src/main/java/com/rtbishop/look4sat/repository/localData/TransmittersDao.kt deleted file mode 100644 index 5082dee3..00000000 --- a/app/src/main/java/com/rtbishop/look4sat/repository/localData/TransmittersDao.kt +++ /dev/null @@ -1,37 +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.repository.localData - -import androidx.room.Dao -import androidx.room.Insert -import androidx.room.OnConflictStrategy -import androidx.room.Query -import com.rtbishop.look4sat.data.SatTrans -import kotlinx.coroutines.flow.Flow - -@Dao -interface TransmittersDao { - - @Query("SELECT * FROM transmitters WHERE isAlive = 1 and catNum = :catNum") - fun getTransmittersForSat(catNum: Int): Flow> - - @Insert(onConflict = OnConflictStrategy.REPLACE) - suspend fun insertTransmitters(transmitters: List) -} diff --git a/app/src/main/java/com/rtbishop/look4sat/repository/remoteData/SatelliteService.kt b/app/src/main/java/com/rtbishop/look4sat/repository/remoteData/SatelliteService.kt new file mode 100644 index 00000000..3d39857e --- /dev/null +++ b/app/src/main/java/com/rtbishop/look4sat/repository/remoteData/SatelliteService.kt @@ -0,0 +1,37 @@ +/******************************************************************************* +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.repository.remoteData + +import com.rtbishop.look4sat.data.SatTrans +import okhttp3.ResponseBody +import retrofit2.Response +import retrofit2.http.GET +import retrofit2.http.Streaming +import retrofit2.http.Url + +interface SatelliteService { + + @Streaming + @GET + suspend fun fetchFile(@Url fileUrl: String): Response + + @GET("transmitters/") + suspend fun fetchTransmitters(): List +} diff --git a/app/src/main/java/com/rtbishop/look4sat/repository/remoteData/TransmittersApi.kt b/app/src/main/java/com/rtbishop/look4sat/repository/remoteData/TransmittersApi.kt deleted file mode 100644 index cdb75164..00000000 --- a/app/src/main/java/com/rtbishop/look4sat/repository/remoteData/TransmittersApi.kt +++ /dev/null @@ -1,29 +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.repository.remoteData - -import com.rtbishop.look4sat.data.SatTrans -import retrofit2.http.GET - -interface TransmittersApi { - - @GET("transmitters/") - suspend fun fetchTransmitters(): List -} diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt index 6df40281..b8ab8f05 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/SharedViewModel.kt @@ -23,9 +23,8 @@ import android.net.Uri import androidx.lifecycle.* import com.github.amsacode.predict4java.SatelliteFactory import com.rtbishop.look4sat.data.* -import com.rtbishop.look4sat.repository.EntriesRepo +import com.rtbishop.look4sat.repository.SatelliteRepo import com.rtbishop.look4sat.repository.PrefsRepo -import com.rtbishop.look4sat.repository.TransmittersRepo import com.rtbishop.look4sat.utility.getPredictor import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers @@ -37,8 +36,7 @@ import javax.inject.Inject @HiltViewModel class SharedViewModel @Inject constructor( private val prefsRepo: PrefsRepo, - private val entriesRepo: EntriesRepo, - private val transmittersRepo: TransmittersRepo + private val satelliteRepo: SatelliteRepo, ) : ViewModel() { private val _passes = MutableLiveData>>() @@ -62,10 +60,10 @@ class SharedViewModel @Inject constructor( } fun getSources() = liveData { emit(prefsRepo.loadTleSources()) } - fun getEntries() = entriesRepo.getEntries().asLiveData() + fun getEntries() = satelliteRepo.getEntries().asLiveData() fun getPasses(): LiveData>> = _passes fun getTransmittersForSat(satId: Int) = - transmittersRepo.getTransmittersForSat(satId).asLiveData() + satelliteRepo.getTransmittersForSat(satId).asLiveData() fun triggerCalculation() { if (shouldTriggerCalculation) { @@ -93,7 +91,7 @@ class SharedViewModel @Inject constructor( postAppEvent(Event(0)) viewModelScope.launch { try { - entriesRepo.updateEntriesFromFile(uri) + satelliteRepo.updateEntriesFromFile(uri) } catch (exception: Exception) { postAppEvent(Event(1)) } @@ -105,8 +103,8 @@ class SharedViewModel @Inject constructor( viewModelScope.launch { try { prefsRepo.saveTleSources(sources) - entriesRepo.updateEntriesFromWeb(sources) - transmittersRepo.updateTransmitters() + satelliteRepo.updateEntriesFromWeb(sources) + satelliteRepo.updateTransmitters() } catch (exception: Exception) { postAppEvent(Event(1)) } @@ -117,7 +115,7 @@ class SharedViewModel @Inject constructor( viewModelScope.launch { selectedEntries = entries.filter { it.isSelected } calculatePasses() - entriesRepo.updateEntriesSelection(selectedEntries.map { it.catNum }) + satelliteRepo.updateEntriesSelection(selectedEntries.map { it.catNum }) } }