<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.browser.user_agent

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.annotation.LayoutRes
import androidx.core.os.bundleOf
import androidx.databinding.DataBindingUtil
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import jp.toastkid.lib.color.IconColorFinder
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.lib.view.CompoundDrawableColorApplier
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.databinding.DialogUserAgentBinding

/**
 * @author toastkidjp
 */
class UserAgentDialogFragment : BottomSheetDialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding: DialogUserAgentBinding =
                DataBindingUtil.inflate(inflater, LAYOUT_ID, container, false)

        val color = IconColorFinder.from(binding.root).invoke()
        CompoundDrawableColorApplier().invoke(color, binding.title)

        initializeList(binding.list)
        return binding.root
    }

    private fun initializeList(
        listView: ListView
    ) {
        listView.choiceMode = ListView.CHOICE_MODE_SINGLE
        val preferenceApplier = PreferenceApplier(listView.context)
        val currentIndex = UserAgent.findCurrentIndex(preferenceApplier.userAgent())
        val adapter = object : ArrayAdapter<CharSequence>(
            listView.context,
            android.R.layout.simple_list_item_single_choice,
            android.R.id.text1,
            UserAgent.titles()
        ) {
            override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                val view = super.getView(position, convertView, parent)
                val isItemChecked = position == currentIndex
                if (isItemChecked) {
                    listView.setItemChecked(position, true)
                }
                return view
            }
        }
        listView.adapter = adapter
        listView.setOnItemClickListener { _, _, position, _ ->
            val userAgent = UserAgent.values()[position]
            preferenceApplier.setUserAgent(userAgent.name)<commit_msg>refactor #313 Remove useless line separator.<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.browser.user_agent

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.annotation.LayoutRes
import androidx.core.os.bundleOf
import androidx.databinding.DataBindingUtil
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import jp.toastkid.lib.color.IconColorFinder
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.lib.view.CompoundDrawableColorApplier
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.databinding.DialogUserAgentBinding

/**
 * @author toastkidjp
 */
class UserAgentDialogFragment : BottomSheetDialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding: DialogUserAgentBinding =
                DataBindingUtil.inflate(inflater, LAYOUT_ID, container, false)

        val color = IconColorFinder.from(binding.root).invoke()
        CompoundDrawableColorApplier().invoke(color, binding.title)

        initializeList(binding.list)
        return binding.root
    }

    private fun initializeList(listView: ListView) {
        listView.choiceMode = ListView.CHOICE_MODE_SINGLE
        val preferenceApplier = PreferenceApplier(listView.context)
        val currentIndex = UserAgent.findCurrentIndex(preferenceApplier.userAgent())
        val adapter = object : ArrayAdapter<CharSequence>(
            listView.context,
            android.R.layout.simple_list_item_single_choice,
            android.R.id.text1,
            UserAgent.titles()
        ) {
            override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                val view = super.getView(position, convertView, parent)
                val isItemChecked = position == currentIndex
                if (isItemChecked) {
                    listView.setItemChecked(position, true)
                }
                return view
            }
        }
        listView.adapter = adapter
        listView.setOnItemClickListener { _, _, position, _ ->
            val userAgent = UserAgent.values()[position]
            preferenceApplier.setUserAgent(userAgent.name)
{'fraction_non_alphanumeric': 0.0787080007425283, 'fraction_numerical': 0.0038982736216818265, 'mean_word_length': 3.0602863602110024, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3755053', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1422, 'n_words': 309}