<commit_before>/*
 * Copyright 2014 JetBrains s.r.o.
 *
 * 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 kotlinx.android.koan

import android.view.View
import android.view.KeyEvent
import android.widget.Toast
import android.widget.ListAdapter
import android.content.DialogInterface.OnClickListener
import android.content.DialogInterface
import android.content.DialogInterface.OnKeyListener
import android.content.Context
import android.app.Fragment
import android.app.AlertDialog
import android.graphics.drawable.Drawable
import android.database.Cursor
import android.view.ViewManager

public fun Fragment.toast(textResource: Int): Unit = ctx.toast(textResource)
public fun Context.toast(textResource: Int) {
    uiThread { Toast.makeText(this, textResource, Toast.LENGTH_SHORT).show() }
}

public fun Fragment.toast(text: CharSequence): Unit = ctx.toast(text)
public fun Context.toast(text: CharSequence) {
    uiThread { Toast.makeText(this, text, Toast.LENGTH_SHORT).show() }
}

public fun Fragment.longToast(textResource: Int): Unit = ctx.longToast(textResource)
public fun Context.longToast(textResource: Int) {
    uiThread { Toast.makeText(this, textResource, Toast.LENGTH_LONG).show() }
}

public fun Fragment.longToast(text: CharSequence): Unit = ctx.longToast(text)
public fun Context.longToast(text: CharSequence) {
    uiThread { Toast.makeText(this, text, Toast.LENGTH_LONG).show() }
}

private val defaultOnCancel = {}

public fun Fragment.selector(
    title: CharSequence = "",
    items: List<CharSequence>,
    onCancel: () -> Unit = defaultOnCancel,
    onClick: (Int) -> Unit): Unit =
    ctx.selector(title, items, onCancel, onClick)

public fun Fragment.alert(message: String, title: String? = null, init: AlertDialogBuilder.() -> Unit = {}): AlertDialogBuilder = ctx.alert(message, title, init)
public fun Context.alert(message: String, title: String? = null, init: AlertDialogBuilder.() -> Unit = {}): AlertDialogBuilder {
    return AlertDialogBuilder(this) {
        if (title != null) {
            title(title)
        }
        message(message)
        init()
    }
}

public fun Fragment.alert(message: Int, title: Int, init: AlertDialogBuilder.() -> Unit = {}): AlertDialogBuilder = ctx.alert(message, title, init)
public fun Context.alert(message: Int, title: Int, init: AlertDialogBuilder.() -> Unit = {}): AlertDialogBuilder {
    return AlertDialogBuilder(this) {
        title(title)
        message(message)
        init()
    }
}

public fun Fragment.alert(init: AlertDialogBuilder.() -> Unit): AlertDialogBuilder = AlertDialogBuilder(ctx, init)
public fun Context.alert(init: AlertDialogBuilder.() -> Unit): AlertDialogBuilder = AlertDialogBuilder(this, init)

public fun Context.selector(
    title: CharSequence = "",
    items: List<CharSequence>,
    onCancel: () -> Unit = defaultOnCancel,
    onClick: (Int) -> Unit) {
    uiThread {
        AlertDialogBuilder(this) {
            title(title)
            items(items, onClick)
            onCancel(onCancel)
        }.show()
    }
}

public class AlertDialogBuilder(val ctx: Context, val init: AlertDialogBuilder.() -> Unit = defaultInit) {
    private val builder: AlertDialog.Builder = AlertDialog.Builder(ctx)
    protected var dialog: AlertDialog? = null;

    {
        init()
    }

    public fun dismiss() {
        dialog?.dismiss()
    }

    public fun show(): AlertDialogBuilder {<commit_msg>ProgressDialog helpers<commit_after>/*
 * Copyright 2014 JetBrains s.r.o.
 *
 * 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 kotlinx.android.koan

import android.view.View
import android.view.KeyEvent
import android.widget.Toast
import android.widget.ListAdapter
import android.content.DialogInterface.OnClickListener
import android.content.DialogInterface
import android.content.DialogInterface.OnKeyListener
import android.content.Context
import android.app.Fragment
import android.app.AlertDialog
import android.graphics.drawable.Drawable
import android.database.Cursor
import android.view.ViewManager
import android.app.ProgressDialog

public fun Fragment.toast(textResource: Int): Unit = ctx.toast(textResource)
public fun Context.toast(textResource: Int) {
    uiThread { Toast.makeText(this, textResource, Toast.LENGTH_SHORT).show() }
}

public fun Fragment.toast(text: CharSequence): Unit = ctx.toast(text)
public fun Context.toast(text: CharSequence) {
    uiThread { Toast.makeText(this, text, Toast.LENGTH_SHORT).show() }
}

public fun Fragment.longToast(textResource: Int): Unit = ctx.longToast(textResource)
public fun Context.longToast(textResource: Int) {
    uiThread { Toast.makeText(this, textResource, Toast.LENGTH_LONG).show() }
}

public fun Fragment.longToast(text: CharSequence): Unit = ctx.longToast(text)
public fun Context.longToast(text: CharSequence) {
    uiThread { Toast.makeText(this, text, Toast.LENGTH_LONG).show() }
}

private val defaultOnCancel = {}

public fun Fragment.selector(
    title: CharSequence = "",
    items: List<CharSequence>,
    onCancel: () -> Unit = defaultOnCancel,
    onClick: (Int) -> Unit): Unit =
    ctx.selector(title, items, onCancel, onClick)

public fun Fragment.alert(message: String, title: String? = null, init: AlertDialogBuilder.() -> Unit = {}): AlertDialogBuilder = ctx.alert(message, title, init)
public fun Context.alert(message: String, title: String? = null, init: AlertDialogBuilder.() -> Unit = {}): AlertDialogBuilder {
    return AlertDialogBuilder(this) {
        if (title != null) {
            title(title)
        }
        message(message)
        init()
    }
}

public fun Fragment.alert(message: Int, title: Int, init: AlertDialogBuilder.() -> Unit = {}): AlertDialogBuilder = ctx.alert(message, title, init)
public fun Context.alert(message: Int, title: Int, init: AlertDialogBuilder.() -> Unit = {}): AlertDialogBuilder {
    return AlertDialogBuilder(this) {
        title(title)
        message(message)
        init()
    }
}

public fun Fragment.alert(init: AlertDialogBuilder.() -> Unit): AlertDialogBuilder = AlertDialogBuilder(ctx, init)
public fun Context.alert(init: AlertDialogBuilder.() -> Unit): AlertDialogBuilder = AlertDialogBuilder(this, init)

public fun Fragment.progressDialog(message: String? = null, title: String? = null, init: ProgressDialog.() -> Unit = {}): ProgressDialog {
    return getActivity().progressDialog(false, message, title, init)
}

public fun Context.progressDialog(message: String? = null, title: String? = null, init: ProgressDialog.() -> Unit = {}): ProgressDialog {
    return progressDialog(false, message, title, init)
}

public fun Fragment.indeterminateProgressDialog(message: String? = null, title: String? = null, init: ProgressDialog.() -> Unit = {}): ProgressDialog {
    return getActivity().progressDialog(true, message, title, init)
}

public fun Context.indeterminateProgressDialog(message: String? = null, title: String? = null, init: ProgressDialog.() -> Unit = {}): ProgressDialog {
    return progressDialog(true, message, title, init)
}

private fun Context.progressDialog(indeterminate: Boolean, message: String? = null, title: String? = null, init: ProgressDialog.() -> Unit = {}): ProgressDialog {
    val dialog = ProgressDialog(this)
    dialog.setIndeterminate(indeterminate)
    if (message != null) dialog.setMessage(message)
    if (title != null) dialog.setTitle(title)
    dialog.init()
    dialog.show()
    return dialog
}

public fun Context.selector(
    title: CharSequence = "",
    items: List<CharSequence>,
    onCancel: () -> Unit = defaultOnCancel,
    onClick: (Int) -> Unit) {
    uiThread {
        AlertDialogBuilder(this) {
            title(title)
            items(items, onClick)
            onCancel(onCancel)
        }.show()
    }
}

public class AlertDialogBuilder(val ctx: Context, val init: AlertDialogBuilder.() -> Unit = defaultInit) {
    private val builder: AlertDialog.Builder = AlertDialog.Builder(ctx)
    protected var dialog: AlertDialog? = null;

    {
        init()
    }

    public fun dismiss() {
        dialog?.dismiss()
    }

    public fun show(): AlertDialogBuilder {
{'fraction_non_alphanumeric': 0.11514818880351263, 'fraction_numerical': 0.001756311745334797, 'mean_word_length': 4.12141652613828, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 34, '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': '6616795', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2515, 'n_words': 729}