mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Updated passes calculation logic with observers and liveData. Passes preferences are now correctly applied to the calculations.
This commit is contained in:
1 parent
0f5b734933
commit
35b4215c72
2 files changed
+44
-29
No files matched your search
@@ -41,9 +41,18 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private val preferences = PreferenceManager.getDefaultSharedPreferences(application)
|
||||
private val fusedLocationClient = LocationServices.getFusedLocationProviderClient(application)
|
||||
|
||||
init {
|
||||
(application as LookingSatApp).appComponent.inject(this)
|
||||
}
|
||||
|
||||
private val _debugMessage = MutableLiveData("")
|
||||
val debugMessage: LiveData<String> = _debugMessage
|
||||
|
||||
var tleMainList = loadTwoLineElementFile().sortedWith(compareBy { it.name })
|
||||
|
||||
private val _tleSelectedMap = MutableLiveData<MutableMap<TLE, Boolean>>(mutableMapOf())
|
||||
val tleSelectedMap: LiveData<MutableMap<TLE, Boolean>> = _tleSelectedMap
|
||||
|
||||
private val _satPassPrefs = MutableLiveData<SatPassPrefs>(
|
||||
SatPassPrefs(
|
||||
preferences.getInt("hoursAhead", 8),
|
||||
@@ -61,13 +70,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
)
|
||||
val gsp: LiveData<GroundStationPosition> = _gsp
|
||||
|
||||
var tleMainList = loadTwoLineElementFile().sortedWith(compareBy { it.name })
|
||||
var tleSelectedMap = mutableMapOf<TLE, Boolean>()
|
||||
|
||||
init {
|
||||
(application as LookingSatApp).appComponent.inject(this)
|
||||
}
|
||||
|
||||
private fun loadTwoLineElementFile(): List<TLE> {
|
||||
return try {
|
||||
TLE.importSat(getApplication<Application>().openFileInput(tleFile))
|
||||
@@ -77,33 +79,30 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getPassesForSelectedSatellites(): List<SatPass> {
|
||||
suspend fun getPasses(satMap: Map<TLE, Boolean>, hours: Int, maxEl: Double): List<SatPass> {
|
||||
val satPassList = mutableListOf<SatPass>()
|
||||
var sortedList = listOf<SatPass>()
|
||||
val hoursAhead = satPassPrefs.value?.hoursAhead ?: 8
|
||||
withContext(Dispatchers.Default) {
|
||||
for ((tle, value) in tleSelectedMap) {
|
||||
satMap.forEach { (tle, value) ->
|
||||
if (value) {
|
||||
try {
|
||||
Log.d(tag, "Trying ${tle.name}")
|
||||
val passPredictor = PassPredictor(tle, gsp.value)
|
||||
val passes = passPredictor.getPasses(Date(), hoursAhead, false)
|
||||
for (pass in passes) {
|
||||
Log.d(tag, "Trying ${pass.maxEl}")
|
||||
satPassList.add(SatPass(tle.name, tle.catnum, pass))
|
||||
}
|
||||
val passes = passPredictor.getPasses(Date(), hours, false)
|
||||
passes.forEach { satPassList.add(SatPass(tle.name, tle.catnum, it)) }
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
val tleProblem = "There was a problem with TLE"
|
||||
Log.d(tag, tleProblem)
|
||||
_debugMessage.postValue(tleProblem)
|
||||
Log.d(tag, "There was a problem with TLE")
|
||||
} catch (exception: SatNotFoundException) {
|
||||
Log.d(tag, "Certain satellites shall not pass")
|
||||
}
|
||||
}
|
||||
}
|
||||
sortedList = satPassList.sortedWith(compareBy { it.pass.startTime })
|
||||
satPassList.retainAll { it.pass.maxEl >= maxEl }
|
||||
satPassList.sortBy { it.pass.startTime }
|
||||
}
|
||||
return sortedList
|
||||
return satPassList
|
||||
}
|
||||
|
||||
fun updateSelectedSatMap(mutableMap: MutableMap<TLE, Boolean>) {
|
||||
_tleSelectedMap.postValue(mutableMap)
|
||||
}
|
||||
|
||||
fun updatePassPrefs(hoursAhead: Int, maxEl: Double) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import android.view.ViewGroup
|
||||
import android.widget.*
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
@@ -17,6 +18,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
import com.rtbishop.lookingsat.MainViewModel
|
||||
import com.rtbishop.lookingsat.R
|
||||
import com.rtbishop.lookingsat.repo.SatPass
|
||||
import com.rtbishop.lookingsat.repo.SatPassPrefs
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.concurrent.TimeUnit
|
||||
@@ -32,7 +34,8 @@ class SkyFragment : Fragment() {
|
||||
private lateinit var btnPassPrefs: ImageButton
|
||||
private lateinit var progressBar: ProgressBar
|
||||
private lateinit var fab: FloatingActionButton
|
||||
|
||||
private lateinit var selectedSatMap: MutableMap<TLE, Boolean>
|
||||
private lateinit var satPassPrefs: SatPassPrefs
|
||||
private var aosTimer: CountDownTimer? = null
|
||||
|
||||
override fun onCreateView(
|
||||
@@ -55,6 +58,18 @@ class SkyFragment : Fragment() {
|
||||
btnPassPrefs.setOnClickListener { showSatPassPrefsDialog() }
|
||||
fab.setOnClickListener { showSelectSatDialog() }
|
||||
|
||||
satPassPrefs = viewModel.satPassPrefs.value ?: SatPassPrefs(8, 20.0)
|
||||
|
||||
viewModel.tleSelectedMap.observe(this, Observer {
|
||||
selectedSatMap = it
|
||||
calculatePasses()
|
||||
})
|
||||
|
||||
viewModel.satPassPrefs.observe(this, Observer {
|
||||
satPassPrefs = it
|
||||
calculatePasses()
|
||||
})
|
||||
|
||||
resetTimer()
|
||||
}
|
||||
|
||||
@@ -82,9 +97,8 @@ class SkyFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun showSelectSatDialog() {
|
||||
val tleSelectedMap = viewModel.tleSelectedMap
|
||||
val tleSelectedMap = selectedSatMap
|
||||
val tleMainListSize = viewModel.tleMainList.size
|
||||
|
||||
val tleNameArray = arrayOfNulls<String>(tleMainListSize)
|
||||
val tleCheckedArray = BooleanArray(tleMainListSize)
|
||||
|
||||
@@ -96,7 +110,7 @@ class SkyFragment : Fragment() {
|
||||
} else {
|
||||
for ((position, tle) in viewModel.tleMainList.withIndex()) {
|
||||
tleNameArray[position] = tle.name
|
||||
tleCheckedArray[position] = viewModel.tleSelectedMap.getOrDefault(tle, false)
|
||||
tleCheckedArray[position] = tleSelectedMap.getOrDefault(tle, false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +123,7 @@ class SkyFragment : Fragment() {
|
||||
.setPositiveButton(getString(R.string.btn_ok)) { _, _ ->
|
||||
for ((tle, value) in selectionMap) {
|
||||
tleSelectedMap[tle] = value
|
||||
calculatePasses()
|
||||
viewModel.updateSelectedSatMap(tleSelectedMap)
|
||||
}
|
||||
}
|
||||
.setNegativeButton(getString(R.string.btn_cancel)) { dialog, _ ->
|
||||
@@ -117,19 +131,21 @@ class SkyFragment : Fragment() {
|
||||
}
|
||||
.setNeutralButton(getString(R.string.btn_clear)) { _, _ ->
|
||||
tleSelectedMap.clear()
|
||||
calculatePasses()
|
||||
viewModel.updateSelectedSatMap(tleSelectedMap)
|
||||
}
|
||||
.create()
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun calculatePasses() {
|
||||
val hoursAhead = satPassPrefs.hoursAhead
|
||||
val maxEl = satPassPrefs.maxEl
|
||||
var satPassList: List<SatPass>
|
||||
lifecycleScope.launch(Dispatchers.Main) {
|
||||
recViewFuture.visibility = View.INVISIBLE
|
||||
progressBar.visibility = View.VISIBLE
|
||||
progressBar.isIndeterminate = true
|
||||
satPassList = viewModel.getPassesForSelectedSatellites()
|
||||
satPassList = viewModel.getPasses(selectedSatMap, hoursAhead, maxEl)
|
||||
recAdapterFuture = SatPassAdapter(satPassList)
|
||||
recViewFuture.adapter = recAdapterFuture
|
||||
progressBar.isIndeterminate = false
|
||||
|
||||
Reference in new issue
Block a user