Skip to content

Commit

Permalink
avniproject#1191 | Deepthi M, Deepti R, Implemented the functionality…
Browse files Browse the repository at this point in the history
… to configure the server url.
  • Loading branch information
deeptirawat1510 committed Nov 20, 2023
1 parent 1acd050 commit 974be11
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ as_prerelease_dev: ; $(call _create_config,prerelease_dev)
as_perf: ; $(call _create_config,perf)
as_prod: ; $(call _create_config,prod)
as_prod_dev: ; $(call _create_config,prod_dev)
as_no_env: ; $(call _create_config,no_env)

release_clean: ## If you get dex errors
rm -rf packages/openchs-android/android/app/build
Expand Down Expand Up @@ -197,6 +198,13 @@ release_prod_universal_without_clean:
release_prod_universal:
enableSeparateBuildPerCPUArchitecture=false make release_prod

release_no_env: as_no_env release

release_no_env_universal_without_clean:
enableSeparateBuildPerCPUArchitecture=false make release_no_env

release_no_env_universal: renew_env release_no_env_universal_without_clean

release_staging_without_clean: as_staging
enableSeparateBuildPerCPUArchitecture=false make release

Expand Down
1 change: 1 addition & 0 deletions makefiles/androidDevice.mk
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ run_app_prerelease: as_prerelease _run_app
run_app_prerelease_dev: as_prerelease_dev _run_app
run_app_prod: as_prod _run_app
run_app_prod_dev: as_prod_dev _run_app
run_app_no_env: as_no_env _run_app

stop_app:
adb shell am force-stop ${app_android_package_name}
Expand Down
4 changes: 4 additions & 0 deletions packages/openchs-android/config/env/no_env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"allowServerURLConfig": true,
"ENV": "prod"
}
3 changes: 2 additions & 1 deletion packages/openchs-android/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import AppStore from "./store/AppStore";
import RealmFactory from "./framework/db/RealmFactory";
import General from "./utility/General";
import EnvironmentConfig from "./framework/EnvironmentConfig";
import Config from './framework/Config';

const {TamperCheckModule} = NativeModules;

