-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigation.js
151 lines (136 loc) · 3.71 KB
/
Navigation.js
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
import React, { Component } from "react";
import { BackHandler, ToastAndroid, AsyncStorage } from "react-native";
import { createAppContainer } from "react-navigation";
import { createBottomTabNavigator } from "react-navigation-tabs";
import { Icon } from "react-native-elements";
import Favorite from "./Bottom-tabs/Favorite/Favorite";
import Home from "./Bottom-tabs/Home/Home";
import Mypage from "./Bottom-tabs/Mypage/Mypage";
import Search from "./Bottom-tabs/Search/Search";
const BottomTab = createBottomTabNavigator(
{
home: {
screen: Home
},
search: {
screen: Search
},
favorite: {
screen: Favorite
},
mypage: {
screen: Mypage
}
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let iconName;
let iconSize;
if (routeName === "home") {
iconName = "home";
iconSize = 25;
} else if (routeName === "search") {
iconName = "search";
iconSize = 25;
} else if (routeName === "favorite") {
iconName = "favorite";
iconSize = 25;
} else if (routeName === "mypage") {
iconName = "person";
iconSize = 28;
}
// You can return any component that you like here!
return (
<Icon
name={iconName}
type="ioicon"
size={iconSize}
color={tintColor}
/>
);
}
}),
tabBarOptions: {
activeTintColor: "#9151bd",
inactiveTintColor: "gray"
}
},
{ initialRouteName: "home" }
);
const BottomNav = createAppContainer(BottomTab);
export default class Navigation extends Component {
constructor(props) {
super(props);
this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
this.state = { favoriteData: [] };
}
componentWillMount() {
BackHandler.addEventListener(
"hardwareBackPress",
this.handleBackButtonClick
);
}
async componentDidMount() {
const token = await AsyncStorage.getItem("token");
let favoriteData;
if (token) {
favoriteData = await fetch("http://3.17.152.1:8000/user/favorite/info/", {
headers: { token }
})
.then(res => res.json())
.then(res => res)
.catch(err => console.error(err));
favoriteData = favoriteData.map(el => {
el.isFavorite = true;
return el;
});
} else {
favoriteData = [];
}
await AsyncStorage.setItem("favoriteData", JSON.stringify(favoriteData));
this.setState({ favoriteData });
}
componentDidUpdate(prevProp, prevState) {
if (prevState.favoriteData !== this.state.favoriteData) {
this.setState({ favoriteData: this.state.favoriteData });
}
}
componentWillUnmount() {
BackHandler.removeEventListener(
"hardwareBackPress",
this.handleBackButtonClick
);
}
handleFavorite = data => {
this.setState({ favoriteData: data });
};
handleBackButtonClick() {
if (this.exitApp === undefined || !this.exitApp) {
ToastAndroid.show("한번 더 누르시면 종료됩니다.", ToastAndroid.SHORT);
this.exitApp = true;
this.timeout = setTimeout(
() => {
this.exitApp = false;
},
2000 // 2초
);
} else {
clearTimeout(this.timeout);
BackHandler.exitApp(); // 앱 종료
}
return true;
}
render() {
return (
<BottomNav
screenProps={{
favoriteData: this.state.favoriteData,
handleFavorite: this.handleFavorite,
popToTop: this.props.navigation.getParam("popToTop")
}}
/>
);
}
}