Skip to content

Commit

Permalink
Add logic to only enable Ionic swipe when Android OS swipe mode disab…
Browse files Browse the repository at this point in the history
…led (#652)
  • Loading branch information
aeharding authored Aug 13, 2023
1 parent d23ab3b commit 2345e7b
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 15 deletions.
1 change: 1 addition & 0 deletions android/app/capacitor.build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
implementation project(':capacitor-keyboard')
implementation project(':capacitor-share')
implementation project(':capacitor-status-bar')
implementation project(':capacitor-android-nav-mode')
implementation project(':capacitor-application-context')
implementation project(':capacitor-plugin-safe-area')
implementation project(':capacitor-stash-media')
Expand Down
3 changes: 3 additions & 0 deletions android/capacitor.settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ project(':capacitor-share').projectDir = new File('../node_modules/.pnpm/@capaci
include ':capacitor-status-bar'
project(':capacitor-status-bar').projectDir = new File('../node_modules/.pnpm/@capacitor+status-bar@5.0.6_@capacitor+core@5.2.2/node_modules/@capacitor/status-bar/android')

include ':capacitor-android-nav-mode'
project(':capacitor-android-nav-mode').projectDir = new File('../node_modules/.pnpm/capacitor-android-nav-mode@0.0.1_@capacitor+core@5.2.2/node_modules/capacitor-android-nav-mode/android')

include ':capacitor-application-context'
project(':capacitor-application-context').projectDir = new File('../node_modules/.pnpm/capacitor-application-context@0.0.1_@capacitor+core@5.2.2/node_modules/capacitor-application-context/android')

Expand Down
1 change: 1 addition & 0 deletions ios/App/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def capacitor_pods
pod 'CapacitorKeyboard', :path => '../../node_modules/.pnpm/@capacitor+keyboard@5.0.6_@capacitor+core@5.2.2/node_modules/@capacitor/keyboard'
pod 'CapacitorShare', :path => '../../node_modules/.pnpm/@capacitor+share@5.0.6_@capacitor+core@5.2.2/node_modules/@capacitor/share'
pod 'CapacitorStatusBar', :path => '../../node_modules/.pnpm/@capacitor+status-bar@5.0.6_@capacitor+core@5.2.2/node_modules/@capacitor/status-bar'
pod 'CapacitorAndroidNavMode', :path => '../../node_modules/.pnpm/capacitor-android-nav-mode@0.0.1_@capacitor+core@5.2.2/node_modules/capacitor-android-nav-mode'
pod 'CapacitorApplicationContext', :path => '../../node_modules/.pnpm/capacitor-application-context@0.0.1_@capacitor+core@5.2.2/node_modules/capacitor-application-context'
pod 'CapacitorPluginSafeArea', :path => '../../node_modules/.pnpm/capacitor-plugin-safe-area@2.0.5_@capacitor+core@5.2.2/node_modules/capacitor-plugin-safe-area'
pod 'CapacitorStashMedia', :path => '../../node_modules/.pnpm/capacitor-stash-media@0.0.3_@capacitor+core@5.2.2/node_modules/capacitor-stash-media'
Expand Down
8 changes: 7 additions & 1 deletion ios/App/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
PODS:
- Capacitor (5.2.2):
- CapacitorCordova
- CapacitorAndroidNavMode (0.0.1):
- Capacitor
- CapacitorApp (5.0.6):
- Capacitor
- CapacitorApplicationContext (0.0.1):
Expand All @@ -27,6 +29,7 @@ PODS:

DEPENDENCIES:
- "Capacitor (from `../../node_modules/.pnpm/@capacitor+ios@5.2.2_@capacitor+core@5.2.2/node_modules/@capacitor/ios`)"
- "CapacitorAndroidNavMode (from `../../node_modules/.pnpm/capacitor-android-nav-mode@0.0.1_@capacitor+core@5.2.2/node_modules/capacitor-android-nav-mode`)"
- "CapacitorApp (from `../../node_modules/.pnpm/@capacitor+app@5.0.6_@capacitor+core@5.2.2/node_modules/@capacitor/app`)"
- "CapacitorApplicationContext (from `../../node_modules/.pnpm/capacitor-application-context@0.0.1_@capacitor+core@5.2.2/node_modules/capacitor-application-context`)"
- "CapacitorBrowser (from `../../node_modules/.pnpm/@capacitor+browser@5.0.6_@capacitor+core@5.2.2/node_modules/@capacitor/browser`)"
Expand All @@ -45,6 +48,8 @@ SPEC REPOS:
EXTERNAL SOURCES:
Capacitor:
:path: "../../node_modules/.pnpm/@capacitor+ios@5.2.2_@capacitor+core@5.2.2/node_modules/@capacitor/ios"
CapacitorAndroidNavMode:
:path: "../../node_modules/.pnpm/capacitor-android-nav-mode@0.0.1_@capacitor+core@5.2.2/node_modules/capacitor-android-nav-mode"
CapacitorApp:
:path: "../../node_modules/.pnpm/@capacitor+app@5.0.6_@capacitor+core@5.2.2/node_modules/@capacitor/app"
CapacitorApplicationContext:
Expand All @@ -68,6 +73,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Capacitor: 070b18988e0f566728ae9a5eb3a7a974595f1626
CapacitorAndroidNavMode: b7bc1b2505e8991bb71999565b9ef3124a42e414
CapacitorApp: 024e1b1bea5f883d79f6330d309bc441c88ad04a
CapacitorApplicationContext: 487dced4aebde33c94a3bf8fd0e519ab59a17eb8
CapacitorBrowser: 6192948e0ce804fd72aaf77f4114a3ad2e08c760
Expand All @@ -80,6 +86,6 @@ SPEC CHECKSUMS:
CapacitorStatusBar: 565c0a1ebd79bb40d797606a8992b4a105885309
SDWebImage: 750adf017a315a280c60fde706ab1e552a3ae4e9

PODFILE CHECKSUM: 64aed244866097bf21833d6fbb682b5d31e29b18
PODFILE CHECKSUM: b8e7c3f88041ca6d3a54b6d338c63b3d039cf987

COCOAPODS: 1.12.1
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@typescript-eslint/parser": "^5.60.0",
"@vitejs/plugin-legacy": "^4.0.5",
"@vitejs/plugin-react": "^4.0.1",
"capacitor-android-nav-mode": "^0.0.1",
"capacitor-application-context": "^0.0.1",
"capacitor-plugin-safe-area": "^2.0.5",
"capacitor-set-version": "^2.0.13",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 25 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import "@ionic/react/css/flex-utils.css";
import "@ionic/react/css/display.css";

import { StoreProvider } from "./store";
import { isAndroid, isInstalled, isNative } from "./helpers/device";
import {
getAndroidNavMode,
isAndroid,
isInstalled,
isNative,
} from "./helpers/device";
import TabbedRoutes from "./TabbedRoutes";
import Auth from "./Auth";
import { AppContextProvider } from "./features/auth/AppContext";
Expand All @@ -31,12 +36,26 @@ import { SafeArea, SafeAreaInsets } from "capacitor-plugin-safe-area";
import { StatusBar } from "@capacitor/status-bar";
import { Keyboard } from "@capacitor/keyboard";
import { TabContextProvider } from "./TabContext";
import { NavModes } from "capacitor-android-nav-mode";

setupIonicReact({
rippleEffect: false,
mode: getDeviceMode(),
swipeBackEnabled: isInstalled() && getDeviceMode() === "ios",
});
// index.tsx ensurxes android nav mode resolves before app is rendered
(async () => {
let navMode;
try {
navMode = await getAndroidNavMode();
} catch (e) {
// ignore errors
}

setupIonicReact({
rippleEffect: false,
mode: getDeviceMode(),
swipeBackEnabled:
isInstalled() &&
getDeviceMode() === "ios" &&
navMode !== NavModes.Gesture,
});
})();

// Android safe area inset management is bad, we have to do it manually
if (isNative() && isAndroid()) {
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/device.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import UAParser from "ua-parser-js";
import { Capacitor } from "@capacitor/core";
import { NavMode, NavModes } from "capacitor-android-nav-mode";

export function isNative() {
return Capacitor.isNativePlatform();
Expand Down Expand Up @@ -50,3 +51,14 @@ export function supportsWebp() {

return name !== "iOS" || (version && +version >= 14);
}

export let androidNavMode: Promise<NavModes> | undefined;

export function getAndroidNavMode() {
if (androidNavMode !== undefined) return androidNavMode;
if (!isAndroid() || !isNative()) return;

const promise = NavMode.getNavigationMode().then(({ mode }) => mode);
androidNavMode = promise;
return promise;
}
23 changes: 15 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
import "./index.css";
import { isNative } from "./helpers/device";
import { getAndroidNavMode, isNative } from "./helpers/device";

const container = document.getElementById("root");
const root = createRoot(container as HTMLElement);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
(async () => {
try {
await getAndroidNavMode();
} finally {
const container = document.getElementById("root");
const root = createRoot(container as HTMLElement);

root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
}
})();

(async () => {
// Native apps should silently accept without user prompt
Expand Down

0 comments on commit 2345e7b

Please sign in to comment.