-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceConnectionModal.tsx
127 lines (120 loc) · 3.02 KB
/
DeviceConnectionModal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React, { FC, useCallback } from "react";
import {
FlatList,
ListRenderItemInfo,
Modal,
SafeAreaView,
Text,
StyleSheet,
TouchableOpacity,
} from "react-native";
import { Device } from "react-native-ble-plx";
type DeviceModalListItemProps = {
item: ListRenderItemInfo<Device>;
connectToPeripheral: (device: Device) => void;
closeModal: () => void;
};
type DeviceModalProps = {
devices: Device[];
visible: boolean;
connectToPeripheral: (device: Device) => void;
closeModal: () => void;
};
const DeviceModalListItem: FC<DeviceModalListItemProps> = (props) => {
const { item, connectToPeripheral, closeModal } = props;
const connectAndCloseModal = useCallback(() => {
connectToPeripheral(item.item);
closeModal();
}, [closeModal, connectToPeripheral, item.item]);
// console.log(`item id: ${item.item.id}, item name: ${item.item.name}`);
// console.log(item.item.id === '88:6B:0F:3E:82:9B');
// console.log('item.item.name: ', item.item.name);
// 88:6B:0F:3E:82:9B
return (
<TouchableOpacity
onPress={connectAndCloseModal}
style={modalStyle.ctaButton}
>
<Text style={modalStyle.ctaButtonText}>{item.item.id}</Text>
</TouchableOpacity>
);
};
const DeviceModal: FC<DeviceModalProps> = (props) => {
const { devices, visible, connectToPeripheral, closeModal } = props;
// console.log('devices: ', devices);
const renderDeviceModalListItem = useCallback(
(item: ListRenderItemInfo<Device>) => {
return (
<DeviceModalListItem
item={item}
connectToPeripheral={connectToPeripheral}
closeModal={closeModal}
/>
);
},
[closeModal, connectToPeripheral]
);
return (
<Modal
style={modalStyle.modalContainer}
animationType="slide"
transparent={false}
visible={visible}
>
<SafeAreaView style={modalStyle.modalTitle}>
<Text style={modalStyle.modalTitleText}>
Tap on a device to connect
</Text>
<FlatList
contentContainerStyle={modalStyle.modalFlatlistContiner}
data={devices}
renderItem={renderDeviceModalListItem}
/>
</SafeAreaView>
</Modal>
);
};
const modalStyle = StyleSheet.create({
modalContainer: {
flex: 1,
backgroundColor: "#f2f2f2",
},
modalFlatlistContiner: {
flex: 1,
justifyContent: "center",
},
modalCellOutline: {
borderWidth: 1,
borderColor: "black",
alignItems: "center",
marginHorizontal: 20,
paddingVertical: 15,
borderRadius: 8,
},
modalTitle: {
flex: 1,
backgroundColor: "#f2f2f2",
},
modalTitleText: {
marginTop: 40,
fontSize: 30,
fontWeight: "bold",
marginHorizontal: 20,
textAlign: "center",
},
ctaButton: {
backgroundColor: "#FF6060",
justifyContent: "center",
alignItems: "center",
height: 50,
marginHorizontal: 20,
marginBottom: 5,
borderRadius: 8,
},
ctaButtonText: {
fontSize: 18,
fontWeight: "bold",
color: "white",
},
});
export default DeviceModal;