Skip to content

Instantly share code, notes, and snippets.

@ravisorathiya
Created December 27, 2024 02:38
Show Gist options
  • Save ravisorathiya/d2529034ed0cd8195c484a3fb0dd7235 to your computer and use it in GitHub Desktop.
Save ravisorathiya/d2529034ed0cd8195c484a3fb0dd7235 to your computer and use it in GitHub Desktop.
fun boxesList(nummm : Int): List<Box> {
var intRange = nummm * nummm
val list = (1..intRange).mapIndexed { index, i ->
Box(
index / nummm,
index % nummm,
false
)
}
return list
}
val magiccc = 15
val boxesList = boxesList(magiccc)
adapter = BoxAdapter(onClick = {clicked->
for (box in boxesList) {
val sameX = box.row == clicked.row
val sameY = box.col == clicked.col
val sameCrossX = box.row - box.col == clicked.row - clicked.col
val samCrossY = box.row + box.col == clicked.row + clicked.col
box.isBlink = sameCrossX || samCrossY || sameX || sameY
}
adapter.notifyDataSetChanged()
})
binding.rv.layoutManager = GridLayoutManager(this, magiccc)
adapter.count = magiccc
binding.rv.adapter = adapter
adapter.submitList(boxesList)
import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.driveapp.Box
import com.example.driveapp.databinding.ItemBoxBinding
class BoxAdapter(
private val onClick: (Box) -> Unit
) : ListAdapter<Box, BoxAdapter.CloudFolderViewHolder>(DiffCallBack()) {
var count = 5
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): CloudFolderViewHolder {
val itemBinding = ItemBoxBinding.inflate(
LayoutInflater.from(
parent.context
), parent, false
)
val value = parent.context.resources.displayMetrics.widthPixels / count
val layoutParams = itemBinding.root.layoutParams
layoutParams.height = value
layoutParams.width = value
itemBinding.root.layoutParams = layoutParams
return CloudFolderViewHolder(itemBinding)
}
override fun onBindViewHolder(holderPaging: CloudFolderViewHolder, position: Int) {
val currentItem = getItem(position)
if (currentItem != null)
holderPaging.bind(currentItem)
}
inner class CloudFolderViewHolder(private val itemBinding: ItemBoxBinding) :
RecyclerView.ViewHolder(itemBinding.root) {
init {
itemBinding.root.setOnClickListener {
val position = bindingAdapterPosition
if (position != RecyclerView.NO_POSITION) {
val item = getItem(position)
onClick(item)
}
}
}
@SuppressLint("SetTextI18n")
fun bind(item: Box) {
itemBinding.imv.setBackgroundColor(if (item.isBlink) {
Color.BLUE
}else Color.YELLOW)
}
}
class DiffCallBack : DiffUtil.ItemCallback<Box>() {
override fun areItemsTheSame(
oldItem: Box,
newItem: Box,
): Boolean =
oldItem.toString().hashCode() == newItem.col.toString().hashCode()
@SuppressLint("DiffUtilEquals")
override fun areContentsTheSame(
oldItem: Box,
newItem: Box,
): Boolean =
oldItem == newItem
}
}
@ravisorathiya
Copy link
Author

n * n dimen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment