<commit_before>/*
 * Licensed under Apache-2.0
 *
 * Designed and developed by <NAME> (@afollestad)
 */
package com.afollestad.materialdialogs.color

import android.R.attr
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.WhichButton.POSITIVE
import com.afollestad.materialdialogs.actions.hasActionButtons
import com.afollestad.materialdialogs.actions.setActionButtonEnabled
import com.afollestad.materialdialogs.color.utils.getColor
import com.afollestad.materialdialogs.color.utils.isColorDark
import com.afollestad.materialdialogs.color.utils.setVisibleOrGone

internal class ColorGridViewHolder(
  itemView: View,
  private val adapter: ColorGridAdapter
) : RecyclerView.ViewHolder(itemView), OnClickListener {

  init {
    itemView.setOnClickListener(this)
  }

  val colorCircle: ColorCircleView? = itemView.findViewById(R.id.color_view)
  val iconView: ImageView = itemView.findViewById(R.id.icon)

  override fun onClick(view: View) = adapter.itemSelected(adapterPosition)
}

/** @author <NAME> (afollestad */
internal class ColorGridAdapter(
  private val dialog: MaterialDialog,
  private val colors: IntArray,
  private val subColors: Array<IntArray>?,
  @ColorInt private val initialSelection: Int?,
  private val waitForPositiveButton: Boolean,
  private val callback: ColorCallback
) : RecyclerView.Adapter<ColorGridViewHolder>() {

  private val upIcon =
    if (getColor(dialog.windowContext, attr = attr.textColorPrimary).isColorDark())
      R.drawable.icon_back_black
    else R.drawable.icon_back_white

  private var selectedTopIndex: Int = -1
  private var selectedSubIndex: Int = -1
  private var inSub: Boolean = false

  internal fun itemSelected(index: Int) {
    if (inSub && index == 0) {
      inSub = false
      notifyDataSetChanged()
      return
    }

    dialog.setActionButtonEnabled(POSITIVE, true)

    if (inSub) {
      val previousSelection = selectedSubIndex
      selectedSubIndex = index
      notifyItemChanged(previousSelection)
      notifyItemChanged(selectedSubIndex)
      invokeCallback()
      return
    }

    if (index != selectedTopIndex) {
      // Different than previous selected top, reset sub index
      selectedSubIndex = -1
    }

    selectedTopIndex = index
    if (subColors != null) {
      inSub = true
      // Preselect top color in sub-colors if it exists
      selectedSubIndex = subColors[selectedTopIndex].indexOfFirst { it == colors[selectedTopIndex] }
    }

    invokeCallback()
    notifyDataSetChanged()
  }

  fun selectedColor(): Int? {
    if (selectedTopIndex > -1) {
      if (selectedSubIndex > -1 && subColors != null) {
        return subColors[selectedTopIndex][selectedSubIndex - 1]
      }
      return colors[selectedTopIndex]
    }
    return null<commit_msg>Fix sub-color preselection index issue<commit_after>/*
 * Licensed under Apache-2.0
 *
 * Designed and developed by <NAME> (@afollestad)
 */
package com.afollestad.materialdialogs.color

import android.R.attr
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.WhichButton.POSITIVE
import com.afollestad.materialdialogs.actions.hasActionButtons
import com.afollestad.materialdialogs.actions.setActionButtonEnabled
import com.afollestad.materialdialogs.color.utils.getColor
import com.afollestad.materialdialogs.color.utils.isColorDark
import com.afollestad.materialdialogs.color.utils.setVisibleOrGone

internal class ColorGridViewHolder(
  itemView: View,
  private val adapter: ColorGridAdapter
) : RecyclerView.ViewHolder(itemView), OnClickListener {

  init {
    itemView.setOnClickListener(this)
  }

  val colorCircle: ColorCircleView? = itemView.findViewById(R.id.color_view)
  val iconView: ImageView = itemView.findViewById(R.id.icon)

  override fun onClick(view: View) = adapter.itemSelected(adapterPosition)
}

/** @author <NAME> (afollestad */
internal class ColorGridAdapter(
  private val dialog: MaterialDialog,
  private val colors: IntArray,
  private val subColors: Array<IntArray>?,
  @ColorInt private val initialSelection: Int?,
  private val waitForPositiveButton: Boolean,
  private val callback: ColorCallback
) : RecyclerView.Adapter<ColorGridViewHolder>() {

  private val upIcon =
    if (getColor(dialog.windowContext, attr = attr.textColorPrimary).isColorDark())
      R.drawable.icon_back_black
    else R.drawable.icon_back_white

  private var selectedTopIndex: Int = -1
  private var selectedSubIndex: Int = -1
  private var inSub: Boolean = false

  internal fun itemSelected(index: Int) {
    if (inSub && index == 0) {
      inSub = false
      notifyDataSetChanged()
      return
    }

    dialog.setActionButtonEnabled(POSITIVE, true)

    if (inSub) {
      val previousSelection = selectedSubIndex
      selectedSubIndex = index
      notifyItemChanged(previousSelection)
      notifyItemChanged(selectedSubIndex)
      invokeCallback()
      return
    }

    if (index != selectedTopIndex) {
      // Different than previous selected top, reset sub index
      selectedSubIndex = -1
    }

    selectedTopIndex = index
    if (subColors != null) {
      inSub = true
      // Preselect top color in sub-colors if it exists
      selectedSubIndex = subColors[selectedTopIndex].indexOfFirst { it == colors[selectedTopIndex] }
      if (selectedSubIndex > -1) {
        // Compensate for the go-up button
        selectedSubIndex++
      }
    }

    invokeCallback()
    notifyDataSetChanged()
  }

  fun selectedColor(): Int? {
    if (selectedTopIndex > -1) {
      if (selectedSubIndex > -1 && subColors != null) {
        return subColors[selectedTopIndex][selectedSubIndex - 1]
      }
      return colors[selectedTopIndex]
    }
    return null
{'fraction_non_alphanumeric': 0.085824493731919, 'fraction_numerical': 0.003053680488588878, 'mean_word_length': 4.022598870056497, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, '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': '141953', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1778, 'n_words': 416}