-
Notifications
You must be signed in to change notification settings - Fork 0
/
Temporary
180 lines (165 loc) · 4.11 KB
/
Temporary
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
import React, { Component } from 'react';
import {
ActivityIndicator,
Button,
View,
Text,
ScrollView,
Image,
StyleSheet,
} from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import { FlatList, TouchableOpacity } from 'react-native-gesture-handler';
/**
* Home Screen Component
*/
class HomeScreen extends React.Component {
constructor() {
super();
// this.productList = [
// new Product(1, 'Dell', 56000),
// new Product(2, 'HP', 46000),
// ];
}
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
title="Fetch"
onPress={() =>
this.props.navigation.navigate({
routeName: 'JSONData',
})
}
/>
</View>
);
}
}
class APIDataScreen extends React.Component {
constructor(props) {
super(props);
this.state = { isLoading: true };
}
componentDidMount() {
this.getData();
}
getData() {
return fetch('https://jsonplaceholder.typicode.com/photos')
.then(response => response.json())
.then(responseJson => {
this.setState(
{
isLoading: false,
dataSource: responseJson,
},
function() {}
);
})
.catch(error => {
console.error(error);
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator size="large" />
<Text>Loading Data from JSON Placeholder API ...</Text>
</View>
);
}
return (
<View style={{ paddingTop: 5 }}>
<FlatList
data={this.state.dataSource}
renderItem={({ item }) => (
<TouchableOpacity
activeOpacity={0.5}
onPress={() =>
this.props.navigation.navigate({
routeName: 'ItemDetails',
params: { itemObj: item },
})
}>
<View style={styles.listContainer}>
<Image
style={{ width: 40, height: 40 }}
source={{ uri: item.thumbnailUrl }}
/>
<View style={{ paddingLeft: 5, paddingRight: 10 }}>
<Text>{item.title}</Text>
</View>
</View>
</TouchableOpacity>
)}
/>
</View>
);
}
}
const ItemDetailsScreen = props => {
const item = props.navigation.getParam('itemObj');
return (
<View style={{ alignItems: 'center', paddingTop: 5 }}>
<Image
style={{ width: '50%', height: '50%' }}
source={{ uri: item.thumbnailUrl }}
/>
<Text style={{ textAlign: 'center' }}>{item.title}</Text>
</View>
);
};
// Creating Stack Navigator
const RootStack = createStackNavigator(
{
Home: HomeScreen,
JSONData: APIDataScreen,
ItemDetails: ItemDetailsScreen,
},
{
initialRouteName: 'Home',
}
);
const headerBackgroundColor = 'darkblue';
// Styling Header of Item Details Screen
APIDataScreen.navigationOptions = {
headerTitle: 'List of Items from API',
headerTintColor: 'white',
headerStyle: {
backgroundColor: headerBackgroundColor,
},
};
// Styling Header of Item Details Screen
ItemDetailsScreen.navigationOptions = {
headerTitle: 'Item Details',
headerTintColor: 'white',
headerStyle: {
backgroundColor: headerBackgroundColor,
},
};
// Styling Header of Details Screen
// DetailsScreen.navigationOptions = {
// headerTitle: 'Details',
// headerTintColor: 'white',
// headerStyle: {
// backgroundColor: headerBackgroundColor,
// },
// };
// Home Screen Does not have a Header
HomeScreen.navigationOptions = {
header: null,
};
// Creating App Container to wrap Root Navigator
export default createAppContainer(RootStack);
const styles = StyleSheet.create({
listContainer: {
flex: 1,
flexDirection: 'row',
padding: 3,
borderBottomWidth: 1,
borderBottomColor: 'grey',
//backgroundColor: 'lightgrey'
},
});