<commit_before>package com.github.laurenttreguier.deck

import android.content.Intent
import android.support.v4.view.animation.FastOutSlowInInterpolator
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.GlideDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.github.laurenttreguier.deck.model.Card
import java.io.File
import java.util.*

class CardAdapter(private val cards: MutableList<Card>) :
        RecyclerView.Adapter<CardAdapter.ViewHolder>() {
    private val cardsBackup = ArrayList<Card>()
    private val selected = ArrayList<Card>()
    private var onSelectionListener: OnSelectionListener? = null

    companion object {
        private val SELECTED_SCALE = 0.9f
        private val UNSELECTED_SCALE = 1f
    }

    init {
        Collections.sort(cards)
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder? {
        val inflater = LayoutInflater.from(parent?.context)
        val cardView = inflater.inflate(R.layout.card, parent, false)

        return ViewHolder(cardView)
    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        val card = cards[position]

        fun updateSelect(animate: Boolean) {
            holder?.let {
                val visibility = if (selected.contains(card)) View.VISIBLE else View.GONE
                val scale = if (selected.contains(card)) SELECTED_SCALE else UNSELECTED_SCALE

                holder.select?.visibility = visibility

                if (animate) {
                    holder.root?.animate()
                            ?.setInterpolator(FastOutSlowInInterpolator())
                            ?.setDuration(holder.root?.context?.resources
                                    ?.getInteger(android.R.integer.config_shortAnimTime)!!.toLong())
                            ?.scaleX(scale)
                            ?.scaleY(scale)
                } else {
                    holder.root?.scaleX = scale
                    holder.root?.scaleY = scale
                }
            }
        }

        fun toggle() {
            if (selected.size == 0) {
                onSelectionListener?.onBegin()
            }

            if (selected.contains(card)) {
                selected.remove(card)
            } else {
                selected.add(card)
            }

            updateSelect(true)

            if (selected.size == 0) {
                onSelectionListener?.onEnd()
            }
        }

        holder?.root?.let {
            holder.title?.text = card.name

            val request = Glide.with(holder.preview?.context).load(File(card.previewPath))
            val params = holder.preview?.layoutParams

            if (params?.width == ViewGroup.LayoutParams.MATCH_PARENT) {
                request.listener(object : RequestListener<File, GlideDrawable> {
                    override fun onException(e: Exception?, model: File?, target: Target<GlideDrawable>?, isFirstResource: Boolean): Boolean {
                        return false
                    }

                    override fun onResourceReady(resource: GlideDrawable?, model: File?, target: Target<GlideDrawable>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
                        holder.preview?.addOnLayoutChangeListener(object : View.OnLayoutChangeListener {
                            override fun onLayoutChange(p0: View?, p1: Int, p2: Int, p3: Int, p4: Int, p5: Int, p6: Int, p7: Int, p8: Int) {
                                holder.preview?.removeOnLayoutChangeListener(this)
                                params?.width = holder.preview?.width
                                params?.height = holder.preview?.height
                                holder.preview?.layoutParams = params
                            }
                        })

                        return false
                    }<commit_msg>Fix card previews sometimes not visible<commit_after>package com.github.laurenttreguier.deck

import android.content.Intent
import android.support.v4.view.animation.FastOutSlowInInterpolator
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.GlideDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.github.laurenttreguier.deck.model.Card
import java.io.File
import java.util.*

class CardAdapter(private val cards: MutableList<Card>) :
        RecyclerView.Adapter<CardAdapter.ViewHolder>() {
    private val cardsBackup = ArrayList<Card>()
    private val selected = ArrayList<Card>()
    private var onSelectionListener: OnSelectionListener? = null

    companion object {
        private val SELECTED_SCALE = 0.9f
        private val UNSELECTED_SCALE = 1f
    }

    init {
        Collections.sort(cards)
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder? {
        val inflater = LayoutInflater.from(parent?.context)
        val cardView = inflater.inflate(R.layout.card, parent, false)

        return ViewHolder(cardView)
    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        val card = cards[position]

        fun updateSelect(animate: Boolean) {
            holder?.let {
                val visibility = if (selected.contains(card)) View.VISIBLE else View.GONE
                val scale = if (selected.contains(card)) SELECTED_SCALE else UNSELECTED_SCALE

                holder.select?.visibility = visibility

                if (animate) {
                    holder.root?.animate()
                            ?.setInterpolator(FastOutSlowInInterpolator())
                            ?.setDuration(holder.root?.context?.resources
                                    ?.getInteger(android.R.integer.config_shortAnimTime)!!.toLong())
                            ?.scaleX(scale)
                            ?.scaleY(scale)
                } else {
                    holder.root?.scaleX = scale
                    holder.root?.scaleY = scale
                }
            }
        }

        fun toggle() {
            if (selected.size == 0) {
                onSelectionListener?.onBegin()
            }

            if (selected.contains(card)) {
                selected.remove(card)
            } else {
                selected.add(card)
            }

            updateSelect(true)

            if (selected.size == 0) {
                onSelectionListener?.onEnd()
            }
        }

        holder?.root?.let {
            holder.title?.text = card.name

            val request = Glide.with(holder.preview?.context).load(File(card.previewPath))
            val params = holder.preview?.layoutParams

            if (params?.width == ViewGroup.LayoutParams.MATCH_PARENT) {
                request.listener(object : RequestListener<File, GlideDrawable> {
                    override fun onException(e: Exception?, model: File?, target: Target<GlideDrawable>?, isFirstResource: Boolean): Boolean {
                        return false
                    }

                    override fun onResourceReady(resource: GlideDrawable?, model: File?, target: Target<GlideDrawable>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
                        holder.preview?.addOnLayoutChangeListener(object : View.OnLayoutChangeListener {
                            override fun onLayoutChange(v: View?, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
                                holder.preview?.let {
                                    it.removeOnLayoutChangeListener(this)

                                    if (it.height > 0) {
                                        params?.width = it.width
                                        params?.height = it.height
                                        holder.preview?.layoutParams = params
                                    }
                                }
                            }
                        })

                        return false
                    }
{'fraction_non_alphanumeric': 0.08966884328358209, 'fraction_numerical': 0.002798507462686567, 'mean_word_length': 1.754335260115607, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4669436', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2069, 'n_words': 429}