<commit_before>package com.ashish.movies.utils

import android.annotation.SuppressLint
import android.app.ProgressDialog
import android.content.Context
import android.content.DialogInterface
import android.support.annotation.StringRes
import android.support.v7.app.AlertDialog
import android.text.style.TypefaceSpan
import android.view.LayoutInflater
import android.view.View
import com.ashish.movies.R
import com.ashish.movies.ui.widget.FontTextView
import com.ashish.movies.utils.FontUtils.MONTSERRAT_MEDIUM
import com.ashish.movies.utils.FontUtils.MONTSERRAT_REGULAR
import com.ashish.movies.utils.FontUtils.getTypeface
import com.ashish.movies.utils.extensions.getTextWithCustomTypeface
import javax.inject.Inject

/**
 * Created by Ashish on Jan 30.
 */
class DialogUtils @Inject constructor(private val context: Context) {

    @SuppressLint("InflateParams")
    private val contentView: View = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null)

    private val mediumTypefaceSpan: TypefaceSpan = CustomTypefaceSpan(getTypeface(context, MONTSERRAT_MEDIUM))
    private val regularTypefaceSpan: TypefaceSpan = CustomTypefaceSpan(getTypeface(context, MONTSERRAT_REGULAR))

    private val contentText: FontTextView
    private var messageDialog: AlertDialog? = null
    private var progressDialog: ProgressDialog? = null

    init {
        contentText = contentView.findViewById(R.id.loading_text) as FontTextView
    }

    @Suppress("unused")
    fun showOkMessageDialog(@StringRes contentId: Int) {
        showOkMessageDialog(getString(contentId))
    }

    fun showOkMessageDialog(content: CharSequence?) {
        buildDialog().withContent(content)
                .withPositiveButton(getString(android.R.string.ok))
                .show()
    }

    fun buildDialog(): DialogUtils {
        if (messageDialog == null) {
            messageDialog = AlertDialog.Builder(context)
                    .setCancelable(false)
                    .create()
        }

        withTitle(null)
        withPositiveButton(null)
        withNegativeButton(null)
        return this
    }

    fun withTitle(@StringRes titleId: Int) = withTitle(getString(titleId))

    fun withTitle(title: CharSequence?): DialogUtils {
        checkMessageDialog()
        messageDialog?.setTitle(title.getTextWithCustomTypeface(mediumTypefaceSpan))
        return this
    }

    fun withContent(@StringRes contentId: Int) = withContent(getString(contentId))

    fun withContent(content: CharSequence?): DialogUtils {
        checkMessageDialog()
        messageDialog?.setMessage(content.getTextWithCustomTypeface(regularTypefaceSpan))
        return this
    }

    fun withPositiveButton(@StringRes positiveTextId: Int, positiveBtnClick: (() -> Unit)? = null): DialogUtils {
        return withPositiveButton(getString(positiveTextId), positiveBtnClick)
    }

    fun withPositiveButton(positiveText: CharSequence?, positiveBtnClick: (() -> Unit)? = null): DialogUtils {
        checkMessageDialog()

        val positiveBtnText = positiveText.getTextWithCustomTypeface(mediumTypefaceSpan)
        messageDialog?.setButton(DialogInterface.BUTTON_POSITIVE, positiveBtnText, { dialogInterface, which ->
            messageDialog?.dismiss()
            positiveBtnClick?.invoke()
        })

        return this
    }

    fun withNegativeButton(@StringRes negativeTextId: Int, negativeBtnClick: (() -> Unit)? = null): DialogUtils {
        return withNegativeButton(getString(negativeTextId), negativeBtnClick)
    }

    fun withNegativeButton(negativeText: CharSequence?, negativeBtnClick: (() -> Unit)? = null): DialogUtils {
        checkMessageDialog()

        val negativeBtnText = negativeText.getTextWithCustomTypeface(mediumTypefaceSpan)
        messageDialog?.setButton(DialogInterface.BUTTON_NEGATIVE, negativeBtnText, { dialogInterface, which ->
            messageDialog?.dismiss()
            negativeBtnClick?.invoke()
        })

        return this
    }

    private fun getString(@StringRes stringId: Int) = if (stringId != 0) context.getString(stringId) else null

    fun show() {
        checkMessageDialog()
        messageDialog?.show()
    }

    private fun checkMessageDialog() {
        if (messageDialog == null) {
            throw NullPointerException("You should call buildDialog() method first!")
        }
    }

    fun dismissMessageDialog() = messageDialog?.dismiss()

    fun showProgressDialog(@StringRes contentId: Int) {
        if (progressDialog == null) {
            progressDialog = ProgressDialog(context)
            progressDialog!!.setContentView(contentView)
            progressDialog!!.setCancelable(false)<commit_msg>Fixed custom progress dialog issue<commit_after>package com.ashish.movies.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.support.annotation.StringRes
import android.support.v7.app.AlertDialog
import android.text.style.TypefaceSpan
import android.view.LayoutInflater
import android.view.View
import com.ashish.movies.R
import com.ashish.movies.ui.widget.FontTextView
import com.ashish.movies.utils.FontUtils.MONTSERRAT_MEDIUM
import com.ashish.movies.utils.FontUtils.MONTSERRAT_REGULAR
import com.ashish.movies.utils.FontUtils.getTypeface
import com.ashish.movies.utils.extensions.getTextWithCustomTypeface
import javax.inject.Inject

/**
 * Created by Ashish on Jan 30.
 */
class DialogUtils @Inject constructor(private val context: Context) {

    @SuppressLint("InflateParams")
    private val contentView: View = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null)

    private val mediumTypefaceSpan: TypefaceSpan = CustomTypefaceSpan(getTypeface(context, MONTSERRAT_MEDIUM))
    private val regularTypefaceSpan: TypefaceSpan = CustomTypefaceSpan(getTypeface(context, MONTSERRAT_REGULAR))

    private val contentText: FontTextView
    private var messageDialog: AlertDialog? = null
    private var progressDialog: AlertDialog? = null

    init {
        contentText = contentView.findViewById(R.id.loading_text) as FontTextView
    }

    @Suppress("unused")
    fun showOkMessageDialog(@StringRes contentId: Int) {
        showOkMessageDialog(getString(contentId))
    }

    fun showOkMessageDialog(content: CharSequence?) {
        buildDialog().withContent(content)
                .withPositiveButton(getString(android.R.string.ok))
                .show()
    }

    fun buildDialog(): DialogUtils {
        if (messageDialog == null) {
            messageDialog = AlertDialog.Builder(context)
                    .setCancelable(false)
                    .create()
        }

        withTitle(null)
        withPositiveButton(null)
        withNegativeButton(null)
        return this
    }

    fun withTitle(@StringRes titleId: Int) = withTitle(getString(titleId))

    fun withTitle(title: CharSequence?): DialogUtils {
        checkMessageDialog()
        messageDialog?.setTitle(title.getTextWithCustomTypeface(mediumTypefaceSpan))
        return this
    }

    fun withContent(@StringRes contentId: Int) = withContent(getString(contentId))

    fun withContent(content: CharSequence?): DialogUtils {
        checkMessageDialog()
        messageDialog?.setMessage(content.getTextWithCustomTypeface(regularTypefaceSpan))
        return this
    }

    fun withPositiveButton(@StringRes positiveTextId: Int, positiveBtnClick: (() -> Unit)? = null): DialogUtils {
        return withPositiveButton(getString(positiveTextId), positiveBtnClick)
    }

    fun withPositiveButton(positiveText: CharSequence?, positiveBtnClick: (() -> Unit)? = null): DialogUtils {
        checkMessageDialog()

        val positiveBtnText = positiveText.getTextWithCustomTypeface(mediumTypefaceSpan)
        messageDialog?.setButton(DialogInterface.BUTTON_POSITIVE, positiveBtnText, { dialogInterface, which ->
            messageDialog?.dismiss()
            positiveBtnClick?.invoke()
        })

        return this
    }

    fun withNegativeButton(@StringRes negativeTextId: Int, negativeBtnClick: (() -> Unit)? = null): DialogUtils {
        return withNegativeButton(getString(negativeTextId), negativeBtnClick)
    }

    fun withNegativeButton(negativeText: CharSequence?, negativeBtnClick: (() -> Unit)? = null): DialogUtils {
        checkMessageDialog()

        val negativeBtnText = negativeText.getTextWithCustomTypeface(mediumTypefaceSpan)
        messageDialog?.setButton(DialogInterface.BUTTON_NEGATIVE, negativeBtnText, { dialogInterface, which ->
            messageDialog?.dismiss()
            negativeBtnClick?.invoke()
        })

        return this
    }

    private fun getString(@StringRes stringId: Int) = if (stringId != 0) context.getString(stringId) else null

    fun show() {
        checkMessageDialog()
        messageDialog?.show()
    }

    private fun checkMessageDialog() {
        if (messageDialog == null) {
            throw NullPointerException("You should call buildDialog() method first!")
        }
    }

    fun dismissMessageDialog() = messageDialog?.dismiss()

    fun showProgressDialog(@StringRes contentId: Int) {
        if (progressDialog == null) {
            progressDialog = AlertDialog.Builder(context)
                    .setView(contentView)
                    .setCancelable(false)
                    .create()
{'fraction_non_alphanumeric': 0.09127832407011544, 'fraction_numerical': 0.0008550662676357417, 'mean_word_length': 3.494236311239193, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '978072', 'n_tokens_mistral': 2504, 'n_tokens_neox': 2413, 'n_words': 482}