From 25ad71080e261de9b418341c8e3c0f21fff6b405 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 16 Jan 2023 14:35:14 +0100 Subject: [PATCH] improvements --- .../app/fedilab/android/MainApplication.java | 11 +++++-- .../android/client/entities/api/Status.java | 1 + .../app/fedilab/android/helper/Helper.java | 2 +- .../android/ui/drawer/StatusAdapter.java | 22 ++++++++++---- .../mathjax/android/MathJaxView.java | 30 +++++++++++++++++++ 5 files changed, 56 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/MainApplication.java b/app/src/main/java/app/fedilab/android/MainApplication.java index e598fe6e..a8841037 100644 --- a/app/src/main/java/app/fedilab/android/MainApplication.java +++ b/app/src/main/java/app/fedilab/android/MainApplication.java @@ -41,7 +41,7 @@ public class MainApplication extends MultiDexApplication { private static MainApplication app; - + private WebView webView; public static MainApplication getApp() { return app; } @@ -52,10 +52,9 @@ public class MainApplication extends MultiDexApplication { app = this; SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(MainApplication.this); try { - new WebView(this); + webView = new WebView(this); } catch (Exception ignored) { } - boolean dynamicColor = sharedpreferences.getBoolean(getString(R.string.SET_DYNAMICCOLOR), false); if (dynamicColor) { DynamicColors.applyToActivitiesIfAvailable(this); @@ -63,6 +62,12 @@ public class MainApplication extends MultiDexApplication { StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); Toasty.Config.getInstance().apply(); + if (webView != null) { + try { + webView.destroy(); + } catch (Exception ignored) { + } + } } diff --git a/app/src/main/java/app/fedilab/android/client/entities/api/Status.java b/app/src/main/java/app/fedilab/android/client/entities/api/Status.java index 7bbc3ba6..b94cfbdc 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/api/Status.java +++ b/app/src/main/java/app/fedilab/android/client/entities/api/Status.java @@ -117,6 +117,7 @@ public class Status implements Serializable, Cloneable { public boolean isChecked = false; public String translationContent; public boolean translationShown; + public boolean mathsShown = false; public boolean canLoadMedia = false; public transient boolean isFocused = false; public transient boolean setCursorToEnd = false; diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 7a0881ed..f6c35ba6 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -327,7 +327,7 @@ public class Helper { public static final Pattern groupPattern = Pattern.compile("(![\\w_]+)"); public static final Pattern mentionPattern = Pattern.compile("(@[\\w_.-]?[\\w]+)"); public static final Pattern mentionLongPattern = Pattern.compile("(@[\\w_.-]+@[a-zA-Z0-9][a-zA-Z0-9.-]{1,61}[a-zA-Z0-9](?:\\.[a-zA-Z]{2,})+)"); - public static final Pattern mathsPattern = Pattern.compile("\\\\\\("); + public static final Pattern mathsPattern = Pattern.compile("\\\\\\(|\\\\\\{|\\\\\\["); public static final Pattern twitterPattern = Pattern.compile("((@[\\w]+)@twitter\\.com)"); public static final Pattern youtubePattern = Pattern.compile("(www\\.|m\\.)?(youtube\\.com|youtu\\.be|youtube-nocookie\\.com)/(((?!([\"'<])).)*)"); public static final Pattern nitterPattern = Pattern.compile("(mobile\\.|www\\.)?twitter.com([\\w-/]+)"); diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java index 632752ed..af9c1ddf 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java @@ -644,6 +644,7 @@ public class StatusAdapter extends RecyclerView.Adapter if (status.isMaths == null) { if (Helper.mathsPattern.matcher(status.content).find()) { holder.binding.actionButtonMaths.setVisibility(View.VISIBLE); + status.isMaths = true; } else { holder.binding.actionButtonMaths.setVisibility(View.GONE); } @@ -654,10 +655,17 @@ public class StatusAdapter extends RecyclerView.Adapter holder.binding.actionButtonMaths.setVisibility(View.GONE); } } + if (status.mathsShown) { + holder.binding.statusContentMaths.setVisibility(View.VISIBLE); + holder.binding.statusContent.setVisibility(View.GONE); + } else { + holder.binding.statusContentMaths.setVisibility(View.GONE); + holder.binding.statusContent.setVisibility(View.VISIBLE); + } holder.binding.actionButtonMaths.setOnClickListener(v -> { - if (holder.binding.statusContentMaths.getVisibility() == View.VISIBLE) { - holder.binding.statusContentMaths.setVisibility(View.GONE); - holder.binding.statusContent.setVisibility(View.VISIBLE); + + if (status.mathsShown) { + status.mathsShown = false; } else { holder.binding.statusContentMaths.removeAllViews(); MathJaxConfig mathJaxConfig = new MathJaxConfig(); @@ -669,12 +677,12 @@ public class StatusAdapter extends RecyclerView.Adapter mathJaxConfig.setTextColor("dark"); break; } + status.mathsShown = true; MathJaxView mathview = new MathJaxView(context, mathJaxConfig); holder.binding.statusContentMaths.addView(mathview); mathview.setInputText(status.contentSpan.toString()); - holder.binding.statusContentMaths.setVisibility(View.VISIBLE); - holder.binding.statusContent.setVisibility(View.GONE); } + adapter.notifyItemChanged(holder.getBindingAdapterPosition()); }); holder.binding.actionButtonFavorite.setActiveImage(R.drawable.ic_round_star_24); holder.binding.actionButtonFavorite.setInactiveImage(R.drawable.ic_round_star_border_24); @@ -1305,7 +1313,9 @@ public class StatusAdapter extends RecyclerView.Adapter if (statusToDeal.content.trim().length() == 0) { holder.binding.mediaContainer.setVisibility(View.GONE); } else { - holder.binding.statusContent.setVisibility(View.VISIBLE); + if (!status.mathsShown) { + holder.binding.statusContent.setVisibility(View.VISIBLE); + } if (statusToDeal.card != null && statusToDeal.quote_id == null && (display_card || statusToDeal.isFocused)) { holder.binding.card.setVisibility(View.VISIBLE); } else { diff --git a/mathjaxandroid/src/main/java/de/timfreiheit/mathjax/android/MathJaxView.java b/mathjaxandroid/src/main/java/de/timfreiheit/mathjax/android/MathJaxView.java index b474d6d8..401f2d66 100644 --- a/mathjaxandroid/src/main/java/de/timfreiheit/mathjax/android/MathJaxView.java +++ b/mathjaxandroid/src/main/java/de/timfreiheit/mathjax/android/MathJaxView.java @@ -9,13 +9,17 @@ import android.os.Handler; import android.text.TextUtils; import android.util.AttributeSet; import android.view.Gravity; +import android.view.MotionEvent; import android.view.View; +import android.view.ViewConfiguration; import android.view.ViewGroup; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.FrameLayout; +import java.util.concurrent.atomic.AtomicReference; + /** * Containerview for an WebView which renders LaTex using MathJax @@ -148,6 +152,32 @@ public class MathJaxView extends FrameLayout { mWebView.setVerticalScrollBarEnabled(verticalScrollbarsEnabled); mWebView.setHorizontalScrollBarEnabled(horizontalScrollbarsEnabled); mWebView.setBackgroundColor(0); + mWebView.getSettings().setLoadWithOverviewMode(true); + float touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); + final boolean[] scrollFlag = {true}; + AtomicReference downX = new AtomicReference<>((float) 0); + AtomicReference downY = new AtomicReference<>((float) 0); + mWebView.setOnTouchListener((View v, MotionEvent event) -> { + if (!scrollFlag[0] && event.getY() < getHeight() / 2) { + + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + downX.set(event.getX()); + downY.set(event.getY()); + break; + case MotionEvent.ACTION_MOVE: + if (Math.abs(event.getY() - downY.get()) < touchSlop && Math.abs(event.getX() - downX.get()) > touchSlop) { + getParent().requestDisallowInterceptTouchEvent(true); + scrollFlag[0] = true; + } + break; + } + } + if (event.getAction() == MotionEvent.ACTION_UP) + scrollFlag[0] = false; + + return false; + }); } /**