Skip to content

Commit

Permalink
feat: sending and receiving - temp
Browse files Browse the repository at this point in the history
  • Loading branch information
iuricmp committed Oct 2, 2024
1 parent ea8b013 commit 62b47c3
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 67 deletions.
25 changes: 14 additions & 11 deletions mobile/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GnoNativeProvider } from "@gnolang/gnonative";
import { IndexerProvider } from "@gno/provider/indexer-provider";
import { NotificationProvider } from "@gno/provider/notification-provider";
import { ReduxProvider } from "redux/redux-provider";
import { LinkingProvider } from "@gno/provider/linking-provider";

const gnoDefaultConfig = {
// @ts-ignore
Expand All @@ -30,17 +31,19 @@ export default function AppLayout() {
<NotificationProvider config={notificationDefaultConfig}>
<IndexerProvider config={indexerDefaultConfig}>
<ReduxProvider>
<ThemeProvider value={DefaultTheme}>
<Guard>
<Stack
screenOptions={{
headerShown: false,
headerLargeTitle: true,
headerBackVisible: false,
}}
/>
</Guard>
</ThemeProvider>
<LinkingProvider>
<ThemeProvider value={DefaultTheme}>
<Guard>
<Stack
screenOptions={{
headerShown: false,
headerLargeTitle: true,
headerBackVisible: false,
}}
/>
</Guard>
</ThemeProvider>
</LinkingProvider>
</ReduxProvider>
</IndexerProvider>
</NotificationProvider>
Expand Down
5 changes: 0 additions & 5 deletions mobile/app/post/[post_id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ function Page() {
}
};

const selectKeyOnGnokeyApp = async () => {
// TODO: implement this function

}

const selectKeyOnGnokeyApp = async () => {
if (!post) return;

Expand Down
76 changes: 44 additions & 32 deletions mobile/app/post/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Stack, useNavigation, useRouter } from "expo-router";
import { useEffect, useState } from "react";
import { KeyboardAvoidingView, Platform } from "react-native";
import { addProgress } from "redux/features/signupSlice";
import { selectAccount, useAppDispatch, useAppSelector } from "@gno/redux";
import { broadcastTxCommit, requestAddressForGnokeyMobile, selectAccount, useAppDispatch, useAppSelector } from "@gno/redux";
import * as Linking from 'expo-linking';

export default function Search() {
Expand Down Expand Up @@ -57,44 +57,57 @@ export default function Search() {
if (queryParams.tx && typeof queryParams.tx === "string") {
const signedTx = decodeURIComponent(queryParams.tx)
console.log("signedTx: ", signedTx);
await broadcastTxCommit(signedTx)

try {
setLoading(true);
await dispatch(broadcastTxCommit(signedTx)).unwrap();
router.push("home");
} catch (error) {
console.error("on broadcastTxCommit", error);
setError("" + error);
} finally {
setLoading(false);
}
}
}
}
})()
}, [url]);

