Updated HistoryScreen: changed UI and added toggleBookmark and deleteQRCode with BookMark Tab. Yet to solve setQrCodes updater function error message

This commit is contained in:
2024-06-19 21:22:05 +08:00
parent 7afe5982e6
commit b64e03d774
3 changed files with 119 additions and 29 deletions

View File

@@ -1,32 +1,82 @@
import React, { useContext, useState } from 'react'; import React, { useContext, useState } from 'react';
import { View, Text, StyleSheet, FlatList, TouchableOpacity } from 'react-native'; import { View, Text, StyleSheet, FlatList, TouchableOpacity } from 'react-native';
import { QRCodeContext } from '../types'; import { QRCodeContext, QRCode } from '../types'; // Import QRCode type
import ScannedDataBox from '../components/ScannedDataBox'; import ScannedDataBox from '../components/ScannedDataBox';
import { Ionicons } from '@expo/vector-icons';
const HistoryScreen: React.FC = () => { const HistoryScreen: React.FC = () => {
const qrCodeContext = useContext(QRCodeContext); const qrCodeContext = useContext(QRCodeContext);
const { qrCodes, setCurrentScannedData } = qrCodeContext || { qrCodes: [], setCurrentScannedData: () => {} }; const qrCodes = qrCodeContext?.qrCodes || [];
const setQrCodes = qrCodeContext?.setQrCodes || (() => {});
const [selectedData, setSelectedData] = useState<string | null>(null); const [selectedData, setSelectedData] = useState<string | null>(null);
const [scanResult, setScanResult] = useState<any>(null); // KI for testing const [selectedScanResult, setSelectedScanResult] = useState<any | null>(null);
const [dataType, setDataType] = useState<string>(''); // KIV const [showBookmarks, setShowBookmarks] = useState<boolean>(false);
const toggleBookmark = (index: number) => {
setQrCodes((prev: QRCode[]) => {
const originalIndex = prev.length - 1 - index; // Compute the original index
const newQrCodes = [...prev];
newQrCodes[originalIndex].bookmarked = !newQrCodes[originalIndex].bookmarked;
return newQrCodes;
});
};
const deleteQRCode = (index: number) => {
setQrCodes((prev: QRCode[]) => {
const originalIndex = prev.length - 1 - index; // Compute the original index
return prev.filter((_, i) => i !== originalIndex);
});
};
const filteredQrCodes = (showBookmarks ? qrCodes.filter(qr => qr.bookmarked) : qrCodes.slice().reverse());
const handleItemPress = (item: any) => {
setSelectedData(item.data);
setSelectedScanResult(item.scanResult);
};
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Text style={styles.welcomeText}>History Screen</Text> <View style={styles.headerContainer}>
<TouchableOpacity onPress={() => setShowBookmarks(false)}>
<Text style={!showBookmarks ? styles.headerTextActive : styles.headerTextInactive}>History</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => setShowBookmarks(true)}>
<Text style={showBookmarks ? styles.headerTextActive : styles.headerTextInactive}>Bookmarks</Text>
</TouchableOpacity>
</View>
{selectedData && ( {selectedData && (
<ScannedDataBox data={selectedData} scanResult={scanResult} dataType={dataType} /> <ScannedDataBox data={selectedData} scanResult={selectedScanResult} dataType="URL" />
)} )}
<FlatList <FlatList
data={qrCodes} data={filteredQrCodes}
renderItem={({ item }) => ( renderItem={({ item, index }) => {
<TouchableOpacity onPress={() => setSelectedData(item)}> console.log('item:', item); // Log the item data for debugging
<View style={styles.dataBox}> const itemData = item.data ? item.data.split('\n')[1]?.split('Data: ')[1] : 'Invalid data';
<Text style={styles.dataText}>{item}</Text> return (
</View> <View style={styles.itemContainer}>
<View style={styles.itemLeft}>
<Ionicons name="qr-code-outline" size={24} color="#ff69b4" style={styles.qrIcon} />
<TouchableOpacity onPress={() => handleItemPress(item)}>
<Text style={styles.dataText}>{itemData}</Text>
</TouchableOpacity> </TouchableOpacity>
)} <Text style={styles.dateText}>{new Date().toLocaleDateString('en-GB', {
day: '2-digit', month: 'short', year: 'numeric', hour: '2-digit', minute: '2-digit'
})}</Text>
</View>
<View style={styles.itemRight}>
<TouchableOpacity onPress={() => toggleBookmark(index)}>
<Ionicons name={item.bookmarked ? "bookmark" : "bookmark-outline"} size={24} color={item.bookmarked ? "#2196F3" : "#ff69b4"} />
</TouchableOpacity>
<TouchableOpacity onPress={() => deleteQRCode(index)}>
<Ionicons name="close-circle-outline" size={24} color="#ff69b4" />
</TouchableOpacity>
</View>
</View>
);
}}
keyExtractor={(item, index) => index.toString()} keyExtractor={(item, index) => index.toString()}
contentContainerStyle={styles.flatListContent} contentContainerStyle={styles.flatListContent}
/> />
@@ -40,26 +90,50 @@ const styles = StyleSheet.create({
backgroundColor: '#f8f0fc', backgroundColor: '#f8f0fc',
padding: 20, padding: 20,
}, },
welcomeText: { headerContainer: {
textAlign: 'center', flexDirection: 'row',
fontSize: 20, justifyContent: 'space-around',
marginVertical: 10, marginBottom: 20,
color: 'black',
}, },
dataBox: { headerTextActive: {
marginVertical: 10, fontSize: 24,
padding: 10, fontWeight: 'bold',
backgroundColor: '#fff', color: '#ff69b4',
borderRadius: 5, },
headerTextInactive: {
fontSize: 24,
fontWeight: 'bold',
color: '#ccc',
},
itemContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', backgroundColor: '#ffe6f0',
padding: 10,
borderRadius: 10,
marginBottom: 10,
},
itemLeft: {
flexDirection: 'column',
},
itemRight: {
flexDirection: 'row',
}, },
dataText: { dataText: {
fontSize: 16, fontSize: 16,
color: '#000', color: '#000',
marginBottom: 5,
},
dateText: {
fontSize: 12,
color: '#000',
},
qrIcon: {
marginBottom: 5,
}, },
flatListContent: { flatListContent: {
paddingBottom: 100, // Add padding to the bottom so that it wont kenna hidden by nav bar paddingBottom: 100,
}, },
}); });

