diff --git a/app/build.gradle b/app/build.gradle index 49da233e..b29c5d9c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { buildToolsVersion "29.0.2" defaultConfig { applicationId "com.rtbishop.lookingsat" - minSdkVersion 22 + minSdkVersion 24 targetSdkVersion 29 versionCode 1 versionName "0.2" diff --git a/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt b/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt index eea85751..2ede8948 100644 --- a/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt +++ b/app/src/main/java/com/rtbishop/lookingsat/ui/SkyFragment.kt @@ -55,7 +55,7 @@ class SkyFragment : Fragment() { recView.visibility = View.INVISIBLE progressBar.visibility = View.VISIBLE progressBar.isIndeterminate = true - satPassList = viewModel.updateRecycler() + satPassList = viewModel.getPassesForSelectedSatellites() recAdapter = SatPassAdapter(satPassList) recView.adapter = recAdapter progressBar.isIndeterminate = false @@ -67,41 +67,43 @@ class SkyFragment : Fragment() { fab.setOnClickListener { showSelectSatDialog() } } - private var selectedSatList: List = emptyList() - private fun showSelectSatDialog() { - val tleList = mutableListOf() - val listSize = viewModel.tleList.size - val satList = arrayOfNulls(listSize) - val checkedSatList = BooleanArray(listSize) - if (selectedSatList.isEmpty()) { - for ((position, tle) in viewModel.tleList.withIndex()) { - satList[position] = tle.name - checkedSatList[position] = false + val tleSelectedMap = viewModel.tleSelectedMap + val tleMainListSize = viewModel.tleMainList.size + + val tleNameArray = arrayOfNulls(tleMainListSize) + val tleCheckedArray = BooleanArray(tleMainListSize) + + if (tleSelectedMap.isEmpty()) { + 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() val builder = AlertDialog.Builder(activity as MainActivity) builder.setTitle("Select satellites to track") - - builder.setMultiChoiceItems(satList, checkedSatList) { _, which, isChecked -> - checkedSatList[which] = isChecked - } - - builder.setPositiveButton("Select") { _, _ -> - for (index in checkedSatList.indices) { - val checked = checkedSatList[index] - if (checked) { - tleList.add(viewModel.tleList[index]) + .setMultiChoiceItems(tleNameArray, tleCheckedArray) { _, which, isChecked -> + selectionMap[viewModel.tleMainList[which]] = isChecked + } + .setPositiveButton("Ok") { _, _ -> + for ((tle, value) in selectionMap) { + tleSelectedMap[tle] = value } } - } - - builder.setNegativeButton("Cancel") { dialog, _ -> - dialog.cancel() - } - - val dialog = builder.create() - dialog.show() + .setNeutralButton("Clear All") { _, _ -> + tleSelectedMap.clear() + } + .setNegativeButton("Cancel") { dialog, _ -> + dialog.cancel() + } + .create() + .show() } } \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt b/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt index cde6e41e..8d9ae206 100644 --- a/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt +++ b/app/src/main/java/com/rtbishop/lookingsat/vm/MainViewModel.kt @@ -50,7 +50,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { ) val gsp: LiveData = _gsp - var tleList: List = loadTwoLineElementFile().sortedWith(compareBy { it.name }) + var tleMainList = loadTwoLineElementFile().sortedWith(compareBy { it.name }) + var tleSelectedMap = mutableMapOf() private fun loadTwoLineElementFile(): List { return try { @@ -61,25 +62,27 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } } - suspend fun updateRecycler(): List { + suspend fun getPassesForSelectedSatellites(): List { val satPassList = mutableListOf() var sortedList = listOf() withContext(Dispatchers.Default) { - for (tle in tleList.subList(0, 1)) { - try { - Log.d(tag, "Trying ${tle.name}") - val passPredictor = PassPredictor(tle, gsp.value) - val passes = passPredictor.getPasses(Date(), 6, false) - for (pass in passes) { - Log.d(tag, "Trying ${pass.maxEl}") - satPassList.add(SatPass(tle.name, tle.catnum, pass)) + for ((tle, value) in tleSelectedMap) { + if (value) { + try { + Log.d(tag, "Trying ${tle.name}") + val passPredictor = PassPredictor(tle, gsp.value) + val passes = passPredictor.getPasses(Date(), 6, true) + for (pass in passes) { + Log.d(tag, "Trying ${pass.maxEl}") + satPassList.add(SatPass(tle.name, tle.catnum, pass)) + } + } catch (exception: IllegalArgumentException) { + val tleProblem = "There was a problem with TLE" + Log.d(tag, tleProblem) + _debugMessage.postValue(tleProblem) + } catch (exception: SatNotFoundException) { + Log.d(tag, "Certain satellites shall not pass") } - } catch (exception: IllegalArgumentException) { - val tleProblem = "There was a problem with TLE" - Log.d(tag, tleProblem) - _debugMessage.postValue(tleProblem) - } catch (exception: SatNotFoundException) { - Log.d(tag, "Certain satellites shall not pass") } } sortedList = satPassList.sortedWith(compareBy { it.pass.startTime }) @@ -111,7 +114,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { getApplication().openFileOutput(tleFile, Context.MODE_PRIVATE).use { it.write(stream.readBytes()) } - tleList = TLE.importSat(stream) + tleMainList = TLE.importSat(stream) _debugMessage.postValue("TLE file was updated") } catch (exception: IOException) { _debugMessage.postValue("Couldn't update TLE file")