diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SourcesAdapter.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SourcesAdapter.kt index 46d3b49f..14422921 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SourcesAdapter.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SourcesAdapter.kt @@ -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 = mutableListOf()) : - RecyclerView.Adapter() { +class SourcesAdapter(private val clickListener: SourcesClickListener) : + RecyclerView.Adapter() { - fun getSources(): List { - return sources.filter { source -> source.url.contains("https://") } - } + private val diffCallback = object : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: DataSource, newItem: DataSource): Boolean { + return oldItem.url == newItem.url + } - fun setSources(list: List) { - 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) = 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) + } } diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SourcesDialog.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SourcesDialog.kt index 3f8f5648..0e97d63d 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SourcesDialog.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SourcesDialog.kt @@ -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) } diff --git a/app/src/main/res/layout/dialog_sources.xml b/app/src/main/res/layout/dialog_sources.xml index 09de92ac..4893ce0a 100644 --- a/app/src/main/res/layout/dialog_sources.xml +++ b/app/src/main/res/layout/dialog_sources.xml @@ -1,82 +1,86 @@ - + android:layout_gravity="center"> - + - + - +