mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-03 19:27:24 +00:00
Various UI and code changes to SourcesDialog.kt
This commit is contained in:
1 parent
22d0db1d78
commit
97e6f8b7f5
8 files changed
+164
-152
No files matched your search
+43
-25
@@ -20,51 +20,69 @@ package com.rtbishop.look4sat.presentation.settingsScreen
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.widget.doOnTextChanged
|
||||
import androidx.recyclerview.widget.AsyncListDiffer
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.rtbishop.look4sat.databinding.ItemSourceBinding
|
||||
import com.rtbishop.look4sat.framework.model.DataSource
|
||||
|
||||
class SourcesAdapter(private val sources: MutableList<DataSource> = mutableListOf()) :
|
||||
RecyclerView.Adapter<SourcesAdapter.TleSourceHolder>() {
|
||||
class SourcesAdapter(private val clickListener: SourcesClickListener) :
|
||||
RecyclerView.Adapter<SourcesAdapter.SourceHolder>() {
|
||||
|
||||
fun getSources(): List<DataSource> {
|
||||
return sources.filter { source -> source.url.contains("https://") }
|
||||
}
|
||||
private val diffCallback = object : DiffUtil.ItemCallback<DataSource>() {
|
||||
override fun areItemsTheSame(oldItem: DataSource, newItem: DataSource): Boolean {
|
||||
return oldItem.url == newItem.url
|
||||
}
|
||||
|
||||
fun setSources(list: List<DataSource>) {
|
||||
sources.clear()
|
||||
sources.addAll(list)
|
||||
override fun areContentsTheSame(oldItem: DataSource, newItem: DataSource): Boolean {
|
||||
return oldItem.url == newItem.url
|
||||
}
|
||||
}
|
||||
private val differ = AsyncListDiffer(this, diffCallback)
|
||||
private val httpsString = "https://"
|
||||
|
||||
fun getSources() = differ.currentList.filter { source -> source.url.contains(httpsString) }
|
||||
|
||||
fun addSource() {
|
||||
sources.add(DataSource())
|
||||
notifyItemInserted(itemCount - 1)
|
||||
submitList((getSources() as MutableList).apply { add(DataSource(httpsString)) })
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TleSourceHolder {
|
||||
val binding = ItemSourceBinding
|
||||
.inflate(LayoutInflater.from(parent.context), parent, false)
|
||||
return TleSourceHolder(binding)
|
||||
fun removeSource(source: DataSource) {
|
||||
submitList((getSources() as MutableList).apply { remove(source) })
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: TleSourceHolder, position: Int) {
|
||||
holder.bind(sources[position])
|
||||
fun submitList(items: List<DataSource>) = differ.submitList(items)
|
||||
|
||||
override fun getItemCount(): Int = differ.currentList.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SourceHolder {
|
||||
return SourceHolder.from(parent)
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return sources.size
|
||||
override fun onBindViewHolder(holder: SourceHolder, position: Int) {
|
||||
holder.bind(differ.currentList[position], clickListener)
|
||||
}
|
||||
|
||||
inner class TleSourceHolder(private val binding: ItemSourceBinding) :
|
||||
class SourceHolder(private val binding: ItemSourceBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
fun bind(source: DataSource) {
|
||||
binding.sourceUrl.setText(source.url)
|
||||
binding.sourceUrl.doOnTextChanged { txt, _, _, _ -> source.url = txt.toString() }
|
||||
binding.sourceBtn.setOnClickListener {
|
||||
sources.remove(source)
|
||||
notifyItemRemoved(adapterPosition)
|
||||
fun bind(source: DataSource, listener: SourcesClickListener) {
|
||||
binding.run {
|
||||
sourceText.setText(source.url)
|
||||
sourceText.doOnTextChanged { text, _, _, _ -> source.url = text.toString() }
|
||||
sourceInput.setEndIconOnClickListener { listener.removeSource(source) }
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun from(parent: ViewGroup): SourceHolder {
|
||||
val inflater = LayoutInflater.from(parent.context)
|
||||
return SourceHolder(ItemSourceBinding.inflate(inflater, parent, false))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface SourcesClickListener {
|
||||
fun removeSource(source: DataSource)
|
||||
}
|
||||
}
|
||||
+12
-8
@@ -25,6 +25,7 @@ import android.view.WindowManager
|
||||
import androidx.appcompat.app.AppCompatDialogFragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.rtbishop.look4sat.R
|
||||
import com.rtbishop.look4sat.databinding.DialogSourcesBinding
|
||||
import com.rtbishop.look4sat.domain.IDataRepository
|
||||
import com.rtbishop.look4sat.framework.model.DataSource
|
||||
@@ -33,11 +34,12 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class SourcesDialog : AppCompatDialogFragment() {
|
||||
class SourcesDialog : AppCompatDialogFragment(), SourcesAdapter.SourcesClickListener {
|
||||
|
||||
@Inject
|
||||
lateinit var repository: IDataRepository
|
||||
private lateinit var binding: DialogSourcesBinding
|
||||
private lateinit var sourcesAdapter: SourcesAdapter
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, group: ViewGroup?, state: Bundle?): View {
|
||||
binding = DialogSourcesBinding.inflate(inflater, group, false)
|
||||
@@ -46,27 +48,29 @@ class SourcesDialog : AppCompatDialogFragment() {
|
||||
|
||||
override fun onViewCreated(view: View, state: Bundle?) {
|
||||
super.onViewCreated(view, state)
|
||||
dialog?.window?.setBackgroundDrawableResource(R.color.transparent)
|
||||
dialog?.window?.setLayout(
|
||||
WindowManager.LayoutParams.MATCH_PARENT,
|
||||
(resources.displayMetrics.widthPixels * 0.94).toInt(),
|
||||
WindowManager.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
lifecycleScope.launchWhenResumed {
|
||||
val adapter = SourcesAdapter()
|
||||
val sources = repository.getDataSources().map { url -> DataSource(url) }
|
||||
val layoutManager = LinearLayoutManager(requireContext())
|
||||
adapter.setSources(repository.getDataSources().map { url -> DataSource(url) })
|
||||
sourcesAdapter = SourcesAdapter(this@SourcesDialog).apply { submitList(sources) }
|
||||
binding.run {
|
||||
sourcesRecycler.apply {
|
||||
setHasFixedSize(true)
|
||||
this.adapter = adapter
|
||||
this.adapter = sourcesAdapter
|
||||
this.layoutManager = layoutManager
|
||||
}
|
||||
sourcesBtnAdd.setOnClickListener { adapter.addSource() }
|
||||
sourcesBtnAdd.setOnClickListener { sourcesAdapter.addSource() }
|
||||
sourcesBtnNeg.setOnClickListener { dismiss() }
|
||||
sourcesBtnPos.setOnClickListener {
|
||||
setNavResult("sources", adapter.getSources().map { source -> source.url })
|
||||
setNavResult("sources", sourcesAdapter.getSources().map { item -> item.url })
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun removeSource(source: DataSource) = sourcesAdapter.removeSource(source)
|
||||
}
|
||||
Reference in new issue
Block a user