-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: app bridge provider, user agent provider 추가
- Loading branch information
Showing
5 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/components/provider/AppBridgeProvider/AppBridgeMessage.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export enum AppBridgeMessageType { | ||
OPEN_CAMERA = "openCamera", | ||
OPEN_GALLERY = "openGallery", | ||
SHARE = "share", | ||
CREATE_REVIEW = "createReview", | ||
COPY = "copy", | ||
} | ||
|
||
export type AppBridgeMessage = | ||
| OpenCameraMessage | ||
| OpenGalleryMessage | ||
| ShareMessage | ||
| CreateReviewMessage | ||
| CopyMessage; | ||
|
||
export interface OpenCameraMessage { | ||
type: AppBridgeMessageType.OPEN_CAMERA; | ||
} | ||
|
||
export interface OpenGalleryMessage { | ||
type: AppBridgeMessageType.OPEN_GALLERY; | ||
} | ||
|
||
export interface ShareMessage { | ||
type: AppBridgeMessageType.SHARE; | ||
} | ||
|
||
export interface CreateReviewMessage { | ||
type: AppBridgeMessageType.CREATE_REVIEW; | ||
payload: { | ||
json: string; | ||
}; | ||
} | ||
|
||
export interface CopyMessage { | ||
type: AppBridgeMessageType.COPY; | ||
payload: { | ||
json: string; | ||
}; | ||
} |
46 changes: 46 additions & 0 deletions
46
src/components/provider/AppBridgeProvider/AppBridgeProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { ReactNode } from "react"; | ||
import { createContext, useContext } from "react"; | ||
|
||
import type { AppBridgeMessage } from "@/components/provider/AppBridgeProvider/AppBridgeMessage.types"; | ||
import { | ||
convertToAndroidAppBridge, | ||
convertToIOSAppBridge, | ||
} from "@/components/provider/AppBridgeProvider/convertToNativeMessage"; | ||
import { useUserAgent } from "@/components/provider/UserAgentProvider"; | ||
|
||
interface AppBridgeProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
interface AppBridge { | ||
send: (message: AppBridgeMessage) => void; | ||
} | ||
|
||
export const AppBridgeContext = createContext<null | AppBridge>(null); | ||
|
||
export function AppBridgeProvider({ children }: AppBridgeProviderProps) { | ||
const userAgent = useUserAgent(); | ||
|
||
const isIOS = userAgent.isIOS; | ||
|
||
const send = (message: AppBridgeMessage) => { | ||
try { | ||
if (isIOS) return convertToIOSAppBridge(message); | ||
return convertToAndroidAppBridge(message); | ||
} catch { | ||
alert("App Bridge API called: " + message.type); | ||
} | ||
}; | ||
|
||
return <AppBridgeContext.Provider value={{ send }}>{children}</AppBridgeContext.Provider>; | ||
} | ||
|
||
export function useAppBridge() { | ||
const appBridge = useContext(AppBridgeContext); | ||
|
||
if (appBridge == null) { | ||
throw new Error("Wrap App Bridge Provider"); | ||
} | ||
|
||
return appBridge; | ||
} |
43 changes: 43 additions & 0 deletions
43
src/components/provider/AppBridgeProvider/convertToNativeMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { AppBridgeMessageType } from "@/components/provider/AppBridgeProvider/AppBridgeMessage.types"; | ||
import type { AppBridgeMessage } from "@/components/provider/AppBridgeProvider/AppBridgeMessage.types"; | ||
|
||
const iosHandlers = { | ||
[AppBridgeMessageType.OPEN_CAMERA]: () => window.webkit?.messageHandlers.openCamera.postMessage(), | ||
[AppBridgeMessageType.OPEN_GALLERY]: () => | ||
window.webkit?.messageHandlers.openGallery.postMessage(), | ||
[AppBridgeMessageType.SHARE]: () => window.webkit?.messageHandlers.share.postMessage(), | ||
[AppBridgeMessageType.CREATE_REVIEW]: (message: { payload: { json: string } }) => | ||
window.webkit?.messageHandlers.createReview.postMessage(message.payload.json), | ||
[AppBridgeMessageType.COPY]: (message: { payload: { json: string } }) => | ||
window.webkit?.messageHandlers.copy.postMessage(message.payload.json), | ||
}; | ||
|
||
const androidHandlers = { | ||
[AppBridgeMessageType.OPEN_CAMERA]: () => window.AndroidBridge?.openCamera(), | ||
[AppBridgeMessageType.OPEN_GALLERY]: () => window.AndroidBridge?.openGallery(), | ||
[AppBridgeMessageType.SHARE]: () => window.AndroidBridge?.share(), | ||
[AppBridgeMessageType.CREATE_REVIEW]: (message: { payload: { json: string } }) => | ||
window.AndroidBridge?.createReview(message.payload.json), | ||
[AppBridgeMessageType.COPY]: (message: { payload: { json: string } }) => | ||
window.AndroidBridge?.copy(message.payload.json), | ||
}; | ||
|
||
export function convertToIOSAppBridge(message: AppBridgeMessage) { | ||
const handler = iosHandlers[message.type]; | ||
if (handler) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
handler(message as any); | ||
} else { | ||
console.warn("Unhandled message type:", message.type); | ||
} | ||
} | ||
|
||
export function convertToAndroidAppBridge(message: AppBridgeMessage) { | ||
const handler = androidHandlers[message.type]; | ||
if (handler) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
handler(message as any); | ||
} else { | ||
console.warn("Unhandled message type:", message.type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"use client"; | ||
|
||
import type { ReactNode } from "react"; | ||
import { createContext, useContext, useEffect, useState } from "react"; | ||
|
||
export interface UserAgent { | ||
rawUA: string; | ||
isIOS: boolean; | ||
isAndroid: boolean; | ||
isMobile: boolean; | ||
} | ||
|
||
export const UserAgentContext = createContext<UserAgent | null>(null); | ||
|
||
export function UserAgentProvider({ children }: { children: ReactNode }) { | ||
const [userAgent, setUserAgent] = useState<UserAgent>({ | ||
isAndroid: false, | ||
isIOS: true, | ||
rawUA: "", | ||
isMobile: true, | ||
}); | ||
|
||
useEffect(() => { | ||
const _userAgent = navigator.userAgent.toLowerCase(); | ||
|
||
const isMobile = _userAgent.indexOf("iphone") > -1 || _userAgent.indexOf("android") > -1; | ||
|
||
if (_userAgent.indexOf("android") > -1) { | ||
setUserAgent({ | ||
isIOS: false, | ||
isAndroid: true, | ||
rawUA: _userAgent, | ||
isMobile, | ||
}); | ||
} else { | ||
setUserAgent({ | ||
isIOS: true, | ||
isAndroid: false, | ||
rawUA: _userAgent, | ||
isMobile, | ||
}); | ||
} | ||
}, []); | ||
|
||
return <UserAgentContext.Provider value={userAgent}>{children}</UserAgentContext.Provider>; | ||
} | ||
|
||
export function useUserAgent() { | ||
const userAgent = useContext(UserAgentContext); | ||
|
||
if (userAgent == null) { | ||
throw new Error("Wrap UserAgent Provider"); | ||
} | ||
return userAgent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters