Skip to content

Instantly share code, notes, and snippets.

@sajjadyousefnia
Created June 25, 2024 18:02
Show Gist options
  • Save sajjadyousefnia/04acf1ad0c2c0b8a15055aeef8f4c7ee to your computer and use it in GitHub Desktop.
Save sajjadyousefnia/04acf1ad0c2c0b8a15055aeef8f4c7ee to your computer and use it in GitHub Desktop.
package com.sands.android.view.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.sands.android.R
import com.sands.android.dao.entity.SubtitleColorModel
import com.sands.android.databinding.AdapterSubtitleColorBinding
class AdapterSubtitleColor(
private val context: Context,
private val colorsList: MutableList<SubtitleColorModel>,
private val delegate: Interaction
) : RecyclerView.Adapter<AdapterSubtitleColor.SubtitleColorViewHolder>() {
public interface Interaction {
public fun onSelectColor(item: SubtitleColorModel)
}
public class SubtitleColorViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val binding = AdapterSubtitleColorBinding.bind(v)
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): SubtitleColorViewHolder {
val adapterView = R.layout.adapter_subtitle_color
val itemView = LayoutInflater.from(viewGroup.context).inflate(adapterView, viewGroup, false)
return SubtitleColorViewHolder(itemView)
}
override fun getItemCount(): Int {
return colorsList.size
}
override fun onBindViewHolder(holder: SubtitleColorViewHolder, position: Int) {
val item = colorsList[position]
holder.binding.root.setCardBackgroundColor(ContextCompat.getColor(context, item.colorCode))
holder.binding.root.setOnClickListener {
delegate.onSelectColor(item)
}
}
}
package com.sands.android.view.dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.sands.android.R
import com.sands.android.dao.entity.SubtitleColorModel
import com.sands.android.databinding.DialogSubtitleSettingsBinding
import com.sands.android.view.adapter.AdapterSubtitleColor
class DialogSubtitleSettings : DialogFragment() {
private lateinit var delegate: Interaction
lateinit var colorsList: MutableList<SubtitleColorModel>
private lateinit var binding: DialogSubtitleSettingsBinding
public interface Interaction {
public fun increaseSubtitleSize()
public fun decreaseSubtitleSize()
public fun selectSubtitleColor(item: SubtitleColorModel)
}
companion object {
fun newInstance(delegate: Interaction): DialogSubtitleSettings {
val frag = DialogSubtitleSettings()
val args = Bundle()
// frag.delegate = delegate
frag.arguments = args
return frag
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
colorsList =
mutableListOf(
SubtitleColorModel(
colorCode = ContextCompat.getColor(
requireContext(),
R.color.subtitle_color_1
)
),
SubtitleColorModel(
colorCode = ContextCompat.getColor(
requireContext(),
R.color.subtitle_color_2
)
),
SubtitleColorModel(
colorCode = ContextCompat.getColor(
requireContext(),
R.color.subtitle_color_3
)
),
SubtitleColorModel(
colorCode = ContextCompat.getColor(
requireContext(),
R.color.subtitle_color_4
)
),
SubtitleColorModel(
colorCode = ContextCompat.getColor(
requireContext(),
R.color.subtitle_color_5
)
),
SubtitleColorModel(
colorCode = ContextCompat.getColor(
requireContext(),
R.color.subtitle_color_6
)
)
)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View = inflater.inflate(R.layout.dialog_subtitle_settings, container, false)
binding = DialogSubtitleSettingsBinding.bind(view)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.rvColors.layoutManager =
LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
binding.rvColors.adapter = AdapterSubtitleColor(requireContext(), colorsList,
object : AdapterSubtitleColor.Interaction {
override fun onSelectColor(subtitleColorModel: SubtitleColorModel) {
if (subtitleColorModel.isSelected == false) {
colorsList.forEach { it.isSelected = false }
subtitleColorModel.isSelected = true
binding.rvColors.adapter?.notifyDataSetChanged()
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment