From e0268a11726155f5f38a81ff7e45e870e84606c7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 23 Nov 2021 21:34:50 +0100 Subject: [PATCH] ToastUtils -> kt --- .../activities/ObjectivesExamDialog.kt | 2 +- .../automation/dialogs/EditEventDialog.kt | 6 +- .../androidaps/utils/ToastUtils.java | 109 ------------------ .../nightscout/androidaps/utils/ToastUtils.kt | 102 ++++++++++++++++ .../AbstractDanaRExecutionService.java | 6 +- 5 files changed, 110 insertions(+), 115 deletions(-) delete mode 100644 core/src/main/java/info/nightscout/androidaps/utils/ToastUtils.java create mode 100644 core/src/main/java/info/nightscout/androidaps/utils/ToastUtils.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt index d04210f2df..ecf004f82f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt @@ -109,7 +109,7 @@ class ObjectivesExamDialog : DaggerDialogFragment() { task.answered = result if (!result) { task.disabledTo = dateUtil.now() + T.hours(1).msecs() - ToastUtils.showToastInUiThread(context, R.string.wronganswer) + context?.let { it1 -> ToastUtils.showToastInUiThread(it1, R.string.wronganswer) } } else task.disabledTo = 0 updateGui() rxBus.send(EventObjectivesUpdateGui()) diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt index 43a1025eff..c27d6937a0 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt @@ -128,7 +128,7 @@ class EditEventDialog : DialogFragmentWithDate() { // check for title val title = binding.inputEventTitle.text?.toString() ?: return false if (title.isEmpty()) { - ToastUtils.showToastInUiThread(context, R.string.automation_missing_task_name) + context?.let { ToastUtils.showToastInUiThread(it, R.string.automation_missing_task_name) } return false } event.title = title @@ -137,12 +137,12 @@ class EditEventDialog : DialogFragmentWithDate() { // check for at least one trigger val con = event.trigger if (con.size() == 0 && !event.userAction) { - ToastUtils.showToastInUiThread(context, R.string.automation_missing_trigger) + context?.let { ToastUtils.showToastInUiThread(it, R.string.automation_missing_trigger) } return false } // check for at least one action if (event.actions.isEmpty()) { - ToastUtils.showToastInUiThread(context, R.string.automation_missing_action) + context?.let { ToastUtils.showToastInUiThread(it, R.string.automation_missing_action) } return false } // store diff --git a/core/src/main/java/info/nightscout/androidaps/utils/ToastUtils.java b/core/src/main/java/info/nightscout/androidaps/utils/ToastUtils.java deleted file mode 100644 index 3f2d483227..0000000000 --- a/core/src/main/java/info/nightscout/androidaps/utils/ToastUtils.java +++ /dev/null @@ -1,109 +0,0 @@ -package info.nightscout.androidaps.utils; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.media.MediaPlayer; -import android.os.Handler; -import android.os.Looper; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; -import android.widget.Toast; - -import androidx.annotation.DrawableRes; -import androidx.appcompat.view.ContextThemeWrapper; - -import info.nightscout.androidaps.core.R; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; -import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; - - -public class ToastUtils { - - private static Toast lastToast = null; - - public static class Long { - - public static void warnToast(final Context ctx, final String string) { - graphicalToast(ctx, string, R.drawable.ic_toast_warn, false); - } - - public static void infoToast(final Context ctx, final String string) { - graphicalToast(ctx, string, R.drawable.ic_toast_info, false); - } - - @SuppressWarnings("unused") - public static void okToast(final Context ctx, final String string) { - graphicalToast(ctx, string, R.drawable.ic_toast_check, false); - } - - public static void errorToast(final Context ctx, final String string) { - graphicalToast(ctx, string, R.drawable.ic_toast_error, false); - } - } - - public static void showToastInUiThread(final Context ctx, final int stringId) { - showToastInUiThread(ctx, ctx.getString(stringId)); - } - - public static void warnToast(final Context ctx, final String string) { - graphicalToast(ctx, string, R.drawable.ic_toast_warn, true); - } - - public static void infoToast(final Context ctx, final String string) { - graphicalToast(ctx, string, R.drawable.ic_toast_info, true); - } - - public static void okToast(final Context ctx, final String string) { - graphicalToast(ctx, string, R.drawable.ic_toast_check, true); - } - - public static void errorToast(final Context ctx, final String string) { - graphicalToast(ctx, string, R.drawable.ic_toast_error, true); - } - - public static void graphicalToast(final Context ctx, final String string, @DrawableRes int iconId) { - graphicalToast(ctx, string, iconId, true); - } - - @SuppressLint("InflateParams") - public static void graphicalToast(final Context ctx, final String string, @DrawableRes int iconId, boolean isShort) { - Handler mainThread = new Handler(Looper.getMainLooper()); - mainThread.post(() -> { - View toastRoot = LayoutInflater.from(new ContextThemeWrapper(ctx, R.style.AppTheme)).inflate(R.layout.toast, null); - TextView toastMessage = toastRoot.findViewById(android.R.id.message); - toastMessage.setText(string); - - ImageView toastIcon = toastRoot.findViewById(android.R.id.icon); - toastIcon.setImageResource(iconId); - - if (lastToast != null) lastToast.cancel(); - lastToast = new Toast(ctx); - lastToast.setDuration(isShort ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG); - lastToast.setView(toastRoot); - lastToast.show(); - }); - } - - public static void showToastInUiThread(final Context ctx, final String string) { - Handler mainThread = new Handler(Looper.getMainLooper()); - mainThread.post(() -> Toast.makeText(ctx, string, Toast.LENGTH_SHORT).show()); - } - - public static void showToastInUiThread(final Context ctx, final RxBus rxBus, - final String string, int soundID) { - - showToastInUiThread(ctx, string); - playSound(ctx, soundID); - Notification notification = new Notification(Notification.TOAST_ALARM, string, Notification.URGENT); - rxBus.send(new EventNewNotification(notification)); - } - - private static void playSound(final Context ctx, final int soundID) { - final MediaPlayer soundMP = MediaPlayer.create(ctx, soundID); - soundMP.start(); - soundMP.setOnCompletionListener(MediaPlayer::release); - } -} \ No newline at end of file diff --git a/core/src/main/java/info/nightscout/androidaps/utils/ToastUtils.kt b/core/src/main/java/info/nightscout/androidaps/utils/ToastUtils.kt new file mode 100644 index 0000000000..0cf79d5326 --- /dev/null +++ b/core/src/main/java/info/nightscout/androidaps/utils/ToastUtils.kt @@ -0,0 +1,102 @@ +package info.nightscout.androidaps.utils + +import android.annotation.SuppressLint +import android.content.Context +import android.media.MediaPlayer +import android.os.Handler +import android.os.Looper +import android.view.LayoutInflater +import android.widget.ImageView +import android.widget.TextView +import android.widget.Toast +import androidx.annotation.DrawableRes +import androidx.appcompat.view.ContextThemeWrapper +import info.nightscout.androidaps.core.R +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification +import info.nightscout.androidaps.plugins.general.overview.notifications.Notification + +object ToastUtils { + + private var lastToast: Toast? = null + fun showToastInUiThread(ctx: Context, stringId: Int) { + showToastInUiThread(ctx, ctx.getString(stringId)) + } + + fun warnToast(ctx: Context?, string: String?) { + graphicalToast(ctx, string, R.drawable.ic_toast_warn, true) + } + + fun infoToast(ctx: Context?, string: String?) { + graphicalToast(ctx, string, R.drawable.ic_toast_info, true) + } + + fun okToast(ctx: Context?, string: String?) { + graphicalToast(ctx, string, R.drawable.ic_toast_check, true) + } + + fun errorToast(ctx: Context?, string: String?) { + graphicalToast(ctx, string, R.drawable.ic_toast_error, true) + } + + fun graphicalToast(ctx: Context?, string: String?, @DrawableRes iconId: Int) { + graphicalToast(ctx, string, iconId, true) + } + + @SuppressLint("InflateParams") + fun graphicalToast(ctx: Context?, string: String?, @DrawableRes iconId: Int, isShort: Boolean) { + val mainThread = Handler(Looper.getMainLooper()) + mainThread.post { + val toastRoot = LayoutInflater.from(ContextThemeWrapper(ctx, R.style.AppTheme)).inflate(R.layout.toast, null) + val toastMessage = toastRoot.findViewById(android.R.id.message) + toastMessage.text = string + val toastIcon = toastRoot.findViewById(android.R.id.icon) + toastIcon.setImageResource(iconId) + lastToast?.cancel() + lastToast = Toast(ctx) + lastToast?.duration = if (isShort) Toast.LENGTH_SHORT else Toast.LENGTH_LONG + lastToast?.view = toastRoot + lastToast?.show() + } + } + + fun showToastInUiThread(ctx: Context?, string: String?) { + val mainThread = Handler(Looper.getMainLooper()) + mainThread.post { Toast.makeText(ctx, string, Toast.LENGTH_SHORT).show() } + } + + fun showToastInUiThread( + ctx: Context?, rxBus: RxBus, + string: String?, soundID: Int + ) { + showToastInUiThread(ctx, string) + playSound(ctx, soundID) + val notification = Notification(Notification.TOAST_ALARM, string!!, Notification.URGENT) + rxBus.send(EventNewNotification(notification)) + } + + private fun playSound(ctx: Context?, soundID: Int) { + val soundMP = MediaPlayer.create(ctx, soundID) + soundMP.start() + soundMP.setOnCompletionListener { obj: MediaPlayer -> obj.release() } + } + + object Long { + + fun warnToast(ctx: Context?, string: String) { + graphicalToast(ctx, string, R.drawable.ic_toast_warn, false) + } + + fun infoToast(ctx: Context?, string: String) { + graphicalToast(ctx, string, R.drawable.ic_toast_info, false) + } + + fun okToast(ctx: Context?, string: String) { + graphicalToast(ctx, string, R.drawable.ic_toast_check, false) + } + + fun errorToast(ctx: Context?, string: String) { + graphicalToast(ctx, string, R.drawable.ic_toast_error, false) + } + } +} \ No newline at end of file diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java b/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java index 49d5e030e4..e60890d4dd 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java +++ b/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java @@ -207,10 +207,12 @@ public abstract class AbstractDanaRExecutionService extends DaggerService { } } } else { - ToastUtils.showToastInUiThread(context.getApplicationContext(), rh.gs(R.string.nobtadapter)); + ToastUtils.INSTANCE.showToastInUiThread(context.getApplicationContext(), + rh.gs(R.string.nobtadapter)); } if (mBTDevice == null) { - ToastUtils.showToastInUiThread(context.getApplicationContext(), rh.gs(R.string.devicenotfound)); + ToastUtils.INSTANCE.showToastInUiThread(context.getApplicationContext(), + rh.gs(R.string.devicenotfound)); } }