From 2ecae7da93a0c8389a5cc288091f321c37808b59 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 17 Apr 2024 19:55:14 -0400 Subject: [PATCH] Implement my own hokey pull to refresh on Android. Nobody's got time for all those dependencies. --- .../unprompted/tildefriends/MainActivity.java | 45 ++++++++++++++++--- src/android/res/layout/activity_main.xml | 14 +++--- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/android/com/unprompted/tildefriends/MainActivity.java b/src/android/com/unprompted/tildefriends/MainActivity.java index 64c94320..a4b55632 100644 --- a/src/android/com/unprompted/tildefriends/MainActivity.java +++ b/src/android/com/unprompted/tildefriends/MainActivity.java @@ -15,7 +15,9 @@ import android.os.strictmode.Violation; import android.util.Base64; import android.util.Log; import android.view.KeyEvent; +import android.view.MotionEvent; import android.view.View; +import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.webkit.CookieManager; import android.webkit.DownloadListener; @@ -55,6 +57,7 @@ public class MainActivity extends Activity { private ValueCallback upload_message; private final static int FILECHOOSER_RESULT = 1; + private float touch_down_y; @Override protected void onCreate(Bundle savedInstanceState) { @@ -238,12 +241,9 @@ public class MainActivity extends Activity { } }); - Button refresh = (Button)findViewById(R.id.refresh); - refresh.setOnClickListener(new View.OnClickListener() { - public void onClick(View view) { - web_view.reload(); - } - }); + TextView refresh = (TextView)findViewById(R.id.refresh); + refresh.setVisibility(View.GONE); + refresh.setText("REFRESH"); } @Override @@ -298,6 +298,39 @@ public class MainActivity extends Activity { return super.onKeyDown(keyCode, event); } + @Override + public boolean dispatchTouchEvent(MotionEvent event) { + final int k_drag_distance = 160; + switch (event.getActionMasked()) { + case MotionEvent.ACTION_DOWN: + touch_down_y = event.getY(); + break; + case MotionEvent.ACTION_MOVE: + { + float delta = event.getY() - touch_down_y; + TextView refresh = (TextView)findViewById(R.id.refresh); + LayoutParams layout = refresh.getLayoutParams(); + layout.height = + Math.min(Math.max((int)delta, 0), k_drag_distance) + + (delta > k_drag_distance ? (int)Math.sqrt(delta - k_drag_distance) : 0); + refresh.setLayoutParams(layout); + refresh.setVisibility(layout.height > 0 ? View.VISIBLE : View.GONE); + } + break; + case MotionEvent.ACTION_UP: + { + float delta = event.getY() - touch_down_y; + if (delta > getWindow().getDecorView().getHeight() / 4) { + web_view.reload(); + } + TextView refresh = (TextView)findViewById(R.id.refresh); + refresh.setVisibility(View.GONE); + } + break; + } + return super.dispatchTouchEvent(event); + } + private int read_port(String path) { try (BufferedReader reader = new BufferedReader(new FileReader(path))) { return Integer.parseInt(reader.readLine()); diff --git a/src/android/res/layout/activity_main.xml b/src/android/res/layout/activity_main.xml index d1eb6b73..da4a4708 100644 --- a/src/android/res/layout/activity_main.xml +++ b/src/android/res/layout/activity_main.xml @@ -13,12 +13,12 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal|center_vertical"/> -