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

Fix react router for mobile #240

Closed
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
2 changes: 1 addition & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions frontend/src/components/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export default class HomePage extends Component {
}

render() {
window.nativeMessage = (event) => {
alert(event.detail)
};
window.ReactNativeWebView.postMessage(JSON.stringify({
title: 'TEST TO ANDROID'
}));
return (
<Router >
<div className='appCenter'>
Expand Down
19 changes: 15 additions & 4 deletions frontend/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ const config: Configuration = {
resolve: {
extensions: [".tsx", ".ts", ".jsx", ".js"],
},
};

var configWeb = Object.assign({}, config, {
name: "configWeb",
output: {
path: path.resolve(__dirname, "static/frontend"),
filename: "main.js",
path: path.resolve(__dirname, "static/frontend"),
filename: "main.js",
},
};
});
var configMobile = Object.assign({}, config, {
name: "configMobile",
output: {
path: path.resolve(__dirname, "../mobile/html/Web.bundle/js"),
filename: "main.js",
},
});

export default config;
export default [configWeb, configMobile];
73 changes: 47 additions & 26 deletions mobile/App.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
import React from 'react';
import { WebView, WebViewMessageEvent } from "react-native-webview";
import React, {useRef} from 'react';
import {WebView, WebViewMessageEvent, WebViewProps} from 'react-native-webview';
import {SafeAreaView, Text, Platform} from 'react-native';
// import Tor from "react-native-tor";
import Tor from 'react-native-tor';

// Initialize the module
// const tor = Tor();
const tor = Tor();

// const makeTorRequest = async()=>{
// // Start the daemon and socks proxy (no need for Orbot and yes iOS supported!)
// await tor.startIfNotStarted();
const makeTorRequest = async () => {
// Start the daemon and socks proxy (no need for Orbot and yes iOS supported!)
await tor.startIfNotStarted();

// try{
// // Use built in client to make REST calls to .onion urls routed through the Sock5 proxy !
// const resp = await tor.get('http://robosats6tkf3eva7x2voqso3a5wcorsnw34jveyxfqi2fu7oyheasid.onion/api/info');
// return resp
// } catch(error){
// // Catch a network or server error like you normally with any other fetch library
// }
// }
try {
// Use built in client to make REST calls to .onion urls routed through the Sock5 proxy !
const resp = await tor.get(
'http://robosats6tkf3eva7x2voqso3a5wcorsnw34jveyxfqi2fu7oyheasid.onion/api/info',
);

return resp;
} catch (error) {
// Catch a network or server error like you normally with any other fetch library
}
};
makeTorRequest();

const App = () => {
// Webview with local html/js in a single location for andrid/iOS
// https://yelotofu.com/react-native-load-local-static-site-inside-webview-2b93eb1c4225
const htmlPath = (Platform.OS === 'android' ? 'file:///android_asset/' : '') + 'Web.bundle/index.html';
const webViewRef = useRef<WebView>();
var uri =
(Platform.OS === 'android' ? 'file:///android_asset/' : '') +
'Web.bundle/index.html';

const uri = 'https://robosats.onion.moe'
const onion = 'http://robosats6tkf3eva7x2voqso3a5wcorsnw34jveyxfqi2fu7oyheasid.onion'
// const uri = 'https://robosats.onion.moe';
const onion =
'http://robosats6tkf3eva7x2voqso3a5wcorsnw34jveyxfqi2fu7oyheasid.onion';

const runFirst = `
// document.body.style.backgroundColor = 'red';
Expand All @@ -34,18 +40,33 @@ const App = () => {
// true; // note: this is required, or you'll sometimes get silent failures
`;

const onMessage = async (event: WebViewMessageEvent) => {
console.log(event);
webViewRef.current?.injectJavaScript(
`
(function() {
window.nativeMessage({"detail": "TEST TO WEB"});
})();
`,
);
};

return (
<SafeAreaView style={{ flex: 1 }}>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this just for testing

<SafeAreaView style={{flex: 1}}>
<WebView
source={{ uri: uri }}
// source={{ baseUrl: 'file:///android_asset/Web.bundle/' }}
source={{
uri: uri,
}}
onMessage={onMessage}
// @ts-ignore
ref={ref => (webViewRef.current = ref)}
javaScriptEnabled={true}
domStorageEnabled={true}
sharedCookiesEnabled={true}
originWhitelist={['*']} //originWhitelist={[uri,uri2]}
originWhitelist={[uri, onion]}
scalesPageToFit={true}
startInLoadingState={true}
mixedContentMode={"always"}
mixedContentMode={'always'}
allowsInlineMediaPlayback={true}
allowsFullscreenVideo={false}
setBuiltInZoomControls={false}
Expand All @@ -56,7 +77,7 @@ const App = () => {
allowsLinkPreview={false}
injectedJavaScript={runFirst}
renderLoading={() => <Text>Loading RoboSats</Text>}
onError={(syntheticEvent) => <Text>{syntheticEvent}</Text>}
onError={syntheticEvent => <Text>{syntheticEvent}</Text>}
/>
</SafeAreaView>
);
Expand Down
10 changes: 6 additions & 4 deletions mobile/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ android {
ndkVersion rootProject.ext.ndkVersion

compileSdkVersion rootProject.ext.compileSdkVersion
sourceSets {
main { assets.srcDirs = ['src/main/assets', '../../html'] }

sourceSets {
main { assets.srcDirs = ['src/main/assets', '../../html'] }
}

defaultConfig {
applicationId "com.robosats"
minSdkVersion rootProject.ext.minSdkVersion
Expand Down Expand Up @@ -267,6 +267,8 @@ dependencies {

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

implementation files("../../node_modules/react-native-tor/android/libs/sifir_android.aar")

if (enableHermes) {
//noinspection GradleDynamicVersion
implementation("com.facebook.react:hermes-engine:+") { // From node_modules
Expand Down
1 change: 1 addition & 0 deletions mobile/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
Expand Down
2 changes: 1 addition & 1 deletion mobile/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.apache.tools.ant.taskdefs.condition.Os
buildscript {
ext {
buildToolsVersion = "31.0.0"
minSdkVersion = 21
minSdkVersion = 26
compileSdkVersion = 31
targetSdkVersion = 31

Expand Down
Loading