X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FMisc.java;h=349a04a5d9866e416cbb0c29216dc852281f255d;hb=40d4f7ebb9085a58133cf47eacf989ed35fa0b4e;hp=d333d4796c3c5dc85f1756134e8227709c451135;hpb=e7cde3553714eb10d2e2df6352334e72c4c7348f;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java b/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java index d333d479..349a04a5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -19,15 +19,22 @@ package net.ktnx.mobileledger.utils; import android.app.Activity; import android.content.res.Configuration; +import android.os.Handler; +import android.os.Looper; +import android.text.Editable; import android.view.WindowManager; +import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; +import org.jetbrains.annotations.Contract; + public class Misc { public static boolean isZero(float f) { return (f < 0.005) && (f > -0.005); } + public static boolean equalFloats(float a, float b) { return isZero(a - b); } public static void showSoftKeyboard(Activity activity) { // make the keyboard appear Configuration cf = activity.getResources() @@ -57,9 +64,47 @@ public class Misc { } public static String emptyIsNull(String str) { - return "".equals(str) ? null : str; + return str != null && str.isEmpty() ? null : str; } public static String nullIsEmpty(String str) { return (str == null) ? "" : str; } + public static String nullIsEmpty(Editable e) { + if (e == null) + return ""; + return e.toString(); + } + public static boolean equalStrings(String u, CharSequence text) { + return nullIsEmpty(u).equals(text.toString()); + } + public static boolean equalStrings(String a, String b) { + return nullIsEmpty(a).equals(nullIsEmpty(b)); + } + public static String trim(@Nullable String string) { + if (string == null) + return null; + + return string.trim(); + } + @Contract(value = "null, null -> true; null, !null -> false; !null, null -> false", pure = true) + public static boolean equalIntegers(Integer a, Integer b) { + if (a == null && b == null) + return true; + if (a == null || b == null) + return false; + + return a.equals(b); + } + @Contract(value = "null, null -> true; null, !null -> false; !null, null -> false", pure = true) + public static boolean equalLongs(Long a, Long b) { + if (a == null && b == null) + return true; + if (a == null || b == null) + return false; + + return a.equals(b); + } + public static void onMainThread(Runnable r) { + new Handler(Looper.getMainLooper()).post(r); + } }