<commit_before>/*
 * Copyright 2015-2018 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guerinet.fg.base

import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.support.annotation.ColorInt
import android.support.annotation.DimenRes
import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
import android.support.v4.content.ContextCompat
import android.support.v4.graphics.drawable.DrawableCompat
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import com.guerinet.fg.FormGenerator
import com.guerinet.fg.R

/**
 * Builder for a [TextView] form item (buttons, texts, switches, inputs)
 * @author <NAME>
 * @since 2.0.0
 *
 * @param fg                    [FormGenerator] instance
 * @param view                  Form item [View]
 * @param childView             [View] of the main child for this item
 * @param isDefaultBackground   True if we should use the default background, false otherwise
 */
@Suppress("UNCHECKED_CAST")
open class BaseTextViewItem<T : BaseTextViewItem<T, TextView>, out V : TextView>(
        fg: FormGenerator,
        view: View,
        protected val childView: V,
        isDefaultBackground: Boolean) : BaseLineItem<T>(fg, view, view.findViewById(R.id.fg_line)
) {

    /**
     * List of empty [Icon]s to keep track of the compound drawables to set
     *  Order: start, top, end, bottom
     */
    private val icons: Array<Icon> = arrayOf(Icon(), Icon(), Icon(), Icon())

    init {
        view.isClickable = false

        // Background
        val backgroundId = fg.defaults.backgroundId
        if (isDefaultBackground && backgroundId != null) {
            @Suppress("LeakingThis")
            backgroundId(backgroundId)
        }

        // Text Color
        textColor(fg.defaults.textColor)

        // Text Size
        textSizeId(fg.defaults.textSizeId)

        // Padding
        val paddingSize = fg.defaults.paddingSize
        if (paddingSize != null) {
            padding(paddingSize)
        }

        // Typeface
        @Suppress("LeakingThis")
        typeface(fg.defaults.textTypeface)
    }

    /**
     * @return Item with the [text] set
     */
    fun text(text: String?): T {
        childView.text = text
        return this as T
    }

    /**
     * @return Item with the text with [stringId] set
     */
    fun text(@StringRes stringId: Int): T {
        childView.setText(stringId)
        return this as T
    }

    /**
     * @return Item with the [hint] set
     */
    open fun hint(hint: String?): T {
        childView.hint = hint
        return this as T
    }

    /**
     * @return Item with the hint with [stringId] set
     */
    open fun hint(@StringRes stringId: Int): T {
        childView.setHint(stringId)
        return this as T
    }

    /**
     * Sets whether the returned item [isFocusable] or not
     */
    fun isFocusable(isFocusable: Boolean): T {
        childView.isFocusable = isFocusable
        return this as T
    }

    /**
     * Sets whether the returned item [isEnabled] or not
     */
    fun isEnabled(isEnabled: Boolean): T {
        childView.isEnabled = isEnabled
        return this as T
    }

    /**
     * @return Item with the text set to the given [color]
     */
    fun textColor(@ColorInt color: Int): T {
        childView.setTextColor(color)
        return this as T
    }

    /**
     * @return Item with the text size set to the dimension with the [sizeId]
     */
    fun textSizeId(@DimenRes sizeId: Int?): T {
        // If it's null, don't do anything
        if (sizeId != null) {
            childView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                    fg.container.resources.getDimension(sizeId))
        }
        return this as T
    }

    /**
     * Sets the [start], [top], [end], and [bottom] paddings (in pixels) on the returned item.
     *  If one of these is null, the padding remains what it is currently for that side
     */
    fun padding(start: Int? = null, top: Int? = null, end: Int? = null, bottom: Int? = null): T {
        // Use the current paddings if one of them is null
        childView.setPaddingRelative(start ?: childView.paddingStart, top ?: childView.paddingTop,
                end ?: childView.paddingEnd, bottom ?: childView.paddingBottom)
        return this as T
    }

    /**
     * Sets the paddings on the returned item using the [startDp], [topDp], [endDp], and [bottomDp].
     *  If one of these is null, the padding remains what it is currently for that side
     */
    fun dpPadding(startDp: Float? = null, topDp: Float? = null, endDp: Float? = null,
                  bottomDp: Float? = null): T {
        return padding(if (startDp != null) dpToPixels(startDp) else null,
                if (topDp != null) dpToPixels(topDp) else null,
                if (endDp != null) dpToPixels(endDp) else null,
                if (bottomDp != null) dpToPixels(bottomDp) else null)
    }

    /**
     * Sets the paddings on the returned item using the [startId], [topId], [endId], and [bottomId]
     *  dimension resource Ids. If one of these is null, the padding remains what it is currently
     *  for that side
     */
    fun paddingId(@DimenRes startId: Int? = null, @DimenRes topId: Int? = null,
                  @DimenRes endId: Int? = null, @DimenRes bottomId: Int? = null): T {
        return padding(if (startId != null) dimenToPixels(startId) else null,
                if (topId != null) dimenToPixels(topId) else null,
                if (endId != null) dimenToPixels(endId) else null,
                if (bottomId != null) dimenToPixels(bottomId) else null)
    }

    /**
     * @return Item with the given [padding] (in pixels) on all 4 sides
     */
    fun padding(padding: Int): T = padding(padding, padding, padding, padding)

    /**
     * @return Item with padding, in [dps], on all 4 sides
     */
    fun dpPadding(dps: Float): T = dpPadding(dps, dps, dps, dps)

    /**
     * @return Item with padding from the given [dimenId] on all 4 sides
     */
    fun paddingId(dimenId: Int): T = paddingId(dimenId, dimenId, dimenId, dimenId)<commit_msg>Renamed padding to pixel padding.<commit_after>/*
 * Copyright 2015-2018 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guerinet.fg.base

import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.support.annotation.ColorInt
import android.support.annotation.DimenRes
import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
import android.support.v4.content.ContextCompat
import android.support.v4.graphics.drawable.DrawableCompat
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import com.guerinet.fg.FormGenerator
import com.guerinet.fg.R

/**
 * Builder for a [TextView] form item (buttons, texts, switches, inputs)
 * @author <NAME>
 * @since 2.0.0
 *
 * @param fg                    [FormGenerator] instance
 * @param view                  Form item [View]
 * @param childView             [View] of the main child for this item
 * @param isDefaultBackground   True if we should use the default background, false otherwise
 */
