<commit_before>/*
 * Copyright (c) 2019 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package jp.toastkid.yobidashi.search.favorite

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.lib.color.IconColorFinder
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.search.SearchAction
import jp.toastkid.yobidashi.search.SearchFragmentViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.util.ArrayList
import kotlin.math.min

/**
 * ModuleAdapter of search history list.
 *
 * @param context
 * @param favoriteSearchRepository Relation
 * @param onClick On click callback
 * @param onVisibilityChanged On changed visibility callback
 * @param onClickAdd
 *
 * @author toastkidjp
 */
internal class ModuleAdapter(
        context: Context,
        private val favoriteSearchRepository: FavoriteSearchRepository,
        private val onVisibilityChanged: (Boolean) -> Unit,
        private val maxItemCount: Int = -1
) : RecyclerView.Adapter<ModuleViewHolder>() {

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

    /**
     * Selected items.
     */
    private val selected: MutableList<FavoriteSearch> = ArrayList(5)

    private var viewModel: SearchFragmentViewModel? = null

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

    override fun onBindViewHolder(holder: ModuleViewHolder, position: Int) {
        val favorite = selected[position]
        holder.setText(favorite.query)
        holder.itemView.setOnClickListener {
            try {
                val query = favorite.query ?: return@setOnClickListener
                val category = favorite.category ?: return@setOnClickListener
                viewModel?.searchWithCategory(query, category)
            } catch (e: Exception) {
                Timber.e(e)
            }
        }
        holder.setOnClickAdd(favorite, { viewModel?.putQuery(it.query ?: "") })<commit_msg>refactor #412 Move lambda argument out of parentheses.<commit_after>/*
 * Copyright (c) 2019 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package jp.toastkid.yobidashi.search.favorite

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.lib.color.IconColorFinder
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.search.SearchAction
import jp.toastkid.yobidashi.search.SearchFragmentViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.util.ArrayList
import kotlin.math.min

/**
 * ModuleAdapter of search history list.
 *
 * @param context
 * @param favoriteSearchRepository Relation
 * @param onClick On click callback
 * @param onVisibilityChanged On changed visibility callback
 * @param onClickAdd
 *
 * @author toastkidjp
 */
internal class ModuleAdapter(
        context: Context,
        private val favoriteSearchRepository: FavoriteSearchRepository,
        private val onVisibilityChanged: (Boolean) -> Unit,
        private val maxItemCount: Int = -1
) : RecyclerView.Adapter<ModuleViewHolder>() {

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

    /**
     * Selected items.
     */
    private val selected: MutableList<FavoriteSearch> = ArrayList(5)

    private var viewModel: SearchFragmentViewModel? = null

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

    override fun onBindViewHolder(holder: ModuleViewHolder, position: Int) {
        val favorite = selected[position]
        holder.setText(favorite.query)
        holder.itemView.setOnClickListener {
            try {
                val query = favorite.query ?: return@setOnClickListener
                val category = favorite.category ?: return@setOnClickListener
                viewModel?.searchWithCategory(query, category)
            } catch (e: Exception) {
                Timber.e(e)
            }
        }
        holder.setOnClickAdd(favorite) { viewModel?.putQuery(it.query ?: "") }
{'fraction_non_alphanumeric': 0.0817575083426029, 'fraction_numerical': 0.004263997033741194, 'mean_word_length': 3.5527426160337554, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '420078', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1457, 'n_words': 369}