Added dialog for satellite selection. Showing only selected passes in the recycler.

This commit is contained in:
Arty Bishop committed 2019-11-08 19:49:46 +01:00
1 parent 09c6353c33
commit bfc0561c34
3 files changed
+36 -31

No files matched your search

+1 -1
View File
@@ -8,7 +8,7 @@ android {
buildToolsVersion "29.0.2" buildToolsVersion "29.0.2"
defaultConfig { defaultConfig {
applicationId "com.rtbishop.lookingsat" applicationId "com.rtbishop.lookingsat"
minSdkVersion 22 minSdkVersion 24
targetSdkVersion 29 targetSdkVersion 29
versionCode 1 versionCode 1
versionName "0.2" versionName "0.2"
@@ -55,7 +55,7 @@ class SkyFragment : Fragment() {
recView.visibility = View.INVISIBLE recView.visibility = View.INVISIBLE
progressBar.visibility = View.VISIBLE progressBar.visibility = View.VISIBLE
progressBar.isIndeterminate = true progressBar.isIndeterminate = true
satPassList = viewModel.updateRecycler() satPassList = viewModel.getPassesForSelectedSatellites()
recAdapter = SatPassAdapter(satPassList) recAdapter = SatPassAdapter(satPassList)
recView.adapter = recAdapter recView.adapter = recAdapter
progressBar.isIndeterminate = false progressBar.isIndeterminate = false
@@ -67,41 +67,43 @@ class SkyFragment : Fragment() {
fab.setOnClickListener { showSelectSatDialog() } fab.setOnClickListener { showSelectSatDialog() }
} }
private var selectedSatList: List<TLE> = emptyList()
private fun showSelectSatDialog() { private fun showSelectSatDialog() {
val tleList = mutableListOf<TLE>() val tleSelectedMap = viewModel.tleSelectedMap
val listSize = viewModel.tleList.size val tleMainListSize = viewModel.tleMainList.size
val satList = arrayOfNulls<String>(listSize)
val checkedSatList = BooleanArray(listSize) val tleNameArray = arrayOfNulls<String>(tleMainListSize)
if (selectedSatList.isEmpty()) { val tleCheckedArray = BooleanArray(tleMainListSize)
for ((position, tle) in viewModel.tleList.withIndex()) {
satList[position] = tle.name if (tleSelectedMap.isEmpty()) {
checkedSatList[position] = false for ((position, tle) in viewModel.tleMainList.withIndex()) {
tleNameArray[position] = tle.name
tleCheckedArray[position] = false
}
} else {
for ((position, tle) in viewModel.tleMainList.withIndex()) {
tleNameArray[position] = tle.name
tleCheckedArray[position] = viewModel.tleSelectedMap.getOrDefault(tle, false)
} }
} }
val selectionMap = mutableMapOf<TLE, Boolean>()
val builder = AlertDialog.Builder(activity as MainActivity) val builder = AlertDialog.Builder(activity as MainActivity)
builder.setTitle("Select satellites to track") builder.setTitle("Select satellites to track")
.setMultiChoiceItems(tleNameArray, tleCheckedArray) { _, which, isChecked ->
builder.setMultiChoiceItems(satList, checkedSatList) { _, which, isChecked -> selectionMap[viewModel.tleMainList[which]] = isChecked
checkedSatList[which] = isChecked
} }
.setPositiveButton("Ok") { _, _ ->
builder.setPositiveButton("Select") { _, _ -> for ((tle, value) in selectionMap) {
for (index in checkedSatList.indices) { tleSelectedMap[tle] = value
val checked = checkedSatList[index]
if (checked) {
tleList.add(viewModel.tleList[index])
} }
} }
.setNeutralButton("Clear All") { _, _ ->
tleSelectedMap.clear()
} }
.setNegativeButton("Cancel") { dialog, _ ->
builder.setNegativeButton("Cancel") { dialog, _ ->
dialog.cancel() dialog.cancel()
} }
.create()
val dialog = builder.create() .show()
dialog.show()
} }
} }
@@ -50,7 +50,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
) )
val gsp: LiveData<GroundStationPosition> = _gsp val gsp: LiveData<GroundStationPosition> = _gsp
var tleList: List<TLE> = loadTwoLineElementFile().sortedWith(compareBy { it.name }) var tleMainList = loadTwoLineElementFile().sortedWith(compareBy { it.name })
var tleSelectedMap = mutableMapOf<TLE, Boolean>()
private fun loadTwoLineElementFile(): List<TLE> { private fun loadTwoLineElementFile(): List<TLE> {
return try { return try {
@@ -61,15 +62,16 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
} }
} }
suspend fun updateRecycler(): List<SatPass> { suspend fun getPassesForSelectedSatellites(): List<SatPass> {
val satPassList = mutableListOf<SatPass>() val satPassList = mutableListOf<SatPass>()
var sortedList = listOf<SatPass>() var sortedList = listOf<SatPass>()
withContext(Dispatchers.Default) { withContext(Dispatchers.Default) {
for (tle in tleList.subList(0, 1)) { for ((tle, value) in tleSelectedMap) {
if (value) {
try { try {
Log.d(tag, "Trying ${tle.name}") Log.d(tag, "Trying ${tle.name}")
val passPredictor = PassPredictor(tle, gsp.value) val passPredictor = PassPredictor(tle, gsp.value)
val passes = passPredictor.getPasses(Date(), 6, false) val passes = passPredictor.getPasses(Date(), 6, true)
for (pass in passes) { for (pass in passes) {
Log.d(tag, "Trying ${pass.maxEl}") Log.d(tag, "Trying ${pass.maxEl}")
satPassList.add(SatPass(tle.name, tle.catnum, pass)) satPassList.add(SatPass(tle.name, tle.catnum, pass))
@@ -82,6 +84,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
Log.d(tag, "Certain satellites shall not pass") Log.d(tag, "Certain satellites shall not pass")
} }
} }
}
sortedList = satPassList.sortedWith(compareBy { it.pass.startTime }) sortedList = satPassList.sortedWith(compareBy { it.pass.startTime })
} }
return sortedList return sortedList
@@ -111,7 +114,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
getApplication<Application>().openFileOutput(tleFile, Context.MODE_PRIVATE).use { getApplication<Application>().openFileOutput(tleFile, Context.MODE_PRIVATE).use {
it.write(stream.readBytes()) it.write(stream.readBytes())
} }
tleList = TLE.importSat(stream) tleMainList = TLE.importSat(stream)
_debugMessage.postValue("TLE file was updated") _debugMessage.postValue("TLE file was updated")
} catch (exception: IOException) { } catch (exception: IOException) {
_debugMessage.postValue("Couldn't update TLE file") _debugMessage.postValue("Couldn't update TLE file")