<commit_before>package jp.toastkid.yobidashi.search.url_suggestion

import android.view.LayoutInflater
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.yobidashi.browser.bookmark.model.BookmarkRepository
import jp.toastkid.yobidashi.browser.history.ViewHistoryRepository
import jp.toastkid.yobidashi.databinding.ModuleUrlSuggestionBinding
import jp.toastkid.yobidashi.libs.db.DatabaseFinder
import jp.toastkid.yobidashi.libs.view.RightSwipeActionAttachment
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @author toastkidjp
 */
class UrlSuggestionModule(
        private val binding: ModuleUrlSuggestionBinding,
        browseCallback: (String) -> Unit,
        browseBackgroundCallback: (String) -> Unit
) {

    /**
     * Adapter.
     */
    private val adapter = Adapter(
            LayoutInflater.from(binding.root.context),
            this::removeAt,
            browseCallback,
            browseBackgroundCallback
            )

    var enable = true

    /**
     * Bookmark's database repository.
     */
    private val bookmarkRepository: BookmarkRepository =
            DatabaseFinder().invoke(binding.root.context).bookmarkRepository()

    /**
     * Database repository.
     */
    private val viewHistoryRepository: ViewHistoryRepository =
            DatabaseFinder().invoke(binding.root.context).viewHistoryRepository()

    init {
        binding.urlSuggestions.adapter = adapter
        binding.urlSuggestions.layoutManager =
                LinearLayoutManager(binding.root.context, RecyclerView.VERTICAL, false)
        RightSwipeActionAttachment()(
                binding.urlSuggestions,
                this::removeAt
        )
    }

    /**
     * Remove item.
     *
     * @param index
     */
    private fun removeAt(index: Int) {
        adapter.removeAt(viewHistoryRepository, index)
    }

    /**
     * Query to database.
     *
     * @param q query string
     */
    fun query(q: CharSequence) {
        adapter.clear()
        if (q.isEmpty()) {
            adapter.notifyDataSetChanged()
            hide()
            return
        }

        CoroutineScope(Dispatchers.Main).launch {
            withContext(Dispatchers.IO) {
                bookmarkRepository.search("%$q%", ITEM_LIMIT).forEach { adapter.add(it) }
            }

            withContext(Dispatchers.IO) {
                viewHistoryRepository.search("%$q%", ITEM_LIMIT).forEach { adapter.add(it) }
            }

            if (adapter.isNotEmpty()) {
                show()
            } else {
                hide()
            }
            adapter.notifyDataSetChanged()
        }
    }

    /**
     * Show this module.
     */
    fun show() {
        if (!binding.root.isVisible && enable) {
            runOnMainThread { binding.root.isVisible = true }
        }
    }

    /**
     * Hide this module.
     */
    fun hide() {
        if (binding.root.isVisible) {<commit_msg>feature #412 Modify URL module content.<commit_after>package jp.toastkid.yobidashi.search.url_suggestion

import android.view.LayoutInflater
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.yobidashi.browser.bookmark.model.BookmarkRepository
import jp.toastkid.yobidashi.browser.history.ViewHistoryRepository
import jp.toastkid.yobidashi.databinding.ModuleUrlSuggestionBinding
import jp.toastkid.yobidashi.libs.db.DatabaseFinder
import jp.toastkid.yobidashi.libs.view.RightSwipeActionAttachment
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @author toastkidjp
 */
class UrlSuggestionModule(
        private val binding: ModuleUrlSuggestionBinding,
        browseCallback: (String) -> Unit,
        browseBackgroundCallback: (String) -> Unit
) {

    /**
     * Adapter.
     */
    private val adapter = Adapter(
            LayoutInflater.from(binding.root.context),
            this::removeAt,
            browseCallback,
            browseBackgroundCallback
            )

    var enable = true

    /**
     * Bookmark's database repository.
     */
    private val bookmarkRepository: BookmarkRepository =
            DatabaseFinder().invoke(binding.root.context).bookmarkRepository()

    /**
     * Database repository.
     */
    private val viewHistoryRepository: ViewHistoryRepository =
            DatabaseFinder().invoke(binding.root.context).viewHistoryRepository()

    init {
        binding.urlSuggestions.adapter = adapter
        binding.urlSuggestions.layoutManager =
                LinearLayoutManager(binding.root.context, RecyclerView.VERTICAL, false)
        RightSwipeActionAttachment()(
                binding.urlSuggestions,
                this::removeAt
        )
    }

    /**
     * Remove item.
     *
     * @param index
     */
    private fun removeAt(index: Int) {
        adapter.removeAt(viewHistoryRepository, index)
    }

    /**
     * Query to database.
     *
     * @param q query string
     */
    fun query(q: CharSequence) {
        adapter.clear()

        CoroutineScope(Dispatchers.Main).launch {
            withContext(Dispatchers.IO) {
                if (q.isBlank()) {
                    return@withContext
                }
                bookmarkRepository.search("%$q%", ITEM_LIMIT).forEach { adapter.add(it) }
            }

            withContext(Dispatchers.IO) {
                viewHistoryRepository.search("%$q%", ITEM_LIMIT).forEach { adapter.add(it) }
            }

            if (adapter.isNotEmpty()) {
                show()
            } else {
                hide()
            }
            adapter.notifyDataSetChanged()
        }
    }

    /**
     * Show this module.
     */
    fun show() {
        if (!binding.root.isVisible && enable) {
            runOnMainThread { binding.root.isVisible = true }
        }
    }

    /**
     * Hide this module.
     */
    fun hide() {
        if (binding.root.isVisible) {
{'fraction_non_alphanumeric': 0.09820857325655791, 'fraction_numerical': 0.0004798464491362764, 'mean_word_length': 2.193564862104188, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4157877', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1741, 'n_words': 279}