View File

@@ -123,17 +123,33 @@ const QRScannerScreen: React.FC<QRScannerScreenProps> = ({ clearScanData }) => {
let newScannedData = `Type: ${dataType}\nData: ${data}`; let newScannedData = `Type: ${dataType}\nData: ${data}`;
let scanResult = {
secureConnection: false,
virusTotalCheck: false,
redirects: 0
};
try { try {
const scanId = await processWithVirusTotal(data); const scanId = await processWithVirusTotal(data);
const positive = await getVirusTotalResults(scanId); const positive = await getVirusTotalResults(scanId);
newScannedData += `\nScore: ${positive}`; newScannedData += `\nScore: ${positive}`;
setScanResult({ positive, scanId }); scanResult = {
secureConnection: true, // Assume secure connection if we get here
virusTotalCheck: positive === 0, // Safe if no positive results
redirects: 2 // Arbitrary value, replace with real data if available
};
} catch (error) { } catch (error) {
console.error('Error handling barcode scan:', error); console.error('Error handling barcode scan:', error);
} }
const qrCode = {
data: newScannedData,
bookmarked: false,
scanResult
};
setScannedData(newScannedData); setScannedData(newScannedData);
setQrCodes([...qrCodes, newScannedData]); setQrCodes([...qrCodes, qrCode]);
}; };
// If the focus is lost focus on this screen , when come reset the scan data // If the focus is lost focus on this screen , when come reset the scan data

View File

@@ -1,6 +1,6 @@
import { createContext } from "react"; import { createContext } from "react";
interface QRCode { export interface QRCode {
data: string; data: string;
bookmarked: boolean; bookmarked: boolean;
scanResult: { scanResult: {