Convert AlertDialogBuilder to Kotlin

pull/1964/head
Alex Baker 2 years ago
parent 9a33f54202
commit 437d69d741

@ -1,131 +0,0 @@
package org.tasks.dialogs;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.ListAdapter;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import java.util.List;
public class AlertDialogBuilder {
private final AlertDialog.Builder builder;
private final Context context;
AlertDialogBuilder(Context context) {
this.context = context;
builder = new MaterialAlertDialogBuilder(context);
}
public AlertDialogBuilder setMessage(int message, Object... formatArgs) {
return setMessage(context.getString(message, formatArgs));
}
public AlertDialogBuilder setMessage(String message) {
builder.setMessage(message);
return this;
}
public AlertDialogBuilder setPositiveButton(
int ok, DialogInterface.OnClickListener onClickListener) {
builder.setPositiveButton(ok, onClickListener);
return this;
}
public AlertDialogBuilder setNegativeButton(
int cancel, DialogInterface.OnClickListener onClickListener) {
builder.setNegativeButton(cancel, onClickListener);
return this;
}
public AlertDialogBuilder setTitle(int title) {
builder.setTitle(title);
return this;
}
public AlertDialogBuilder setTitle(int title, Object... formatArgs) {
builder.setTitle(context.getString(title, formatArgs));
return this;
}
public AlertDialogBuilder setItems(
List<String> strings, DialogInterface.OnClickListener onClickListener) {
return setItems(strings.toArray(new String[0]), onClickListener);
}
public AlertDialogBuilder setItems(
String[] strings, DialogInterface.OnClickListener onClickListener) {
builder.setItems(strings.clone(), onClickListener);
return this;
}
public AlertDialogBuilder setAdapter(
ListAdapter adapter, DialogInterface.OnClickListener onClickListener) {
builder.setAdapter(adapter, onClickListener);
return this;
}
public AlertDialogBuilder setView(View dialogView) {
builder.setView(dialogView);
return this;
}
public AlertDialogBuilder setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
builder.setOnCancelListener(onCancelListener);
return this;
}
public AlertDialogBuilder setSingleChoiceItems(
List<String> strings, int selectedIndex, DialogInterface.OnClickListener onClickListener) {
return setSingleChoiceItems(strings.toArray(new String[0]), selectedIndex, onClickListener);
}
public AlertDialogBuilder setSingleChoiceItems(
String[] strings, int selectedIndex, OnClickListener onClickListener) {
builder.setSingleChoiceItems(strings, selectedIndex, onClickListener);
return this;
}
public AlertDialogBuilder setSingleChoiceItems(
ListAdapter adapter, int selectedIndex, DialogInterface.OnClickListener onClickListener) {
builder.setSingleChoiceItems(adapter, selectedIndex, onClickListener);
return this;
}
public AlertDialogBuilder setNeutralButton(
int resId, DialogInterface.OnClickListener onClickListener) {
builder.setNeutralButton(resId, onClickListener);
return this;
}
AlertDialogBuilder setTitle(String title) {
builder.setTitle(title);
return this;
}
public AlertDialogBuilder setOnDismissListener(
DialogInterface.OnDismissListener onDismissListener) {
builder.setOnDismissListener(onDismissListener);
return this;
}
public AlertDialogBuilder setCancelable(boolean cancelable) {
builder.setCancelable(cancelable);
return this;
}
public AlertDialog create() {
return builder.create();
}
public AlertDialog show() {
AlertDialog dialog = create();
dialog.show();
return dialog;
}
}

@ -0,0 +1,135 @@
package org.tasks.dialogs
import android.content.Context
import android.content.DialogInterface
import android.view.View
import android.widget.ListAdapter
import androidx.appcompat.app.AlertDialog
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class AlertDialogBuilder internal constructor(private val context: Context) {
private val builder: AlertDialog.Builder
init {
builder = MaterialAlertDialogBuilder(context)
}
fun setMessage(message: Int, vararg formatArgs: Any?): AlertDialogBuilder {
return setMessage(context.getString(message, *formatArgs))
}
fun setMessage(message: String?): AlertDialogBuilder {
builder.setMessage(message)
return this
}
fun setPositiveButton(
ok: Int, onClickListener: DialogInterface.OnClickListener?
): AlertDialogBuilder {
builder.setPositiveButton(ok, onClickListener)
return this
}
fun setNegativeButton(
cancel: Int, onClickListener: DialogInterface.OnClickListener?
): AlertDialogBuilder {
builder.setNegativeButton(cancel, onClickListener)
return this
}
fun setTitle(title: Int): AlertDialogBuilder {
builder.setTitle(title)
return this
}
fun setTitle(title: Int, vararg formatArgs: Any?): AlertDialogBuilder {
builder.setTitle(context.getString(title, *formatArgs))
return this
}
fun setItems(
strings: List<String>, onClickListener: DialogInterface.OnClickListener?
): AlertDialogBuilder {
return setItems(strings.toTypedArray(), onClickListener)
}
fun setItems(
strings: Array<String>, onClickListener: DialogInterface.OnClickListener?
): AlertDialogBuilder {
builder.setItems(strings.clone(), onClickListener)
return this
}
fun setAdapter(
adapter: ListAdapter?, onClickListener: DialogInterface.OnClickListener?
): AlertDialogBuilder {
builder.setAdapter(adapter, onClickListener)
return this
}
fun setView(dialogView: View?): AlertDialogBuilder {
builder.setView(dialogView)
return this
}
fun setOnCancelListener(onCancelListener: DialogInterface.OnCancelListener?): AlertDialogBuilder {
builder.setOnCancelListener(onCancelListener)
return this
}
fun setSingleChoiceItems(
strings: List<String>, selectedIndex: Int, onClickListener: DialogInterface.OnClickListener?
): AlertDialogBuilder {
return setSingleChoiceItems(strings.toTypedArray(), selectedIndex, onClickListener)
}
fun setSingleChoiceItems(
strings: Array<String>?,
selectedIndex: Int,
onClickListener: DialogInterface.OnClickListener?
): AlertDialogBuilder {
builder.setSingleChoiceItems(strings, selectedIndex, onClickListener)
return this
}
fun setSingleChoiceItems(
adapter: ListAdapter?, selectedIndex: Int, onClickListener: DialogInterface.OnClickListener?
): AlertDialogBuilder {
builder.setSingleChoiceItems(adapter, selectedIndex, onClickListener)
return this
}
fun setNeutralButton(
resId: Int, onClickListener: DialogInterface.OnClickListener?
): AlertDialogBuilder {
builder.setNeutralButton(resId, onClickListener)
return this
}
fun setTitle(title: String?): AlertDialogBuilder {
builder.setTitle(title)
return this
}
fun setOnDismissListener(
onDismissListener: DialogInterface.OnDismissListener?
): AlertDialogBuilder {
builder.setOnDismissListener(onDismissListener)
return this
}
fun setCancelable(cancelable: Boolean): AlertDialogBuilder {
builder.setCancelable(cancelable)
return this
}
fun create(): AlertDialog {
return builder.create()
}
fun show(): AlertDialog {
val dialog = create()
dialog.show()
return dialog
}
}
Loading…
Cancel
Save