Added room type converter for Satellite objects

This commit is contained in:
Arty Bishop committed 2021-03-06 17:08:36 +00:00
1 parent 4705ff6915
commit 0dfd7e6250
4 files changed
+24 -15

No files matched your search

@@ -21,6 +21,7 @@ package com.rtbishop.look4sat.repository
import android.content.ContentResolver import android.content.ContentResolver
import android.net.Uri import android.net.Uri
import com.github.amsacode.predict4java.Satellite
import com.github.amsacode.predict4java.TLE import com.github.amsacode.predict4java.TLE
import com.rtbishop.look4sat.data.SatEntry import com.rtbishop.look4sat.data.SatEntry
import com.rtbishop.look4sat.data.SatItem import com.rtbishop.look4sat.data.SatItem
@@ -46,8 +47,8 @@ class SatelliteRepo @Inject constructor(
return satelliteDao.getSatItems() return satelliteDao.getSatItems()
} }
suspend fun getSelectedEntries(): List<SatEntry> { suspend fun getSelectedSatellites(): List<Satellite> {
return satelliteDao.getSelectedEntries() return satelliteDao.getSelectedSatellites()
} }
suspend fun updateEntriesSelection(catNums: List<Int>) { suspend fun updateEntriesSelection(catNums: List<Int>) {
@@ -20,6 +20,8 @@ You should have received a copy of the GNU General Public License along
package com.rtbishop.look4sat.repository.localData package com.rtbishop.look4sat.repository.localData
import androidx.room.TypeConverter import androidx.room.TypeConverter
import com.github.amsacode.predict4java.Satellite
import com.github.amsacode.predict4java.SatelliteFactory
import com.github.amsacode.predict4java.TLE import com.github.amsacode.predict4java.TLE
import com.squareup.moshi.JsonAdapter import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
@@ -43,4 +45,11 @@ object RoomConverters {
fun tleFromString(string: String): TLE? { fun tleFromString(string: String): TLE? {
return tleAdapter.fromJson(string) return tleAdapter.fromJson(string)
} }
@JvmStatic
@TypeConverter
fun satFromString(string: String): Satellite? {
val tle = tleAdapter.fromJson(string)
return SatelliteFactory.createSatellite(tle)
}
} }
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
package com.rtbishop.look4sat.repository.localData package com.rtbishop.look4sat.repository.localData
import androidx.room.* import androidx.room.*
import com.github.amsacode.predict4java.Satellite
import com.rtbishop.look4sat.data.SatEntry import com.rtbishop.look4sat.data.SatEntry
import com.rtbishop.look4sat.data.SatItem import com.rtbishop.look4sat.data.SatItem
import com.rtbishop.look4sat.data.SatTrans import com.rtbishop.look4sat.data.SatTrans
@@ -37,8 +38,8 @@ interface SatelliteDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertEntries(entries: List<SatEntry>) suspend fun insertEntries(entries: List<SatEntry>)
@Query("SELECT * FROM entries WHERE isSelected = 1 ORDER BY name ASC") @Query("SELECT tle FROM entries WHERE isSelected = 1")
suspend fun getSelectedEntries(): List<SatEntry> suspend fun getSelectedSatellites(): List<Satellite>
@Query("SELECT catNum FROM entries WHERE isSelected = 1") @Query("SELECT catNum FROM entries WHERE isSelected = 1")
suspend fun getSelectedCatNums(): List<Int> suspend fun getSelectedCatNums(): List<Int>
@@ -21,7 +21,7 @@ package com.rtbishop.look4sat.ui
import android.net.Uri import android.net.Uri
import androidx.lifecycle.* import androidx.lifecycle.*
import com.github.amsacode.predict4java.SatelliteFactory import com.github.amsacode.predict4java.Satellite
import com.rtbishop.look4sat.data.* import com.rtbishop.look4sat.data.*
import com.rtbishop.look4sat.repository.PrefsRepo import com.rtbishop.look4sat.repository.PrefsRepo
import com.rtbishop.look4sat.repository.SatelliteRepo import com.rtbishop.look4sat.repository.SatelliteRepo
@@ -68,8 +68,8 @@ class SharedViewModel @Inject constructor(
_passes.value = Result.InProgress _passes.value = Result.InProgress
viewModelScope.launch(Dispatchers.Default) { viewModelScope.launch(Dispatchers.Default) {
val passes = mutableListOf<SatPass>() val passes = mutableListOf<SatPass>()
satelliteRepo.getSelectedEntries().forEach { entry -> satelliteRepo.getSelectedSatellites().forEach { satellite ->
passes.addAll(getPasses(entry, dateNow)) passes.addAll(getPasses(satellite, dateNow))
} }
val filteredPasses = sortList(passes, dateNow) val filteredPasses = sortList(passes, dateNow)
_passes.postValue(Result.Success(filteredPasses)) _passes.postValue(Result.Success(filteredPasses))
@@ -112,16 +112,14 @@ class SharedViewModel @Inject constructor(
private fun postAppEvent(event: Event<Int>) { private fun postAppEvent(event: Event<Int>) {
_appEvent.value = event _appEvent.value = event
} }
private fun getPasses(entry: SatEntry, dateNow: Date): MutableList<SatPass> { private fun getPasses(satellite: Satellite, dateNow: Date): MutableList<SatPass> {
val gsp = prefsRepo.getStationPosition() val predictor = satellite.getPredictor(prefsRepo.getStationPosition())
val predictor = SatelliteFactory.createSatellite(entry.tle).getPredictor(gsp) val passes = predictor.getPasses(dateNow, prefsRepo.getHoursAhead(), true)
val hoursAhead = prefsRepo.getHoursAhead() val passList = passes.map { SatPass(satellite.tle, predictor, it) }
val passes = predictor.getPasses(dateNow, hoursAhead, true)
val passList = passes.map { SatPass(entry.tle, predictor, it) }
return passList as MutableList<SatPass> return passList as MutableList<SatPass>
} }
private fun sortList(passes: MutableList<SatPass>, dateNow: Date): MutableList<SatPass> { private fun sortList(passes: MutableList<SatPass>, dateNow: Date): MutableList<SatPass> {
val hoursAhead = prefsRepo.getHoursAhead() val hoursAhead = prefsRepo.getHoursAhead()
val dateFuture = Calendar.getInstance().apply { val dateFuture = Calendar.getInstance().apply {