Added SatEntry dialog for satellite selection with search capabilities
This commit is contained in:
1 parent
f9039d670f
commit
df314da3ea
3 files changed
+167
-40
No files matched your search
@@ -41,6 +41,7 @@ import com.github.amsacode.predict4java.TLE
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
import com.rtbishop.look4sat.MainViewModel
|
||||
import com.rtbishop.look4sat.R
|
||||
import com.rtbishop.look4sat.repo.SatEntry
|
||||
import com.rtbishop.look4sat.repo.SatPass
|
||||
import com.rtbishop.look4sat.ui.adapters.SatPassAdapter
|
||||
import java.util.*
|
||||
@@ -185,37 +186,30 @@ class PassListFragment : Fragment() {
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun showSelectSatDialog(tleMainList: List<TLE>, selectionList: MutableList<Int>) {
|
||||
private fun showSelectSatDialog(tleMainList: List<TLE>, selection: MutableList<Int>) {
|
||||
if (tleMainList.isEmpty()) {
|
||||
Toast.makeText(mainActivity, getString(R.string.err_update_tle), Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
} else {
|
||||
val tleNameArray = arrayOfNulls<String>(tleMainList.size).apply {
|
||||
tleMainList.withIndex().forEach { (position, tle) -> this[position] = tle.name }
|
||||
}
|
||||
val tleCheckedArray = BooleanArray(tleMainList.size).apply {
|
||||
selectionList.forEach { this[it] = true }
|
||||
}
|
||||
val builder = AlertDialog.Builder(mainActivity)
|
||||
builder.setTitle(getString(R.string.dialog_select_sat))
|
||||
.setMultiChoiceItems(tleNameArray, tleCheckedArray) { _, which, isChecked ->
|
||||
if (isChecked) selectionList.add(which)
|
||||
else if (selectionList.contains(which)) selectionList.remove(which)
|
||||
val entriesList = mutableListOf<SatEntry>().apply {
|
||||
tleMainList.withIndex().forEach {
|
||||
this.add(SatEntry(it.index, it.value.name))
|
||||
}
|
||||
.setPositiveButton(getString(R.string.btn_ok)) { _, _ ->
|
||||
viewModel.updateAndSaveSelectionList(selectionList)
|
||||
}
|
||||
|
||||
val listener = object : SatEntryDialog.EntriesSubmitListener {
|
||||
override fun onEntriesSubmit(list: MutableList<Int>) {
|
||||
viewModel.updateAndSaveSelectionList(list)
|
||||
calculatePasses()
|
||||
}
|
||||
.setNegativeButton(getString(R.string.btn_cancel)) { dialog, _ ->
|
||||
dialog.cancel()
|
||||
}
|
||||
.setNeutralButton(getString(R.string.btn_clear)) { _, _ ->
|
||||
selectionList.clear()
|
||||
viewModel.updateAndSaveSelectionList(selectionList)
|
||||
calculatePasses()
|
||||
}
|
||||
.create()
|
||||
.show()
|
||||
}
|
||||
|
||||
val dialogFragment = SatEntryDialog().apply {
|
||||
setEntriesList(entriesList)
|
||||
setSelectionList(selection)
|
||||
setEntriesListener(listener)
|
||||
}
|
||||
dialogFragment.show(mainActivity.supportFragmentManager, "SatEntryDialog")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.rtbishop.look4sat.ui
|
||||
|
||||
import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import android.view.ViewGroup
|
||||
import android.view.Window
|
||||
import android.widget.SearchView
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatDialogFragment
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.rtbishop.look4sat.R
|
||||
import com.rtbishop.look4sat.repo.SatEntry
|
||||
import com.rtbishop.look4sat.ui.adapters.SatEntryAdapter
|
||||
import java.util.*
|
||||
|
||||
class SatEntryDialog : AppCompatDialogFragment(), SearchView.OnQueryTextListener {
|
||||
|
||||
private lateinit var searchView: SearchView
|
||||
private lateinit var recyclerView: RecyclerView
|
||||
private lateinit var dialogBtnPositive: TextView
|
||||
private lateinit var dialogBtnNegative: TextView
|
||||
private lateinit var dialogBtnNeutral: TextView
|
||||
private lateinit var satEntryAdapter: SatEntryAdapter
|
||||
private lateinit var entriesListener: EntriesSubmitListener
|
||||
|
||||
private var entriesList = mutableListOf<SatEntry>()
|
||||
private var selectionList = mutableListOf<Int>()
|
||||
private var selectAllToggle = true
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
val satEntryDialog = Dialog(requireActivity()).apply {
|
||||
window?.requestFeature(Window.FEATURE_NO_TITLE)
|
||||
setContentView(R.layout.dialog_sat_entry)
|
||||
window?.setLayout(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT
|
||||
)
|
||||
}
|
||||
|
||||
entriesList = checkSelectedEntries(entriesList, selectionList)
|
||||
satEntryAdapter = SatEntryAdapter(entriesList)
|
||||
|
||||
findViews(satEntryDialog)
|
||||
setListeners()
|
||||
|
||||
searchView.apply {
|
||||
setOnQueryTextListener(this@SatEntryDialog)
|
||||
onActionViewExpanded()
|
||||
clearFocus()
|
||||
}
|
||||
|
||||
recyclerView.apply {
|
||||
layoutManager = LinearLayoutManager(activity)
|
||||
adapter = satEntryAdapter
|
||||
}
|
||||
|
||||
return satEntryDialog
|
||||
}
|
||||
|
||||
fun setEntriesList(list: MutableList<SatEntry>): SatEntryDialog {
|
||||
entriesList = list
|
||||
return this
|
||||
}
|
||||
|
||||
fun setSelectionList(list: MutableList<Int>): SatEntryDialog {
|
||||
selectAllToggle = list.isEmpty()
|
||||
selectionList = list
|
||||
return this
|
||||
}
|
||||
|
||||
fun setEntriesListener(listener: EntriesSubmitListener): SatEntryDialog {
|
||||
entriesListener = listener
|
||||
return this
|
||||
}
|
||||
|
||||
private fun findViews(dialog: Dialog) {
|
||||
searchView = dialog.findViewById(R.id.dialog_search)
|
||||
recyclerView = dialog.findViewById(R.id.dialog_recycler)
|
||||
dialogBtnPositive = dialog.findViewById(R.id.dialog_btn_positive)
|
||||
dialogBtnNegative = dialog.findViewById(R.id.dialog_btn_negative)
|
||||
dialogBtnNeutral = dialog.findViewById(R.id.dialog_btn_neutral)
|
||||
}
|
||||
|
||||
private fun setListeners() {
|
||||
dialogBtnPositive.setOnClickListener { onPositiveClicked() }
|
||||
dialogBtnNegative.setOnClickListener { onNegativeClicked() }
|
||||
dialogBtnNeutral.setOnClickListener { onNeutralClicked() }
|
||||
}
|
||||
|
||||
private fun checkSelectedEntries(entries: MutableList<SatEntry>, selection: MutableList<Int>)
|
||||
: MutableList<SatEntry> {
|
||||
selection.forEach { entries[it].isSelected = true }
|
||||
return entries
|
||||
}
|
||||
|
||||
private fun filterEntries(list: MutableList<SatEntry>, query: String): MutableList<SatEntry> {
|
||||
val searchQuery = query.toLowerCase(Locale.getDefault())
|
||||
if ((searchQuery == "") or searchQuery.isEmpty()) return list
|
||||
|
||||
val filteredList = mutableListOf<SatEntry>()
|
||||
list.forEach {
|
||||
val name = it.name.toLowerCase(Locale.getDefault())
|
||||
if (name.contains(searchQuery)) filteredList.add(it)
|
||||
}
|
||||
return filteredList
|
||||
}
|
||||
|
||||
private fun onPositiveClicked() {
|
||||
val listToSubmit = mutableListOf<Int>().apply {
|
||||
entriesList.forEach { if (it.isSelected) this.add(it.id) }
|
||||
}
|
||||
entriesListener.onEntriesSubmit(listToSubmit)
|
||||
dismiss()
|
||||
}
|
||||
|
||||
private fun onNegativeClicked() {
|
||||
dismiss()
|
||||
}
|
||||
|
||||
private fun onNeutralClicked() {
|
||||
selectAllToggle = if (selectAllToggle) {
|
||||
entriesList.forEach { it.isSelected = true }
|
||||
satEntryAdapter.setEntries(entriesList)
|
||||
false
|
||||
} else {
|
||||
entriesList.forEach { it.isSelected = false }
|
||||
satEntryAdapter.setEntries(entriesList)
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
override fun onQueryTextChange(newText: String): Boolean {
|
||||
entriesList = checkSelectedEntries(entriesList, selectionList)
|
||||
val filteredList = filterEntries(entriesList, newText)
|
||||
satEntryAdapter.setEntries(filteredList)
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onQueryTextSubmit(query: String): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
interface EntriesSubmitListener {
|
||||
fun onEntriesSubmit(list: MutableList<Int>)
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import android.widget.CheckBox
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.rtbishop.look4sat.R
|
||||
import com.rtbishop.look4sat.repo.SatEntry
|
||||
import com.rtbishop.look4sat.ui.SatEntryDialog
|
||||
|
||||
internal class SatEntryAdapter(private var entriesList: MutableList<SatEntry>) :
|
||||
RecyclerView.Adapter<SatEntryAdapter.SatEntryHolder>() {
|
||||
@@ -37,21 +36,8 @@ internal class SatEntryAdapter(private var entriesList: MutableList<SatEntry>) :
|
||||
|
||||
fun bind(satEntry: SatEntry) {
|
||||
satEntryItem.text = satEntry.name
|
||||
satEntryItem.isChecked = SatEntryDialog.tempSelectionList.contains(satEntry.id)
|
||||
|
||||
itemView.setOnClickListener {
|
||||
if (satEntryItem.isChecked) {
|
||||
if (!SatEntryDialog.tempSelectionList.contains(satEntry.id)) {
|
||||
SatEntryDialog.tempSelectionList.add(satEntry.id)
|
||||
satEntry.isSelected = true
|
||||
}
|
||||
} else {
|
||||
if (SatEntryDialog.tempSelectionList.contains(satEntry.id)) {
|
||||
SatEntryDialog.tempSelectionList.remove(satEntry.id)
|
||||
satEntry.isSelected = false
|
||||
}
|
||||
}
|
||||
}
|
||||
satEntryItem.isChecked = satEntry.isSelected
|
||||
itemView.setOnClickListener { satEntry.isSelected = satEntryItem.isChecked }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user