0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 12:02:16 +02:00

Migrated StyledProgressDialog to kotlin.

Co-authored-by: David Allison <62114487+david-allison-1@users.noreply.github.com>
This commit is contained in:
Piyush Goel 2021-11-06 09:21:06 +05:30 committed by Mike Hardy
parent 7ac0c8f77d
commit e87c19e093
3 changed files with 96 additions and 65 deletions

View File

@ -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"),

View File

@ -16,84 +16,76 @@
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
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
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2021 David Allison <davidallisongithub@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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
}