mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-02 19:17:21 +00:00
Added application context to ViewModel Injector. Changed data type of fetchTles() method to ByteArray.
This commit is contained in:
1 parent
53e64c30c2
commit
d530eb9031
5 files changed
+32
-11
No files matched your search
@@ -37,6 +37,6 @@ object Injector {
|
||||
}
|
||||
|
||||
private fun provideViewModelFactory(context: Context): ViewModelProvider.Factory {
|
||||
return ViewModelFactory(provideRepository(context))
|
||||
return ViewModelFactory(context, provideRepository(context))
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.rtbishop.lookingsat.api
|
||||
|
||||
import com.rtbishop.lookingsat.repo.Transmitter
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.io.InputStream
|
||||
import java.io.SequenceInputStream
|
||||
|
||||
private const val TLE_AMATEUR = "https://celestrak.com/NORAD/elements/amateur.txt"
|
||||
private const val TLE_WEATHER = "https://celestrak.com/NORAD/elements/weather.txt"
|
||||
@@ -14,10 +14,14 @@ class RemoteDataSource(private val api: TransmittersApi, private val client: OkH
|
||||
private val requestAmateur = Request.Builder().url(TLE_AMATEUR).build()
|
||||
private val requestWeather = Request.Builder().url(TLE_WEATHER).build()
|
||||
|
||||
suspend fun fetchTleFiles(): InputStream {
|
||||
val streamAmateur = client.newCall(requestAmateur).execute().body()?.byteStream()
|
||||
val streamWeather = client.newCall(requestWeather).execute().body()?.byteStream()
|
||||
return SequenceInputStream(streamAmateur, streamWeather)
|
||||
suspend fun fetchTles(): ByteArray {
|
||||
var streamAmateur: ByteArray? = null
|
||||
var streamWeather: ByteArray? = null
|
||||
withContext(Dispatchers.IO) {
|
||||
streamAmateur = client.newCall(requestAmateur).execute().body()?.bytes()
|
||||
streamWeather = client.newCall(requestWeather).execute().body()?.bytes()
|
||||
}
|
||||
return streamAmateur ?: ByteArray(0).plus(streamWeather ?: ByteArray(0))
|
||||
}
|
||||
|
||||
suspend fun fetchTransmitters(): List<Transmitter> {
|
||||
|
||||
@@ -6,4 +6,18 @@ import com.rtbishop.lookingsat.db.LocalDataSource
|
||||
class Repository(
|
||||
private val localSource: LocalDataSource,
|
||||
private val remoteSource: RemoteDataSource
|
||||
)
|
||||
) {
|
||||
suspend fun fetchTles(): ByteArray {
|
||||
return remoteSource.fetchTles()
|
||||
}
|
||||
|
||||
suspend fun updateTransmitters() {
|
||||
val transmitters = remoteSource.fetchTransmitters()
|
||||
localSource.insert(transmitters)
|
||||
}
|
||||
|
||||
suspend fun getTransmittersForSat(id: Int): List<Transmitter> {
|
||||
updateTransmitters()
|
||||
return localSource.getTransmittersForSat(id)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.rtbishop.lookingsat.vm
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.rtbishop.lookingsat.repo.Repository
|
||||
|
||||
class MainViewModel(private val repository: Repository) : ViewModel()
|
||||
class MainViewModel(private val context: Context, private val repository: Repository) : ViewModel()
|
||||
@@ -1,14 +1,16 @@
|
||||
package com.rtbishop.lookingsat.vm
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.rtbishop.lookingsat.repo.Repository
|
||||
|
||||
class ViewModelFactory(private val repository: Repository) : ViewModelProvider.Factory {
|
||||
class ViewModelFactory(private val context: Context, private val repository: Repository) :
|
||||
ViewModelProvider.Factory {
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||
if (modelClass.isAssignableFrom(MainViewModel::class.java)) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return MainViewModel(repository) as T
|
||||
return MainViewModel(context.applicationContext, repository) as T
|
||||
}
|
||||
throw IllegalArgumentException("Unknown ViewModel")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user