-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
87 lines (80 loc) · 2.4 KB
/
App.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
import React from "react";
import { FlatList, SafeAreaView, Text, TouchableOpacity } from "react-native";
import HomeKitModule, { Home } from "./src/HomeKitModule/HomeKitModule";
const App = () => {
const [home, setHome] = React.useState<Home>();
const addAndSetupAccessories = async () => {
const homeData = await HomeKitModule.addAndSetupAccessories("");
setHome(homeData);
};
const toggleDeviceState = async (deviceName: string) => {
const device = home?.accessories.find(
accessory => accessory.name === deviceName,
);
const newState = device?.isOn === true ? 0 : 1;
await HomeKitModule.setDeviceState(deviceName, newState);
const updatedHomeData = await HomeKitModule.getHomeData("");
setHome({ ...updatedHomeData });
};
React.useEffect(() => {
const fetchHomeData = async () => {
try {
const initialHomeData = await HomeKitModule.getHomeData("");
console.log(initialHomeData);
setHome(initialHomeData);
} catch (error) {
console.error(error);
}
};
fetchHomeData();
}, []);
return (
<SafeAreaView>
<TouchableOpacity
onPress={addAndSetupAccessories}
style={{
backgroundColor: "#007AFF",
padding: 10,
margin: 10,
borderRadius: 10,
height: 200,
justifyContent: "center",
alignItems: "center",
}}>
<Text
style={{
color: "white",
fontSize: 34,
}}>
+ Add HomeKit Device
</Text>
</TouchableOpacity>
<FlatList
contentContainerStyle={{ paddingBottom: 300 }}
data={home?.accessories.sort((a, b) => a.name.localeCompare(b.name))}
renderItem={({ item }) => (
<TouchableOpacity
onPress={async () => await toggleDeviceState(item.name)}
style={{
backgroundColor: item.isOn === true ? "#adbffa" : "#D6D6D6",
padding: 10,
margin: 10,
borderRadius: 10,
justifyContent: "center",
alignItems: "center",
}}>
<Text
style={{
color: "black",
fontSize: 34,
fontWeight: "200",
}}>
{item.name}
</Text>
</TouchableOpacity>
)}
/>
</SafeAreaView>
);
};
export default App;