const requestAddressForGnokeyMobile = async () => {
const requestAddress = async () => {
console.log("requesting address for GnokeyMobile");
// await dispatch(requestAddressForGnokeyMobile()).unwrap();
const callback = encodeURIComponent('tech.berty.dsocial://post');
Linking.openURL(`land.gno.gnokey://toselect?callback=${callback}`);
}

const broadcastTxCommit = async (signedTx: string) => {

setLoading(true);
setError(undefined);
dispatch(addProgress(`posting a message.`))

try {
for await (const response of await gnonative.broadcastTxCommit(signedTx)) {
const result = JSON.parse(JSON.stringify(response)).result;
console.log("broadcast result:", result);
}
router.push("home");
} catch (error) {
dispatch(addProgress(`error on broadcasting a tx: ` + JSON.stringify(error)))
console.error("on post screen", error);
setError("" + error);
} finally {
setLoading(false);
}

}

const makeCallTx = async (bech32:string) => {
// const broadcastTxCommit = async (signedTx: string) => {

// setLoading(true);
// setError(undefined);
// dispatch(addProgress(`posting a message.`))

// try {
// for await (const response of await gnonative.broadcastTxCommit(signedTx)) {
// const result = JSON.parse(JSON.stringify(response)).result;
// console.log("broadcast result:", result);
// }
// router.push("home");
// } catch (error) {
// dispatch(addProgress(`error on broadcasting a tx: ` + JSON.stringify(error)))
// console.error("on post screen", error);
// setError("" + error);
// } finally {
// setLoading(false);
// }

// }

const makeCallTx = async (bech32: string) => {
setLoading(true);
setError(undefined);
dispatch(addProgress(`posting a message.`))
dispatch(addProgress(`address ${bech32} selected. calling makeCallTx.`))

if (!account) throw new Error("No active account"); // never happens, but just in case

Expand All @@ -103,15 +116,14 @@ export default function Search() {
const gasWanted = BigInt(10000000);
const args: Array<string> = [postContent];

// const address = await gnonative.addressFromBech32('g1xz4h2uaxc2p6n9l8swpfyvnklzc7840rygkxrf');
const address = await gnonative.addressFromBech32(bech32);
const argsTx = await gnonative.makeCallTx("gno.land/r/berty/social", "PostMessage", args, gasFee, gasWanted, address)

console.log("txJson: ", argsTx.txJson);
console.log("account: ", JSON.stringify(account));
console.log("Opening Gnokey to sign the transaction, argsTx: ", argsTx.txJson);

setTimeout(() =>
Linking.openURL('land.gno.gnokey://tosign?tx=' + encodeURIComponent(argsTx.txJson)), 500)

Linking.openURL('land.gno.gnokey://tosign?tx=' + encodeURIComponent(argsTx.txJson));

} catch (error) {
dispatch(addProgress(`error on makeCallTx: ` + JSON.stringify(error)))
console.error("on post screen", error);
Expand Down Expand Up @@ -143,7 +155,7 @@ export default function Search() {
style={{ height: 200 }}
/>
<Spacer space={24} />
<Button.TouchableOpacity loading={loading} title="Post" variant="primary" onPress={requestAddressForGnokeyMobile} />
<Button.TouchableOpacity loading={loading} title="Post" variant="primary" onPress={requestAddress} />
<Spacer space={48} />
</KeyboardAvoidingView>
</Layout.BodyAlignedBotton>
Expand Down
32 changes: 14 additions & 18 deletions mobile/redux/features/txSlice.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { createAsyncThunk, createSlice, RootState } from "@reduxjs/toolkit";
import { Post } from "@gno/types";
import * as Linking from 'expo-linking';
import { ThunkExtra } from "redux/redux-provider";
interface State {
postToReply: Post | undefined;
}

const initialState: State = {
postToReply: undefined,
};

export const requestAddressForGnokeyMobile = createAsyncThunk<Promise<boolean>>("tx/requestAddressForGnokeyMobile", () => {
export const requestAddressForGnokeyMobile = createAsyncThunk<boolean>("tx/requestAddressForGnokeyMobile", async () => {
console.log("requesting address for GnokeyMobile");
const callback = encodeURIComponent('tech.berty.dsocial://post');
return Linking.openURL(`land.gno.gnokey://toselect?callback=${callback}`);
return await Linking.openURL(`land.gno.gnokey://toselect?callback=${callback}`);
});

export const broadcastTxCommit = createAsyncThunk<void, string, ThunkExtra>("tx/broadcastTxCommit", async (signedTx, thunkAPI) => {
console.log("broadcasting tx: ", signedTx);

const gnonative = thunkAPI.extra.gnonative;

await gnonative.broadcastTxCommit(signedTx);
});

export const txSlice = createSlice({
name: "tx",
initialState,
reducers: {},
selectors: {
selectPostToReply: (state) => state.postToReply,
},
extraReducers(builder) {
// builder.addCase(setPostToReply.fulfilled, (state, action) => {
// state.postToReply = action.payload.post;
// });
// builder.addCase(setPostToReply.rejected, (state, action) => {
// console.log("Error while replying a post, please, check the logs. %s", action.error.message);
// });
},
selectors: {}
});

// export const { selectPostToReply } = txSlice.selectors;
3 changes: 2 additions & 1 deletion mobile/redux/redux-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { Provider } from "react-redux";
import { configureStore } from "@reduxjs/toolkit";
import { accountSlice, profileSlice, replySlice } from "./features";
import { accountSlice, profileSlice, replySlice, txSlice } from "./features";
import { GnoNativeApi, useGnoNativeContext } from "@gnolang/gnonative";
import { signUpSlice } from "./features/signupSlice";
import { useSearch, UseSearchReturnType } from "@gno/hooks/use-search";
Expand Down Expand Up @@ -30,6 +30,7 @@ const ReduxProvider: React.FC<Props> = ({ children }) => {
[profileSlice.reducerPath]: profileSlice.reducer,
[replySlice.reducerPath]: replySlice.reducer,
[signUpSlice.reducerPath]: signUpSlice.reducer,
[txSlice.reducerPath]: txSlice.reducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
Expand Down
35 changes: 35 additions & 0 deletions mobile/src/provider/linking-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Linking from 'expo-linking';
import { useEffect } from 'react';


const LinkingProvider = ({ children }: { children: React.ReactNode }) => {
const url = Linking.useURL();

// const dispatch = useAppDispatch();

useEffect(() => {
if (url) {
const { hostname, path, queryParams } = Linking.parse(url);

console.log("link url", url);
console.log("link hostname", hostname);
console.log("link path", path);
console.log("link queryParams", queryParams);

if (queryParams) {

// if (queryParams.tx && typeof queryParams.tx === "string") {
// dispatch(setTxInput({ txInput: queryParams.tx }));
// }

// if (queryParams.callback && typeof queryParams.callback === "string") {
// dispatch(setCallback({ callback: decodeURIComponent(queryParams.callback) }));
// }
}
}
}, [url]);

return <>{children}</>;
};

export { LinkingProvider };

0 comments on commit 62b47c3

Please sign in to comment.