Skip to content

Commit

Permalink
Merge pull request #36 from mym0404/fix/share/ios-link-execution-params
Browse files Browse the repository at this point in the history
fix(share): ios: swap mobile execution params values into string from object
  • Loading branch information
mym0404 authored Jul 2, 2024
2 parents 71a136e + 1d7a4c9 commit b87a008
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 35 deletions.
26 changes: 14 additions & 12 deletions example/src/app/share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import { Txt } from '../component/Txt';
const link: KakaoTemplateLink = {
webUrl: 'https://mjstudio.net',
mobileWebUrl: 'https://mjstudio.net',
iosExecutionParams: {},
androidExecutionParams: {},
};

const social: KakaoTemplateSocial = {
Expand Down Expand Up @@ -197,15 +199,15 @@ export default function Page() {
shareFeedTemplate({
template: feed,
serverCallbackArgs: {},
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
title={'Send Feed Template To Me'}
onPress={() => {
sendFeedTemplateToMe({
template: feed,
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
Expand All @@ -225,15 +227,15 @@ export default function Page() {
shareListTemplate({
template: list,
serverCallbackArgs: {},
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
title={'Send List Template To Me'}
onPress={() => {
sendListTemplateToMe({
template: list,
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
Expand All @@ -253,15 +255,15 @@ export default function Page() {
shareLocationTemplate({
template: location,
serverCallbackArgs: {},
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
title={'Send Location Template To Me'}
onPress={() => {
sendLocationTemplateToMe({
template: location,
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
Expand All @@ -281,15 +283,15 @@ export default function Page() {
shareCommerceTemplate({
template: commerce,
serverCallbackArgs: {},
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
title={'Send Commerce Template To Me'}
onPress={() => {
sendCommerceTemplateToMe({
template: commerce,
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
Expand All @@ -309,15 +311,15 @@ export default function Page() {
shareTextTemplate({
template: text,
serverCallbackArgs: {},
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
title={'Send Text Template To Me'}
onPress={() => {
sendTextTemplateToMe({
template: text,
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
Expand All @@ -337,15 +339,15 @@ export default function Page() {
shareCalendarTemplate({
template: calendar,
serverCallbackArgs: {},
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
title={'Send Calendar Template To Me'}
onPress={() => {
sendCalendarTemplateToMe({
template: calendar,
});
}).catch((e) => showMessage({ type: 'warning', message: e.message }));
}}
/>
<Btn
Expand Down
2 changes: 1 addition & 1 deletion packages/share/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@
},
"gitHead": "5ca9177f7ac369140ffb58d3c1285c2e5c96057d",
"dependencies": {
"@mj-studio/js-util": "1.1.3"
"@mj-studio/js-util": "1.1.14"
}
}
71 changes: 50 additions & 21 deletions packages/share/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NativeModules, Platform } from 'react-native';
import type { Double, UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';

import type {
KakaoCalendarTemplate,
Expand All @@ -16,6 +17,7 @@ import type {
KakaoTextTemplate,
Spec,
} from './spec/NativeKakaoShare';
import { swapMobileExecutionParamsFieldValueIntoStringInIOS } from './util/swapMobileExecutionParamsFieldValueIntoStringInIOS';

export type {
KakaoTemplateSocial,
Expand Down Expand Up @@ -55,6 +57,33 @@ const Native: Spec = Module
},
);

function shareOrSendMeOrSendFriendOrWhatever(
/* share, send-me, send-friend */
sendType: string,
/* custom, feed, list, location, commerce, text, calendar */
templateType: string,
/* only for custom */
templateId: Double,
/* only for default templates */
templateJson: UnsafeObject,
/* only for pass friend uuid directly */
receiverUuids: ReadonlyArray<string>,
useWebBrowserIfKakaoTalkNotAvailable: boolean,
templateArgs: Readonly<{ [key: string]: string }>,
serverCallbackArgs: Readonly<{ [key: string]: string }>,
): Promise<any> {
return Native.shareOrSendMeOrSendFriendOrWhatever(
sendType,
templateType,
templateId,
swapMobileExecutionParamsFieldValueIntoStringInIOS(templateJson),
receiverUuids,
useWebBrowserIfKakaoTalkNotAvailable,
templateArgs,
serverCallbackArgs,
);
}

const SendTypes = { share: 'share', sendMe: 'send-me', sendFriend: 'send-friend' };
const TemplateTypes = {
custom: 'custom',
Expand All @@ -77,7 +106,7 @@ export function shareCustomTemplate({
templateArgs?: Record<string, string>;
serverCallbackArgs?: Record<string, string>;
}) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.share,
TemplateTypes.custom,
templateId,
Expand All @@ -96,7 +125,7 @@ export function sendCustomTemplateToMe({
templateId: number;
templateArgs?: Record<string, string>;
}) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendMe,
TemplateTypes.custom,
templateId,
Expand All @@ -117,7 +146,7 @@ export function sendCustomTemplateToFriends({
templateArgs?: Record<string, string>;
receiverUuids: string[];
}): Promise<string[]> {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendFriend,
TemplateTypes.custom,
templateId,
Expand All @@ -138,7 +167,7 @@ export function shareFeedTemplate({
useWebBrowserIfKakaoTalkNotAvailable?: boolean;
serverCallbackArgs?: Record<string, string>;
}) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.share,
TemplateTypes.feed,
-1,
Expand All @@ -151,7 +180,7 @@ export function shareFeedTemplate({
}

export function sendFeedTemplateToMe({ template }: { template: KakaoFeedTemplate }) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendMe,
TemplateTypes.feed,
-1,
Expand All @@ -170,7 +199,7 @@ export function sendFeedTemplateToFriends({
template: KakaoFeedTemplate;
receiverUuids: string[];
}): Promise<string[]> {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendFriend,
TemplateTypes.feed,
-1,
Expand All @@ -191,7 +220,7 @@ export function shareListTemplate({
useWebBrowserIfKakaoTalkNotAvailable?: boolean;
serverCallbackArgs?: Record<string, string>;
}) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.share,
TemplateTypes.list,
-1,
Expand All @@ -204,7 +233,7 @@ export function shareListTemplate({
}

export function sendListTemplateToMe({ template }: { template: KakaoListTemplate }) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendMe,
TemplateTypes.list,
-1,
Expand All @@ -223,7 +252,7 @@ export function sendListTemplateToFriends({
template: KakaoListTemplate;
receiverUuids: string[];
}): Promise<string[]> {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendFriend,
TemplateTypes.list,
-1,
Expand All @@ -244,7 +273,7 @@ export function shareLocationTemplate({
useWebBrowserIfKakaoTalkNotAvailable?: boolean;
serverCallbackArgs?: Record<string, string>;
}) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.share,
TemplateTypes.location,
-1,
Expand All @@ -257,7 +286,7 @@ export function shareLocationTemplate({
}

export function sendLocationTemplateToMe({ template }: { template: KakaoLocationTemplate }) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendMe,
TemplateTypes.location,
-1,
Expand All @@ -276,7 +305,7 @@ export function sendLocationTemplateToFriends({
template: KakaoLocationTemplate;
receiverUuids: string[];
}): Promise<string[]> {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendFriend,
TemplateTypes.location,
-1,
Expand All @@ -297,7 +326,7 @@ export function shareCommerceTemplate({
useWebBrowserIfKakaoTalkNotAvailable?: boolean;
serverCallbackArgs?: Record<string, string>;
}) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.share,
TemplateTypes.commerce,
-1,
Expand All @@ -310,7 +339,7 @@ export function shareCommerceTemplate({
}

export function sendCommerceTemplateToMe({ template }: { template: KakaoCommerceTemplate }) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendMe,
TemplateTypes.commerce,
-1,
Expand All @@ -329,7 +358,7 @@ export function sendCommerceTemplateToFriends({
template: KakaoCommerceTemplate;
receiverUuids: string[];
}): Promise<string[]> {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendFriend,
TemplateTypes.commerce,
-1,
Expand All @@ -350,7 +379,7 @@ export function shareTextTemplate({
useWebBrowserIfKakaoTalkNotAvailable?: boolean;
serverCallbackArgs?: Record<string, string>;
}) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.share,
TemplateTypes.text,
-1,
Expand All @@ -363,7 +392,7 @@ export function shareTextTemplate({
}

export function sendTextTemplateToMe({ template }: { template: KakaoTextTemplate }) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendMe,
TemplateTypes.text,
-1,
Expand All @@ -382,7 +411,7 @@ export function sendTextTemplateToFriends({
template: KakaoTextTemplate;
receiverUuids: string[];
}): Promise<string[]> {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendFriend,
TemplateTypes.text,
-1,
Expand All @@ -403,7 +432,7 @@ export function shareCalendarTemplate({
useWebBrowserIfKakaoTalkNotAvailable?: boolean;
serverCallbackArgs?: Record<string, string>;
}) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.share,
TemplateTypes.calendar,
-1,
Expand All @@ -416,7 +445,7 @@ export function shareCalendarTemplate({
}

export function sendCalendarTemplateToMe({ template }: { template: KakaoCalendarTemplate }) {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendMe,
TemplateTypes.calendar,
-1,
Expand All @@ -435,7 +464,7 @@ export function sendCalendarTemplateToFriends({
template: KakaoCalendarTemplate;
receiverUuids: string[];
}): Promise<string[]> {
return Native.shareOrSendMeOrSendFriendOrWhatever(
return shareOrSendMeOrSendFriendOrWhatever(
SendTypes.sendFriend,
TemplateTypes.calendar,
-1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Platform } from 'react-native';
import { replaceJsonValuesRecursively } from '@mj-studio/js-util';

export function swapMobileExecutionParamsFieldValueIntoStringInIOS<T extends object>(
template: T,
): T {
if (Platform.OS !== 'ios') {
return template;
}

return replaceJsonValuesRecursively(template, {
replacer: {
androidExecutionParams: (value: Record<string, string>) =>
Object.entries(value)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&'),
iosExecutionParams: (value: Record<string, string>) =>
Object.entries(value)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&'),
},
});
}
Loading

0 comments on commit b87a008

Please sign in to comment.