mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Added SharedPref and LocationClient injection to the ViewModel via AndroidModule. Minor fixes.
This commit is contained in:
1 parent
0174ca48c1
commit
06cb63aa11
4 files changed
+42
-39
No files matched your search
@@ -8,12 +8,11 @@ import androidx.core.content.edit
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.github.amsacode.predict4java.GroundStationPosition
|
||||
import com.github.amsacode.predict4java.PassPredictor
|
||||
import com.github.amsacode.predict4java.SatNotFoundException
|
||||
import com.github.amsacode.predict4java.TLE
|
||||
import com.google.android.gms.location.LocationServices
|
||||
import com.google.android.gms.location.FusedLocationProviderClient
|
||||
import com.rtbishop.lookingsat.repo.Repository
|
||||
import com.rtbishop.lookingsat.repo.SatPass
|
||||
import com.rtbishop.lookingsat.repo.SatPassPrefs
|
||||
@@ -30,15 +29,15 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
private val keyLat = "latitude"
|
||||
private val keyLon = "longitude"
|
||||
private val keyHeight = "height"
|
||||
private val keyAlt = "altitude"
|
||||
private val keyHours = "hoursAhead"
|
||||
private val keyMaxEl = "maxElevation"
|
||||
private val tleFileName = "tleFile.txt"
|
||||
private val preferences = PreferenceManager.getDefaultSharedPreferences(application)
|
||||
private val locationClient = LocationServices.getFusedLocationProviderClient(application)
|
||||
|
||||
val debugMessage = MutableLiveData("")
|
||||
|
||||
@Inject
|
||||
lateinit var locationClient: FusedLocationProviderClient
|
||||
@Inject
|
||||
lateinit var preferences: SharedPreferences
|
||||
@Inject
|
||||
lateinit var repository: Repository
|
||||
|
||||
@@ -46,19 +45,18 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
(application as LookingSatApp).appComponent.inject(this)
|
||||
}
|
||||
|
||||
var satPassList = emptyList<SatPass>()
|
||||
|
||||
val debugMessage = MutableLiveData("")
|
||||
val gsp = MutableLiveData<GroundStationPosition>(
|
||||
GroundStationPosition(
|
||||
preferences.getDouble(keyLat, 0.0),
|
||||
preferences.getDouble(keyLon, 0.0),
|
||||
preferences.getDouble(keyHeight, 0.0)
|
||||
preferences.getDouble(keyAlt, 0.0)
|
||||
)
|
||||
)
|
||||
|
||||
var satPassList = emptyList<SatPass>()
|
||||
var tleMainList = loadTwoLineElementFile()
|
||||
var tleSelectedMap = mutableMapOf<TLE, Boolean>()
|
||||
var selectedSingleSat = tleMainList[0]
|
||||
var passPrefs = SatPassPrefs(
|
||||
preferences.getInt(keyHours, 8),
|
||||
preferences.getDouble(keyMaxEl, 16.0)
|
||||
@@ -74,10 +72,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
fun updateSelectedSingleSat(sat: TLE) {
|
||||
selectedSingleSat = sat
|
||||
}
|
||||
|
||||
suspend fun getPasses() {
|
||||
val passList = mutableListOf<SatPass>()
|
||||
withContext(Dispatchers.Default) {
|
||||
@@ -85,12 +79,12 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
if (value) {
|
||||
try {
|
||||
val predictor = PassPredictor(tle, gsp.value)
|
||||
val passes = predictor.getPasses(Date(), passPrefs.hoursAhead, false)
|
||||
val passes = predictor.getPasses(Date(), passPrefs.hoursAhead, true)
|
||||
passes.forEach { passList.add(SatPass(tle, predictor, it)) }
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
debugMessage.postValue("There was a problem with TLE")
|
||||
debugMessage.postValue("There was a problem with ${tle.name}")
|
||||
} catch (exception: SatNotFoundException) {
|
||||
debugMessage.postValue("Certain satellites shall not pass")
|
||||
debugMessage.postValue("${tle.name} shall not pass")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,15 +111,15 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
locationClient.lastLocation.addOnSuccessListener { location: Location? ->
|
||||
val lat = location?.latitude ?: 0.0
|
||||
val lon = location?.longitude ?: 0.0
|
||||
val height = location?.altitude ?: 0.0
|
||||
val alt = location?.altitude ?: 0.0
|
||||
|
||||
preferences.edit {
|
||||
putDouble(keyLat, lat)
|
||||
putDouble(keyLon, lon)
|
||||
putDouble(keyHeight, height)
|
||||
putDouble(keyAlt, alt)
|
||||
apply()
|
||||
}
|
||||
gsp.postValue(GroundStationPosition(lat, lon, height))
|
||||
gsp.postValue(GroundStationPosition(lat, lon, alt))
|
||||
debugMessage.postValue("Location was updated")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.rtbishop.lookingsat.di
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.google.android.gms.location.FusedLocationProviderClient
|
||||
import com.google.android.gms.location.LocationServices
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
class AndroidModule {
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideSharedPreferences(context: Context): SharedPreferences {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideLocationClient(context: Context): FusedLocationProviderClient {
|
||||
return LocationServices.getFusedLocationProviderClient(context)
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import dagger.Component
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
@Component(modules = [NetworkModule::class, StorageModule::class])
|
||||
@Component(modules = [AndroidModule::class, NetworkModule::class, StorageModule::class])
|
||||
interface AppComponent {
|
||||
|
||||
@Component.Factory
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.rtbishop.lookingsat.storage
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import com.rtbishop.lookingsat.repo.Transmitter
|
||||
|
||||
@@ -10,19 +8,4 @@ import com.rtbishop.lookingsat.repo.Transmitter
|
||||
abstract class TransmittersDb : RoomDatabase() {
|
||||
|
||||
abstract fun transmittersDao(): TransmittersDao
|
||||
|
||||
companion object {
|
||||
|
||||
@Volatile
|
||||
private var INSTANCE: TransmittersDb? = null
|
||||
|
||||
fun getInstance(context: Context): TransmittersDb =
|
||||
INSTANCE ?: synchronized(this) {
|
||||
INSTANCE ?: Room.databaseBuilder(
|
||||
context,
|
||||
TransmittersDb::class.java,
|
||||
"transmitters"
|
||||
).build()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user