-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostsScreenComponent.js
203 lines (189 loc) · 7.79 KB
/
PostsScreenComponent.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import React, { Component } from 'react';
import { styles, getUser, COLORS } from './Utility';
import { View, FlatList, RefreshControl, Picker, Text } from 'react-native';
import { Post, renderPost } from './Post'
import { pullAccountFromDatabase, pullPostFromDatabase } from './firebaseConfig'
export default class PostsPage extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
refreshing: false,
data: [],
error: null,
sortingMode: "Newest"
};
this.isRefreshing = false;
this.flatlistRef = null;
}
componentDidMount() {
//this.makeRemoteRequest();
this.refresh();
//console.log(data);
}
//All posts this user can see, including their peers and their own.
async getAllPosts() {
this.setState({loading: true});
const allPosts = [];
const currUser = getUser();
//console.log("\\getAllPosts(): currUser: " + currUser.getMiD());
//For each of my peers:
for(const u of currUser.getAllPeers()) {
const peer = await pullAccountFromDatabase(u);
//console.log(peer.getMiD());
//console.log(peer.getMyPosts());
const peerPosts = peer.getMyPosts();
//Reciprocity: If this peer also has me as a peer...
//console.log("This peer:" + JSON.stringify(peer));
//console.log("Peer's peers:" + peer.getAllPeers());
if(peer.getAllPeers().includes(currUser.getMiD())) {
//For each of their posts:
for(const p of peerPosts) {
//Fetch the post from firebase using ID
console.log("Adding posts of peer: " + peer.getMiD())
const post = await pullPostFromDatabase(p);
allPosts.push(post);
}
}
}
//Do the same for current user:
//TODO: This logic should be refactored so that local posts are also pulled from FB instead.
//Local data is ONLY for P2P functionality. It should not be used in this context while we are using firebase.
const myPostsArr = currUser.getMyPosts();
//console.log(myPostsArr);
for(const post of myPostsArr) {
const FBpost = await pullPostFromDatabase(post);
allPosts.push(FBpost);
}
//console.log("All posts:" + JSON.stringify(allPosts));
//console.log(allPosts.includes(null))
//console.log(allPosts[1] == null)
if(true) {
//console.log("Null value detected!");
for(let i = 0;i < allPosts.length;++i) {
//console.log("Post element at " + i + " is null! Replacing...");
const dummyPost = new Post(0,('error-'+i),0,"Error: post not found!",Date().toString());
if(allPosts[i] == null) {
allPosts[i] = dummyPost;
}
}
}
//console.log("All posts:" + JSON.stringify(allPosts));
this.sortPostsArray(allPosts);
//Update the state
this.setState({
data: allPosts,
error: null,
loading: false,
});
}
sortPostsArray(allPosts) {
if(this.state.sortingMode == "Newest") {
allPosts.sort((b, a) =>
(new Date(a.timestamp)) - (new Date(b.timestamp))
)
} else if(this.state.sortingMode == "Oldest") {
allPosts.sort((a, b) =>
(new Date(a.timestamp)) - (new Date(b.timestamp))
)
} else if(this.state.sortingMode == "Score(H)") {
allPosts.sort((b, a) =>
(a.starting_score - b.starting_score)
)
} else if(this.state.sortingMode == "Score(L)") {
allPosts.sort((a, b) =>
(a.starting_score - b.starting_score)
)
} else if(this.state.sortingMode == "Weighted") {
allPosts.sort((b, a) =>
(((new Date(a.timestamp)) - (new Date(b.timestamp))) + ((a.starting_score - b.starting_score)*10000000))
)
}
}
renderSeparator = () => {
return (
<View
style={{
height: 1,
width: '100%',
backgroundColor: '#CED0CE',
marginBottom: '3%',
}}
/>
);
};
render() {
if(true) {
return (
<View style={{marginTop: 0}} testID='PostScreenComponent'>
<View style={{ marginTop: 0, height: 40, width: '45%', elevation: 2, position: 'absolute', borderRadius: 4, backgroundColor:'#181D27', alignSelf: 'flex-end'}}>
<Picker
selectedValue={this.state.sortingMode}
style={styles.PickerStyle}
mode='dropdown'
onValueChange={(itemValue, itemIndex) => {this.setState({sortingMode: itemValue});this.refresh()}}
>
<Picker.Item label="Date (Newest)" value="Newest" />
<Picker.Item label="Date (Oldest)" value="Oldest" />
<Picker.Item label="Score (Highest)" value="Score(H)" />
<Picker.Item label="Score (Lowest)" value="Score(L)" />
<Picker.Item label="Weighted" value="Weighted" />
</Picker>
</View>
<FlatList
ref={(ref) => this.flatlistRef = ref}
refreshControl={
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={() => {this.refresh()}}
/>
}
data={this.state.data}
renderItem={post => renderPost(post)}
ItemSeparatorComponent={this.renderSeparator}
keyExtractor={item => JSON.stringify(item)}
testID='PostFlatlist'
/>
</View>
);
} else {
return (
<View style={{marginTop: 0}} testID='PostScreenComponent'>
<FlatList
ref={(ref) => this.flatlistRef = ref}
refreshControl={
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={() => {this.refresh()}}
/>
}
data={this.state.data}
renderItem={post => renderPost(post)}
ItemSeparatorComponent={this.renderSeparator}
keyExtractor={item => JSON.stringify(item)}
testID='PostFlatlist'
/>
</View>
);
}
}
refresh() {
console.log("Refresh called...");
if(!this.isRefreshing) {
if(this.flatlistRef && this.state.data.length > 0) {
this.flatlistRef.scrollToIndex({index: 0});
}
this.isRefreshing = true;
this.setState({refreshing: true});
console.log("Refreshing set to true!");
//Do something
this.getAllPosts();
wait(800).then(() => {this.setState({refreshing: false});
this.isRefreshing = false;
console.log("Refreshing set to false!");});
}
}
}
const wait = (timeout) => {
return new Promise(resolve => setTimeout(resolve, timeout));
}