<commit_before>package jp.toastkid.yobidashi.search

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Build
import android.os.Bundle
import android.speech.RecognizerIntent
import android.support.annotation.ColorInt
import android.support.annotation.LayoutRes
import android.support.annotation.StringRes
import android.support.v4.graphics.ColorUtils
import android.support.v7.widget.Toolbar
import android.text.Editable
import android.text.TextWatcher
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.Spinner
import io.reactivex.Completable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.schedulers.Schedulers
import jp.toastkid.yobidashi.BaseActivity
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.databinding.*
import jp.toastkid.yobidashi.libs.Colors
import jp.toastkid.yobidashi.libs.Inputs
import jp.toastkid.yobidashi.libs.Toaster
import jp.toastkid.yobidashi.libs.preference.ColorPair
import jp.toastkid.yobidashi.search.favorite.FavoriteSearchActivity
import jp.toastkid.yobidashi.search.favorite.FavoriteSearchModule
import jp.toastkid.yobidashi.search.history.HistoryModule
import jp.toastkid.yobidashi.search.history.SearchHistoryActivity
import jp.toastkid.yobidashi.search.suggestion.SuggestionModule
import jp.toastkid.yobidashi.search.url_suggestion.UrlSuggestionModule
import jp.toastkid.yobidashi.search.voice.VoiceSearch
import timber.log.Timber

/**
 * Search activity.
 *
 * @author toastkidjp
 */
class SearchActivity : BaseActivity() {

    /**
     * Disposables.
     */
    private val disposables: CompositeDisposable = CompositeDisposable()

    /**
     * View binder.
     */
    private var binding: ActivitySearchBinding? = null

    /**
     * Favorite search module.
     */
    private var favoriteModule: FavoriteSearchModule? = null

    /**
     * History module.
     */
    private var historyModule: HistoryModule? = null

    /**
     * Suggestion module.
     */
    private var suggestionModule: SuggestionModule? = null

    /**
     * Suggestion module.
     */
    private var urlSuggestionModule: UrlSuggestionModule? = null

    /**
     * Does use voice search?
     */
    private var useVoice: Boolean = true

    /**
     * Without exit animation flag.
     */
    private var withoutExitAnimation: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(LAYOUT_ID)
        binding = DataBindingUtil.setContentView(this, LAYOUT_ID)
        binding?.activity = this
        binding?.searchClear?.setOnClickListener ({ v -> binding?.searchInput?.setText("") })
        SearchCategorySpinnerInitializer.invoke(binding?.searchCategories as Spinner)

        initFavoriteModule()

        initHistoryModule()

        initSearchInput()

        applyColor()

        suggestionModule = SuggestionModule(
                binding?.suggestionModule as ModuleSearchSuggestionBinding,
                binding?.searchInput as EditText,
                { suggestion -> search(binding?.searchCategories?.selectedItem.toString(), suggestion) },
                { suggestion -> search(binding?.searchCategories?.selectedItem.toString(), suggestion, true) },
                this::hideKeyboard
        )

        urlSuggestionModule = UrlSuggestionModule(
                binding?.urlSuggestionModule as ModuleUrlSuggestionBinding,
                { suggestion -> search(binding?.searchCategories?.selectedItem.toString(), suggestion) },
                { suggestion -> search(binding?.searchCategories?.selectedItem.toString(), suggestion, true) }
        )