@Suppress("UNCHECKED_CAST")
open class BaseTextViewItem<T : BaseTextViewItem<T, TextView>, out V : TextView>(
        fg: FormGenerator,
        view: View,
        protected val childView: V,
        isDefaultBackground: Boolean) : BaseLineItem<T>(fg, view, view.findViewById(R.id.fg_line)
) {

    /**
     * List of empty [Icon]s to keep track of the compound drawables to set
     *  Order: start, top, end, bottom
     */
    private val icons: Array<Icon> = arrayOf(Icon(), Icon(), Icon(), Icon())

    init {
        view.isClickable = false

        // Background
        val backgroundId = fg.defaults.backgroundId
        if (isDefaultBackground && backgroundId != null) {
            @Suppress("LeakingThis")
            backgroundId(backgroundId)
        }

        // Text Color
        textColor(fg.defaults.textColor)

        // Text Size
        textSizeId(fg.defaults.textSizeId)

        // Padding
        val paddingSize = fg.defaults.paddingSize
        if (paddingSize != null) {
            pixelPadding(paddingSize)
        }

        // Typeface
        @Suppress("LeakingThis")
        typeface(fg.defaults.textTypeface)
    }

    /**
     * @return Item with the [text] set
     */
    fun text(text: String?): T {
        childView.text = text
        return this as T
    }

    /**
     * @return Item with the text with [stringId] set
     */
    fun text(@StringRes stringId: Int): T {
        childView.setText(stringId)
        return this as T
    }

    /**
     * @return Item with the [hint] set
     */
    open fun hint(hint: String?): T {
        childView.hint = hint
        return this as T
    }

    /**
     * @return Item with the hint with [stringId] set
     */
    open fun hint(@StringRes stringId: Int): T {
        childView.setHint(stringId)
        return this as T
    }

    /**
     * Sets whether the returned item [isFocusable] or not
     */
    fun isFocusable(isFocusable: Boolean): T {
        childView.isFocusable = isFocusable
        return this as T
    }

    /**
     * Sets whether the returned item [isEnabled] or not
     */
    fun isEnabled(isEnabled: Boolean): T {
        childView.isEnabled = isEnabled
        return this as T
    }

    /**
     * @return Item with the text set to the given [color]
     */
    fun textColor(@ColorInt color: Int): T {
        childView.setTextColor(color)
        return this as T
    }

    /**
     * @return Item with the text size set to the dimension with the [sizeId]
     */
    fun textSizeId(@DimenRes sizeId: Int?): T {
        // If it's null, don't do anything
        if (sizeId != null) {
            childView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                    fg.container.resources.getDimension(sizeId))
        }
        return this as T
    }

    /**
     * Sets the [start], [top], [end], and [bottom] paddings (in pixels) on the returned item.
     *  If one of these is null, the padding remains what it is currently for that side
     */
    fun pixelPadding(start: Int? = null, top: Int? = null, end: Int? = null, bottom: Int? = null)
            : T {
        // Use the current paddings if one of them is null
        childView.setPaddingRelative(start ?: childView.paddingStart, top ?: childView.paddingTop,
                end ?: childView.paddingEnd, bottom ?: childView.paddingBottom)
        return this as T
    }

    /**
     * Sets the paddings on the returned item using the [startDp], [topDp], [endDp], and [bottomDp].
     *  If one of these is null, the padding remains what it is currently for that side
     */
    fun dpPadding(startDp: Float? = null, topDp: Float? = null, endDp: Float? = null,
                  bottomDp: Float? = null): T {
        return pixelPadding(if (startDp != null) dpToPixels(startDp) else null,
                if (topDp != null) dpToPixels(topDp) else null,
                if (endDp != null) dpToPixels(endDp) else null,
                if (bottomDp != null) dpToPixels(bottomDp) else null)
    }

    /**
     * Sets the paddings on the returned item using the [startId], [topId], [endId], and [bottomId]
     *  dimension resource Ids. If one of these is null, the padding remains what it is currently
     *  for that side
     */
    fun paddingId(@DimenRes startId: Int? = null, @DimenRes topId: Int? = null,
                  @DimenRes endId: Int? = null, @DimenRes bottomId: Int? = null): T {
        return pixelPadding(if (startId != null) dimenToPixels(startId) else null,
                if (topId != null) dimenToPixels(topId) else null,
                if (endId != null) dimenToPixels(endId) else null,
                if (bottomId != null) dimenToPixels(bottomId) else null)
    }

    /**
     * @return Item with the given [padding] (in pixels) on all 4 sides
     */
    fun pixelPadding(padding: Int): T = pixelPadding(padding, padding, padding, padding)

    /**
     * @return Item with padding, in [dps], on all 4 sides
     */
    fun dpPadding(dps: Float): T = dpPadding(dps, dps, dps, dps)

    /**
     * @return Item with padding from the given [dimenId] on all 4 sides
     */
    fun paddingId(dimenId: Int): T = paddingId(dimenId, dimenId, dimenId, dimenId)
{'fraction_non_alphanumeric': 0.10085090640029597, 'fraction_numerical': 0.0029596744358120607, 'mean_word_length': 2.4139934326850216, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '2369381', 'n_tokens_mistral': 4006, 'n_tokens_neox': 3893, 'n_words': 1329}