Browse Source

Added facebook news feed on home screen using Graph API

keplyx 4 years ago
parent
commit
d52fdfd7d2
3 changed files with 153 additions and 13 deletions
  1. 1
    1
      screens/About/AboutScreen.js
  2. 151
    11
      screens/HomeScreen.js
  3. 1
    1
      screens/SettingsScreen.js

+ 1
- 1
screens/About/AboutScreen.js View File

@@ -157,7 +157,7 @@ export default class AboutScreen extends React.Component<Props> {
157 157
         return (
158 158
             <Container>
159 159
                 <CustomHeader navigation={nav} title={i18n.t('screens.about')}/>
160
-                <Content>
160
+                <Content padder>
161 161
                     <Card>
162 162
                         <CardItem>
163 163
                             <Left>

+ 151
- 11
screens/HomeScreen.js View File

@@ -1,33 +1,173 @@
1 1
 // @flow
2 2
 
3 3
 import * as React from 'react';
4
-import {Container, Content, Text, Button, Icon} from 'native-base';
4
+import {Image, Linking, RefreshControl, FlatList} from 'react-native';
5
+import {Container, Content, Text, Button, Card, CardItem, Left, Body, Thumbnail, H2, Toast} from 'native-base';
5 6
 import CustomHeader from '../components/CustomHeader';
6 7
 import i18n from "i18n-js";
7
-import NotificationsManager from '../utils/NotificationsManager'
8
+import CustomMaterialIcon from '../components/CustomMaterialIcon';
9
+
10
+const ICON_AMICALE = require('../assets/amicale.png');
8 11
 
9 12
 type Props = {
10 13
     navigation: Object,
11 14
 }
12 15
 
16
+type State = {
17
+    refreshing: boolean,
18
+    firstLoading: boolean,
19
+    data: Object,
20
+};
21
+
22
+const FB_URL = "https://graph.facebook.com/v3.3/amicale.deseleves/posts?fields=message%2Cfull_picture%2Ccreated_time%2Cpermalink_url&access_token=EAAGliUs4Ei8BAC9J8U6EpL8LKLImMhvcgBq6iWK2BPDAhjZB7v4t8Fy5q3SsdfUq8nPovQfyFmwURSrNGWypwwBC1VZAf7Vmfff76UkpZCm0KTp56TRVK0NkH9YI4kRO8VqEucejFCBPQZBflZASwSbfRGZBLi4FWqBopBTEZCXg7RZAiMM02WXKXcc7b3AOl6wZD";
23
+
24
+let test_data = [
25
+    {
26
+        title: "News de l'Amicale",
27
+        date: "June 15, 2019",
28
+        thumbnail: require("../assets/amicale.png"),
29
+        image: require("../assets/drawer-cover.png"),
30
+        text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus congue sapien leo, ac dignissim odio dignissim sit amet. Quisque tempor, turpis sed scelerisque faucibus, dolor tortor porta sapien, eget tincidunt ante elit et ex. Sed sagittis dui non nisl aliquet viverra. Integer quis convallis enim, sit amet auctor ante. Praesent quis lacinia magna. Sed augue lacus, congue eu turpis vel, consectetur pellentesque nulla. Maecenas blandit diam odio, et finibus urna egestas non. Quisque congue finibus efficitur. Sed pretium mauris nec neque mattis, eu condimentum velit ultrices. Fusce eleifend porttitor nunc non suscipit. Aenean porttitor feugiat ipsum sit amet interdum. Maecenas tempor felis non tempus vehicula. Suspendisse sit amet eros neque. ",
31
+        link: "https://en.wikipedia.org/wiki/Main_Page"
32
+    },
33
+    {
34
+        title: "Lancement de la super appli de la mort avec un titre super long",
35
+        date: "June 14, 2019",
36
+        thumbnail: require("../assets/amicale.png"),
37
+        image: require("../assets/image-missing.png"),
38
+        text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus congue sapien leo, eget tincidunt ante elit et ex. Sed sagittis dui non nisl aliquet viverra. Integer quis convallis enim, sit amet auctor ante. Praesent quis lacinia magna. Sed augue lacus, congue eu turpis vel, consectetur pellentesque nulla. Maecenas blandit diam odio, et finibus urna egestas non. Quisque congue finibus efficitur. Sed pretium mauris nec neque mattis, eu condimentum velit ultrices. Fusce eleifend porttitor nunc non suscipit.",
39
+        link: "https://en.wikipedia.org/wiki/Central_Link"
40
+    }
41
+];
42
+
43
+/**
44
+ * Opens a link in the device's browser
45
+ * @param link The link to open
46
+ */
47
+function openWebLink(link) {
48
+    Linking.openURL(link).catch((err) => console.error('Error opening link', err));
49
+}
50
+
13 51
 /**
14 52
  * Class defining the app's home screen
15 53
  */
16
-export default class HomeScreen extends React.Component<Props> {
54
+export default class HomeScreen extends React.Component<Props, State> {
55
+
56
+    state = {
57
+        refreshing: false,
58
+        firstLoading: true,
59
+        data: {},
60
+    };
61
+
62
+    async readData() {
63
+        try {
64
+            let response = await fetch(FB_URL);
65
+            let responseJson = await response.json();
66
+            this.setState({
67
+                data: responseJson
68
+            });
69
+        } catch (error) {
70
+            console.log('Could not read data from server');
71
+            console.log(error);
72
+            this.setState({
73
+                data: {}
74
+            });
75
+        }
76
+    }
77
+
78
+    isDataObjectValid() {
79
+        return Object.keys(this.state.data).length > 0;
80
+    }
81
+
82
+    _onRefresh = () => {
83
+        this.setState({refreshing: true});
84
+        this.readData().then(() => {
85
+            this.setState({
86
+                refreshing: false,
87
+                firstLoading: false
88
+            });
89
+            if (this.isDataObjectValid()) {
90
+                Toast.show({
91
+                    text: i18n.t('proxiwashScreen.listUpdated'),
92
+                    buttonText: 'OK',
93
+                    type: "success",
94
+                    duration: 2000
95
+                })
96
+            } else {
97
+                Toast.show({
98
+                    text: i18n.t('proxiwashScreen.listUpdateFail'),
99
+                    buttonText: 'OK',
100
+                    type: "danger",
101
+                    duration: 4000
102
+                })
103
+            }
104
+        });
105
+    };
106
+
107
+    getRenderItem(item: Object) {
108
+        return (
109
+            <Card style={{flex: 0}}>
110
+                <CardItem>
111
+                    <Left>
112
+                        <Thumbnail source={ICON_AMICALE}/>
113
+                        <Body>
114
+                            <Text>Amicale</Text>
115
+                            <Text note>{item.created_time}</Text>
116
+                        </Body>
117
+                    </Left>
118
+                </CardItem>
119
+                <CardItem>
120
+                    <Body>
121
+                        <Image source={{uri: item.full_picture}}
122
+                               style={{width: '100%', height: 200, flex: 1}}/>
123
+                        <Text>{item.message}</Text>
124
+                    </Body>
125
+                </CardItem>
126
+                <CardItem>
127
+                    <Left>
128
+                        <Button transparent info
129
+                        onPress={() => openWebLink(item.permalink_url)}>
130
+                            <CustomMaterialIcon
131
+                                icon="facebook"
132
+                                color="#57aeff"
133
+                                width={20}/>
134
+                            <Text>En savoir plus</Text>
135
+                        </Button>
136
+                    </Left>
137
+                </CardItem>
138
+            </Card>
139
+        );
140
+    }
141
+
142
+    /**
143
+     * Refresh the data on first screen load
144
+     */
145
+    componentDidMount() {
146
+        this._onRefresh();
147
+    }
148
+
149
+
17 150
     render() {
18 151
         const nav = this.props.navigation;
152
+        let displayData = this.state.data.data;
19 153
         return (
20 154
             <Container>
21 155
                 <CustomHeader navigation={nav} title={i18n.t('screens.home')}/>
22 156
                 <Content padder>
23
-                    <Button onPress={() => NotificationsManager.sendNotificationImmediately('coucou', 'whoa')}>
24
-                        <Icon
25
-                            active
26
-                            name={'bell-ring'}
27
-                            type={'MaterialCommunityIcons'}
28
-                        />
29
-                        <Text>Instant Notification</Text>
30
-                    </Button>
157
+                    <FlatList
158
+                        data={displayData}
159
+                        extraData={this.state}
160
+                        keyExtractor={(item) => item.id}
161
+                        refreshControl={
162
+                            <RefreshControl
163
+                                refreshing={this.state.refreshing}
164
+                                onRefresh={this._onRefresh}
165
+                            />
166
+                        }
167
+                        renderItem={({item}) =>
168
+                            this.getRenderItem(item)
169
+                        }
170
+                    />
31 171
                 </Content>
32 172
             </Container>
33 173
         );

+ 1
- 1
screens/SettingsScreen.js View File

@@ -190,7 +190,7 @@ export default class SettingsScreen extends React.Component<Props, State> {
190 190
         return (
191 191
             <Container>
192 192
                 <CustomHeader navigation={nav} title={i18n.t('screens.settings')}/>
193
-                <Content>
193
+                <Content padder>
194 194
                     <Card>
195 195
                         <CardItem header>
196 196
                             <Text>{i18n.t('settingsScreen.appearanceCard')}</Text>

Loading…
Cancel
Save