Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#706 Flutter handler migration #708

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { RootStack } from "@/screens";
import { NavigationContainer } from "@react-navigation/native";
import Constants from "expo-constants";
import { StatusBar } from "expo-status-bar";
import { View } from "react-native";
import { Platform, SafeAreaView } from "react-native";

export function App() {
return (
<View style={{ flex: 1 }}>
<NavigationContainer>
<RootStack />
</NavigationContainer>
<>
<StatusBar style="auto" />
</View>
<SafeAreaView
style={{
flex: 1,
paddingTop: Platform.OS === "ios" ? 0 : Constants.statusBarHeight,
}}
>
<NavigationContainer>
<RootStack />
</NavigationContainer>
</SafeAreaView>
</>
);
}
10 changes: 8 additions & 2 deletions packages/app/src/components/TaxiWebView.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { env } from "@/env";
import { mapWebRoutes } from "@/navigation/web";
import { useWebViewEvent } from "@/utils/eventHandler";
import { isSameScreen } from "@/utils/navigation";
import { useIsFocused, useNavigation } from "@react-navigation/native";
import React, { useCallback, useMemo, useRef } from "react";
import React, { useCallback, useEffect, useMemo, useRef } from "react";
import { Platform } from "react-native";
import { WebView, type WebViewNavigation } from "react-native-webview";

Expand All @@ -19,6 +20,7 @@ export const TaxiWebView: React.FC<TaxiWebViewProps> = ({ path }) => {
const currentScreen = useMemo(() => mapWebRoutes(uri), [uri]);
const isFocused = useIsFocused();
const navigation = useNavigation();
const { addEvent, removeEvent, handleMessage } = useWebViewEvent();

const onNavigationStateChange = useCallback(
(event: WebViewNavigation) => {
Expand All @@ -32,13 +34,17 @@ export const TaxiWebView: React.FC<TaxiWebViewProps> = ({ path }) => {
[isFocused, currentScreen, navigation]
);

useEffect(() => {}, []);

return (
<WebView
cacheEnabled
ref={ref}
userAgent={`taxi-app-webview/${Platform.OS}`}
userAgent={`taxi-app-rn-webview/${Platform.OS}`}
source={{ uri }}
onNavigationStateChange={onNavigationStateChange}
onMessage={handleMessage}
webviewDebuggingEnabled={true}
/>
);
};
37 changes: 37 additions & 0 deletions packages/app/src/utils/eventHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useEffect, useRef } from "react";
import { WebViewMessageEvent } from "react-native-webview";

export interface RNEventProps {
event: string;
data?: any;
}

export interface RNEventListenrProps {
event: string;
listener: (data: any) => any;
}

export const useWebViewEvent = () => {
const eventRef = useRef<Map<string, RNEventListenrProps>>(new Map());

useEffect(() => {
eventRef.current = new Map();
}, []);

const addEvent = (event: RNEventListenrProps) => {
eventRef.current?.set(event.event, event);
};
const removeEvent = (event: RNEventListenrProps) => {
eventRef.current?.delete(event.event);
};
const handleMessage = (event: WebViewMessageEvent) => {
const data = JSON.parse(event.nativeEvent.data);
const eventProps = eventRef.current?.get(data.event);
eventProps?.listener(data.data);
};
return {
addEvent,
removeEvent,
handleMessage,
};
};