<commit_before>package jp.toastkid.yobidashi.browser.archive

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.lib.storage.FilesDir
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.databinding.ItemArchiveBinding
import timber.log.Timber
import java.io.IOException
import java.text.NumberFormat
import java.util.Locale

/**
 * Initialize with Context.
 *
 * @param context
 * @param callback Return read content
 *
 * @author toastkidjp
 */
internal class Adapter(
        context: Context,
        private val callback: (String) -> Unit
) : RecyclerView.Adapter<ViewHolder>() {

    /**
     * Archive folder wrapper.
     */
    private val archiveDir: FilesDir = Archive.makeNew(context)

    /**
     * Layout inflater.
     */
    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

    /**
     * Preference's wrapper.
     */
    private val preferenceApplier = PreferenceApplier(context)

    /**
     * Data binding object.
     */
    private var binding: ItemArchiveBinding? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        binding = DataBindingUtil.inflate(
                layoutInflater, R.layout.item_archive, parent, false)
        return ViewHolder(binding as ItemArchiveBinding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val file = archiveDir.get(position) ?: return
        holder.setText(file.name)
        holder.setSubText(
                "${toLastModifiedText(file.lastModified())} / ${toKiloBytes(file.length())}[KB]")
        holder.itemView.setOnClickListener {
            try {
                callback(file.absolutePath)
            } catch (e: IOException) {
                Timber.e(e)
            }
        }
        holder.setDelete(View.OnClickListener{
            file.delete()
            notifyItemRemoved(position)
        })
        holder.setIconColor(preferenceApplier.color)
    }

    /**
     * Convert milliseconds to text.
     *
     * @param lastModifiedMs milliseconds
     */
    private fun toLastModifiedText(lastModifiedMs: Long) =
        android.text.format.DateFormat.format("yyyyMMdd HH:mm:ss", lastModifiedMs)

    /**
     * Convert file byte length to KB text.
     *
     * @param length file byte length
     */
    private fun toKiloBytes(length: Long): String
            = NumberFormat.getIntegerInstance(Locale.getDefault()).format(length / 1024)

    override fun getItemCount(): Int = archiveDir.count

    companion object {

    }
}<commit_msg>refactor #718 Delete useless companion object.<commit_after>package jp.toastkid.yobidashi.browser.archive

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.lib.storage.FilesDir
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.databinding.ItemArchiveBinding
import timber.log.Timber
import java.io.IOException
import java.text.NumberFormat
import java.util.Locale

/**
 * Initialize with Context.
 *
 * @param context
 * @param callback Return read content
 *
 * @author toastkidjp
 */
internal class Adapter(
        context: Context,
        private val callback: (String) -> Unit
) : RecyclerView.Adapter<ViewHolder>() {

    /**
     * Archive folder wrapper.
     */
    private val archiveDir: FilesDir = Archive.makeNew(context)

    /**
     * Layout inflater.
     */
    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

    /**
     * Preference's wrapper.
     */
    private val preferenceApplier = PreferenceApplier(context)

    /**
     * Data binding object.
     */
    private var binding: ItemArchiveBinding? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        binding = DataBindingUtil.inflate(
                layoutInflater, R.layout.item_archive, parent, false)
        return ViewHolder(binding as ItemArchiveBinding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val file = archiveDir.get(position) ?: return
        holder.setText(file.name)
        holder.setSubText(
                "${toLastModifiedText(file.lastModified())} / ${toKiloBytes(file.length())}[KB]")
        holder.itemView.setOnClickListener {
            try {
                callback(file.absolutePath)
            } catch (e: IOException) {
                Timber.e(e)
            }
        }
        holder.setDelete(View.OnClickListener{
            file.delete()
            notifyItemRemoved(position)
        })
        holder.setIconColor(preferenceApplier.color)
    }

    /**
     * Convert milliseconds to text.
     *
     * @param lastModifiedMs milliseconds
     */
    private fun toLastModifiedText(lastModifiedMs: Long) =
        android.text.format.DateFormat.format("yyyyMMdd HH:mm:ss", lastModifiedMs)

    /**
     * Convert file byte length to KB text.
     *
     * @param length file byte length
     */
    private fun toKiloBytes(length: Long): String
            = NumberFormat.getIntegerInstance(Locale.getDefault()).format(length / 1024)

    override fun getItemCount(): Int = archiveDir.count

}
{'fraction_non_alphanumeric': 0.0967104080532087, 'fraction_numerical': 0.0019773503505302894, 'mean_word_length': 2.8161865569272977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '2892447', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1524, 'n_words': 331}