From 76c36daee09be00d36477ea21c8b97f11444b7d2 Mon Sep 17 00:00:00 2001
From: 0xrxL <120989892+0xrxL@users.noreply.github.com>
Date: Sat, 31 Dec 2022 19:16:52 +0100
Subject: [PATCH] fix(youtube/general-ads): restore swipe back to exit gesture
 (#264)

Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
---
 .../patches/FixBackToExitGesturePatch.java    | 42 +++++++++++++++++++
 .../app/watchwhile/WatchWhileActivity.java    |  7 ++++
 2 files changed, 49 insertions(+)
 create mode 100644 app/src/main/java/app/revanced/integrations/patches/FixBackToExitGesturePatch.java
 create mode 100644 dummy/src/main/java/com/google/android/apps/youtube/app/watchwhile/WatchWhileActivity.java

diff --git a/app/src/main/java/app/revanced/integrations/patches/FixBackToExitGesturePatch.java b/app/src/main/java/app/revanced/integrations/patches/FixBackToExitGesturePatch.java
new file mode 100644
index 00000000..ab210d27
--- /dev/null
+++ b/app/src/main/java/app/revanced/integrations/patches/FixBackToExitGesturePatch.java
@@ -0,0 +1,42 @@
+package app.revanced.integrations.patches;
+
+import com.google.android.apps.youtube.app.watchwhile.WatchWhileActivity;
+import app.revanced.integrations.utils.LogHelper;
+
+public class FixBackToExitGesturePatch {
+    /**
+     * State whether the scroll position reaches the top.
+     */
+    public static boolean isTopView = false;
+
+    /**
+     * Handle the event after clicking the back button.
+     *
+     * @param activity The activity, the app is launched with to finish.
+     */
+    public static void onBackPressed(WatchWhileActivity activity) {
+        if (!isTopView) return;
+
+        LogHelper.printDebug(() -> "Activity is closed");
+
+        activity.finish();
+    }
+
+    /**
+     * Handle the event when the homepage list of views is being scrolled.
+     */
+    public static void onScrollingViews() {
+        LogHelper.printDebug(() -> "Views are scrolling");
+
+        isTopView = false;
+    }
+
+    /**
+     * Handle the event when the homepage list of views reached the top.
+     */
+    public static void onTopView() {
+        LogHelper.printDebug(() -> "Scrolling reached the top");
+
+        isTopView = true;
+    }
+}
diff --git a/dummy/src/main/java/com/google/android/apps/youtube/app/watchwhile/WatchWhileActivity.java b/dummy/src/main/java/com/google/android/apps/youtube/app/watchwhile/WatchWhileActivity.java
new file mode 100644
index 00000000..d86113d0
--- /dev/null
+++ b/dummy/src/main/java/com/google/android/apps/youtube/app/watchwhile/WatchWhileActivity.java
@@ -0,0 +1,7 @@
+package com.google.android.apps.youtube.app.watchwhile;
+
+import android.app.Activity;
+
+// Dummy class
+public final class WatchWhileActivity extends Activity { }
+