ToastUtils -> kt

This commit is contained in:
Milos Kozak 2021-11-23 21:34:50 +01:00
parent 68793233e8
commit e0268a1172
5 changed files with 110 additions and 115 deletions

View file

@ -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())

View file

@ -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

View file

@ -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);
}
}

View file

@ -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<TextView>(android.R.id.message)
toastMessage.text = string
val toastIcon = toastRoot.findViewById<ImageView>(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)
}
}
}

View file

@ -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));
}
}