X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FMisc.java;h=67dc0958686a9722f15b3f797d3634740c531dbc;hb=1fb6688fd48866ace7c8a3561f15a72fd795ec31;hp=927a34d936b44326cfeed93aa9866ccd64ff770f;hpb=b0404689e0dbb2b08f02deca7ee5d14636a3baa6;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 927a34d9..67dc0958 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 © 2020 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,6 +19,8 @@ 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; @@ -29,9 +31,11 @@ import androidx.fragment.app.FragmentActivity; import org.jetbrains.annotations.Contract; public class Misc { + public static final char ZERO_WIDTH_SPACE = '\u200B'; 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() @@ -61,7 +65,7 @@ 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; @@ -85,16 +89,41 @@ public class Misc { } @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; + 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; + 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); + } + public static String addWrapHints(String input) { + if (input == null) + return null; + StringBuilder result = new StringBuilder(); + int lastPos = 0; + int pos = input.indexOf(':'); + + while (pos >= 0) { + result.append(input.substring(lastPos, pos + 1)) + .append(ZERO_WIDTH_SPACE); + lastPos = pos + 1; + pos = input.indexOf(':', lastPos + 1); + } + if (lastPos > 0) + result.append(input.substring(lastPos)); + + return result.toString(); + } }