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

Add from contacts feature added,present time setting updated,modals c… #26

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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
258 changes: 183 additions & 75 deletions RouteMaster/Screens/Map1.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Alert,
TouchableOpacity,
Modal,
FlatList,

} from "react-native";
import MapView, { Marker } from "react-native-maps";
Expand All @@ -41,6 +42,7 @@ import Toast from "react-native-toast-message";
import CustomMarker from "../components/CustomMarker";
import { SMS } from "../components/SMS";
import { ScrollView } from "react-native-gesture-handler";
import * as Contacts from 'expo-contacts';


const API_KEY = process.env.EXPO_PUBLIC_GOOGLE_API_KEY;
Expand Down Expand Up @@ -79,6 +81,9 @@ const Map1 = () => {
const [RouteFound,setRouteFound]=useState(true);
const [cumulativeTime, setCumulativeTime] = useState(0);
const [loading, setLoading] = useState(false); // Added loading state
const [contactModalVisible, setContactModalVisible] = useState(false);
const [contacts, setContacts] = useState([]);
const [search, setSearch] = useState('');



Expand Down Expand Up @@ -171,7 +176,31 @@ const Map1 = () => {
}, []);


useEffect(() => {
const fetchContacts = async () => {
const { status } = await Contacts.requestPermissionsAsync();
if (status === 'granted') {
const { data } = await Contacts.getContactsAsync({
fields: [Contacts.Fields.Name, Contacts.Fields.PhoneNumbers],
});
setContacts(data);
}
};

fetchContacts();
}, []);

const handleContactSelect = (contact) => {
setUserName(contact.name);
if (contact.phoneNumbers && contact.phoneNumbers.length > 0) {
setUserMobile(contact.phoneNumbers[0].number);
}
setContactModalVisible(false);
};

const filteredContacts = contacts.filter(contact =>
contact.name.toLowerCase().includes(search.toLowerCase())
);

