Main Tle list in now LiveData. Changes to the ground station position now lead to passes recalculation. Minor refactoring.

pull/30/head
Arty Bishop 2019-11-24 22:49:27 +00:00
rodzic 35b4215c72
commit e44379f43e
2 zmienionych plików z 33 dodań i 17 usunięć

Wyświetl plik

@ -48,7 +48,17 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
private val _debugMessage = MutableLiveData("")
val debugMessage: LiveData<String> = _debugMessage
var tleMainList = loadTwoLineElementFile().sortedWith(compareBy { it.name })
private val _tleMainList = MutableLiveData<List<TLE>>(
try {
TLE.importSat(getApplication<Application>().openFileInput(tleFile))
.sortedWith(compareBy { it.name })
} catch (exception: FileNotFoundException) {
_debugMessage.postValue("TLE file wasn't found")
emptyList<TLE>()
}
)
val tleMainList: LiveData<List<TLE>> = _tleMainList
private val _tleSelectedMap = MutableLiveData<MutableMap<TLE, Boolean>>(mutableMapOf())
val tleSelectedMap: LiveData<MutableMap<TLE, Boolean>> = _tleSelectedMap
@ -70,15 +80,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
)
val gsp: LiveData<GroundStationPosition> = _gsp
private fun loadTwoLineElementFile(): List<TLE> {
return try {
TLE.importSat(getApplication<Application>().openFileInput(tleFile))
} catch (exception: FileNotFoundException) {
_debugMessage.postValue("TLE file wasn't found")
emptyList()
}
}
suspend fun getPasses(satMap: Map<TLE, Boolean>, hours: Int, maxEl: Double): List<SatPass> {
val satPassList = mutableListOf<SatPass>()
withContext(Dispatchers.Default) {
@ -138,7 +139,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
getApplication<Application>().openFileOutput(tleFile, Context.MODE_PRIVATE).use {
it.write(stream.readBytes())
}
tleMainList = TLE.importSat(stream)
_tleMainList.postValue(TLE.importSat(stream))
_debugMessage.postValue("TLE file was updated")
} catch (exception: IOException) {
_debugMessage.postValue("Couldn't update TLE file")

Wyświetl plik

@ -34,6 +34,7 @@ class SkyFragment : Fragment() {
private lateinit var btnPassPrefs: ImageButton
private lateinit var progressBar: ProgressBar
private lateinit var fab: FloatingActionButton
private lateinit var tleMainList: List<TLE>
private lateinit var selectedSatMap: MutableMap<TLE, Boolean>
private lateinit var satPassPrefs: SatPassPrefs
private var aosTimer: CountDownTimer? = null
@ -46,6 +47,12 @@ class SkyFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupViews(view)
setupObservers()
resetTimer()
}
private fun setupViews(view: View) {
viewModel = ViewModelProvider(activity as MainActivity).get(MainViewModel::class.java)
timeToAos = (activity as MainActivity).findViewById(R.id.toolbar_time_to_aos)
btnPassPrefs = (activity as MainActivity).findViewById(R.id.toolbar_btn_refresh)
@ -59,6 +66,12 @@ class SkyFragment : Fragment() {
fab.setOnClickListener { showSelectSatDialog() }
satPassPrefs = viewModel.satPassPrefs.value ?: SatPassPrefs(8, 20.0)
}
private fun setupObservers() {
viewModel.tleMainList.observe(this, Observer {
tleMainList = it
})
viewModel.tleSelectedMap.observe(this, Observer {
selectedSatMap = it
@ -70,7 +83,9 @@ class SkyFragment : Fragment() {
calculatePasses()
})
resetTimer()
viewModel.gsp.observe(this, Observer {
calculatePasses()
})
}
private fun showSatPassPrefsDialog() {
@ -97,18 +112,19 @@ class SkyFragment : Fragment() {
}
private fun showSelectSatDialog() {
val tleList = tleMainList
val tleSelectedMap = selectedSatMap
val tleMainListSize = viewModel.tleMainList.size
val tleMainListSize = tleList.size
val tleNameArray = arrayOfNulls<String>(tleMainListSize)
val tleCheckedArray = BooleanArray(tleMainListSize)
if (tleSelectedMap.isEmpty()) {
for ((position, tle) in viewModel.tleMainList.withIndex()) {
tleList.withIndex().forEach { (position, tle) ->
tleNameArray[position] = tle.name
tleCheckedArray[position] = false
}
} else {
for ((position, tle) in viewModel.tleMainList.withIndex()) {
tleList.withIndex().forEach { (position, tle) ->
tleNameArray[position] = tle.name
tleCheckedArray[position] = tleSelectedMap.getOrDefault(tle, false)
}
@ -118,7 +134,7 @@ class SkyFragment : Fragment() {
val builder = AlertDialog.Builder(activity as MainActivity)
builder.setTitle(getString(R.string.title_select_sat))
.setMultiChoiceItems(tleNameArray, tleCheckedArray) { _, which, isChecked ->
selectionMap[viewModel.tleMainList[which]] = isChecked
selectionMap[tleList[which]] = isChecked
}
.setPositiveButton(getString(R.string.btn_ok)) { _, _ ->
for ((tle, value) in selectionMap) {
@ -166,7 +182,6 @@ class SkyFragment : Fragment() {
override fun onFinish() {
Toast.makeText(activity, "Time is up!", Toast.LENGTH_SHORT).show()
this.cancel()
resetTimer()
}
override fun onTick(millisUntilFinished: Long) {