kopia lustrzana https://github.com/rt-bishop/Look4Sat
Swapped RetrofitRemoteSource.kt for a simplified one
rodzic
13f1cc6455
commit
c8f7a29812
|
@ -51,7 +51,7 @@ android {
|
|||
targetCompatibility JavaVersion.VERSION_11
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = "$jvm_version"
|
||||
jvmTarget = JavaVersion.VERSION_11.toString()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,6 +71,7 @@ dependencies {
|
|||
implementation "com.google.dagger:hilt-android:$hilt_version"
|
||||
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
|
||||
|
||||
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
|
||||
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
|
||||
implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
|
||||
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.rtbishop.look4sat.framework.remote
|
||||
|
||||
import com.rtbishop.look4sat.data.RemoteDataSource
|
||||
import com.rtbishop.look4sat.domain.model.Transmitter
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.InputStream
|
||||
import java.net.URL
|
||||
|
||||
class DefaultRemoteSource(private val dispatcher: CoroutineDispatcher) : RemoteDataSource {
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
override suspend fun fetchFileStream(url: String): InputStream? = withContext(dispatcher) {
|
||||
return@withContext URL(url).openStream()
|
||||
}
|
||||
|
||||
override suspend fun fetchTransmitters(url: String): List<Transmitter> {
|
||||
val stream = URL(url).openStream()
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
private suspend fun download(url: String): InputStream? = withContext(dispatcher) {
|
||||
return@withContext try {
|
||||
URL(url).openStream().bufferedReader().useLines { lines ->
|
||||
lines.forEachIndexed { index, line ->
|
||||
val items = line.split(",")
|
||||
}
|
||||
}
|
||||
null
|
||||
} catch (e: Exception) {
|
||||
return@withContext null
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,13 +22,13 @@ import com.rtbishop.look4sat.domain.model.Transmitter
|
|||
import com.rtbishop.look4sat.framework.toDomain
|
||||
import java.io.InputStream
|
||||
|
||||
class RemoteSource(private val satelliteApi: SatelliteApi) : RemoteDataSource {
|
||||
class RetrofitRemoteSource(private val satelliteApi: SatelliteApi) : RemoteDataSource {
|
||||
|
||||
override suspend fun fetchFileStream(url: String): InputStream? {
|
||||
return satelliteApi.fetchFileStream(url).body()?.byteStream()
|
||||
}
|
||||
|
||||
override suspend fun fetchTransmitters(): List<Transmitter> {
|
||||
return satelliteApi.fetchTransmitters().toDomain()
|
||||
override suspend fun fetchTransmitters(url: String): List<Transmitter> {
|
||||
return satelliteApi.fetchTransmitters(url).toDomain()
|
||||
}
|
||||
}
|
|
@ -30,6 +30,6 @@ interface SatelliteApi {
|
|||
@GET
|
||||
suspend fun fetchFileStream(@Url url: String): Response<ResponseBody>
|
||||
|
||||
@GET("https://db.satnogs.org/api/transmitters/")
|
||||
suspend fun fetchTransmitters(): List<Transmitter>
|
||||
@GET
|
||||
suspend fun fetchTransmitters(@Url url: String): List<Transmitter>
|
||||
}
|
||||
|
|
|
@ -19,54 +19,39 @@ package com.rtbishop.look4sat.injection
|
|||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import com.rtbishop.look4sat.data.LocalDataSource
|
||||
import com.rtbishop.look4sat.data.RemoteDataSource
|
||||
import com.rtbishop.look4sat.data.DefaultRepository
|
||||
import com.rtbishop.look4sat.framework.remote.SatelliteApi
|
||||
import com.rtbishop.look4sat.domain.DataRepository
|
||||
import com.rtbishop.look4sat.domain.DataReporter
|
||||
import com.rtbishop.look4sat.domain.predict.Predictor
|
||||
import com.rtbishop.look4sat.framework.remote.RemoteSource
|
||||
import com.rtbishop.look4sat.framework.local.*
|
||||
import com.rtbishop.look4sat.framework.remote.DefaultRemoteSource
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object CoreModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideLocalDataSource(@ApplicationContext context: Context): LocalDataSource {
|
||||
val database = Room.databaseBuilder(context, SatelliteDb::class.java, "SatelliteDb")
|
||||
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4).build()
|
||||
return LocalSource(database.entriesDao(), database.sourcesDao(), database.transmittersDao())
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRemoteDataSource(): RemoteDataSource {
|
||||
val satelliteApi = Retrofit.Builder().baseUrl("https://localhost")
|
||||
.addConverterFactory(MoshiConverterFactory.create()).build()
|
||||
.create(SatelliteApi::class.java)
|
||||
return RemoteSource(satelliteApi)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideSatelliteRepo(
|
||||
localSource: LocalDataSource,
|
||||
remoteSource: RemoteDataSource,
|
||||
@ApplicationContext context: Context,
|
||||
@IoDispatcher dispatcher: CoroutineDispatcher
|
||||
): DataRepository {
|
||||
return DefaultRepository(localSource, remoteSource, dispatcher)
|
||||
val db = Room.databaseBuilder(context, SatelliteDb::class.java, "SatelliteDb")
|
||||
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4).build()
|
||||
val roomLocalSource = LocalSource(db.entriesDao(), db.sourcesDao(), db.transmittersDao())
|
||||
// val satelliteApi = Retrofit.Builder().baseUrl("https://localhost")
|
||||
// .addConverterFactory(MoshiConverterFactory.create()).build()
|
||||
// .create(SatelliteApi::class.java)
|
||||
// val retrofitRemoteSource = RetrofitRemoteSource(satelliteApi)
|
||||
val defaultRemoteSource = DefaultRemoteSource(dispatcher)
|
||||
return DefaultRepository(roomLocalSource, defaultRemoteSource, dispatcher)
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
|
|
@ -11,13 +11,13 @@ buildscript {
|
|||
preference_version = '1.1.1'
|
||||
room_version = '2.3.0'
|
||||
hilt_version = '2.39.1'
|
||||
okhttp_version = '4.9.0'
|
||||
retrofit_version = '2.9.0'
|
||||
osmdroid_version = '6.1.11'
|
||||
timber_version = '5.0.1'
|
||||
junit_version = '4.13.2'
|
||||
mockito_version = '4.0.0'
|
||||
leak_canary_version = '2.7'
|
||||
jvm_version = '11'
|
||||
}
|
||||
repositories {
|
||||
google()
|
||||
|
|
|
@ -7,7 +7,7 @@ tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
|
|||
targetCompatibility = JavaVersion.VERSION_11
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = "$jvm_version"
|
||||
jvmTarget = JavaVersion.VERSION_11.toString()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -40,6 +40,7 @@ class DefaultRepository(
|
|||
"https://www.prismnet.com/~mmccants/tles/classfd.zip",
|
||||
"https://www.prismnet.com/~mmccants/tles/inttles.zip"
|
||||
)
|
||||
override val transmittersSource = "https://db.satnogs.org/api/transmitters/"
|
||||
|
||||
override fun getSatelliteItems() = localSource.getSatelliteItems()
|
||||
|
||||
|
@ -78,7 +79,7 @@ class DefaultRepository(
|
|||
localSource.updateEntries(entries)
|
||||
}
|
||||
launch(repoDispatcher) {
|
||||
val transmitters = remoteSource.fetchTransmitters().filter { it.isAlive }
|
||||
val transmitters = remoteSource.fetchTransmitters(transmittersSource)
|
||||
localSource.updateTransmitters(transmitters)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,5 +24,5 @@ interface RemoteDataSource {
|
|||
|
||||
suspend fun fetchFileStream(url: String): InputStream?
|
||||
|
||||
suspend fun fetchTransmitters(): List<Transmitter>
|
||||
suspend fun fetchTransmitters(url: String): List<Transmitter>
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ interface DataRepository {
|
|||
|
||||
val defaultSelection: List<Int>
|
||||
val defaultSources: List<String>
|
||||
val transmittersSource: String
|
||||
|
||||
fun getSatelliteItems(): Flow<List<SatItem>>
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue