-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.js
182 lines (167 loc) · 5.19 KB
/
feed.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
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
import React from 'react';
import { FlatList, Text, View, TouchableOpacity, BackHandler } from 'react-native';
import * as rssParser from 'react-native-rss-parser';
import styles from './styles.js';
import GNMenu from './gnmenu.js';
import { MenuProvider } from 'react-native-popup-menu';
const HOME = 'https://golangnews.com/index.xml';
const TAGPREFIX = 'https://golangnews.com/stories.xml?q=%23';
export default class feed extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [],
title: 'Go News',
};
this.renderRow = this.renderRow.bind(this);
this._goHome = this._goHome.bind(this);
this._loadRSS = this._loadRSS.bind(this);
this.getDay = this.getDay.bind(this);
this.getDomain = this.getDomain.bind(this);
this.LastVisit = [];
this.prev = HOME;
this.refreshing = false;
}
static navigationOptions = ({ navigation }) => {
return {
headerTitle: feed.getHeaderTitle(navigation),// <Button style={styles.HeaderText} title={'Go News'} onPress={()=>{}}/>,
headerStyle: styles.Header,
//headerTitleStyle: styles.HeaderText,
headerTintColor: '#fff',
}
};
static getHeaderTitle(navigation) {
return (
<View style={{ flexDirection: 'row' }}>
<GNMenu mAction={navigation.getParam('loadRSS')} />
<TouchableOpacity onPress={navigation.getParam('goHome')}>
<Text style={styles.HeaderText}>Go News</Text>
</TouchableOpacity>
</View>
);
}
_goHome() {
this._loadRSS(HOME)
}
async _loadRSS(link, push = true) {
this.refreshing = true;
fetch(link)
.then((response) => response.text())
.then((responseData) => rssParser.parse(responseData))
.then((rss) => {
this.setState({
title: link,
items: rss.items.map(item => {
let link = (item.links.length > 0) ? item.links[0].url : null;
let obj = {};
if (link != null) {
obj['key'] = item.id;
obj['title'] = item.title;
obj['published'] = item.published;
obj['description'] = item.description;
obj['link'] = link;
}
return obj;
})
});
this.refreshing = false;
});
if (link == HOME) {
this.LastVisit = [];
} else if (push) {
this.LastVisit.push(this.prev);
}
this.prev = link;
}
async componentDidMount() {
this.props.navigation.setParams({ goHome: this._goHome });
this.props.navigation.setParams({ loadRSS: this._loadRSS });
this._loadRSS(HOME);
this.backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
if (this.LastVisit.length > 0) {
this._loadRSS(this.LastVisit.pop(), false);
} else {
BackHandler.exitApp();
}
return true;
});
}
componentWillUnmount() {
this.backHandler.remove();
}
renderRow({ item }) {
let { title, published, link, description } = item;
if (!link.includes('http') && !link.includes('https')) {
return null;
}
let btns = [];
if (item.title.includes('#')) {
let iter = item.title.split(' ');
for (let v of iter) {
if (v.startsWith('#')) {
const tag = v.substring(1);
const btn = (<TouchableOpacity key={v} onPress={() => { this._loadRSS(TAGPREFIX + tag); }}>
<Text style={styles.itemTag}>{tag}</Text>
</TouchableOpacity>);
btns.push(btn);
title = title.replace(v, "");
}
}
}
return (
<TouchableOpacity onPress={() => {
this.props.navigation.navigate('Page', {
link: link,
title: item.title
});
}}>
<View style={styles.item}>
<Text style={styles.itemMetaText}>{this.getDomain(link)}</Text>
<View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
<Text style={styles.itemText}>{title}</Text>
{btns}
</View>
<Text style={styles.itemMetaText}>{this.getDay(published)}</Text>
</View>
</TouchableOpacity>
)
}
getDomain(link) {
let matches = link.match(/^https?\:\/\/([^\/:?#]+)(?:[\/:?#]|$)/i);
return matches && matches[1];
}
getDay(published) {
const denominator = 1000 * 3600 * 24;
const d = new Date(published);
const now = Date.now();
const result = Math.floor((now - d) / denominator);
if (result === 0) {
return "Today";
}
const days = (result == 1) ? ' day' : ' days'
const r = result.toString();
return r + days + " ago";
}
renderHeader() {
return (
<Text style={styles.Header}>Golang News</Text>
)
}
renderSeparator() {
return (
<View style={styles.separator} />
)
}
render() {
return (
<View style={styles.containerCenter}>
<FlatList
data={this.state.items}
renderItem={this.renderRow}
keyExtractor={item => item.key}
ItemSeparatorComponent={this.renderSeparator}
/>
</View>
);
}
}