Expand Down Expand Up @@ -49,7 +50,7 @@ class App extends Component {
const clipboardString = `${this.state.error.message}\nStacktrace:${this.state.stacktrace}`;
General.logError("App", `renderError: ${clipboardString}`);

if (EnvironmentConfig.inNonDevMode()) {
if (EnvironmentConfig.inNonDevMode() && !Config.allowServerURLConfig) {
Alert.alert("App will restart now", this.state.error.message,
[
{
Expand Down
4 changes: 3 additions & 1 deletion packages/openchs-android/src/Avni.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {StatusBar, StyleSheet, View} from 'react-native';
import Colors from "./views/primitives/Colors";
import { LogBox } from 'react-native';
import General from "./utility/General";
import Config from './framework/Config';

export default class Avni extends Component {
static styles = StyleSheet.create({
Expand All @@ -18,10 +19,11 @@ export default class Avni extends Component {
LogBox.ignoreAllLogs();
General.logDebug("Avni", "=====================>>>>>>>Rendering main app component");
const App = require('./App').default;
const ServerUrlConfiguration = require('./ServerUrlConfiguration').default;
return (
<View style={Avni.styles.container}>
<StatusBar backgroundColor={Colors.headerBackgroundColor} barStyle={'default'}/>
<App/>
{Config.allowServerURLConfig ? <ServerUrlConfiguration /> : <App />}
</View>
);
}
Expand Down
133 changes: 133 additions & 0 deletions packages/openchs-android/src/ServerUrlConfiguration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import React, { Component } from 'react';
import { Image, Text, View, TextInput, TouchableOpacity } from 'react-native';
import Colors from './views/primitives/Colors';
const App = require('./App').default;
import AsyncStorage from '@react-native-async-storage/async-storage';
import { getJSON } from './framework/http/requests';

class ServerUrlConfiguration extends Component {
constructor(props) {
super(props);
this.state = {
serverUrl: '',
isValidUrl: true,
isURLInitialised: false,
isLoading: false,
isVerifying: false,
};
}

async componentDidMount() {
this.setState({ isLoading: true });
if (!_.isNil(await AsyncStorage.getItem('serverUrl'))) {
this.setState({
isURLInitialised: true
})
}
this.setState({ isLoading: false });
}

isUrlValid = async (url) => {
const urlRegex = /^(https?:\/\/)?(?!www\.)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\/.*)?$/;
const ipRegex = /^(https?:\/\/)?(\d{1,3}\.){3}\d{1,3}:\d{1,5}$/;
if (urlRegex.test(url) || ipRegex.test(url)) {
return getJSON(url + "/idp-details", true).then((idpDetails) => {
return true;
}).catch((error) => {
return false;
});
}
else {
return false;
}

};

handleUrlChange = (text) => {
this.setState({ serverUrl: text, isValidUrl: true });
};

handleSubmit = async () => {
this.setState({ isVerifying: true });
const lowerCaseUrl = this.state.serverUrl.replace(/^https/i, 'https');
if (await this.isUrlValid(lowerCaseUrl)) {
this.storeServerUrl(lowerCaseUrl);
this.setState({ isURLInitialised: true });
} else {
this.setState({ isValidUrl: false });
}
this.setState({ isVerifying: false });
};

storeServerUrl = async (url) => {
try {
await AsyncStorage.setItem('serverUrl', url);
console.log('Server URL stored successfully');
} catch (error) {
console.error('Error storing server URL:', error);
}
};

renderURLConfigView = () => {
return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Image source={{ uri: 'asset:/logo.png' }} style={{ height: 120, width: 120, alignSelf: 'center' }} resizeMode={'center'} />

<View style={{ paddingHorizontal: 48, marginTop: 20, width: '80%' }}>
<TextInput
value={this.state.serverUrl}
onChangeText={this.handleUrlChange}
placeholder="Enter Server URL"
style={{
borderBottomWidth: 1,
borderColor: Colors.primaryColor,
paddingVertical: 8,
fontSize: 16,
}}
/>

{!this.state.isValidUrl && (
<Text style={{ color: Colors.ValidationError, fontSize: 14, marginTop: 10 }}>Please enter a valid server URL.</Text>
)}

{this.state.isVerifying && <Text style={{ color: Colors.Complimentary, fontSize: 14, marginTop: 10 }}>Validating URL....</Text>}

<TouchableOpacity
onPress={() => {
this.handleSubmit();
}}
style={{
marginTop: 20,
backgroundColor: this.state.serverUrl && this.state.isValidUrl && !this.state.isVerifying ? "#009973" : "gray",
paddingVertical: 10,
borderRadius: 5,
alignItems: 'center',
}}
disabled={!this.state.serverUrl || !this.state.isValidUrl || this.state.isVerifying}
>
<Text style={{ color: 'white', fontSize: 16 }}>Submit</Text>
</TouchableOpacity>
</View>
</View>)
}

renderTextView = (text) => {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }} >
<Text>{text}</Text>
</View>);
}

render() {
if (this.state.isLoading) {
return this.renderTextView("Loading...");
}
if (this.state.isURLInitialised && !this.state.isLoading) {
return <App />
}
if (!this.state.isURLInitialised && !this.state.isLoading) {
return this.renderURLConfigView();
}
}
}

export default ServerUrlConfiguration;
10 changes: 8 additions & 2 deletions packages/openchs-android/src/service/SettingsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {ModelGeneral, Settings, LocaleMapping, OrganisationConfig} from 'openchs
import Config from '../framework/Config';
import _ from 'lodash';
import EnvironmentConfig from "../framework/EnvironmentConfig";
import AsyncStorage from '@react-native-async-storage/async-storage';
import RNRestart from 'react-native-restart';

@Service("settingsService")
class SettingsService extends BaseService {
Expand All @@ -16,7 +18,8 @@ class SettingsService extends BaseService {
super(db, beanStore);
}

init() {
async init() {
let url = Config.allowServerURLConfig ? await AsyncStorage.getItem('serverUrl') : Config.SERVER_URL;
const dbInScope = this.db;
General.logDebug("SettingsService", `Config.ENV: ${Config.ENV}`);
this.db.write(() => {
Expand All @@ -28,9 +31,12 @@ class SettingsService extends BaseService {
settings.password = "";
settings.logLevel = InitialSettings.logLevel;
settings.pageSize = InitialSettings.pageSize;
settings.serverURL = Config.SERVER_URL;
settings.serverURL = url;
settings.poolId = "";
settings.clientId = Config.CLIENT_ID || "";
if (Config.allowServerURLConfig){
RNRestart.Restart();
}
}

if (EnvironmentConfig.isDevMode()) {
Expand Down

0 comments on commit 974be11

Please sign in to comment.