Back to single repo, using retrofit to fetch satellite data

pull/49/head
Arty Bishop 2021-03-04 20:58:40 +00:00
rodzic ef51f671b7
commit 06633625ba
11 zmienionych plików z 85 dodań i 166 usunięć

Wyświetl plik

@ -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)
}
}

Wyświetl plik

@ -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

Wyświetl plik

@ -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<List<SatEntry>> {
return entriesDao.getEntries()
}
suspend fun getSelectedEntries(): List<SatEntry> {
return entriesDao.getSelectedEntries()
return satelliteDao.getEntries()
}
suspend fun updateEntriesSelection(catNums: List<Int>) {
entriesDao.updateEntriesSelection(catNums)
satelliteDao.updateEntriesSelection(catNums)
}
suspend fun updateEntriesFromFile(uri: Uri) {
@ -68,8 +64,7 @@ class EntriesRepo @Inject constructor(
withContext(ioDispatcher) {
val streams = mutableListOf<InputStream>()
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<SatEntry>()
@ -81,9 +76,17 @@ class EntriesRepo @Inject constructor(
}
}
fun getTransmittersForSat(catNum: Int): Flow<List<SatTrans>> {
return satelliteDao.getTransmittersForSat(catNum)
}
suspend fun updateTransmitters() {
satelliteDao.insertTransmitters(satelliteService.fetchTransmitters())
}
private suspend fun updateAndRestoreSelection(entries: List<SatEntry>) {
val selectedCatNums = entriesDao.getSelectedCatNums()
entriesDao.insertEntries(entries)
entriesDao.updateEntriesSelection(selectedCatNums)
val selectedCatNums = satelliteDao.getSelectedCatNums()
satelliteDao.insertEntries(entries)
satelliteDao.updateEntriesSelection(selectedCatNums)
}
}

Wyświetl plik

@ -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<List<SatTrans>> {
return transmittersDao.getTransmittersForSat(catNum)
}
suspend fun updateTransmitters() {
transmittersDao.insertTransmitters(transmittersApi.fetchTransmitters())
}
}

Wyświetl plik

@ -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

Wyświetl plik

@ -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<List<SatEntry>>
@ -32,9 +33,6 @@ interface EntriesDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertEntries(entries: List<SatEntry>)
@Query("SELECT * FROM entries WHERE isSelected = 1")
suspend fun getSelectedEntries(): List<SatEntry>
@Query("SELECT catNum FROM entries WHERE isSelected = 1")
suspend fun getSelectedCatNums(): List<Int>
@ -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<SatTrans>)
@Query("SELECT * FROM transmitters WHERE isAlive = 1 and catNum = :catNum")
fun getTransmittersForSat(catNum: Int): Flow<List<SatTrans>>
}

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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<List<SatTrans>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertTransmitters(transmitters: List<SatTrans>)
}

Wyświetl plik

@ -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<ResponseBody>
@GET("transmitters/")
suspend fun fetchTransmitters(): List<SatTrans>
}

Wyświetl plik

@ -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<SatTrans>
}

Wyświetl plik

@ -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<Result<MutableList<SatPass>>>()
@ -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<Result<MutableList<SatPass>>> = _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 })
}
}