<commit_before>package com.ternaryop.photoshelf.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Callback
import com.squareup.picasso.Picasso
import com.ternaryop.photoshelf.R
import com.ternaryop.photoshelf.api.extractor.ImageInfo
import com.ternaryop.widget.CheckableImageView
import java.lang.Exception

class ImagePickerAdapter(private val context: Context)
    : AbsBaseAdapter<ImagePickerAdapter.ViewHolder>(), View.OnClickListener, View.OnLongClickListener {
    private var onPhotoBrowseClick: OnPhotoBrowseClickMultiChoice? = null
    private val items: MutableList<ImageInfo> = mutableListOf()
    var showButtons: Boolean = false

    val selection = SelectionArrayViewHolder(this)

    val selectedItems: List<ImageInfo>
        get() = selection.selectedPositions.map { getItem(it) }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.gridview_photo_picker_item, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val listener: View.OnClickListener? = if (onPhotoBrowseClick == null) null else this
        holder.bindModel(items[position], showButtons, selection.isSelected(position))
        if (showButtons && listener != null) {
            holder.setOnClickListeners(listener)
        }
        holder.setOnClickMultiChoiceListeners(listener, this)
    }

    override fun getItemCount() = items.size

    fun getItem(position: Int) = items[position]

    fun getIndex(item: ImageInfo) = items.indexOfFirst { it == item }

    fun addAll(list: List<ImageInfo>) {
        items.addAll(list)
        notifyDataSetChanged()
    }

    fun clear() {
        val size = items.size
        items.clear()
        notifyItemRangeRemoved(0, size)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.ic_show_image_action -> onPhotoBrowseClick!!.onThumbnailImageClick(v.tag as Int)
            R.id.list_row -> onPhotoBrowseClick!!.onItemClick(v.tag as Int)
        }
    }

    override fun onLongClick(v: View): Boolean {
        onPhotoBrowseClick!!.onItemLongClick(v.tag as Int)
        return true
    }

    @Suppress("MemberVisibilityCanBePrivate")
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        internal val showImageAction = itemView.findViewById<View>(R.id.ic_show_image_action) as ImageView
        internal val thumbImage = itemView.findViewById<View>(R.id.thumbnail_image) as CheckableImageView
        internal val bgAction = itemView.findViewById<View>(R.id.bg_actions) as ImageView

        fun bindModel(imageInfo: ImageInfo, showButtons: Boolean, checked: Boolean) {
            setVisibility(showButtons)
            displayImage(imageInfo, checked)
        }

        private fun setVisibility(showButtons: Boolean) {
            showImageAction.visibility = if (showButtons) View.VISIBLE else View.INVISIBLE
            bgAction.visibility = if (showButtons) View.VISIBLE else View.INVISIBLE
        }

        private fun displayImage(imageInfo: ImageInfo, checked: Boolean) {
            Picasso
                .get()
                .load(imageInfo.thumbnailUrl!!)
                .placeholder(R.drawable.stub)
                .noFade()
                .into(thumbImage, object: Callback {
                    override fun onSuccess() {<commit_msg>resize thumbnail to avoid memory footprints<commit_after>package com.ternaryop.photoshelf.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Callback
import com.squareup.picasso.Picasso
import com.ternaryop.photoshelf.R
import com.ternaryop.photoshelf.api.extractor.ImageInfo
import com.ternaryop.widget.CheckableImageView

class ImagePickerAdapter(private val context: Context)
    : AbsBaseAdapter<ImagePickerAdapter.ViewHolder>(), View.OnClickListener, View.OnLongClickListener {
    private var onPhotoBrowseClick: OnPhotoBrowseClickMultiChoice? = null
    private val items: MutableList<ImageInfo> = mutableListOf()
    var showButtons: Boolean = false

    val selection = SelectionArrayViewHolder(this)

    val selectedItems: List<ImageInfo>
        get() = selection.selectedPositions.map { getItem(it) }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.gridview_photo_picker_item, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val listener: View.OnClickListener? = if (onPhotoBrowseClick == null) null else this
        holder.bindModel(items[position], showButtons, selection.isSelected(position))
        if (showButtons && listener != null) {
            holder.setOnClickListeners(listener)
        }
        holder.setOnClickMultiChoiceListeners(listener, this)
    }

    override fun getItemCount() = items.size

    fun getItem(position: Int) = items[position]

    fun getIndex(item: ImageInfo) = items.indexOfFirst { it == item }

    fun addAll(list: List<ImageInfo>) {
        items.addAll(list)
        notifyDataSetChanged()
    }

    fun clear() {
        val size = items.size
        items.clear()
        notifyItemRangeRemoved(0, size)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.ic_show_image_action -> onPhotoBrowseClick!!.onThumbnailImageClick(v.tag as Int)
            R.id.list_row -> onPhotoBrowseClick!!.onItemClick(v.tag as Int)
        }
    }

    override fun onLongClick(v: View): Boolean {
        onPhotoBrowseClick!!.onItemLongClick(v.tag as Int)
        return true
    }

    @Suppress("MemberVisibilityCanBePrivate")
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        internal val showImageAction = itemView.findViewById<View>(R.id.ic_show_image_action) as ImageView
        internal val thumbImage = itemView.findViewById<View>(R.id.thumbnail_image) as CheckableImageView
        internal val bgAction = itemView.findViewById<View>(R.id.bg_actions) as ImageView

        fun bindModel(imageInfo: ImageInfo, showButtons: Boolean, checked: Boolean) {
            setVisibility(showButtons)
            displayImage(imageInfo, checked)
        }

        private fun setVisibility(showButtons: Boolean) {
            showImageAction.visibility = if (showButtons) View.VISIBLE else View.INVISIBLE
            bgAction.visibility = if (showButtons) View.VISIBLE else View.INVISIBLE
        }

        private fun displayImage(imageInfo: ImageInfo, checked: Boolean) {
            // thumbnail urls could be the same than destination images (i.e. very large images) causing
            // a huge memory footprint so we resize the images using fit/centerCrop
            Picasso
                .get()
                .load(imageInfo.thumbnailUrl!!)
                .placeholder(R.drawable.stub)
                .noFade()
                .fit()
                .centerCrop()
                .into(thumbImage, object: Callback {
                    override fun onSuccess() {
{'fraction_non_alphanumeric': 0.0884134033104562, 'fraction_numerical': 0.00026914278024491994, 'mean_word_length': 3.1426978818283167, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4265774', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2050, 'n_words': 450}