        binding?.scroll?.setOnTouchListener({ v, event ->
            hideKeyboard()
            false
        })

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            binding?.searchBar?.transitionName = "share"
        }

        binding?.toolbar?.let {
            initToolbar(it)
            it.setNavigationIcon(null)
            it.setPadding(0,0,0,0)
            it.setContentInsetsAbsolute(0,0)

            it.inflateMenu(R.menu.search_menu)
            it.menu.findItem(R.id.suggestion_check)?.isChecked = preferenceApplier.isEnableSuggestion
            it.menu.findItem(R.id.history_check)?.isChecked = preferenceApplier.isEnableSearchHistory

            // Set query.
            intent?.getStringExtra(EXTRA_KEY_QUERY)?.let {
                binding?.searchInput?.setText(it)
                overridePendingTransition(0, 0)
                withoutExitAnimation = true
            }
        }

        Toaster.snackShort(
                binding?.background as View,
                getString(R.string.message_search_on_background),
                colorPair()
                )
    }

    override fun clickMenu(item: MenuItem): Boolean {
        val itemId = item.itemId
        return when (itemId) {
            R.id.suggestion_check -> {
                preferenceApplier.switchEnableSuggestion()
                item.isChecked = preferenceApplier.isEnableSuggestion
                true
            }
            R.id.history_check -> {
                preferenceApplier.switchEnableSearchHistory()
                item.isChecked = preferenceApplier.isEnableSearchHistory
                true
            }
            R.id.open_favorite_search -> {
                startActivity(FavoriteSearchActivity.makeIntent(this))
                true
            }
            R.id.open_search_history -> {
                startActivity(SearchHistoryActivity.makeIntent(this))
                true
            }
            else -> super.clickMenu(item)
        }
    }

    private fun initFavoriteModule() {
        Completable.fromAction {
            favoriteModule = FavoriteSearchModule(
                    binding?.favoriteModule as ModuleSearchFavoriteBinding,
                    { fav -> search(fav.category as String, fav.query as String) },
                    this::hideKeyboard,
                    { fav ->
                        binding?.searchInput?.setText("${fav.query} ")<commit_msg>Refactor clickMenu().<commit_after>package jp.toastkid.yobidashi.search

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Build
import android.os.Bundle
import android.speech.RecognizerIntent
import android.support.annotation.ColorInt
import android.support.annotation.LayoutRes
import android.support.annotation.StringRes
import android.support.v4.graphics.ColorUtils
import android.support.v7.widget.Toolbar
import android.text.Editable
import android.text.TextWatcher
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.Spinner
import io.reactivex.Completable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.schedulers.Schedulers
import jp.toastkid.yobidashi.BaseActivity
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.databinding.*
import jp.toastkid.yobidashi.libs.Colors
import jp.toastkid.yobidashi.libs.Inputs
import jp.toastkid.yobidashi.libs.Toaster
import jp.toastkid.yobidashi.libs.preference.ColorPair
import jp.toastkid.yobidashi.search.favorite.FavoriteSearchActivity
import jp.toastkid.yobidashi.search.favorite.FavoriteSearchModule
import jp.toastkid.yobidashi.search.history.HistoryModule
import jp.toastkid.yobidashi.search.history.SearchHistoryActivity
import jp.toastkid.yobidashi.search.suggestion.SuggestionModule
import jp.toastkid.yobidashi.search.url_suggestion.UrlSuggestionModule
import jp.toastkid.yobidashi.search.voice.VoiceSearch
import timber.log.Timber

/**
 * Search activity.
 *
 * @author toastkidjp
 */
class SearchActivity : BaseActivity() {

    /**
     * Disposables.
     */
    private val disposables: CompositeDisposable = CompositeDisposable()

    /**
     * View binder.
     */
    private var binding: ActivitySearchBinding? = null

    /**
     * Favorite search module.
     */
    private var favoriteModule: FavoriteSearchModule? = null

    /**
     * History module.
     */
    private var historyModule: HistoryModule? = null

    /**
     * Suggestion module.
     */
    private var suggestionModule: SuggestionModule? = null

    /**
     * Suggestion module.
     */
    private var urlSuggestionModule: UrlSuggestionModule? = null

    /**
     * Does use voice search?
     */
    private var useVoice: Boolean = true

    /**
     * Without exit animation flag.
     */
    private var withoutExitAnimation: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(LAYOUT_ID)
        binding = DataBindingUtil.setContentView(this, LAYOUT_ID)
        binding?.activity = this
        binding?.searchClear?.setOnClickListener ({ v -> binding?.searchInput?.setText("") })
        SearchCategorySpinnerInitializer.invoke(binding?.searchCategories as Spinner)

        initFavoriteModule()

        initHistoryModule()

        initSearchInput()

        applyColor()

        suggestionModule = SuggestionModule(
                binding?.suggestionModule as ModuleSearchSuggestionBinding,
                binding?.searchInput as EditText,
                { suggestion -> search(binding?.searchCategories?.selectedItem.toString(), suggestion) },
                { suggestion -> search(binding?.searchCategories?.selectedItem.toString(), suggestion, true) },
                this::hideKeyboard
        )

        urlSuggestionModule = UrlSuggestionModule(
                binding?.urlSuggestionModule as ModuleUrlSuggestionBinding,
                { suggestion -> search(binding?.searchCategories?.selectedItem.toString(), suggestion) },
                { suggestion -> search(binding?.searchCategories?.selectedItem.toString(), suggestion, true) }
        )

        binding?.scroll?.setOnTouchListener({ v, event ->
            hideKeyboard()
            false
        })

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            binding?.searchBar?.transitionName = "share"
        }

        binding?.toolbar?.let {
            initToolbar(it)
            it.setNavigationIcon(null)
            it.setPadding(0,0,0,0)
            it.setContentInsetsAbsolute(0,0)

            it.inflateMenu(R.menu.search_menu)
            it.menu.findItem(R.id.suggestion_check)?.isChecked = preferenceApplier.isEnableSuggestion
            it.menu.findItem(R.id.history_check)?.isChecked = preferenceApplier.isEnableSearchHistory

            // Set query.
            intent?.getStringExtra(EXTRA_KEY_QUERY)?.let {
                binding?.searchInput?.setText(it)
                overridePendingTransition(0, 0)
                withoutExitAnimation = true
            }
        }

        Toaster.snackShort(
                binding?.background as View,
                getString(R.string.message_search_on_background),
                colorPair()
                )
    }

    override fun clickMenu(item: MenuItem): Boolean = when (item.itemId) {
        R.id.suggestion_check -> {
            preferenceApplier.switchEnableSuggestion()
            item.isChecked = preferenceApplier.isEnableSuggestion
            true
        }
        R.id.history_check -> {
            preferenceApplier.switchEnableSearchHistory()
            item.isChecked = preferenceApplier.isEnableSearchHistory
            true
        }
        R.id.open_favorite_search -> {
            startActivity(FavoriteSearchActivity.makeIntent(this))
            true
        }
        R.id.open_search_history -> {
            startActivity(SearchHistoryActivity.makeIntent(this))
            true
        }
        else -> super.clickMenu(item)
    }

    private fun initFavoriteModule() {
        Completable.fromAction {
            favoriteModule = FavoriteSearchModule(
                    binding?.favoriteModule as ModuleSearchFavoriteBinding,
                    { fav -> search(fav.category as String, fav.query as String) },
                    this::hideKeyboard,
                    { fav ->
                        binding?.searchInput?.setText("${fav.query} ")
{'fraction_non_alphanumeric': 0.08985971943887776, 'fraction_numerical': 0.0016032064128256513, 'mean_word_length': 2.557456515540348, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '4043720', 'n_tokens_mistral': 3509, 'n_tokens_neox': 3300, 'n_words': 549}