-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetting.js
More file actions
185 lines (180 loc) · 5.63 KB
/
Setting.js
File metadata and controls
185 lines (180 loc) · 5.63 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import React, { Component, useContext, useEffect, useState } from "react";
import { StyleSheet, View, Text, FlatList, Switch } from "react-native";
import { createMaterialTopTabNavigator } from "@react-navigation/material-top-tabs";
const Tabs = createMaterialTopTabNavigator();
import MovieGenre from "./MovieGenre";
import TVGenre from "./TVGenre";
import { Context } from "./Context";
import Loading from "./Loading";
import {
deleteFavTvGenre,
deleteFavMovieGenre,
insertFavMovieGenre,
insertFavTvGenre,
fetchFavTvGenre,
fetchFavMovieGenre,
} from "./Database";
const SettingsTabs = () => {
return (
<Tabs.Navigator
screenOptions={() => ({
headerShown: false,
tabBarLabelStyle: {
textTransform: "none",
fontSize: 16,
fontWeight: "bold",
color: "#14b8a6",
},
tabBarStyle: {
backgroundColor: "#5b21b6",
}, // Background color for the tab bar
//tabStyle: { backgroundColor: "orange" },
// tabBarActiveTintColor: "#14b8a6",
// tabBarInactiveTintColor: "#14b8a6",
tabBarIndicatorStyle: { backgroundColor: "#14b8a6" },
})}
>
<Tabs.Screen
name="Movies"
component={Settings}
initialParams={{ type: "movie" }}
/>
<Tabs.Screen
name="TV Shows"
component={Settings}
initialParams={{ type: "tv" }}
/>
</Tabs.Navigator>
);
};
const Settings = ({ route }) => {
const {
setFavTvGenreList,
setFavMovieGenreList,
favMovieGenreList,
favTvGenreList,
} = useContext(Context);
const fetchFavTvGenreFromDatabase = async () => {
try {
const tvGenreListFromDB = await fetchFavTvGenre();
setLocalTvList(tvGenreListFromDB);
setFavTvGenreList(tvGenreListFromDB);
console.log("fetched favTvGenre: ", tvGenreListFromDB);
} catch (error) {
console.log("Error fetching favTvGenre list:", error);
}
setIsLoading(false);
};
const fetchFavMovieGenreFromDatabase = async () => {
try {
const movieGenreListFromDB = await fetchFavMovieGenre();
setLocalMovieList(movieGenreListFromDB);
setFavMovieGenreList(movieGenreListFromDB);
console.log("fetched favMovieGenre: ", movieGenreListFromDB);
} catch (error) {
console.log("Error fetching favMovieGenre list:", error);
}
setIsLoading(false);
};
const { type } = route.params;
const [localMovieList, setLocalMovieList] = useState([]);
const [localTvList, setLocalTvList] = useState([]);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
if (type === "movie") {
fetchFavMovieGenreFromDatabase();
setIsLoading(false);
}
if (type === "tv") {
fetchFavTvGenreFromDatabase();
setIsLoading(false);
}
}, []);
const toggleSwitch = (id) => {
setIsLoading(true);
if (type === "movie") {
if (localMovieList.some((object) => object.favMovieGenreId === id)) {
const handleDeleteMovieGenre = async (id) => {
try {
await deleteFavMovieGenre(id);
fetchFavMovieGenreFromDatabase(); // Fetch updated notes after deleting a note
} catch (error) {
console.error("Error deleting movie genre", error);
}
};
handleDeleteMovieGenre(id);
} else {
const handleAddMovieGenre = async (id) => {
try {
await insertFavMovieGenre(id);
fetchFavMovieGenreFromDatabase(); // Fetch updated notes after deleting a note
} catch (error) {
console.error("Error adding movie genre", error);
}
};
handleAddMovieGenre(id);
}
}
if (type === "tv") {
if (localTvList.some((object) => object.favTvGenreId === id)) {
const handleDeleteTvGenre = async (id) => {
try {
await deleteFavTvGenre(id);
fetchFavTvGenreFromDatabase(); // Fetch updated notes after deleting a note
} catch (error) {
console.error("Error deleting tv genre", error);
}
};
handleDeleteTvGenre(id);
} else {
const handleAddTvGenre = async (id) => {
try {
await insertFavTvGenre(id);
fetchFavTvGenreFromDatabase(); // Fetch updated notes after deleting a note
} catch (error) {
console.error("Error adding tv genre", error);
}
};
handleAddTvGenre(id);
}
}
};
return isLoading ? (
<Loading />
) : (
<FlatList
className="bg-teal-500"
numColumns={2}
data={type === "movie" ? MovieGenre : TVGenre}
renderItem={({ item }) => {
let switchValue = false;
if (type === "movie") {
switchValue = localMovieList.some(
(object) => object.favMovieGenreId === item.id
);
}
if (type === "tv") {
switchValue = localTvList.some(
(object) => object.favTvGenreId === item.id
);
}
return (
<View className="flex flex-row items-center w-[49%] self-center px-8 justify-between">
<View className="w-[75%]">
<Text className="font-bold text-blue-800">{item.name}</Text>
</View>
<Switch
trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={switchValue ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={() => toggleSwitch(item.id)}
value={switchValue ? true : false}
/>
</View>
);
}}
keyExtractor={(item) => item.id}
/>
);
};
export default SettingsTabs;