This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Agenda.js
368 lines (342 loc) · 11.1 KB
/
Agenda.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* Created by Layman(http://github.com/anysome) on 16/11/26.
*/
import React from 'react';
import {View, RefreshControl, StyleSheet} from 'react-native';
import moment from 'moment';
import SwipeableListViewDataSource from 'SwipeableListViewDataSource';
import SwipeableListView from 'SwipeableListView';
import SwipeableQuickActions from 'SwipeableQuickActions';
import SwipeableQuickActionButton from 'SwipeableQuickActionButton';
import {analytics, airloy, styles, colors, api, L, toast, hang} from '../../app';
import util from '../../libs/Util';
import ListSource from '../../logic/ListSource';
import LocalNotifications from '../../logic/LocalNotifications';
import EventTypes from '../../logic/EventTypes';
import Controller from '../Controller';
import ListSectionView from '../../widgets/ListSectionView';
import ActionSheet from '@yfuks/react-native-action-sheet';
import ListRow from './ListRow';
import Edit from './Edit';
import Commit from './Commit';
import Timer from './Timer';
import Dones from './Dones';
export default class Agenda extends Controller {
constructor(props) {
super(props);
this.name = 'Agenda';
this.listSource = null;
this.state = {
isRefreshing: true,
showCommit: false,
showTimer: false,
selectedRow: {},
dataSource: new SwipeableListViewDataSource({
getSectionHeaderData: (dataBlob, sectionId) => dataBlob[sectionId],
getRowData: (dataBlob, sectionId, rowId) => dataBlob[sectionId].getRow(rowId),
rowHasChanged: (row1, row2) => row1 !== row2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2
})
};
}
componentWillMount() {
if (this.route) {// Logout and then login cause currentRoute to be null. Maybe a bug.
this.route.leftButtonIcon = require('../../../resources/icons/dones.png');
this.route.onLeftButtonPress = () => {
this.forward({
title: '近期完成',
component: Dones
});
};
this.route.rightButtonIcon = require('../../../resources/icons/create.png');
this.route.onRightButtonPress = () => {
this.forward({
title: '添加',
component: Edit,
passProps: {
today: this.today,
onFeedback: (agenda) => this.addRow(agenda)
}
});
};
util.isAndroid() || this.props.navigator.replace(this.route);
}
airloy.event.on(EventTypes.agendaChange, ()=> {
// call network request or mark stale until page visible
this.visible ? this.reload() : this.markStale();
});
airloy.event.on(EventTypes.agendaAdd, (agenda)=> {
this.listSource.add(agenda);
this._sortList();
});
}
addRow(rowData) {
this.listSource.add(rowData);
this._sortList();
this.backward();
}
async _reload() {
this.setState({
isRefreshing: true
});
let result = await airloy.net.httpGet(api.agenda.list.focus);
if (result.success) {
this.listSource = new ListSource(result.info, (agenda) => {
// check each item and schedule reminder if necessary
agenda.reminder && LocalNotifications.scheduleAgenda(agenda);
});
this._sortList();
this.setState({
isRefreshing: false
});
} else {
// event emit will unmount this component
result.message !== 'error.request.auth' && this.setState({
isRefreshing: false
});
toast(L(result.message));
}
}
_sortList() {
let section0 = new ListSectionView.DataSource({id: 0, name: moment(this.today).format('D日(ddd)')});
let section1 = new ListSectionView.DataSource({id: 1, name: '计划内'});
let section2 = new ListSectionView.DataSource({id: 2, name: '今日完成'});
for (let rowData of this.listSource) {
this._sortRow(rowData, section0, section1, section2);
}
this.setState({
dataSource: this.state.dataSource.cloneWithRowsAndSections(
[section0, section1, section2],
[0, 1, 2],
[section0.rowIds, section1.rowIds, section2.rowIds]
)
});
}
_sortRow(agenda, section0, section1, section2) {
var section;
if (agenda.status === '1') {
section = section2;
} else {
if (agenda.today > this.today) {
section = section1;
} else {
section = section0;
}
}
section.push(agenda);
}
_renderRow(rowData, sectionId, rowId) {
return <ListRow data={rowData} sectionId={sectionId} today={this.today}
onPress={() => this._pressRow(rowData)}
onIconClick={() => this._pressRowIcon(rowData, sectionId)}/>;
}
_pressRow(rowData) {
if (rowData.status === '1') {
this.forward({
title: '查看',
component: Edit,
passProps: {
today: this.today,
data: rowData
}
});
} else {
this.forward({
title: '修改',
component: Edit,
passProps: {
today: this.today,
data: rowData,
onFeedback: (agenda) => this.updateRow(agenda)
}
});
}
}
async _pressRowIcon(rowData, sectionId) {
if (sectionId === 2) {
this._pressRow(rowData);
} else {
if (sectionId === 1) {
let result = await airloy.net.httpPost(api.agenda.update, {
id: rowData.id,
today: new Date(this.today)
});
if (result.success) {
rowData.today = this.today;
this.updateRow(rowData);
} else {
toast(L(result.message));
}
analytics.onEvent('click_agenda_schedule');
} else {
this.setState({
showCommit: true,
selectedRow: rowData
});
}
}
}
updateRow(rowData) {
this._updateData(rowData);
this.backward();
}
_updateData(rowData) {
// add or remove notification
LocalNotifications.scheduleAgenda(rowData);
this.listSource.update(rowData);
this._sortList();
}
deleteRow(rowData) {
rowData.reminder && LocalNotifications.cancelAgenda(rowData.id);
this.listSource.remove(rowData);
this._sortList();
}
commitRow(rowData) {
if (rowData) {
this.listSource.update(rowData);
this._sortList();
}
this.setState({
showCommit: false
});
}
updateTimer(rowData) {
if (rowData) {
this._updateData(util.clone(rowData));
}
this.setState({
showTimer: false
});
}
_renderSectionHeader(sectionData, sectionId) {
return <ListSectionView data={sectionData}/>;
}
_renderSeparator(sectionId, rowId, adjacentRowHighlighted) {
return <View key={rowId + '_separator'} style={styles.hr}></View>
}
_renderActions(rowData, sectionId) {
return (
<SwipeableQuickActions style={styles.rowActions}>
{ sectionId !== 2 &&
<SwipeableQuickActionButton imageSource={{}} text={"更多"}
onPress={() => this._moreActions(rowData, sectionId)}
style={styles.rowAction} textStyle={styles.rowText}/>
}
<SwipeableQuickActionButton imageSource={{}} text={"删除"}
onPress={() => this._toDelete(rowData)}
style={styles.rowActionDestructive} textStyle={styles.rowText}/>
</SwipeableQuickActions>
);
}
_moreActions(rowData, sectionId) {
let isToday = sectionId === 0;
let scheduleOption = isToday ? '推迟到明天' : '提前到今天';
let priorityOption = rowData.priority === 9 ? '没那么重要' : '当日第一优先';
let BUTTONS = ['提醒', scheduleOption, priorityOption, '取消'];
ActionSheet.showActionSheetWithOptions({
options: BUTTONS,
cancelButtonIndex: 3,
tintColor: colors.dark2
},
async (buttonIndex) => {
switch (buttonIndex) {
case 0:
this.setState({
showTimer: true,
selectedRow: rowData
});
this.state.dataSource.setOpenRowID(null);
break;
case 1:
let newDate;
if (isToday) {
newDate = new Date(this.today + 86400000);
} else {
newDate = new Date(this.today);
}
hang();
let result = await airloy.net.httpPost(api.agenda.update, {
id: rowData.id,
today: newDate
}
);
hang(false);
if (result.success) {
this._updateData(result.info);
} else {
toast(L(result.message));
}
analytics.onEvent('click_agenda_schedule');
break;
case 2:
hang();
let result2 = await airloy.net.httpPost(api.agenda.update, {
id: rowData.id,
priority: rowData.priority === 9 ? 1 : 9
}
);
hang(false);
if (result2.success) {
this.state.dataSource.setOpenRowID(null);
this._updateData(result2.info);
} else {
toast(L(result2.message));
}
analytics.onEvent('click_agenda_priority');
break;
default :
console.log('cancel options');
}
}
);
}
async _toDelete(rowData) {
hang();
let result = await airloy.net.httpGet(api.agenda.remove, {id: rowData.id});
hang(false);
if (result.success) {
if (rowData.targetId) {
airloy.event.emit(EventTypes.targetChange);
} else if (rowData.projectId) {
airloy.event.emit(EventTypes.taskChange);
} else {
airloy.event.emit(EventTypes.choreChange);
}
rowData.reminder && LocalNotifications.cancelAgenda(rowData.id);
this.listSource.remove(rowData);
this._sortList();
} else {
toast(L(result.message));
}
}
render() {
return (
<View style={styles.flex}>
<SwipeableListView
maxSwipeDistance={120}
renderQuickActions={(rowData, sectionId, rowId) => this._renderActions(rowData, sectionId)}
enableEmptySections={true}
initialListSize={10}
pageSize={10}
dataSource={this.state.dataSource}
renderRow={(rowData, sectionId, rowId) => this._renderRow(rowData, sectionId, rowId)}
renderSectionHeader={this._renderSectionHeader}
renderSeparator={this._renderSeparator}
refreshControl={
<RefreshControl
refreshing={this.state.isRefreshing}
onRefresh={() => this.reload()}
tintColor={colors.accent}
title={'加载中...'}
colors={[colors.accent, colors.action]}
progressBackgroundColor={colors.bright1}
/>}
/>
<Commit data={this.state.selectedRow} visible={this.state.showCommit}
onFeedback={(agenda) => this.commitRow(agenda)}/>
<Timer data={this.state.selectedRow} visible={this.state.showTimer}
onFeedback={(agenda) => this.updateTimer(agenda)}/>
</View>
);
}
}