Skip to content

Commit

Permalink
iOS prevent swipe down hack (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk authored Nov 23, 2023
1 parent 45ce13d commit 63f9e38
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 44 deletions.
33 changes: 9 additions & 24 deletions src/lib/telegram/prevent-telegram-swipe-down-closing.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
// @ts-nocheck
import React, { ReactNode, useEffect, useRef } from "react";
import WebApp from "@twa-dev/sdk";
import { css } from "@emotion/css";

function throttle(func, limit) {
let inThrottle;
return function () {
// eslint-disable-next-line
const args = arguments;
// eslint-disable-next-line
const context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
}
import { throttle } from "../throttle/throttle.ts";

type Props = {
condition: boolean;
children: ReactNode;
withScroll: boolean;
};

export const PreventTelegramSwipeDownClosing = (props: Props) => {
const { condition, children, withScroll } = props;
const { condition, children } = props;
const ref = useRef<HTMLDivElement | null>(null);

useEffect(() => {
if (!condition) {
return;
}
const scrollableElement = ref.current;
if (!scrollableElement) {
return;
}

const onTouchMove = throttle((e: MouseEvent) => {
const withScroll = (ref.current?.scrollHeight || 0) > window.innerHeight;

const onTouchMove = throttle((e) => {
if (withScroll) {
requestAnimationFrame(() => {
e.preventDefault();
Expand Down Expand Up @@ -85,13 +74,9 @@ export const PreventTelegramSwipeDownClosing = (props: Props) => {

export const PreventTelegramSwipeDownClosingIos = (props: {
children: ReactNode;
withScroll: boolean;
}) => {
return (
<PreventTelegramSwipeDownClosing
condition={WebApp.platform === "ios"}
withScroll={props.withScroll}
>
<PreventTelegramSwipeDownClosing condition={WebApp.platform === "ios"}>
{props.children}
</PreventTelegramSwipeDownClosing>
);
Expand Down
16 changes: 16 additions & 0 deletions src/lib/throttle/throttle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-nocheck

export function throttle(func, limit) {
let inThrottle;
return function() {
// eslint-disable-next-line
const args = arguments;
// eslint-disable-next-line
const context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
}
30 changes: 21 additions & 9 deletions src/screens/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,32 @@ export const App = observer(() => {
return (
<div>
<VersionWarning />
{screenStore.screen.type === "main" && <MainScreen />}
{screenStore.screen.type === "main" && (
<PreventTelegramSwipeDownClosingIos>
<MainScreen />
</PreventTelegramSwipeDownClosingIos>
)}
{screenStore.isDeckPreviewScreen && (
<ReviewStoreProvider>
<DeckScreen />
</ReviewStoreProvider>
<PreventTelegramSwipeDownClosingIos>
<ReviewStoreProvider>
<DeckScreen />
</ReviewStoreProvider>
</PreventTelegramSwipeDownClosingIos>
)}
{screenStore.screen.type === "deckForm" && (
<DeckFormStoreProvider>
<DeckFormScreen />
</DeckFormStoreProvider>
<PreventTelegramSwipeDownClosingIos>
<DeckFormStoreProvider>
<DeckFormScreen />
</DeckFormStoreProvider>
</PreventTelegramSwipeDownClosingIos>
)}
{screenStore.screen.type === "cardQuickAddForm" && (
<PreventTelegramSwipeDownClosingIos>
<QuickAddCardForm />
</PreventTelegramSwipeDownClosingIos>
)}
{screenStore.screen.type === "cardQuickAddForm" && <QuickAddCardForm />}
{screenStore.screen.type === "userSettings" && (
<PreventTelegramSwipeDownClosingIos withScroll={false}>
<PreventTelegramSwipeDownClosingIos>
<UserSettingsStoreProvider>
<UserSettingsMain />
</UserSettingsStoreProvider>
Expand Down
13 changes: 2 additions & 11 deletions src/screens/deck-review/deck-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@ import { Review } from "./review.tsx";
import { DeckPreview } from "./deck-preview.tsx";
import { useReviewStore } from "../../store/review-store-context.tsx";
import { DeckFinished } from "./deck-finished.tsx";
import { PreventTelegramSwipeDownClosingIos } from "../../lib/telegram/prevent-telegram-swipe-down-closing.tsx";

export const DeckScreen = observer(() => {
const reviewStore = useReviewStore();

if (reviewStore.isFinished) {
return (
<PreventTelegramSwipeDownClosingIos withScroll={false}>
<DeckFinished />
</PreventTelegramSwipeDownClosingIos>
);
return <DeckFinished />;
} else if (reviewStore.currentCardId) {
return (
<PreventTelegramSwipeDownClosingIos withScroll={false}>
<Review />
</PreventTelegramSwipeDownClosingIos>
);
return <Review />;
}
return <DeckPreview />;
});

0 comments on commit 63f9e38

Please sign in to comment.