<commit_before>package jp.toastkid.yobidashi.tab.tab_list

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.ColorUtils
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.libs.Toaster
import jp.toastkid.yobidashi.libs.preference.ColorPair
import jp.toastkid.yobidashi.libs.preference.PreferenceApplier
import jp.toastkid.yobidashi.tab.TabThumbnails

/**
 * WebTab list adapter.
 * Initialize with context and so on...
 *
 * @param context
 * @param callback WebTab list model
 *
 * @author toastkidjp
 */
internal class Adapter(
        private val context: Context,
        private val callback: TabListDialogFragment.Callback
) : RecyclerView.Adapter<ViewHolder>() {

    /**
     * For getting Data binding object.
     */
    private val inflater: LayoutInflater = LayoutInflater.from(context)

    /**
     * For snackbar and view color.
     */
    private val colorPair: ColorPair = PreferenceApplier(context).colorPair()

    private val tabThumbnails = TabThumbnails { context }

    /**
     * Current index.
     */
    private var index = -1

    private var viewModel: TabListViewModel? = null

    init {
        if (context is FragmentActivity) {
            viewModel = ViewModelProvider(context).get(TabListViewModel::class.java)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
                DataBindingUtil.inflate(inflater, R.layout.item_tab_list, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val tab = callback.getTabByIndexFromTabList(position) ?: return
        holder.itemView.setOnClickListener {
            callback.replaceTabFromTabList(tab)
            callback.onCloseOnly()
        }

        holder.itemView.setOnLongClickListener {
            viewModel?.sendStartDrag(holder)
            Toaster.snackShort(
                    holder.itemView,
                    "Start to move tab \"${tab.title()}\".",
                    colorPair
            )
            return@setOnLongClickListener true
        }

        holder.setImagePath(tabThumbnails.assignNewFile(tab.thumbnailPath()).absolutePath)
        holder.setTitle(tab.title())
        holder.setCloseAction(View.OnClickListener { closeAt(callback.tabIndexOfFromTabList(tab)) })
        holder.setColor(colorPair)
        holder.setBackgroundColor(
                if (index == position) {
                    ColorUtils.setAlphaComponent(colorPair.bgColor(), 128)
                } else {
                    Color.TRANSPARENT
                }
        )
    }

    /**
     * Close tab at index.
     * @param position
     */
    private fun closeAt(position: Int) {
        callback.closeTabFromTabList(position)
        notifyItemRemoved(position)
    }

    override fun getItemCount(): Int = callback.getTabAdapterSizeFromTabList()

    /**
     * Set current index.
     */
    fun setCurrentIndex(newIndex: Int) {
        index = newIndex
    }

    fun swap(from: Int, to: Int) {
        callback.swapTabsFromTabList(from, to)
        notifyItemMoved(from, to)
    }
}
<commit_msg>refactor #442 Extract layout ID to constant.<commit_after>package jp.toastkid.yobidashi.tab.tab_list

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.core.graphics.ColorUtils
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.libs.Toaster
import jp.toastkid.yobidashi.libs.preference.ColorPair
import jp.toastkid.yobidashi.libs.preference.PreferenceApplier
import jp.toastkid.yobidashi.tab.TabThumbnails

/**
 * WebTab list adapter.
 * Initialize with context and so on...
 *
 * @param context
 * @param callback WebTab list model
 *
 * @author toastkidjp
 */
internal class Adapter(
        private val context: Context,
        private val callback: TabListDialogFragment.Callback
) : RecyclerView.Adapter<ViewHolder>() {

    /**
     * For getting Data binding object.
     */
    private val inflater: LayoutInflater = LayoutInflater.from(context)

    /**
     * For snackbar and view color.
     */
    private val colorPair: ColorPair = PreferenceApplier(context).colorPair()

    private val tabThumbnails = TabThumbnails { context }

    /**
     * Current index.
     */
    private var index = -1

    private var viewModel: TabListViewModel? = null

    init {
        if (context is FragmentActivity) {
            viewModel = ViewModelProvider(context).get(TabListViewModel::class.java)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
                DataBindingUtil.inflate(inflater, LAYOUT_ID, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val tab = callback.getTabByIndexFromTabList(position) ?: return
        holder.itemView.setOnClickListener {
            callback.replaceTabFromTabList(tab)
            callback.onCloseOnly()
        }

        holder.itemView.setOnLongClickListener {
            viewModel?.sendStartDrag(holder)
            Toaster.snackShort(
                    holder.itemView,
                    "Start to move tab \"${tab.title()}\".",
                    colorPair
            )
            return@setOnLongClickListener true
        }

        holder.setImagePath(tabThumbnails.assignNewFile(tab.thumbnailPath()).absolutePath)
        holder.setTitle(tab.title())
        holder.setCloseAction(View.OnClickListener { closeAt(callback.tabIndexOfFromTabList(tab)) })
        holder.setColor(colorPair)
        holder.setBackgroundColor(
                if (index == position) {
                    ColorUtils.setAlphaComponent(colorPair.bgColor(), 128)
                } else {
                    Color.TRANSPARENT
                }
        )
    }

    /**
     * Close tab at index.
     * @param position
     */
    private fun closeAt(position: Int) {
        callback.closeTabFromTabList(position)
        notifyItemRemoved(position)
    }

    override fun getItemCount(): Int = callback.getTabAdapterSizeFromTabList()

    /**
     * Set current index.
     */
    fun setCurrentIndex(newIndex: Int) {
        index = newIndex
    }

    fun swap(from: Int, to: Int) {
        callback.swapTabsFromTabList(from, to)
        notifyItemMoved(from, to)
    }

    companion object {

        @LayoutRes
        private const val LAYOUT_ID = R.layout.item_tab_list
    }
}

{'fraction_non_alphanumeric': 0.08931102920328891, 'fraction_numerical': 0.0015593989225971081, 'mean_word_length': 2.6013272077590606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3314663', 'n_tokens_mistral': 2070, 'n_tokens_neox': 1957, 'n_words': 396}