const handleConfirmLocation = () => {
setModalVisible(true);
Expand Down Expand Up @@ -295,6 +324,8 @@ const Map1 = () => {
setMarker(null)
setModalVisible(false);
setShowConfirmButton(false);
setUserMobile("");
setUserName("");
Alert.alert(
"Location Confirmed",
`Name: ${userName}\nMobile: ${userMobile}\nDelivery Time: ${formatTime(
Expand Down Expand Up @@ -396,7 +427,6 @@ const Map1 = () => {
};

const handleGetDirections = async () => {
setPresentTime(new Date());
setLoading(true); // Show loader
const latLongArray = locations.map((location) => ({
latitude: location.latitude,
Expand Down Expand Up @@ -555,6 +585,8 @@ const optimizedRouteCoordinates = data[0]
}
}finally{
setLoading(false);
setPresentTime(new Date());

}
};

Expand Down Expand Up @@ -1027,83 +1059,125 @@ const optimizedRouteCoordinates = data[0]
</ScrollView>
</BottomSheetView>
</BottomSheetModal>)}
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
setModalVisible(!modalVisible);
}}
>
<View style={styles.overlay}>
<View style={styles.modalView}>
{/* <Text style={{fontSize:20}}>Customer Details</Text> */}
<TouchableOpacity
style={styles.closeButton}
onPress={() => setModalVisible(!modalVisible)}
>
<Entypo name="cross" size={30} color="black" />
</TouchableOpacity>
<TextInput
style={styles.input}
placeholder="Name"
value={userName}
onChangeText={setUserName}
/>
<TextInput
style={styles.input}
placeholder="Mobile"
value={userMobile}
onChangeText={setUserMobile}
keyboardType="phone-pad"
/>
<TouchableOpacity onPress={() => setShowStartPicker(true)}>
<Text style={styles.timeText}>
START TIME: {formatTime(startTime)}
</Text>
</TouchableOpacity>
{showStartPicker && (
<DateTimePicker
value={startTime}
mode="time"
is24Hour={false}
display="default"
onChange={(event, selectedTime) => {
setShowStartPicker(false);
setStartTime(selectedTime || startTime);
}}
/>
)}
<TouchableOpacity onPress={() => setShowEndPicker(true)}>
<Text style={styles.timeText}>
END TIME: {formatTime(endTime)}
</Text>
</TouchableOpacity>
{showEndPicker && (
<DateTimePicker
value={endTime}
mode="time"
is24Hour={false}
display="default"
onChange={(event, selectedTime) => {
setShowEndPicker(false);
setEndTime(selectedTime || endTime);
}}
/>
)}
<View style={styles.loginbutton}>
<TouchableOpacity
style={styles.inBut}
onPress={handleSubmit}
>
<View>
<Text style={styles.textSign}>SUBMIT</Text>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
setModalVisible(!modalVisible);
}}
>
<View style={styles.overlay}>
<View style={styles.modalView}>
<TouchableOpacity
style={styles.closeButton}
onPress={() => setModalVisible(!modalVisible)}
>
<Entypo name="cross" size={30} color="black" />
</TouchableOpacity>
<TextInput
style={styles.input}
placeholder="Name"
value={userName}
onChangeText={setUserName}
/>
<TextInput
style={styles.input}
placeholder="Mobile"
value={userMobile}
onChangeText={setUserMobile}
keyboardType="phone-pad"
/>
<TouchableOpacity onPress={() => setContactModalVisible(true)}>
<Text style={styles.contactPickerText}>Select from Contacts</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => setShowStartPicker(true)}>
<Text style={styles.timeText}>
START TIME: {formatTime(startTime)}
</Text>
</TouchableOpacity>
{showStartPicker && (
<DateTimePicker
value={startTime}
mode="time"
is24Hour={false}
display="default"
onChange={(event, selectedTime) => {
setShowStartPicker(false);
setStartTime(selectedTime || startTime);
}}
/>
)}
<TouchableOpacity onPress={() => setShowEndPicker(true)}>
<Text style={styles.timeText}>
END TIME: {formatTime(endTime)}
</Text>
</TouchableOpacity>
{showEndPicker && (
<DateTimePicker
value={endTime}
mode="time"
is24Hour={false}
display="default"
onChange={(event, selectedTime) => {
setShowEndPicker(false);
setEndTime(selectedTime || endTime);
}}
/>
)}
<View style={styles.loginbutton}>
<TouchableOpacity
style={styles.inBut}
onPress={handleSubmit}
>
<View>
<Text style={styles.textSign}>SUBMIT</Text>
</View>
</TouchableOpacity>
</View>
</View>
</View>

<Modal
animationType="slide"
transparent={true}
visible={contactModalVisible}
onRequestClose={() => {
setContactModalVisible(!contactModalVisible);
}}
>
<View style={styles.overlay}>
<View style={styles.contactModalView}>
<TouchableOpacity
style={styles.closeButton}
onPress={() => setContactModalVisible(!contactModalVisible)}
>
<Entypo name="cross" size={30} color="black" />
</TouchableOpacity>
<TextInput
style={styles.input}
placeholder="Search Contacts"
value={search}
onChangeText={setSearch}
/>
<FlatList
data={filteredContacts}
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<TouchableOpacity onPress={() => handleContactSelect(item)}>
<View style={styles.contactItem}>
<Text style={styles.contactName}>{item.name}</Text>
{item.phoneNumbers && item.phoneNumbers.length > 0 && (
<Text style={styles.contactNumber}>{item.phoneNumbers[0].number}</Text>
)}
</View>
</TouchableOpacity>
</View>
</View>
)}
/>
</View>
</Modal>
</View>
</Modal>
</Modal>
</View>
</BottomSheetModalProvider>
);
Expand Down Expand Up @@ -1299,6 +1373,40 @@ const styles = StyleSheet.create({
fontWeight: 'bold',
color: 'black',
},
contactItem: {
padding: 10,
borderBottomWidth: 1,
borderBottomColor: 'gray',
},
contactName: {
fontSize: 16,
},
contactNumber: {
fontSize: 14,
color: 'gray',
},
contactPickerText: {
color: '#34A751',
marginBottom: 20,
textAlign: 'center',
},
contactModalView: {
width: '90%',
height: '80%',
backgroundColor: 'white',
borderRadius: 15,
paddingVertical: 30,
paddingHorizontal: 20,
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5,
position: 'relative',
},
});

export default Map1;
Loading