Fixed SourcesDialog behaviour, code cleanup

This commit is contained in:
Arty Bishop committed 2020-10-06 23:24:17 +01:00
1 parent f89a462216
commit e38ee6384e
3 files changed
+52 -86

No files matched your search

@@ -31,6 +31,15 @@ import com.rtbishop.look4sat.databinding.ItemTleSourceBinding
class SourcesAdapter(private var sources: MutableList<TleSource>) :
RecyclerView.Adapter<SourcesAdapter.TleSourceHolder>() {
fun getSources(): MutableList<TleSource> {
return sources
}
fun setSources(list: MutableList<TleSource>) {
sources = list
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TleSourceHolder {
val binding = ItemTleSourceBinding
.inflate(LayoutInflater.from(parent.context), parent, false)
@@ -46,19 +55,8 @@ class SourcesAdapter(private var sources: MutableList<TleSource>) :
return sources.size
}
fun getSources(): MutableList<TleSource> {
return sources
}
fun setSources(list: MutableList<TleSource>) {
sources = list
notifyDataSetChanged()
}
inner class TleSourceHolder(
itemView: View,
val binding: ItemTleSourceBinding,
val textWatcher: TleSourceWatcher
itemView: View, val binding: ItemTleSourceBinding, val textWatcher: TleSourceWatcher
) :
RecyclerView.ViewHolder(itemView) {
@@ -20,8 +20,7 @@ import com.rtbishop.look4sat.ui.SharedViewModel
import com.rtbishop.look4sat.ui.adapters.EntriesAdapter
import javax.inject.Inject
class EntriesFragment : Fragment(R.layout.fragment_entries),
SourcesDialog.SourcesSubmitListener {
class EntriesFragment : Fragment(R.layout.fragment_entries) {
@Inject
lateinit var factory: ViewModelFactory
@@ -37,16 +36,12 @@ class EntriesFragment : Fragment(R.layout.fragment_entries),
binding = FragmentEntriesBinding.bind(view)
(requireActivity().application as Look4SatApp).appComponent.inject(this)
viewModel = ViewModelProvider(requireActivity(), factory).get(SharedViewModel::class.java)
setupUI()
setupComponents()
setupObservers()
}
private fun setupUI() {
private fun setupComponents() {
binding.apply {
importWeb.setOnClickListener { showSourcesDialog() }
importFile.setOnClickListener { showFileDialog() }
searchBar.setOnQueryTextListener(entriesAdapter)
selectAll.setOnClickListener { entriesAdapter.selectAll() }
entriesRecycler.apply {
val linearLayoutMgr = LinearLayoutManager(requireContext())
val divider = DividerItemDecoration(requireContext(), linearLayoutMgr.orientation)
@@ -57,36 +52,33 @@ class EntriesFragment : Fragment(R.layout.fragment_entries),
adapter = entriesAdapter
addItemDecoration(divider)
}
importWeb.setOnClickListener { showImportFromWebDialog() }
importFile.setOnClickListener { showImportFromFileDialog() }
searchBar.setOnQueryTextListener(entriesAdapter)
selectAll.setOnClickListener { entriesAdapter.selectAll() }
entriesFab.setOnClickListener { goToPassesAndCalculateForSelection() }
}
}
private fun setupObservers() {
viewModel.tleSources.observe(viewLifecycleOwner, { sources ->
tleSources = sources
})
viewModel.allEntries.observe(viewLifecycleOwner, { entries ->
entriesAdapter.setEntries(entries as MutableList<SatEntry>)
viewModel.tleSources.observe(viewLifecycleOwner, { tleSources = it })
viewModel.allEntries.observe(viewLifecycleOwner, {
entriesAdapter.setEntries(it as MutableList<SatEntry>)
})
}
// Entries update from web
private fun showSourcesDialog() {
val fragmentManager = childFragmentManager
SourcesDialog(tleSources).apply {
setSourcesListener(this@EntriesFragment)
show(fragmentManager, "TleSourcesDialog")
}
private fun goToPassesAndCalculateForSelection() {
val catNumList = mutableListOf<Int>()
entriesAdapter.getEntries().forEach { if (it.isSelected) catNumList.add(it.catNum) }
viewModel.updateEntriesSelection(catNumList)
requireView().findNavController().navigate(R.id.action_entries_to_passes)
}
override fun onSourcesSubmit(list: List<TleSource>) {
viewModel.updateSatelliteData(list)
private fun showImportFromWebDialog() {
SourcesDialog(tleSources, viewModel).show(childFragmentManager, "SourcesDialog")
}
// Entries update from file
private fun showFileDialog() {
private fun showImportFromFileDialog() {
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
@@ -99,14 +91,4 @@ class EntriesFragment : Fragment(R.layout.fragment_entries),
data?.data?.also { uri -> viewModel.updateEntriesFromFile(uri) }
} else super.onActivityResult(requestCode, resultCode, data)
}
// Search and select entries
private fun goToPassesAndCalculateForSelection() {
val catNumList = mutableListOf<Int>().apply {
entriesAdapter.getEntries().forEach { if (it.isSelected) this.add(it.catNum) }
}
viewModel.updateEntriesSelection(catNumList)
requireView().findNavController().navigate(R.id.action_entries_to_passes)
}
}
@@ -22,58 +22,44 @@ package com.rtbishop.look4sat.ui.fragments
import android.app.Dialog
import android.os.Bundle
import android.view.ViewGroup
import android.view.Window
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.databinding.DialogSourcesBinding
import com.rtbishop.look4sat.ui.SharedViewModel
import com.rtbishop.look4sat.ui.adapters.SourcesAdapter
class SourcesDialog(sources: List<TleSource>) : AppCompatDialogFragment() {
class SourcesDialog(sources: List<TleSource>, private val viewModel: SharedViewModel) :
AppCompatDialogFragment() {
private lateinit var sourcesListener: SourcesSubmitListener
private val sourcesAdapter = SourcesAdapter(sources.toMutableList())
private val sourcesAdapter = SourcesAdapter(sources as MutableList<TleSource>)
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogSourcesBinding.inflate(requireActivity().layoutInflater)
val tleSourcesDialog = Dialog(requireActivity()).apply {
window?.requestFeature(Window.FEATURE_NO_TITLE)
val binding = DialogSourcesBinding.inflate(requireActivity().layoutInflater).apply {
tleSourcesRecycler.apply {
layoutManager = LinearLayoutManager(requireContext())
adapter = sourcesAdapter
}
tleSourceBtnAdd.setOnClickListener {
val tempSources = sourcesAdapter.getSources()
tempSources.add(TleSource(String()))
sourcesAdapter.setSources(tempSources)
}
tleSourcesBtnPos.setOnClickListener {
val filteredSources = sourcesAdapter.getSources()
.filter { it.url != String() && it.url != " " && it.url.contains("https://") }
viewModel.updateSatelliteData(filteredSources)
dismiss()
}
tleSourcesBtnNeg.setOnClickListener { dismiss() }
}
return Dialog(requireActivity()).apply {
setContentView(binding.root)
window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
}
binding.tleSourcesRecycler.apply {
layoutManager = LinearLayoutManager(activity)
adapter = sourcesAdapter
}
binding.tleSourceBtnAdd.setOnClickListener {
val tempSources = sourcesAdapter.getSources()
tempSources.add(TleSource(""))
sourcesAdapter.setSources(tempSources)
sourcesAdapter.notifyDataSetChanged()
}
binding.tleSourcesBtnNeg.setOnClickListener { dismiss() }
binding.tleSourcesBtnPos.setOnClickListener {
val filteredSources = sourcesAdapter.getSources()
.filter { it.url != String() && it.url != " " && it.url.contains("https://") }
sourcesListener.onSourcesSubmit(filteredSources)
dismiss()
}
return tleSourcesDialog
}
fun setSourcesListener(listener: SourcesSubmitListener): SourcesDialog {
sourcesListener = listener
return this
}
interface SourcesSubmitListener {
fun onSourcesSubmit(list: List<TleSource>)
}
}