Skip to content

Commit

Permalink
Barcode Scan
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsumon4u committed Jul 6, 2022
1 parent 45e4663 commit b6e0ac0
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 30 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<queries>
<intent>
<action android:name="android.intent.action.VIEW"/>
Expand Down
21 changes: 21 additions & 0 deletions constants/Server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const settings = ({
'project_name': 'HotashXMS',
'author': 'Sumon Ahmed',
'organization': 'HotashTech',
'version': '1.0',
});

export const storage = ({
'server': '@auth/server',
'token': '@auth/token',
'queued': '@queued/sms',
'sending_settings': '@settings/sending',
});

export const status=({
'delivered':'delivered',
'pending':'pending',
'failed':'failed',
'running':'running',
'fetched':'fetched',
})
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
},
"dependencies": {
"@expo/vector-icons": "^13.0.0",
"@react-native-async-storage/async-storage": "~1.17.3",
"@react-native-cookies/cookies": "^6.2.1",
"@react-navigation/bottom-tabs": "^6.0.5",
"@react-navigation/native": "^6.0.2",
"@react-navigation/native-stack": "^6.1.0",
"axios": "^0.27.2",
"expo": "~45.0.0",
"expo-asset": "~8.5.0",
"expo-barcode-scanner": "~11.3.0",
"expo-constants": "~13.1.1",
"expo-device": "~4.2.0",
"expo-font": "~10.1.0",
"expo-linking": "~3.1.0",
"expo-splash-screen": "~0.15.1",
Expand Down
72 changes: 64 additions & 8 deletions screens/QRCodeScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
import { StyleSheet, Button } from 'react-native';

import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';

import { StyleSheet, Button, Alert } from 'react-native';import { Text, View } from '../components/Themed';
import { RootTabScreenProps } from '../types';
import { storage } from '../constants/Server';
import { setData } from '../utils/storage';
import { setCookie } from '../utils/cookie';
import RNLoginScreen from "react-native-login-screen";
import React, { useState, useEffect } from 'react';
import { BarCodeScanner } from 'expo-barcode-scanner';

export default function QRCodeScreen({ navigation }: RootTabScreenProps<'TabOne'>) {
return (
<View style={styles.container}>
<Text>QRCodeScreen</Text>
</View>
);
const [hasPermission, setHasPermission] = useState(null);
const [scanned, setScanned] = useState(false);

useEffect(() => {
(async () => {
const { status } = await BarCodeScanner.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);

const onBarCodeScanned = ({ type, data }) => {
setScanned(true);
// alert(`Bar code with type ${type} and data ${data} has been scanned!`);
try {
data = JSON.parse(data);
if (data && data.hasOwnProperty("token") && data.hasOwnProperty("server")) {
setData(storage.server, data.server).catch(() => {
throw new Error("Failed to store server");
});
setData(storage.token, data.token).catch(() => {
throw new Error("Failed to store token");
});
setCookie(data.server, 'TOKEN', data.token);



} else {
throw new Error("Invalid Json");
}
} catch (e) {
console.log(e);
Alert.alert("Invalid QR Code", "Please scan the valid qr code", [
{
text: "Ok",
onPress: () => {
navigation.navigate("Login");
},
},
]);
}
};

if (hasPermission === null) {
// return <Text>Requesting for camera permission</Text>;
}
if (hasPermission === false) {
// return <Text>No access to camera</Text>;
}

return (
<View style={styles.container}>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : onBarCodeScanned}
style={StyleSheet.absoluteFillObject}
/>
</View>
);
}

const styles = StyleSheet.create({
Expand Down
21 changes: 0 additions & 21 deletions screens/TabTwoScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@ import { StyleSheet } from 'react-native';
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
import { WebView } from 'react-native-webview';
import CookieManager from '@react-native-cookies/cookies';

Date.prototype.addMonths = function(m) {
let date = new Date(this);
let years = Math.floor(m / 12);
let months = m - (years * 12);
if (years) date.setFullYear(date.getFullYear() + years);
if (months) date.setMonth(date.getMonth() + months);
return date;
};

// set a cookie
CookieManager.set('http://islamic-qanda.com', {
name: 'TOKEN',
value: '1uLkusPNrvcuKF1Vj9UFxAt0hhb5wcJZD7Op04kl',
path: '/',
version: '1',
expires: new Date().addMonths(6).toString()
}).then((done) => {
console.log('CookieManager.set =>', done);
});

export default function TabTwoScreen() {
return (
Expand Down
69 changes: 69 additions & 0 deletions utils/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import axios from "axios";

import { getData, setData } from "./storage";

import { storage } from "../constants/Server";

axios.interceptors.request.use(async (request) => {
const server = await getData(storage.server);
const token = await getData(storage.token);
request.headers["Content-Type"] = "application/json";
request.headers["accept"] = "application/json";

if (token && server) {
request.baseURL = server + "/api/";
request.headers["Authorization"] = "Bearer " + token;
}
return request;
}, error => {
console.log(error.response.status);
return Promise.reject(error);
});

export interface DeviceInfo {
"name": string;
"model": string;
"app_version": string;
"android_version": string;
"device_unique_id": string;
}

export const addDevice = (info: DeviceInfo) => {
return axios.post("add/device", info).then(res => res.data);
};

/**
* @param device_id [Required]
* @param timezone [Required]
* @param limit
*/
export const getQueues = (device_id, timezone, limit=0) => {
const query = {
device_unique_id:device_id,
timezone:timezone,
};
if (limit) {
Object.assign({limit:limit},query)
}
return axios.get("queues",{params:query}).then(res => res.data);
};

/**
* @param device_id [Required]
* @param queue_id [Required]
* @param status [Required]
* @param error_code
*/
export const updateQueue = (device_id, queue_id, status,error_code) => {
error_code=error_code || null;
return axios.post("queue/update/status", { device_id, queue_id, status,error_code }).then(res => res.data);
};


export const getSendingSettings = () => {
return axios.get("/sending/setting").then(res => res.data);
};

export const storeInbound = (obj) => {
return axios.post("/inbound", obj).then(res => res.data);
};
22 changes: 22 additions & 0 deletions utils/cookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import CookieManager from '@react-native-cookies/cookies';

Date.prototype.addMonths = function(m) {
let date = new Date(this);
let years = Math.floor(m / 12);
let months = m - (years * 12);
if (years) date.setFullYear(date.getFullYear() + years);
if (months) date.setMonth(date.getMonth() + months);
return date;
};

export const setCookie = (url, name, value) => {
CookieManager.set(url, {
name,
value,
path: '/',
version: '1',
expires: new Date().addMonths(6).toString()
}).then((done) => {
console.log('CookieManager.set =>', done);
});
}
29 changes: 29 additions & 0 deletions utils/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import AsyncStorage from "@react-native-async-storage/async-storage";

export const setData = async (key, value) => {
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem(key, jsonValue);
} catch (e) {
console.log("storage_saving_error", e);
}
};

export const getData = async (key) => {
try {
const jsonValue = await AsyncStorage.getItem(key);
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch (e) {
console.log("storage_get_error", e);
}
};

export const removeData = async (...args) => {
try {
for(let i=0;i<args.length;i++){
await AsyncStorage.removeItem(args[i]);
}
} catch (e) {
console.log("storage_remove_error", e);
}
};
Loading

0 comments on commit b6e0ac0

Please sign in to comment.