<commit_before>/*
 * Copyright (c) 2018 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.history

import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.reactivex.Completable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.addTo
import jp.toastkid.yobidashi.databinding.ModuleSearchHistoryBinding
import jp.toastkid.yobidashi.libs.db.DatabaseFinder
import jp.toastkid.yobidashi.libs.facade.BaseModule

/**
 * Search history module.
 *
 * @param binding Data binding object
 * @param searchCallback
 * @param onClickAdd
 *
 * @author toastkidjp
 */
class HistoryModule(
        private val binding: ModuleSearchHistoryBinding,
        searchCallback: (SearchHistory) -> Unit,
        onClickAdd: (SearchHistory) -> Unit
) : BaseModule(binding.root) {

    /**
     * RecyclerView's moduleAdapter.
     */
    private val moduleAdapter: ModuleAdapter

    /**
     * Last subscription.
     */
    private var disposable: Disposable? = null

    /**
     * Use for disposing.
     */
    private val disposables: CompositeDisposable = CompositeDisposable()

    init {
        binding.module = this

        binding.searchHistories.layoutManager =
                LinearLayoutManager(context(), RecyclerView.VERTICAL, false)

        val repository = DatabaseFinder().invoke(context()).searchHistoryRepository()

        moduleAdapter = ModuleAdapter(
                context(),
                repository,
                searchCallback,
                { visible -> if (visible) { show() } else { hide() } },
                { history -> onClickAdd(history) }
        )
        binding.searchHistories.adapter = moduleAdapter

        Completable.fromAction { SwipeActionAttachment().invoke(binding.searchHistories) }
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe()
                .addTo(disposables)
    }

    /**
     * Query table with passed word.
     *
     * @param s query string
     */
    fun query(s: CharSequence) {
        disposable?.dispose()
        disposable = moduleAdapter.query(s)
    }

    /**
     * Clear search history.
     */
    fun confirmClear() {
        val activityContext = context()
        if (activityContext is FragmentActivity) {
            ClearSearchHistoryDialogFragment().show(
                    activityContext.supportFragmentManager,
                    ClearSearchHistoryDialogFragment::class.java.simpleName
            )
        }
    }

    fun clear() {
        moduleAdapter.clear()
        hide()
    }

    /**
     * Dispose last subscription.
     */
    fun dispose() {
        disposable?.dispose()
    }

}<commit_msg>refactor #442 Detach BaseModule from HistoryModule.<commit_after>/*
 * Copyright (c) 2018 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.history

import androidx.core.view.isVisible
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.reactivex.Completable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.addTo
import jp.toastkid.yobidashi.databinding.ModuleSearchHistoryBinding
import jp.toastkid.yobidashi.libs.db.DatabaseFinder
import timber.log.Timber

/**
 * Search history module.
 *
 * @param binding Data binding object
 * @param searchCallback
 * @param onClickAdd
 *
 * @author toastkidjp
 */
class HistoryModule(
        private val binding: ModuleSearchHistoryBinding,
        searchCallback: (SearchHistory) -> Unit,
        onClickAdd: (SearchHistory) -> Unit
) {

    /**
     * RecyclerView's moduleAdapter.
     */
    private val moduleAdapter: ModuleAdapter

    var enable: Boolean = true

    /**
     * Last subscription.
     */
    private var disposable: Disposable? = null

    /**
     * Use for disposing.
     */
    private val disposables: CompositeDisposable = CompositeDisposable()

    init {
        binding.module = this

        val context = binding.root.context
        binding.searchHistories.layoutManager =
                LinearLayoutManager(context, RecyclerView.VERTICAL, false)

        val repository = DatabaseFinder().invoke(context).searchHistoryRepository()

        moduleAdapter = ModuleAdapter(
                context,
                repository,
                searchCallback,
                { visible -> if (visible) { show() } else { hide() } },
                { history -> onClickAdd(history) }
        )
        binding.searchHistories.adapter = moduleAdapter

        Completable.fromAction { SwipeActionAttachment().invoke(binding.searchHistories) }
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe()
                .addTo(disposables)
    }

    /**
     * Query table with passed word.
     *
     * @param s query string
     */
    fun query(s: CharSequence) {
        disposable?.dispose()
        disposable = moduleAdapter.query(s)
    }

    /**
     * Clear search history.
     */
    fun confirmClear() {
        val activityContext = binding.root.context
        if (activityContext is FragmentActivity) {
            ClearSearchHistoryDialogFragment().show(
                    activityContext.supportFragmentManager,
                    ClearSearchHistoryDialogFragment::class.java.simpleName
            )
        }
    }

    fun clear() {
        moduleAdapter.clear()
        hide()
    }

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

    /**
     * Hide this module.
     */
    fun hide() {
        if (binding.root.isVisible) {
            runOnMainThread { binding.root.isVisible = false }
                    .addTo(disposables)
        }
    }

    private fun runOnMainThread(action: () -> Unit) =
            Completable.fromAction { action() }
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            {},
                            Timber::e
                    )

    /**
     * Dispose last subscription.
     */
    fun dispose() {
        disposable?.dispose()
    }

}
{'fraction_non_alphanumeric': 0.09301998311286237, 'fraction_numerical': 0.00267379679144385, 'mean_word_length': 2.284195933456562, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '2016596', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1845, 'n_words': 405}