mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Swapped RetrofitRemoteSource.kt for a simplified one
This commit is contained in:
1 parent
13f1cc6455
commit
c8f7a29812
10 files changed
+58
-36
No files matched your search
+2
-1
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -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
|
||||
|
||||
Reference in new issue
Block a user