diff --git a/AnkiDroid/kotlinMigration.gradle b/AnkiDroid/kotlinMigration.gradle index 225b0abf36..45448327ee 100644 --- a/AnkiDroid/kotlinMigration.gradle +++ b/AnkiDroid/kotlinMigration.gradle @@ -43,7 +43,7 @@ permission notice: // Example of class name: "/com/ichi2/anki/UIUtils.kt" // Ensure that it starts with '/' (slash) def source = Source.MAIN -def className = "/com/ichi2/themes/StyledProgressDialog.kt" +def className = "" enum Source { MAIN("/src/main/java"), diff --git a/AnkiDroid/src/main/java/com/ichi2/themes/StyledProgressDialog.kt b/AnkiDroid/src/main/java/com/ichi2/themes/StyledProgressDialog.kt index 1c842ead08..cb01914925 100644 --- a/AnkiDroid/src/main/java/com/ichi2/themes/StyledProgressDialog.kt +++ b/AnkiDroid/src/main/java/com/ichi2/themes/StyledProgressDialog.kt @@ -16,84 +16,76 @@ * this program. If not, see . * ****************************************************************************************/ -package com.ichi2.themes; +package com.ichi2.themes -import android.app.Dialog; -import android.content.Context; -import android.content.DialogInterface; -import android.view.WindowManager.BadTokenException; +import android.app.Dialog +import android.content.Context +import android.content.DialogInterface +import android.view.WindowManager.BadTokenException +import com.afollestad.materialdialogs.MaterialDialog +import com.ichi2.anki.AnkiActivity +import com.ichi2.utils.cancelListenerNullable +import com.ichi2.utils.contentNullable +import com.ichi2.utils.titleNullable +import timber.log.Timber -import com.afollestad.materialdialogs.MaterialDialog; -import com.ichi2.anki.AnkiActivity; - -import timber.log.Timber; - -public class StyledProgressDialog extends Dialog { - - - public StyledProgressDialog(Context context) { - super(context); - } - - - @Override - public void show() { +class StyledProgressDialog(context: Context?) : Dialog(context!!) { + override fun show() { try { - setCanceledOnTouchOutside(false); - super.show(); - } catch (BadTokenException e) { - Timber.e(e, "Could not show dialog"); + setCanceledOnTouchOutside(false) + super.show() + } catch (e: BadTokenException) { + Timber.e(e, "Could not show dialog") } } - - public static MaterialDialog show(Context context, CharSequence title, CharSequence message) { - return show(context, title, message, false, null); + @Suppress("unused_parameter") + fun setMax(max: Int) { + // TODO } - - public static MaterialDialog show(Context context, CharSequence title, CharSequence message, - boolean cancelable) { - return show(context, title, message, cancelable, null); + @Suppress("unused_parameter") + fun setProgress(progress: Int) { + // TODO } + @Suppress("unused_parameter") + fun setProgressStyle(style: Int) { + // TODO + } - public static MaterialDialog show(Context context, CharSequence title, CharSequence message, - boolean cancelable, DialogInterface.OnCancelListener cancelListener) { - if ("".equals(title)) { - title = null; - Timber.d("Invalid title was provided. Using null"); - } - return new MaterialDialog.Builder(context) - .title(title) - .content(message) + companion object { + @JvmStatic + @JvmOverloads + fun show( + context: Context, + title: CharSequence?, + message: CharSequence?, + cancelable: Boolean = false, + cancelListener: DialogInterface.OnCancelListener? = null + ): MaterialDialog { + var t = title + if ("" == t) { + t = null + Timber.d("Invalid title was provided. Using null") + } + return MaterialDialog.Builder(context) + .titleNullable(t) + .contentNullable(message) .progress(true, 0) .cancelable(cancelable) - .cancelListener(cancelListener) - .show(); - } + .cancelListenerNullable(cancelListener) + .show() + } - - private static boolean animationEnabled(Context context) { - if (context instanceof AnkiActivity) { - return ((AnkiActivity) context).animationEnabled(); - } else { - return true; + @Suppress("unused") + @JvmStatic + private fun animationEnabled(context: Context): Boolean { + return if (context is AnkiActivity) { + context.animationEnabled() + } else { + true + } } } - - public void setMax(int max) { - // TODO - } - - - public void setProgress(int progress) { - // TODO - } - - - public void setProgressStyle(int style) { - // TODO - } - } diff --git a/AnkiDroid/src/main/java/com/ichi2/utils/MaterialBuilderUtil.kt b/AnkiDroid/src/main/java/com/ichi2/utils/MaterialBuilderUtil.kt new file mode 100644 index 0000000000..e929a83951 --- /dev/null +++ b/AnkiDroid/src/main/java/com/ichi2/utils/MaterialBuilderUtil.kt @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 David Allison + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +package com.ichi2.utils + +import android.content.DialogInterface +import com.afollestad.materialdialogs.MaterialDialog + +// Extension methods for MaterialDialog.Builder workarounds in Kotlin +// Previously the methods accepted null into a @NonNull parameter, +// and fixing this would break the fluent interface + +fun MaterialDialog.Builder.titleNullable(title: CharSequence?): MaterialDialog.Builder { + title?.let { this.title(it) } + return this +} + +fun MaterialDialog.Builder.contentNullable(content: CharSequence?): MaterialDialog.Builder { + content?.let { this.content(it) } + return this +} + +fun MaterialDialog.Builder.cancelListenerNullable(cancelListener: DialogInterface.OnCancelListener?): MaterialDialog.Builder { + cancelListener?.let { this.cancelListener(it) } + return this +}